diff options
Diffstat (limited to 'src/devices/input_device.rs')
-rw-r--r-- | src/devices/input_device.rs | 202 |
1 files changed, 105 insertions, 97 deletions
diff --git a/src/devices/input_device.rs b/src/devices/input_device.rs index 9b7038c..d2dd682 100644 --- a/src/devices/input_device.rs +++ b/src/devices/input_device.rs @@ -1,35 +1,10 @@ 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, @@ -38,27 +13,83 @@ pub struct InputDevice { pub h_scroll_delta: f32, pub v_scroll_delta: f32, - pub keyboard_active: bool, - pub characters: VecDeque<u8>, + pub pointer_buttons: u8, + pub pointer_active: bool, + pub navigation: u8, pub modifiers: u8, + pub characters: VecDeque<u8>, + pub keyboard_active: bool, pub gamepad_1: u8, pub gamepad_2: u8, pub gamepad_3: u8, pub gamepad_4: u8, + + 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.read_horizontal_scroll(), + 0x5 => self.read_vertical_scroll(), + 0x6 => self.pointer_buttons, + 0x7 => read_b!(self.pointer_active), + 0x8 => self.navigation, + 0x9 => self.modifiers, + 0xa => self.characters.pop_front().unwrap_or(0), + 0xb => read_b!(self.keyboard_active), + 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 => (), + 0xa => self.characters.clear(), + 0xb => (), + 0xc => (), + 0xd => (), + 0xe => (), + 0xf => (), + _ => unreachable!(), + }; + return None; + } + + fn wake(&mut self) -> bool { + self.accessed = true; + std::mem::take(&mut self.wake) + } +} + + 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, @@ -67,15 +98,21 @@ impl InputDevice { h_scroll_delta: 0.0, v_scroll_delta: 0.0, - keyboard_active: true, - characters: VecDeque::new(), - modifiers: 0, + pointer_active: false, + pointer_buttons: 0, + navigation: 0, + modifiers: 0, + characters: VecDeque::new(), + keyboard_active: true, gamepad_1: 0, gamepad_2: 0, gamepad_3: 0, gamepad_4: 0, + + accessed: false, + wake: false, } } @@ -90,12 +127,12 @@ impl InputDevice { } pub fn on_cursor_move(&mut self, position: Position) { - let screen_position = ScreenPosition { + 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; } } @@ -117,8 +154,33 @@ 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 += delta; + while self.h_scroll_delta >= 1.0 { + self.h_scroll = self.h_scroll.saturating_add(1); + self.h_scroll_delta -= 1.0; + self.wake = true; + } + while self.h_scroll_delta <= -1.0 { + self.h_scroll = self.h_scroll.saturating_sub(1); + self.h_scroll_delta += 1.0; + self.wake = true; + } + } + + pub fn on_vertical_scroll(&mut self, delta: f32) { + self.v_scroll_delta += delta; + while self.v_scroll_delta >= 1.0 { + self.v_scroll = self.v_scroll.saturating_add(1); + self.v_scroll_delta -= 1.0; + self.wake = true; + } + while self.v_scroll_delta <= -1.0 { + self.v_scroll = self.v_scroll.saturating_sub(1); + self.v_scroll_delta += 1.0; + self.wake = true; + } + } pub fn read_horizontal_scroll(&mut self) -> u8 { std::mem::take(&mut self.h_scroll) as u8 @@ -178,57 +240,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) - } -} |