Add InputConfig
This commit is contained in:
parent
460630d0c8
commit
8e92c9c922
|
@ -198,12 +198,15 @@ pub struct GlutinInputBackend
|
||||||
window: Rc<Window>,
|
window: Rc<Window>,
|
||||||
time_counter: u32,
|
time_counter: u32,
|
||||||
seat: Seat,
|
seat: Seat,
|
||||||
handler: Option<Box<InputHandler + 'static>>,
|
input_config: (),
|
||||||
|
handler: Option<Box<InputHandler<GlutinInputBackend> + 'static>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputBackend for GlutinInputBackend
|
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() {
|
if self.handler.is_some() {
|
||||||
self.clear_handler();
|
self.clear_handler();
|
||||||
}
|
}
|
||||||
|
@ -211,9 +214,9 @@ impl InputBackend for GlutinInputBackend
|
||||||
self.handler = Some(Box::new(handler));
|
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) {
|
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<(), ()> {
|
fn set_cursor_position(&mut self, x: u32, y: u32) -> Result<(), ()> {
|
||||||
if let Some((win_x, win_y)) = self.window.get_position() {
|
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)
|
self.window.set_cursor_position(win_x + x as i32, win_y + y as i32)
|
||||||
|
@ -239,6 +246,7 @@ impl GlutinInputBackend
|
||||||
window: window,
|
window: window,
|
||||||
time_counter: 0,
|
time_counter: 0,
|
||||||
seat: Seat::new(0),
|
seat: Seat::new(0),
|
||||||
|
input_config: (),
|
||||||
handler: None,
|
handler: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,13 +158,19 @@ pub enum TouchEvent
|
||||||
/// Trait that describes objects providing a source of input events. All input backends
|
/// 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
|
/// need to implemenent this and provide the same base gurantees about the presicion of
|
||||||
/// given events.
|
/// 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`
|
/// 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
|
/// 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
|
/// Clears the currently handler, if one is set
|
||||||
fn clear_handler(&mut self);
|
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.
|
/// Sets the cursor position, useful for e.g. pointer wrapping.
|
||||||
///
|
///
|
||||||
/// Not guaranteed to be supported on every backend. The result usually
|
/// 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`.
|
/// Implement to receive input events from any `InputBackend`.
|
||||||
pub trait InputHandler {
|
pub trait InputHandler<B: InputBackend> {
|
||||||
/// Called when a new `Seat` has been created
|
/// Called when a new `Seat` has been created
|
||||||
fn on_seat_created(&mut self, seat: &Seat);
|
fn on_seat_created(&mut self, seat: &Seat);
|
||||||
/// Called when an existing `Seat` has been destroyed.
|
/// Called when an existing `Seat` has been destroyed.
|
||||||
|
@ -244,9 +250,15 @@ pub trait InputHandler {
|
||||||
/// - check if events can arrive out of order.
|
/// - check if events can arrive out of order.
|
||||||
/// - Make stronger time guarantees
|
/// - Make stronger time guarantees
|
||||||
fn on_touch(&mut self, seat: &Seat, time: u32, event: TouchEvent);
|
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) {
|
fn on_seat_created(&mut self, seat: &Seat) {
|
||||||
(**self).on_seat_created(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) {
|
fn on_touch(&mut self, seat: &Seat, time: u32, event: TouchEvent) {
|
||||||
(**self).on_touch(seat, time, event)
|
(**self).on_touch(seat, time, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn on_input_config_changed(&mut self, config: &mut B::InputConfig) {
|
||||||
|
(**self).on_input_config_changed(config)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue