mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 06:00:20 +00:00
Asset events and AddAsset builder
This commit is contained in:
parent
a7eaf32e7c
commit
16b568e00e
8 changed files with 64 additions and 23 deletions
|
@ -5,6 +5,8 @@ authors = ["Carter Anderson <mcanders1@gmail.com>"]
|
|||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bevy_app = { path = "../bevy_app" }
|
||||
bevy_core = { path = "../bevy_core" }
|
||||
legion = { path = "../bevy_legion" }
|
||||
|
||||
uuid = { version = "0.8", features = ["v4", "serde"] }
|
|
@ -1,12 +1,19 @@
|
|||
mod handle;
|
||||
pub use handle::*;
|
||||
|
||||
use bevy_app::{stage, AppBuilder, Events};
|
||||
use bevy_core::bytes::GetBytes;
|
||||
use legion::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub enum AssetEvent<T> {
|
||||
Created { handle: Handle<T> },
|
||||
}
|
||||
|
||||
pub struct AssetStorage<T> {
|
||||
assets: HashMap<HandleId, T>,
|
||||
names: HashMap<String, Handle<T>>,
|
||||
events: Events<AssetEvent<T>>,
|
||||
}
|
||||
|
||||
impl<T> AssetStorage<T> {
|
||||
|
@ -14,6 +21,7 @@ impl<T> AssetStorage<T> {
|
|||
AssetStorage {
|
||||
assets: HashMap::new(),
|
||||
names: HashMap::new(),
|
||||
events: Events::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,16 +32,21 @@ impl<T> AssetStorage<T> {
|
|||
pub fn add(&mut self, asset: T) -> Handle<T> {
|
||||
let id = HandleId::new();
|
||||
self.assets.insert(id, asset);
|
||||
Handle::from_id(id)
|
||||
let handle = Handle::from_id(id);
|
||||
self.events.send(AssetEvent::Created { handle });
|
||||
handle
|
||||
}
|
||||
|
||||
pub fn add_with_handle(&mut self, handle: Handle<T>, asset: T) {
|
||||
self.assets.insert(handle.id, asset);
|
||||
self.events.send(AssetEvent::Created { handle });
|
||||
}
|
||||
|
||||
pub fn add_default(&mut self, asset: T) -> Handle<T> {
|
||||
self.assets.insert(DEFAULT_HANDLE_ID, asset);
|
||||
Handle::default()
|
||||
let handle = Handle::default();
|
||||
self.events.send(AssetEvent::Created { handle });
|
||||
handle
|
||||
}
|
||||
|
||||
pub fn set_name(&mut self, name: &str, handle: Handle<T>) {
|
||||
|
@ -59,6 +72,13 @@ impl<T> AssetStorage<T> {
|
|||
pub fn iter(&self) -> impl Iterator<Item = (Handle<T>, &T)> {
|
||||
self.assets.iter().map(|(k, v)| (Handle::from_id(*k), v))
|
||||
}
|
||||
|
||||
pub fn asset_event_system(
|
||||
mut events: ResourceMut<Events<AssetEvent<T>>>,
|
||||
mut storage: ResourceMut<AssetStorage<T>>,
|
||||
) {
|
||||
events.extend(storage.events.drain())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GetBytes for Handle<T> {
|
||||
|
@ -70,3 +90,23 @@ impl<T> GetBytes for Handle<T> {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AddAsset {
|
||||
fn add_asset<T>(&mut self) -> &mut Self
|
||||
where
|
||||
T: Send + Sync + 'static;
|
||||
}
|
||||
|
||||
impl AddAsset for AppBuilder {
|
||||
fn add_asset<T>(&mut self) -> &mut Self
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
self.add_resource(AssetStorage::<T>::new())
|
||||
.add_system_to_stage(
|
||||
stage::EVENT_UPDATE,
|
||||
AssetStorage::<T>::asset_event_system.system(),
|
||||
)
|
||||
.add_event::<AssetEvent<T>>()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ mod forward_pbr_render_graph;
|
|||
pub use forward_pbr_render_graph::*;
|
||||
|
||||
use bevy_app::{stage, AppBuilder, AppPlugin};
|
||||
use bevy_asset::AssetStorage;
|
||||
use bevy_asset::AddAsset;
|
||||
use bevy_render::{render_graph::RenderGraph, shader};
|
||||
use legion::prelude::IntoSystem;
|
||||
use material::StandardMaterial;
|
||||
|
@ -19,11 +19,10 @@ pub struct PbrPlugin;
|
|||
|
||||
impl AppPlugin for PbrPlugin {
|
||||
fn build(&self, app: &mut AppBuilder) {
|
||||
app.add_resource(AssetStorage::<StandardMaterial>::new())
|
||||
.add_system_to_stage(
|
||||
stage::POST_UPDATE,
|
||||
shader::asset_handle_shader_def_system::<StandardMaterial>.system(),
|
||||
);
|
||||
app.add_asset::<StandardMaterial>().add_system_to_stage(
|
||||
stage::POST_UPDATE,
|
||||
shader::asset_handle_shader_def_system::<StandardMaterial>.system(),
|
||||
);
|
||||
let resources = app.resources();
|
||||
let mut render_graph = resources.get_mut::<RenderGraph>().unwrap();
|
||||
render_graph.add_pbr_graph(resources);
|
||||
|
|
|
@ -41,7 +41,7 @@ use self::{
|
|||
|
||||
use base_render_graph::{BaseRenderGraphBuilder, BaseRenderGraphConfig};
|
||||
use bevy_app::{stage, AppBuilder, AppPlugin};
|
||||
use bevy_asset::AssetStorage;
|
||||
use bevy_asset::AddAsset;
|
||||
use mesh::mesh_resource_provider_system;
|
||||
use render_graph::RenderGraph;
|
||||
|
||||
|
@ -71,11 +71,11 @@ impl AppPlugin for RenderPlugin {
|
|||
app.add_stage_after(stage::POST_UPDATE, RENDER_RESOURCE_STAGE)
|
||||
.add_stage_after(RENDER_RESOURCE_STAGE, RENDER_STAGE)
|
||||
// resources
|
||||
.add_asset::<Mesh>()
|
||||
.add_asset::<Texture>()
|
||||
.add_asset::<Shader>()
|
||||
.add_asset::<PipelineDescriptor>()
|
||||
.add_resource(render_graph)
|
||||
.add_resource(AssetStorage::<Mesh>::new())
|
||||
.add_resource(AssetStorage::<Texture>::new())
|
||||
.add_resource(AssetStorage::<Shader>::new())
|
||||
.add_resource(AssetStorage::<PipelineDescriptor>::new())
|
||||
.add_resource(PipelineAssignments::new())
|
||||
.add_resource(PipelineCompiler::new())
|
||||
.add_resource(RenderResourceAssignments::default())
|
||||
|
|
|
@ -18,7 +18,7 @@ pub use sprite::*;
|
|||
pub use ui_update_system::*;
|
||||
|
||||
use bevy_app::{stage, AppBuilder, AppPlugin};
|
||||
use bevy_asset::{AssetStorage, Handle};
|
||||
use bevy_asset::{AddAsset, AssetStorage, Handle};
|
||||
use bevy_render::{
|
||||
mesh::{shape::Quad, Mesh},
|
||||
render_graph::RenderGraph,
|
||||
|
@ -35,10 +35,7 @@ pub const QUAD_HANDLE: Handle<Mesh> = Handle::from_u128(142404619811301375266013
|
|||
|
||||
impl AppPlugin for UiPlugin {
|
||||
fn build(&self, app: &mut AppBuilder) {
|
||||
let mut color_materials = AssetStorage::<ColorMaterial>::new();
|
||||
color_materials.add_default(ColorMaterial::default());
|
||||
|
||||
app.add_resource(color_materials)
|
||||
app.add_asset::<ColorMaterial>()
|
||||
.add_system_to_stage(
|
||||
stage::POST_UPDATE,
|
||||
asset_handle_shader_def_system::<ColorMaterial>.system(),
|
||||
|
@ -57,5 +54,8 @@ impl AppPlugin for UiPlugin {
|
|||
size: Vec2::new(1.0, 1.0),
|
||||
}),
|
||||
);
|
||||
|
||||
let mut color_materials = resources.get_mut::<AssetStorage<ColorMaterial>>().unwrap();
|
||||
color_materials.add_default(ColorMaterial::default());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ use bevy::prelude::*;
|
|||
fn main() {
|
||||
App::build()
|
||||
.add_default_plugins()
|
||||
.add_asset::<MyMaterial>()
|
||||
.add_startup_system(setup)
|
||||
.run();
|
||||
}
|
||||
|
@ -66,11 +67,10 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
};
|
||||
|
||||
// create materials
|
||||
let mut material_storage = AssetStorage::<MyMaterial>::new();
|
||||
let mut material_storage = resources.get_mut::<AssetStorage<MyMaterial>>().unwrap();
|
||||
let material = material_storage.add(MyMaterial {
|
||||
color: Color::rgb(0.0, 0.8, 0.0),
|
||||
});
|
||||
resources.insert(material_storage);
|
||||
|
||||
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
||||
let cube_handle = mesh_storage.add(Mesh::from(shape::Cube));
|
||||
|
|
|
@ -3,6 +3,7 @@ use bevy::{prelude::*, render::shader};
|
|||
fn main() {
|
||||
App::build()
|
||||
.add_default_plugins()
|
||||
.add_asset::<MyMaterial>()
|
||||
.add_startup_system(setup)
|
||||
.add_system_to_stage(
|
||||
stage::POST_UPDATE,
|
||||
|
@ -76,7 +77,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
};
|
||||
|
||||
// create materials
|
||||
let mut material_storage = AssetStorage::<MyMaterial>::new();
|
||||
let mut material_storage = resources.get_mut::<AssetStorage<MyMaterial>>().unwrap();
|
||||
let green_material = material_storage.add(MyMaterial {
|
||||
color: Color::rgb(0.0, 0.8, 0.0),
|
||||
always_red: false,
|
||||
|
@ -86,7 +87,6 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
color: Color::rgb(0.0, 0.0, 0.0),
|
||||
always_red: true,
|
||||
});
|
||||
resources.insert(material_storage);
|
||||
|
||||
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
||||
let cube_handle = mesh_storage.add(Mesh::from(shape::Cube));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#[cfg(feature = "asset")]
|
||||
pub use crate::asset::{AssetStorage, Handle};
|
||||
pub use crate::asset::{AddAsset, AssetEvent, AssetStorage, Handle};
|
||||
#[cfg(feature = "core")]
|
||||
pub use crate::core::{
|
||||
time::Time,
|
||||
|
|
Loading…
Reference in a new issue