Skip to content

Commit

Permalink
change: event bus publish takes ownership of event
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAPenguin0 committed May 22, 2023
1 parent cad355c commit e4b06d6
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 25 deletions.
29 changes: 14 additions & 15 deletions crates/app/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Driver {
let mut world = inject.write_sync::<World>().unwrap();
let assets = inject.get::<AssetStorage>().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,
}));
Expand Down Expand Up @@ -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::<World>().unwrap();
Expand Down Expand Up @@ -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,
}))?;
Expand All @@ -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,
}))?;
Expand All @@ -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,
}))?;
Expand All @@ -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,
}))?;
Expand All @@ -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 {
..
Expand Down
6 changes: 3 additions & 3 deletions crates/gui/src/editor/brushes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})?;
Expand All @@ -40,7 +40,7 @@ impl BrushWidget {
let mut overlay = di.write_sync::<WorldOverlayInfo>().unwrap();
overlay.brush_decal = None;
}
self.bus.publish(&EndStrokeEvent)?;
self.bus.publish(EndStrokeEvent)?;
Ok(())
}
}
Expand Down Expand Up @@ -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,
})?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/gui/src/editor/camera_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DI>) -> Result<()> {
let hover = response.hovered();
bus.publish(&EnableCameraEvent {
bus.publish(EnableCameraEvent {
enabled: hover,
})?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/hot_reload/src/dynamic_pipeline_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions crates/scheduler/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ impl<E: Event + 'static, T: 'static> TypedEventBus<E, T> {
self.systems.push(Box::new(system));
}

fn publish(&self, event: &E, context: &mut EventContext<T>) -> Result<Vec<E::Result>> {
fn publish(&self, event: E, context: &mut EventContext<T>) -> Result<Vec<E::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)
}
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<T: Clone + Send + Sync + 'static> EventBus<T> {
}

/// Publish an event to the bus
pub fn publish<E: Event + 'static>(&self, event: &E) -> Result<Vec<E::Result>> {
pub fn publish<E: Event + 'static>(&self, event: E) -> Result<Vec<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.
Expand Down
2 changes: 1 addition & 1 deletion crates/scheduler/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<T: Clone + Send + Sync + 'static> EventContext<T> {
}
}

pub fn publish<E: Event + 'static>(&mut self, event: &E) -> Result<Vec<E::Result>> {
pub fn publish<E: Event + 'static>(&mut self, event: E) -> Result<Vec<E::Result>> {
self.bus.publish(event)
}

Expand Down
15 changes: 15 additions & 0 deletions crates/scheduler/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ impl<
self(system, event, context)
}
}

pub trait SinkHandler<S, E: Event, T: 'static> {
fn handle(&self, system: &mut S, event: E, context: &mut EventContext<T>) -> Result<E::Result>;
}

impl<S, E, T, F> SinkHandler<S, E, T> for F
where
E: Event + 'static,
T: 'static,
F: Fn(&mut S, E, &mut EventContext<T>) -> Result<E::Result>,
{
fn handle(&self, system: &mut S, event: E, context: &mut EventContext<T>) -> Result<E::Result> {
self(system, event, context)
}
}
24 changes: 24 additions & 0 deletions crates/scheduler/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand All @@ -32,6 +33,18 @@ impl<S: 'static> StoredSystemInner<S> {
.ok_or_else(|| anyhow!("No handler for this event"))?;
handler.handle(&mut self.state, event, context)
}

fn handle_sink<E: Event + 'static, T: 'static>(
&mut self,
event: E,
context: &mut EventContext<T>,
) -> Result<E::Result> {
let handler = self
.handlers
.get_dyn::<dyn SinkHandler<S, E, T>>()
.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.
Expand Down Expand Up @@ -61,6 +74,17 @@ impl<S: 'static> StoredSystem<S> {
.handlers
.put_dyn::<dyn Handler<S, E, T>>(handler);
}

pub(crate) fn subscribe_sink<E: Event + 'static, T: 'static>(
&self,
handler: impl SinkHandler<S, E, T> + 'static,
) {
self.0
.lock()
.unwrap()
.handlers
.put_dyn::<dyn SinkHandler<S, E, T>>(handler);
}
}

impl<S: 'static, E: Event + 'static, T: 'static> Caller<E, T> for StoredSystem<S> {
Expand Down
Binary file removed data/heightmaps/iceland.nc
Binary file not shown.

0 comments on commit e4b06d6

Please sign in to comment.