Commit c41c3118 authored by Florian Blasius's avatar Florian Blasius 🤘

[#168] add key and resize event.

parent 519ce936
use std::{
cell::RefCell,
collections::BTreeMap,
rc::Rc,
};
use std::{cell::RefCell, collections::BTreeMap, rc::Rc};
use dces::prelude::{Entity, World};
......@@ -42,26 +38,59 @@ impl backend::Updater for WorldWrapper {
impl backend::WindowAdapter for WindowAdapter {
fn update(&mut self) {}
fn resize(&mut self, width: f64, height: f64) {
self.event_queue
.register_event(WindowEvent::Resize { width, height }, self.root);
}
fn mouse(&mut self, x: f64, y: f64) {
self.event_queue.register_event(MouseMoveEvent {
position: Point::new(x, y)
}, self.root)
}
fn mouse_event(&mut self, event: backend::MouseEvent) {
match event.state {
backend::ButtonState::Up => {
self.event_queue.register_event(MouseUpEvent {
backend::ButtonState::Up => self.event_queue.register_event(
MouseUpEvent {
x: event.x,
y: event.y,
button: event.button,
}, self.root)
},
backend::ButtonState::Down => {
self.event_queue.register_event(MouseDownEvent {
},
self.root,
),
backend::ButtonState::Down => self.event_queue.register_event(
MouseDownEvent {
x: event.x,
y: event.y,
button: event.button,
},
self.root,
),
}
}
}, self.root)
}
fn key_event(&mut self, event: backend::KeyEvent) {
match event.state {
backend::ButtonState::Up => self.event_queue.register_event(
KeyUpEvent {
key: event.key
},
self.root,
),
backend::ButtonState::Down => self.event_queue.register_event(
KeyDownEvent {
key: event.key
},
self.root,
),
}
// self.event_queue.register_event(event: E, source: Entity)
}
fn quite_event(&mut self) {
self.event_queue
.register_event(SystemEvent::Quit, self.root);
}
}
......@@ -72,4 +101,4 @@ impl Into<Box<backend::WindowAdapter>> for WindowAdapter {
}
#[derive(Default, Debug)]
pub struct ContextProvider {}
\ No newline at end of file
pub struct ContextProvider {}
......@@ -69,10 +69,14 @@ pub trait FontMeasure {
pub trait WindowAdapter {
fn update(&mut self);
fn resize(&mut self, width: f64, height: f64);
fn mouse(&mut self, x: f64, y: f64);
fn mouse_event(&mut self, event: MouseEvent);
fn key_event(&mut self, event: KeyEvent);
fn quite_event(&mut self);
}
pub use self::platform::{WindowBuilder, ShellRunner, WindowShell, Updater, MouseButton, MouseEvent, ButtonState };
pub use self::platform::{WindowBuilder, ShellRunner, WindowShell, Updater, MouseButton, MouseEvent, ButtonState, KeyEvent };
pub use self::platform::FONT_MEASURE;
#[cfg(not(platform_arch = "wasm32"))]
......
......@@ -33,6 +33,13 @@ pub struct MouseEvent {
pub state: ButtonState
}
#[derive(Debug)]
pub struct KeyEvent {
pub key: Key,
pub state: ButtonState
}
// todo: generic adapter A where A : ShellAdapber...
/// Implementation of the OrbClient based backend.
......@@ -76,14 +83,10 @@ impl<A> WindowShell<A> where A: WindowAdapter {
for event in self.inner.events() {
match event.to_option() {
orbclient::EventOption::Mouse(mouse) => {
self.mouse_position.x = mouse.x as f64;
self.mouse_position.y = mouse.y as f64;
// self.event_queue
// .borrow_mut()
// .register_event(MouseMoveEvent {
// position: self.mouse_position,
// });
orbclient::EventOption::Mouse(event) => {
self.mouse_position.x = event.x as f64;
self.mouse_position.y = event.y as f64;
self.adapter.mouse(event.x as f64, event.y as f64);
}
orbclient::EventOption::Button(button) => {
if !button.left && !button.middle && !button.right {
......@@ -140,36 +143,16 @@ impl<A> WindowShell<A> where A: WindowAdapter {
};
if key_event.pressed {
// todo call adapter method
// self.event_queue
// .borrow_mut()
// .register_event(KeyUpEvent { key }, 0);
self.adapter.key_event(KeyEvent { key, state: ButtonState::Up} );
} else {
// todo call adapter method
// self.event_queue
// .borrow_mut()
// .register_event(KeyDownEvent { key }, 0);
self.adapter.key_event(KeyEvent { key, state: ButtonState::Down} );
}
}
orbclient::EventOption::Quit(_quit_event) => {
// todo call adapter method
// self.event_queue
// .borrow_mut()
// .register_event(SystemEvent::Quit, 0);
self.adapter.quite_event();
}
orbclient::EventOption::Resize(resize_event) => {
// todo call adapter method
// self.event_queue.borrow_mut().register_event(
// WindowEvent::Resize {
// width: resize_event.width as f64,
// height: resize_event.height as f64,
// },
// 0,
// );
orbclient::EventOption::Resize(event) => {
self.adapter.resize(event.width as f64, event.height as f64);
}
_ => {}
}
......
......@@ -4,7 +4,7 @@ use std::sync::Arc;
use orbfont::Font;
pub use self::backend::{WindowBuilder, WindowShell, ShellRunner, Updater, MouseButton, MouseEvent, ButtonState };
pub use self::backend::{WindowBuilder, WindowShell, ShellRunner, Updater, MouseButton, MouseEvent, ButtonState, KeyEvent };
use crate::backend::*;
......
......@@ -72,6 +72,7 @@ impl EventQueue {
.push(EventBox::new::<E>(event, strategy, source));
}
// todo rename to enqueue event
pub fn register_event<E: Event>(&mut self, event: E, source: Entity) {
self.event_queue
.push(EventBox::new::<E>(event, EventStrategy::BottomUp, source));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment