You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.7 KiB
92 lines
2.7 KiB
use ggez::{
|
|
event::{EventHandler, KeyCode, KeyMods, MouseButton},
|
|
Context, GameResult,
|
|
};
|
|
|
|
pub struct EventLayer<L: EventHandler> {
|
|
layers: Vec<Box<dyn EventHandler>>,
|
|
render: L,
|
|
}
|
|
|
|
impl<L: EventHandler> EventLayer<L> {
|
|
pub fn new(render: L) -> Self {
|
|
Self {
|
|
layers: vec![],
|
|
render,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<L: EventHandler> EventHandler for EventLayer<L> {
|
|
fn update(&mut self, ctx: &mut Context) -> GameResult<()> {
|
|
self.layers
|
|
.iter_mut()
|
|
.map(|l| l.update(ctx))
|
|
.collect::<GameResult<Vec<()>>>()?;
|
|
|
|
self.render.update(ctx)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
|
|
self.render.draw(ctx)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn mouse_button_down_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
|
|
self.layers
|
|
.iter_mut()
|
|
.map(|l| l.mouse_button_down_event(ctx, button, x, y))
|
|
.collect::<Vec<_>>();
|
|
}
|
|
|
|
fn mouse_button_up_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
|
|
self.layers
|
|
.iter_mut()
|
|
.map(|l| l.mouse_button_up_event(ctx, button, x, y))
|
|
.collect::<Vec<_>>();
|
|
}
|
|
|
|
fn mouse_motion_event(&mut self, ctx: &mut Context, x: f32, y: f32, dx: f32, dy: f32) {
|
|
self.layers
|
|
.iter_mut()
|
|
.map(|l| l.mouse_motion_event(ctx, x, y, dx, dy))
|
|
.collect::<Vec<_>>();
|
|
}
|
|
|
|
/// A keyboard button was pressed.
|
|
///
|
|
/// The default implementation of this will call `ggez::event::quit()`
|
|
/// when the escape key is pressed. If you override this with
|
|
/// your own event handler you have to re-implment that
|
|
/// functionality yourself.
|
|
fn key_down_event(
|
|
&mut self,
|
|
ctx: &mut Context,
|
|
keycode: KeyCode,
|
|
keymods: KeyMods,
|
|
repeat: bool,
|
|
) {
|
|
self.layers
|
|
.iter_mut()
|
|
.map(|l| l.key_down_event(ctx, keycode, keymods, repeat))
|
|
.collect::<Vec<_>>();
|
|
}
|
|
|
|
/// A keyboard button was released.
|
|
fn key_up_event(&mut self, ctx: &mut Context, keycode: KeyCode, keymods: KeyMods) {
|
|
self.layers
|
|
.iter_mut()
|
|
.map(|l| l.key_up_event(ctx, keycode, keymods))
|
|
.collect::<Vec<_>>();
|
|
}
|
|
|
|
/// A unicode character was received, usually from keyboard input.
|
|
/// This is the intended way of facilitating text input.
|
|
fn text_input_event(&mut self, ctx: &mut Context, character: char) {
|
|
self.layers
|
|
.iter_mut()
|
|
.map(|l| l.text_input_event(ctx, character))
|
|
.collect::<Vec<_>>();
|
|
}
|
|
}
|
|
|