aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-08-25 17:44:22 +0100
committertcmal <me@aria.rip>2024-08-25 17:44:22 +0100
commitb380ad94647c9bc446d5a76bb16dcc286077275a (patch)
treeb62b99d26474ce8f7c0caeff37f2e3b9ec166f91 /examples
parent5ad62fff05064a6a025a9b947ebab05ed7770e6c (diff)
feat(input): virtual input mapping and codegen
Diffstat (limited to 'examples')
-rw-r--r--examples/input-codegen/Cargo.toml9
-rw-r--r--examples/input-codegen/src/main.rs86
2 files changed, 95 insertions, 0 deletions
diff --git a/examples/input-codegen/Cargo.toml b/examples/input-codegen/Cargo.toml
new file mode 100644
index 0000000..3fc010f
--- /dev/null
+++ b/examples/input-codegen/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "input-codegen"
+version = "0.1.0"
+authors = ["Oscar Shrimpton <oscar.shrimpton.personal@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+stockton-input = { path = "../../stockton-input" }
+stockton-input-codegen = { path = "../../stockton-input-codegen" }
diff --git a/examples/input-codegen/src/main.rs b/examples/input-codegen/src/main.rs
new file mode 100644
index 0000000..8835f2e
--- /dev/null
+++ b/examples/input-codegen/src/main.rs
@@ -0,0 +1,86 @@
+#[macro_use]
+extern crate stockton_input_codegen;
+
+use std::collections::BTreeMap;
+use stockton_input::Action;
+use stockton_input::{Axis, Button, InputManager, InputMutation};
+
+#[derive(InputManager, Default, Debug, Clone)]
+struct MovementInputs {
+ #[axis]
+ vertical: Axis,
+
+ #[axis]
+ horizontal: Axis,
+
+ #[button]
+ jump: Button,
+}
+
+const TEST_ACTIONS: [Action; 10] = [
+ Action::KeyPress(1),
+ Action::KeyRelease(1),
+ Action::KeyPress(2),
+ Action::KeyPress(3),
+ Action::KeyRelease(2),
+ Action::KeyRelease(3),
+ Action::KeyPress(4),
+ Action::KeyPress(5),
+ Action::KeyRelease(4),
+ Action::KeyRelease(5),
+];
+
+// For testing, 1 = w 2 = a
+// 3 = s 4 = d
+// 5 = jump
+fn main() {
+ let mut action_schema = BTreeMap::new();
+ action_schema.insert(
+ 1,
+ (MovementInputsFields::Vertical, InputMutation::PositiveAxis),
+ );
+ action_schema.insert(
+ 3,
+ (MovementInputsFields::Vertical, InputMutation::NegativeAxis),
+ );
+ action_schema.insert(
+ 4,
+ (
+ MovementInputsFields::Horizontal,
+ InputMutation::PositiveAxis,
+ ),
+ );
+ action_schema.insert(
+ 2,
+ (
+ MovementInputsFields::Horizontal,
+ InputMutation::NegativeAxis,
+ ),
+ );
+ action_schema.insert(5, (MovementInputsFields::Jump, InputMutation::MapToButton));
+
+ let mut manager = MovementInputsManager::new(action_schema);
+
+ for action in TEST_ACTIONS.iter() {
+ pretty_print_state(&manager.inputs);
+ manager.handle_frame(std::iter::once(action));
+ }
+ pretty_print_state(&manager.inputs);
+}
+
+fn pretty_print_state(inputs: &MovementInputs) {
+ if *inputs.vertical != 0 {
+ print!("vertical = {} ", *inputs.vertical);
+ }
+ if *inputs.horizontal != 0 {
+ print!("horizontal = {} ", *inputs.horizontal);
+ }
+ if inputs.jump.is_down() {
+ if inputs.jump.is_hot {
+ print!("jump!")
+ } else {
+ print!("jump")
+ }
+ }
+ println!();
+}