Add InputConfig

This commit is contained in:
Drakulix 2017-03-18 17:09:29 +01:00
parent 460630d0c8
commit 8e92c9c922
2 changed files with 33 additions and 9 deletions

View File

@ -198,12 +198,15 @@ pub struct GlutinInputBackend
window: Rc<Window>,
time_counter: u32,
seat: Seat,
handler: Option<Box<InputHandler + 'static>>,
input_config: (),
handler: Option<Box<InputHandler<GlutinInputBackend> + 'static>>,
}
impl InputBackend for GlutinInputBackend
{
fn set_handler<H: InputHandler + 'static>(&mut self, mut handler: H) {
type InputConfig = ();
fn set_handler<H: InputHandler<Self> + 'static>(&mut self, mut handler: H) {
if self.handler.is_some() {
self.clear_handler();
}
@ -211,9 +214,9 @@ impl InputBackend for GlutinInputBackend
self.handler = Some(Box::new(handler));
}
fn get_handler(&mut self) -> Option<&mut InputHandler>
fn get_handler(&mut self) -> Option<&mut InputHandler<Self>>
{
self.handler.as_mut().map(|handler| handler as &mut InputHandler)
self.handler.as_mut().map(|handler| handler as &mut InputHandler<Self>)
}
fn clear_handler(&mut self) {
@ -222,6 +225,10 @@ impl InputBackend for GlutinInputBackend
}
}
fn input_config(&mut self) -> &mut Self::InputConfig {
&mut self.input_config
}
fn set_cursor_position(&mut self, x: u32, y: u32) -> Result<(), ()> {
if let Some((win_x, win_y)) = self.window.get_position() {
self.window.set_cursor_position(win_x + x as i32, win_y + y as i32)
@ -239,6 +246,7 @@ impl GlutinInputBackend
window: window,
time_counter: 0,
seat: Seat::new(0),
input_config: (),
handler: None,
}
}

View File

@ -158,13 +158,19 @@ pub enum TouchEvent
/// Trait that describes objects providing a source of input events. All input backends
/// need to implemenent this and provide the same base gurantees about the presicion of
/// given events.
pub trait InputBackend {
pub trait InputBackend: Sized {
/// Type of input device associated with the backend
type InputConfig;
/// Sets a new handler for this `InputBackend`
fn set_handler<H: InputHandler + 'static>(&mut self, handler: H);
fn set_handler<H: InputHandler<Self> + 'static>(&mut self, handler: H);
/// Get a reference to the currently set handler, if any
fn get_handler(&mut self) -> Option<&mut InputHandler>;
fn get_handler(&mut self) -> Option<&mut InputHandler<Self>>;
/// Clears the currently handler, if one is set
fn clear_handler(&mut self);
/// Get current `InputConfig`
fn input_config(&mut self) -> &mut Self::InputConfig;
/// Sets the cursor position, useful for e.g. pointer wrapping.
///
/// Not guaranteed to be supported on every backend. The result usually
@ -174,7 +180,7 @@ pub trait InputBackend {
}
/// Implement to receive input events from any `InputBackend`.
pub trait InputHandler {
pub trait InputHandler<B: InputBackend> {
/// Called when a new `Seat` has been created
fn on_seat_created(&mut self, seat: &Seat);
/// Called when an existing `Seat` has been destroyed.
@ -244,9 +250,15 @@ pub trait InputHandler {
/// - check if events can arrive out of order.
/// - Make stronger time guarantees
fn on_touch(&mut self, seat: &Seat, time: u32, event: TouchEvent);
/// Called when the `InputConfig` was changed through an external event.
///
/// What kind of events can trigger this call is completely backend dependent.
/// E.g. an input devices was attached/detached or changed it's own configuration.
fn on_input_config_changed(&mut self, config: &mut B::InputConfig);
}
impl InputHandler for Box<InputHandler> {
impl<B: InputBackend> InputHandler<B> for Box<InputHandler<B>> {
fn on_seat_created(&mut self, seat: &Seat) {
(**self).on_seat_created(seat)
}
@ -274,4 +286,8 @@ impl InputHandler for Box<InputHandler> {
fn on_touch(&mut self, seat: &Seat, time: u32, event: TouchEvent) {
(**self).on_touch(seat, time, event)
}
fn on_input_config_changed(&mut self, config: &mut B::InputConfig) {
(**self).on_input_config_changed(config)
}
}