summaryrefslogtreecommitdiff
path: root/src/devices/input_device.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/devices/input_device.rs')
-rw-r--r--src/devices/input_device.rs265
1 files changed, 144 insertions, 121 deletions
diff --git a/src/devices/input_device.rs b/src/devices/input_device.rs
index 9b7038c..3ebeb4c 100644
--- a/src/devices/input_device.rs
+++ b/src/devices/input_device.rs
@@ -1,84 +1,149 @@
use crate::*;
-use bedrock_core::*;
-use phosphor::*;
use std::collections::VecDeque;
-macro_rules! fn_on_scroll {
- ($fn_name:ident($value:ident, $delta:ident)) => {
- pub fn $fn_name(&mut self, delta: f32) {
- self.$delta += delta;
- while self.$delta >= 1.0 {
- self.$value = self.$value.saturating_add(1);
- self.$delta -= 1.0;
- self.wake = true;
- }
- while self.$delta <= -1.0 {
- self.$value = self.$value.saturating_sub(1);
- self.$delta += 1.0;
- self.wake = true;
- }
- }
- };
-}
-
pub struct InputDevice {
- pub wake: bool,
- pub accessed: bool,
-
- pub pointer_active: bool,
- pub pointer_buttons: u8,
- pub position: ScreenPosition,
+ pub cursor: ScreenPosition,
pub x_read: u16,
pub y_read: u16,
+ pub h_scroll_read: i16,
+ pub v_scroll_read: i16,
+ pub h_scroll: f32,
+ pub v_scroll: f32,
+ pub pointer_buttons: u8,
+ pub pointer_active: bool,
- pub h_scroll: i8,
- pub v_scroll: i8,
- pub h_scroll_delta: f32,
- pub v_scroll_delta: f32,
-
- pub keyboard_active: bool,
- pub characters: VecDeque<u8>,
pub navigation: u8,
pub modifiers: u8,
+ pub characters: VecDeque<u8>,
+ pub gamepad_1: OwnedGamepad,
+ pub gamepad_2: OwnedGamepad,
+ pub gamepad_3: OwnedGamepad,
+ pub gamepad_4: OwnedGamepad,
+
+ pub accessed: bool,
+ pub wake: bool,
+}
+
+
+impl Device for InputDevice {
+ fn read(&mut self, port: u8) -> u8 {
+ self.accessed = true;
+ match port {
+ 0x0 => { self.x_read = self.cursor.x; read_h!(self.x_read) },
+ 0x1 => read_l!(self.cursor.x),
+ 0x2 => { self.y_read = self.cursor.y; read_h!(self.y_read) },
+ 0x3 => read_l!(self.cursor.y),
+ 0x4 => { self.update_horizontal_scroll(); read_h!(self.h_scroll_read) },
+ 0x5 => read_l!(self.h_scroll_read),
+ 0x6 => { self.update_vertical_scroll(); read_h!(self.v_scroll_read) },
+ 0x7 => read_l!(self.v_scroll_read),
+ 0x8 => read_b!(self.pointer_active),
+ 0x9 => self.pointer_buttons,
+ 0xA => self.characters.pop_front().unwrap_or(0),
+ 0xB => self.modifiers,
+ 0xC => self.gamepad_1.state() | self.navigation,
+ 0xD => self.gamepad_2.state(),
+ 0xE => self.gamepad_3.state(),
+ 0xF => self.gamepad_4.state(),
+ _ => unreachable!(),
+ }
+ }
- pub gamepad_1: u8,
- pub gamepad_2: u8,
- pub gamepad_3: u8,
- pub gamepad_4: u8,
+ fn write(&mut self, port: u8, _value: u8) -> Option<Signal> {
+ let signal = if self.accessed { None } else { Some(Signal::Break) };
+ self.accessed = true;
+ match port {
+ 0x0 => (),
+ 0x1 => (),
+ 0x2 => (),
+ 0x3 => (),
+ 0x4 => (),
+ 0x5 => (),
+ 0x6 => (),
+ 0x7 => (),
+ 0x8 => (),
+ 0x9 => (),
+ 0xA => self.characters.clear(),
+ 0xB => (),
+ 0xC => (),
+ 0xD => (),
+ 0xE => (),
+ 0xF => (),
+ _ => unreachable!(),
+ };
+ return signal;
+ }
+
+ fn wake(&mut self) -> bool {
+ self.accessed = true;
+ std::mem::take(&mut self.wake)
+ }
+
+ fn reset(&mut self) {
+ self.cursor = ScreenPosition::ZERO;
+ self.x_read = 0;
+ self.y_read = 0;
+ self.h_scroll_read = 0;
+ self.v_scroll_read = 0;
+ self.h_scroll = 0.0;
+ self.v_scroll = 0.0;
+ self.pointer_active = false;
+ self.pointer_buttons = 0;
+
+ self.navigation = 0;
+ self.modifiers = 0;
+ self.characters.clear();
+ self.gamepad_1.reset();
+ self.gamepad_2.reset();
+ self.gamepad_3.reset();
+ self.gamepad_4.reset();
+
+ self.accessed = false;
+ self.wake = false;
+ }
}
+
impl InputDevice {
pub fn new() -> Self {
Self {
- wake: false,
- accessed: false,
-
- pointer_active: false,
- pointer_buttons: 0,
-
- position: ScreenPosition::ZERO,
+ cursor: ScreenPosition::ZERO,
x_read: 0,
y_read: 0,
+ h_scroll_read: 0,
+ v_scroll_read: 0,
+ h_scroll: 0.0,
+ v_scroll: 0.0,
+ pointer_active: false,
+ pointer_buttons: 0,
- h_scroll: 0,
- v_scroll: 0,
- h_scroll_delta: 0.0,
- v_scroll_delta: 0.0,
-
- keyboard_active: true,
- characters: VecDeque::new(),
- modifiers: 0,
navigation: 0,
+ modifiers: 0,
+ characters: VecDeque::new(),
+ gamepad_1: OwnedGamepad::new(1),
+ gamepad_2: OwnedGamepad::new(2),
+ gamepad_3: OwnedGamepad::new(3),
+ gamepad_4: OwnedGamepad::new(4),
- gamepad_1: 0,
- gamepad_2: 0,
- gamepad_3: 0,
- gamepad_4: 0,
+ accessed: false,
+ wake: false,
}
}
+ #[cfg(feature = "gamepad")]
+ pub fn on_gamepad_event(&mut self, event: gilrs::Event) {
+ if let Some(g) = self.gamepad_1.register(event.id) {
+ self.wake |= g.process_event(&event); return; }
+ if let Some(g) = self.gamepad_2.register(event.id) {
+ self.wake |= g.process_event(&event); return; }
+ if let Some(g) = self.gamepad_3.register(event.id) {
+ self.wake |= g.process_event(&event); return; }
+ if let Some(g) = self.gamepad_4.register(event.id) {
+ self.wake |= g.process_event(&event); return; }
+ }
+
pub fn on_cursor_enter(&mut self) {
self.pointer_active = true;
self.wake = true;
@@ -90,12 +155,13 @@ impl InputDevice {
}
pub fn on_cursor_move(&mut self, position: Position) {
- let screen_position = ScreenPosition {
+ self.pointer_active = true;
+ let cursor_position = ScreenPosition {
x: position.x as i16 as u16,
y: position.y as i16 as u16,
};
- if self.position != screen_position {
- self.position = screen_position;
+ if self.cursor != cursor_position {
+ self.cursor = cursor_position;
self.wake = true;
}
}
@@ -103,8 +169,8 @@ impl InputDevice {
pub fn on_mouse_button(&mut self, button: MouseButton, action: Action) {
let mask = match button {
MouseButton::Left => 0x80,
- MouseButton::Middle => 0x40,
- MouseButton::Right => 0x20,
+ MouseButton::Right => 0x40,
+ MouseButton::Middle => 0x20,
_ => return,
};
let pointer_buttons = match action {
@@ -117,15 +183,26 @@ impl InputDevice {
}
}
- fn_on_scroll!(on_horizontal_scroll(h_scroll, h_scroll_delta));
- fn_on_scroll!(on_vertical_scroll(v_scroll, v_scroll_delta));
+ pub fn on_horizontal_scroll(&mut self, delta: f32) {
+ self.h_scroll += delta;
+ self.h_scroll = self.h_scroll.clamp(-32768.0, 32767.0);
+ if self.h_scroll.abs() >= 1.0 { self.wake = true; }
+ }
- pub fn read_horizontal_scroll(&mut self) -> u8 {
- std::mem::take(&mut self.h_scroll) as u8
+ pub fn on_vertical_scroll(&mut self, delta: f32) {
+ self.v_scroll += delta;
+ self.v_scroll = self.v_scroll.clamp(i16::MIN as f32, i16::MAX as f32);
+ if self.v_scroll.abs() >= 1.0 { self.wake = true; }
}
- pub fn read_vertical_scroll(&mut self) -> u8 {
- std::mem::take(&mut self.v_scroll) as u8
+ pub fn update_horizontal_scroll(&mut self) {
+ self.h_scroll_read = self.h_scroll.trunc() as i16;
+ self.h_scroll -= self.h_scroll.trunc();
+ }
+
+ pub fn update_vertical_scroll(&mut self) {
+ self.v_scroll_read = self.v_scroll.trunc() as i16;
+ self.v_scroll -= self.v_scroll.trunc();
}
pub fn on_character(&mut self, character: char) {
@@ -178,57 +255,3 @@ impl InputDevice {
}
}
}
-
-impl Device for InputDevice {
- fn read(&mut self, port: u8) -> u8 {
- self.accessed = true;
- match port {
- 0x0 => read_b!(self.pointer_active),
- 0x1 => self.pointer_buttons,
- 0x2 => self.read_horizontal_scroll(),
- 0x3 => self.read_vertical_scroll(),
- 0x4 => { self.x_read = self.position.x as u16; read_h!(self.x_read) },
- 0x5 => read_l!(self.position.x),
- 0x6 => { self.y_read = self.position.y as u16; read_h!(self.y_read) },
- 0x7 => read_l!(self.position.y),
- 0x8 => read_b!(self.keyboard_active),
- 0x9 => self.characters.pop_front().unwrap_or(0),
- 0xa => self.navigation,
- 0xb => self.modifiers,
- 0xc => self.gamepad_1,
- 0xd => self.gamepad_2,
- 0xe => self.gamepad_3,
- 0xf => self.gamepad_4,
- _ => unreachable!(),
- }
- }
-
- fn write(&mut self, port: u8, _value: u8) -> Option<Signal> {
- self.accessed = true;
- match port {
- 0x0 => (),
- 0x1 => (),
- 0x2 => (),
- 0x3 => (),
- 0x4 => (),
- 0x5 => (),
- 0x6 => (),
- 0x7 => (),
- 0x8 => (),
- 0x9 => self.characters.clear(),
- 0xa => (),
- 0xb => (),
- 0xc => (),
- 0xd => (),
- 0xe => (),
- 0xf => (),
- _ => unreachable!(),
- };
- return None;
- }
-
- fn wake(&mut self) -> bool {
- self.accessed = true;
- std::mem::take(&mut self.wake)
- }
-}