aboutsummaryrefslogtreecommitdiff
path: root/examples/input-codegen/src/main.rs
blob: 8835f2e9c67493be2eda5d187a2f22f1189476d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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!();
}