diff options
Diffstat (limited to 'stockton-input')
-rw-r--r-- | stockton-input/Cargo.toml | 7 | ||||
-rw-r--r-- | stockton-input/src/axis.rs | 62 | ||||
-rw-r--r-- | stockton-input/src/button.rs | 74 | ||||
-rw-r--r-- | stockton-input/src/lib.rs | 24 | ||||
-rw-r--r-- | stockton-input/src/manager.rs | 50 |
5 files changed, 217 insertions, 0 deletions
diff --git a/stockton-input/Cargo.toml b/stockton-input/Cargo.toml new file mode 100644 index 0000000..39c0666 --- /dev/null +++ b/stockton-input/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "stockton-input" +version = "0.1.0" +authors = ["Oscar Shrimpton <oscar.shrimpton.personal@gmail.com>"] +edition = "2018" + +[dependencies]
\ No newline at end of file diff --git a/stockton-input/src/axis.rs b/stockton-input/src/axis.rs new file mode 100644 index 0000000..ebf52bb --- /dev/null +++ b/stockton-input/src/axis.rs @@ -0,0 +1,62 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +use std::fmt::Debug; +use std::ops::{Deref, DerefMut}; + +#[derive(Debug, Clone)] +/// A linear axis, usually with a value from -1 to 1. +pub struct Axis(i8); + +impl Axis { + /// Get a new instance with the value set to zero + pub fn zero() -> Self { + Axis(0) + } + + /// Get the normalized value, ie always positive. + pub fn normalized(&self) -> i8 { + if self.0 < 0 { + -self.0 + } else { + self.0 + } + } + + pub fn modify(&mut self, val: i8) { + self.0 += val + } +} + +impl Default for Axis { + fn default() -> Self { + Self::zero() + } +} + +impl Deref for Axis { + type Target = i8; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Axis { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} diff --git a/stockton-input/src/button.rs b/stockton-input/src/button.rs new file mode 100644 index 0000000..9fd5e84 --- /dev/null +++ b/stockton-input/src/button.rs @@ -0,0 +1,74 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ +use std::fmt::Debug; + +#[derive(Debug, Clone, PartialEq)] +/// A boolean input, with additional tracking for if it just changed state. +pub struct Button { + /// How many of the mapped inputs are currently pressed. + /// This is used so that holding one button, then another, then releasing the first, will keep the button down continuously as expected. + inputs_down: u8, + + /// Whether or not the button changed state in the last batch of actions processed + /// Note that pushing 2 buttons bound to this action one after the other won't trigger this twice. + pub is_hot: bool, +} + +impl Button { + pub fn new() -> Self { + Button { + inputs_down: 0, + is_hot: false, + } + } + + pub fn is_down(&self) -> bool { + self.inputs_down > 0 + } + pub fn is_up(&self) -> bool { + self.inputs_down == 0 + } + + pub fn is_just_down(&self) -> bool { + self.is_down() && self.is_hot + } + pub fn is_just_up(&self) -> bool { + self.is_up() && self.is_hot + } + + pub fn modify_inputs(&mut self, add: bool) { + self.inputs_down = if add { + self.inputs_down + 1 + } else { + self.inputs_down - 1 + }; + + if self.inputs_down == 1 || self.inputs_down == 0 { + self.is_hot = true; + } + } + + pub fn set_not_hot(&mut self) { + self.is_hot = false; + } +} + +impl Default for Button { + fn default() -> Self { + Self::new() + } +} diff --git a/stockton-input/src/lib.rs b/stockton-input/src/lib.rs new file mode 100644 index 0000000..7bb779f --- /dev/null +++ b/stockton-input/src/lib.rs @@ -0,0 +1,24 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +pub mod axis; +pub mod button; +pub mod manager; + +pub use axis::Axis; +pub use button::Button; +pub use manager::*; diff --git a/stockton-input/src/manager.rs b/stockton-input/src/manager.rs new file mode 100644 index 0000000..2b62a93 --- /dev/null +++ b/stockton-input/src/manager.rs @@ -0,0 +1,50 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/// A thing that pressing a button can do to an input. +#[derive(Debug, Clone, Copy)] +pub enum InputMutation { + MapToButton, + NegativeAxis, + PositiveAxis, +} + +/// A key being pressed or released +#[derive(Debug, Clone)] +pub enum Action { + KeyPress(u8), + KeyRelease(u8), +} + +impl Action { + pub fn keycode(&self) -> u8 { + match self { + Action::KeyPress(x) => *x, + Action::KeyRelease(x) => *x, + } + } + pub fn is_down(&self) -> bool { + match self { + Action::KeyPress(_) => true, + Action::KeyRelease(_) => false, + } + } +} + +pub trait InputManager { + fn handle_frame<'a, X: IntoIterator<Item = &'a Action>>(&mut self, actions: X); +} |