aboutsummaryrefslogtreecommitdiff
path: root/examples/input-codegen/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/input-codegen/src/main.rs')
-rw-r--r--examples/input-codegen/src/main.rs86
1 files changed, 86 insertions, 0 deletions
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!();
+}