-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_controller_state.rs
More file actions
65 lines (58 loc) · 2.4 KB
/
handle_controller_state.rs
File metadata and controls
65 lines (58 loc) · 2.4 KB
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
use winapi::um::xinput::*;
use winapi::shared::minwindef::DWORD;
use super::mutable_xinput_state::*;
use super::function_scheduler::*;
use retour::static_detour;
// set up the detour for the XInputGetState function
static_detour! {
pub static XInputGetStateHook: unsafe extern "system" fn(DWORD, *mut XINPUT_STATE) -> DWORD;
}
// a user-defined function that modifies the controller state based on the current state
// some examples are included below
pub fn handle_controller_state(controller_state: &MutableXInputState, scheduled_functions: &mut Vec<ScheduledFunctionStack>) {
// Example 1:
// When the B button is pressed, press the X button
if controller_state.east_button() {
controller_state.set_west_button(ButtonState::DOWN);
}
// Example 2:
// When the D-Pad Up button is pressed, press the A button for
// 2 seconds, then release it for 2 seconds, then repeat
if controller_state.arrow_up() {
scheduled_functions.push(
scheduled_function_stack!(
2000 => |cs| { cs.set_south_button(ButtonState::DOWN) },
2000 => |cs| { cs.set_south_button(ButtonState::UP) },
2000 => |cs| { cs.set_south_button(ButtonState::DOWN) },
2000 => |cs| { cs.set_south_button(ButtonState::UP) },
2000 => |cs| { cs.set_south_button(ButtonState::DOWN) },
2000 => |cs| { cs.set_south_button(ButtonState::UP) },
)
);
}
// Example 3:
// When the D-Pad Down button is pressed, interupt the D-Pad Down button,
// the press the right trigger for 2 seconds, then release it for 2 seconds,
// then repeat
if controller_state.arrow_down() {
// interupt the D-Pad Down button
controller_state.set_arrow_down(ButtonState::UP);
// schedule the right trigger to be pressed and released
scheduled_functions.push(
scheduled_function_stack!(
2000 => |cs| { cs.set_right_trigger(u8::MAX) },
2000 => |cs| { cs.set_right_trigger(u8::MIN) },
2000 => |cs| { cs.set_right_trigger(u8::MAX) },
2000 => |cs| { cs.set_right_trigger(u8::MIN) },
2000 => |cs| { cs.set_right_trigger(u8::MAX) },
2000 => |cs| { cs.set_right_trigger(u8::MIN) },
)
);
}
// Example 4:
// Swap the left and right sticks
let left_stick = controller_state.left_stick_raw();
let right_stick = controller_state.right_stick_raw();
controller_state.set_left_stick_raw(right_stick);
controller_state.set_right_stick_raw(left_stick);
}