diff --git a/crates/app/src/driver.rs b/crates/app/src/driver.rs index a6ad0db..80287b9 100644 --- a/crates/app/src/driver.rs +++ b/crates/app/src/driver.rs @@ -71,7 +71,7 @@ impl Driver { let mut world = inject.write_sync::().unwrap(); let assets = inject.get::().unwrap(); world.terrain = Some(assets.load(TerrainLoadInfo::FromHeightmap { - height_path: "data/heightmaps/deccer.png".into(), + height_path: "data/heightmaps/mountain.png".into(), texture_path: "data/textures/blank.png".into(), options: world.terrain_options, })); @@ -102,7 +102,7 @@ impl Driver { .new_frame(); } - self.bus.publish(&Tick)?; + self.bus.publish(Tick)?; let inject = self.bus.data().read().unwrap(); let world = inject.read_sync::().unwrap(); @@ -147,13 +147,13 @@ impl Driver { None => {} Some(VirtualKeyCode::Escape) => match input.state { ElementState::Pressed => { - self.bus.publish(&InputEvent::Button(KeyState { + self.bus.publish(InputEvent::Button(KeyState { state: ButtonState::Pressed, button: Key::Escape, }))?; } ElementState::Released => { - self.bus.publish(&InputEvent::Button(KeyState { + self.bus.publish(InputEvent::Button(KeyState { state: ButtonState::Released, button: Key::Escape, }))?; @@ -163,12 +163,12 @@ impl Driver { }, WindowEvent::ModifiersChanged(state) => { if state.shift() { - self.bus.publish(&InputEvent::Button(KeyState { + self.bus.publish(InputEvent::Button(KeyState { state: ButtonState::Pressed, button: Key::Shift, }))?; } else { - self.bus.publish(&InputEvent::Button(KeyState { + self.bus.publish(InputEvent::Button(KeyState { state: ButtonState::Released, button: Key::Shift, }))?; @@ -188,11 +188,11 @@ impl Driver { .unwrap() .mouse(); // Publish two events: One for the absolute mouse position, one for the mouse movement - self.bus.publish(&InputEvent::MousePosition(MousePosition { + self.bus.publish(InputEvent::MousePosition(MousePosition { x: position.x, y: position.y, }))?; - self.bus.publish(&InputEvent::MouseMove(MouseDelta { + self.bus.publish(InputEvent::MouseMove(MouseDelta { x: position.x - prev.x, y: position.y - prev.y, }))?; @@ -209,13 +209,13 @@ impl Driver { } => { match delta { MouseScrollDelta::LineDelta(x, y) => { - self.bus.publish(&InputEvent::Scroll(ScrollInfo { + self.bus.publish(InputEvent::Scroll(ScrollInfo { delta_x: x, delta_y: y, }))?; } MouseScrollDelta::PixelDelta(px) => { - self.bus.publish(&InputEvent::Scroll(ScrollInfo { + self.bus.publish(InputEvent::Scroll(ScrollInfo { delta_x: px.x as f32, delta_y: px.y as f32, }))?; @@ -227,11 +227,10 @@ impl Driver { button, .. } => { - self.bus - .publish(&InputEvent::MouseButton(MouseButtonState { - state: state.into(), - button: button.into(), - }))?; + self.bus.publish(InputEvent::MouseButton(MouseButtonState { + state: state.into(), + button: button.into(), + }))?; } WindowEvent::TouchpadMagnify { .. diff --git a/crates/gui/src/editor/brushes.rs b/crates/gui/src/editor/brushes.rs index b1fef08..2954020 100644 --- a/crates/gui/src/editor/brushes.rs +++ b/crates/gui/src/editor/brushes.rs @@ -25,7 +25,7 @@ impl BrushWidget { match &self.active_brush { None => {} Some(brush) => { - self.bus.publish(&BeginStrokeEvent { + self.bus.publish(BeginStrokeEvent { settings: self.settings, brush: *brush, })?; @@ -40,7 +40,7 @@ impl BrushWidget { let mut overlay = di.write_sync::().unwrap(); overlay.brush_decal = None; } - self.bus.publish(&EndStrokeEvent)?; + self.bus.publish(EndStrokeEvent)?; Ok(()) } } @@ -170,7 +170,7 @@ impl BrushWidget { x: mouse.x - left_top.x as f64, y: mouse.y - left_top.y as f64, }; - self.bus.publish(&DragWorldView { + self.bus.publish(DragWorldView { position: window_space_pos, })?; } diff --git a/crates/gui/src/editor/camera_controller.rs b/crates/gui/src/editor/camera_controller.rs index 038012f..0b2ef33 100644 --- a/crates/gui/src/editor/camera_controller.rs +++ b/crates/gui/src/editor/camera_controller.rs @@ -6,7 +6,7 @@ use scheduler::EventBus; /// Enable the camera controls when this widget is hovered pub fn enable_camera_over(response: &egui::Response, bus: &EventBus) -> Result<()> { let hover = response.hovered(); - bus.publish(&EnableCameraEvent { + bus.publish(EnableCameraEvent { enabled: hover, })?; Ok(()) diff --git a/crates/hot_reload/src/dynamic_pipeline_builder.rs b/crates/hot_reload/src/dynamic_pipeline_builder.rs index 1b5101e..1d60feb 100644 --- a/crates/hot_reload/src/dynamic_pipeline_builder.rs +++ b/crates/hot_reload/src/dynamic_pipeline_builder.rs @@ -53,7 +53,7 @@ impl DynamicPipelineBuilder { .shaders .into_iter() .map(|shader| { - bus.publish(&AddShaderEvent { + bus.publish(AddShaderEvent { path: shader.path, stage: shader.stage, pipeline: shader.pipeline, @@ -93,7 +93,7 @@ impl DynamicComputePipelineBuilder { cache.create_named_compute_pipeline(pci)?; let shader = self.shader.expect("Must set a shader"); - bus.publish(&AddShaderEvent { + bus.publish(AddShaderEvent { path: shader.path, stage: shader.stage, pipeline: shader.pipeline, diff --git a/crates/scheduler/src/bus.rs b/crates/scheduler/src/bus.rs index b119858..9804417 100644 --- a/crates/scheduler/src/bus.rs +++ b/crates/scheduler/src/bus.rs @@ -30,10 +30,10 @@ impl TypedEventBus { self.systems.push(Box::new(system)); } - fn publish(&self, event: &E, context: &mut EventContext) -> Result> { + fn publish(&self, event: E, context: &mut EventContext) -> Result> { let mut results = Vec::with_capacity(self.systems.len()); for system in &self.systems { - results.push(system.call(event, context)?); + results.push(system.call(&event, context)?); } Ok(results) } @@ -137,7 +137,7 @@ impl EventBus { } /// Publish an event to the bus - pub fn publish(&self, event: &E) -> Result> { + pub fn publish(&self, event: E) -> Result> { // Note: We only lock the entire bus for a short time to get access to the registry. // After that we only lock the individual event bus. This will cause the program to deadlock when recursively // triggering events, which is not something that is supported anyway. diff --git a/crates/scheduler/src/event.rs b/crates/scheduler/src/event.rs index 4811c4e..4001d9c 100644 --- a/crates/scheduler/src/event.rs +++ b/crates/scheduler/src/event.rs @@ -20,7 +20,7 @@ impl EventContext { } } - pub fn publish(&mut self, event: &E) -> Result> { + pub fn publish(&mut self, event: E) -> Result> { self.bus.publish(event) } diff --git a/crates/scheduler/src/handler.rs b/crates/scheduler/src/handler.rs index f70231c..d2b882a 100644 --- a/crates/scheduler/src/handler.rs +++ b/crates/scheduler/src/handler.rs @@ -25,3 +25,18 @@ impl< self(system, event, context) } } + +pub trait SinkHandler { + fn handle(&self, system: &mut S, event: E, context: &mut EventContext) -> Result; +} + +impl SinkHandler for F +where + E: Event + 'static, + T: 'static, + F: Fn(&mut S, E, &mut EventContext) -> Result, +{ + fn handle(&self, system: &mut S, event: E, context: &mut EventContext) -> Result { + self(system, event, context) + } +} diff --git a/crates/scheduler/src/system.rs b/crates/scheduler/src/system.rs index b187d57..d470559 100644 --- a/crates/scheduler/src/system.rs +++ b/crates/scheduler/src/system.rs @@ -7,6 +7,7 @@ use crate::bus::EventBus; use crate::caller::Caller; use crate::event::{Event, EventContext}; use crate::handler::Handler; +use crate::SinkHandler; /// A system must implement this to subscribe to events on the bus pub trait System { @@ -32,6 +33,18 @@ impl StoredSystemInner { .ok_or_else(|| anyhow!("No handler for this event"))?; handler.handle(&mut self.state, event, context) } + + fn handle_sink( + &mut self, + event: E, + context: &mut EventContext, + ) -> Result { + let handler = self + .handlers + .get_dyn::>() + .ok_or_else(|| anyhow!("No sink handler for this event"))?; + handler.handle(&mut self.state, event, context) + } } /// A system stored in the event bus. It is created for you when adding a system. @@ -61,6 +74,17 @@ impl StoredSystem { .handlers .put_dyn::>(handler); } + + pub(crate) fn subscribe_sink( + &self, + handler: impl SinkHandler + 'static, + ) { + self.0 + .lock() + .unwrap() + .handlers + .put_dyn::>(handler); + } } impl Caller for StoredSystem { diff --git a/data/heightmaps/iceland.nc b/data/heightmaps/iceland.nc deleted file mode 100644 index f8487de..0000000 Binary files a/data/heightmaps/iceland.nc and /dev/null differ