forked from fishfolk/jumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.rs
502 lines (456 loc) · 18.7 KB
/
input.rs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use crate::{
prelude::*,
settings::{InputKind, PlayerControlMapping, PlayerControlSetting, Settings},
};
#[cfg(not(target_arch = "wasm32"))]
use bones_framework::networking::{input::NetworkPlayerControl, proto::DenseMoveDirection};
use strum::EnumIter;
pub fn game_plugin(game: &mut Game) {
game.systems.add_startup_system(load_controler_mapping);
game.insert_shared_resource(EguiInputHook::new(handle_egui_input));
game.init_shared_resource::<GlobalPlayerControls>();
game.init_shared_resource::<PlayerInputCollector>();
}
// Startup system to load game control mapping resource from the storage and insert the player input
// collector.
fn load_controler_mapping(game: &mut Game) {
let control_mapping = {
let storage = game.shared_resource::<Storage>().unwrap();
storage.get::<Settings>().unwrap().player_controls.clone()
};
game.insert_shared_resource(control_mapping);
}
fn collect_player_controls(game: &mut Game) {
let controls = 'controls: {
let mut collector = game.shared_resource_mut::<PlayerInputCollector>().unwrap();
let Some(mapping) = game.shared_resource::<PlayerControlMapping>() else {
break 'controls default();
};
let keyboard = game.shared_resource::<KeyboardInputs>().unwrap();
let gamepad = game.shared_resource::<GamepadInputs>().unwrap();
collector.apply_inputs(&mapping, &keyboard, &gamepad);
collector.update_just_pressed();
collector.advance_frame();
GlobalPlayerControls(
collector
.get_current_controls()
.clone()
.into_iter()
.collect(),
)
};
game.insert_shared_resource(controls);
}
/// Settings to configure the [`handle_egui_input`] system
#[derive(Default, Debug, Clone, Copy)]
pub struct EguiInputSettings {
/// If set to `true`, then all keyboard inputs will be sucked into a black hole so that egui
/// doesn't read them.
pub disable_keyboard_input: bool,
/// If set to `true`, gamepad inputs will not be converted to egui inputs for menu navigation.
pub disable_gamepad_input: bool,
}
/// Game system that takes the raw input events and converts it to player controls based on the
/// player input map.
pub fn handle_egui_input(game: &mut Game, egui_input: &mut egui::RawInput) {
// We collect the global player controls here in the egui input hoook so that it will be
// available immediately to egui, and then available to the rest of the systems that run after.
collect_player_controls(game);
let ctx = game.shared_resource::<EguiCtx>().unwrap();
let controls = game.shared_resource::<GlobalPlayerControls>().unwrap();
let settings = ctx.get_state::<EguiInputSettings>();
let events = &mut egui_input.events;
// Remove keyboard events if disabled.
if settings.disable_keyboard_input {
events.retain(|x| !matches!(x, egui::Event::Key { .. }));
}
// Forward gamepad events to egui if not disabled.
if !settings.disable_gamepad_input {
let push_key = |events: &mut Vec<egui::Event>, key| {
events.push(egui::Event::Key {
key,
pressed: true,
repeat: false,
modifiers: default(),
});
};
for player_control in controls.values() {
if player_control.just_moved {
if player_control.move_direction.y > 0.1 {
push_key(events, egui::Key::ArrowUp);
} else if player_control.move_direction.y < -0.1 {
push_key(events, egui::Key::ArrowDown);
} else if player_control.move_direction.x < -0.1 {
push_key(events, egui::Key::ArrowLeft);
} else if player_control.move_direction.x > 0.1 {
push_key(events, egui::Key::ArrowRight);
}
}
if player_control.menu_confirm_just_pressed {
push_key(events, egui::Key::Enter);
}
if player_control.menu_back_just_pressed {
push_key(events, egui::Key::Escape);
}
}
}
}
/// Resource containing the global player control inputs.
///
/// It is important to note that these controls are updated every system frame, and therefore
/// the `just_pressed` and `just_moved` flags are not accurate in the context of a fixed
/// update match loop. Matches with fixed updates have their own input resource.
///
/// This resource is used throughout the menu where the inputs are collected every frame, not
/// every fixed update.
#[derive(HasSchema, Clone, Default, Deref, DerefMut)]
pub struct GlobalPlayerControls(HashMap<ControlSource, PlayerControl>);
impl GlobalPlayerControls {
/// Iterator over inputs that originated from gamepads.
pub fn gamepads(&self) -> impl Iterator<Item = &PlayerControl> {
self.iter().filter_map(|(source, control)| {
matches!(source, ControlSource::Gamepad(_)).then_some(control)
})
}
}
/// The source of player control inputs
#[derive(Debug, Clone, Copy, Default, HasSchema, Hash, Eq, PartialEq, EnumIter)]
#[repr(C, u8)]
pub enum ControlSource {
#[default]
/// The first keyboard controls
Keyboard1,
/// The second keyboard controls
Keyboard2,
/// A gamepad control with the given index
Gamepad(u32),
}
/// Player control input state
#[derive(HasSchema, Default, Clone, Copy, Debug)]
#[repr(C)]
pub struct PlayerControl {
pub left: f32,
pub right: f32,
pub up: f32,
pub down: f32,
pub move_direction: Vec2,
pub just_moved: bool,
pub moving: bool,
pub menu_back_pressed: bool,
pub menu_back_just_pressed: bool,
pub menu_confirm_pressed: bool,
pub menu_confirm_just_pressed: bool,
pub menu_start_pressed: bool,
pub menu_start_just_pressed: bool,
pub pause_pressed: bool,
pub pause_just_pressed: bool,
pub jump_pressed: bool,
pub jump_just_pressed: bool,
pub shoot_pressed: bool,
pub shoot_just_pressed: bool,
pub grab_pressed: bool,
pub grab_just_pressed: bool,
pub slide_pressed: bool,
pub slide_just_pressed: bool,
pub ragdoll_pressed: bool,
pub ragdoll_just_pressed: bool,
}
#[derive(HasSchema, Clone)]
pub struct PlayerInputCollector {
current_controls: HashMap<ControlSource, PlayerControl>,
last_controls: HashMap<ControlSource, PlayerControl>,
}
impl PlayerInputCollector {
pub fn get_current_controls(&self) -> &HashMap<ControlSource, PlayerControl> {
&self.current_controls
}
}
impl Default for PlayerInputCollector {
fn default() -> Self {
let def_controls = || {
let mut m = HashMap::default();
// We always have the keyboard controls "plugged in"
m.insert(ControlSource::Keyboard1, default());
m.insert(ControlSource::Keyboard2, default());
for i in 0..MAX_PLAYERS {
m.insert(ControlSource::Gamepad(i), default());
}
m
};
Self {
current_controls: def_controls(),
last_controls: def_controls(),
}
}
}
impl<'a>
bones_framework::input::InputCollector<'a, PlayerControlMapping, ControlSource, PlayerControl>
for PlayerInputCollector
{
fn update_just_pressed(&mut self) {
self.current_controls
.iter_mut()
.for_each(|(source, current)| {
let last = self.last_controls.entry(*source).or_default();
current.move_direction =
vec2(current.right - current.left, current.up - current.down);
current.moving = current.move_direction.length_squared() > 0.01;
for (just_pressed, current_pressed, last_pressed) in [
(
&mut current.pause_just_pressed,
current.pause_pressed,
last.pause_pressed,
),
(
&mut current.jump_just_pressed,
current.jump_pressed,
last.jump_pressed,
),
(
&mut current.shoot_just_pressed,
current.shoot_pressed,
last.shoot_pressed,
),
(
&mut current.grab_just_pressed,
current.grab_pressed,
last.grab_pressed,
),
(
&mut current.slide_just_pressed,
current.slide_pressed,
last.slide_pressed,
),
(
&mut current.ragdoll_just_pressed,
current.ragdoll_pressed,
last.ragdoll_pressed,
),
(
&mut current.menu_back_just_pressed,
current.menu_back_pressed,
last.menu_back_pressed,
),
(
&mut current.menu_confirm_just_pressed,
current.menu_confirm_pressed,
last.menu_confirm_pressed,
),
(
&mut current.menu_start_just_pressed,
current.menu_start_pressed,
last.menu_start_pressed,
),
(&mut current.just_moved, current.moving, last.moving),
] {
*just_pressed = current_pressed && !last_pressed;
}
});
}
fn advance_frame(&mut self) {
self.last_controls = self.current_controls.clone();
}
/// Update the internal state with new inputs. This must be called every render frame with the
/// input events.
fn apply_inputs(
&mut self,
mapping: &crate::settings::PlayerControlMapping,
keyboard: &KeyboardInputs,
gamepad: &GamepadInputs,
) {
// Helper to get the value of the given input type for the given player.
let get_input_value = |input_map: &InputKind, control_source: &ControlSource| match (
input_map,
control_source,
) {
(InputKind::Button(mapped_button), ControlSource::Gamepad(idx)) => {
let mut out = None;
for input in &gamepad.gamepad_events {
if let GamepadEvent::Button(e) = input {
if &e.button == mapped_button && e.gamepad == *idx {
let value = if e.value < 0.1 { 0.0 } else { e.value };
out = Some(value);
}
}
}
out
}
(InputKind::AxisPositive(mapped_axis), ControlSource::Gamepad(idx)) => {
let mut out = None;
for input in &gamepad.gamepad_events {
if let GamepadEvent::Axis(e) = input {
if &e.axis == mapped_axis && e.gamepad == *idx {
let value = if e.value < 0.1 { 0.0 } else { e.value };
out = Some(value);
}
}
}
out
}
(InputKind::AxisNegative(mapped_axis), ControlSource::Gamepad(idx)) => {
let mut out = None;
for input in &gamepad.gamepad_events {
if let GamepadEvent::Axis(e) = input {
if &e.axis == mapped_axis && e.gamepad == *idx {
let value = if e.value > -0.1 { 0.0 } else { e.value };
out = Some(value);
}
}
}
out
}
(
InputKind::Keyboard(mapped_key),
ControlSource::Keyboard1 | ControlSource::Keyboard2,
) => {
let mut out = None;
for input in &keyboard.key_events {
if input.key_code.option() == Some(*mapped_key) {
out = Some(if input.button_state.pressed() {
1.0
} else {
0.0
});
}
}
out
}
_ => None,
};
let apply_controls = |control: &mut PlayerControl,
source: &ControlSource,
mapping: &PlayerControlSetting| {
for (button_pressed, button_map) in [
(&mut control.pause_pressed, &mapping.pause),
(&mut control.jump_pressed, &mapping.jump),
(&mut control.grab_pressed, &mapping.grab),
(&mut control.shoot_pressed, &mapping.shoot),
(&mut control.slide_pressed, &mapping.slide),
(&mut control.ragdoll_pressed, &mapping.ragdoll),
(&mut control.menu_back_pressed, &mapping.menu_back),
(&mut control.menu_confirm_pressed, &mapping.menu_confirm),
(&mut control.menu_start_pressed, &mapping.menu_start),
] {
if let Some(value) = get_input_value(button_map, source) {
*button_pressed = value > 0.0;
}
}
// helper for merging two inputs (like dpad + joystick for example) allowing multiple bindings
// for same control
let merge_inputs = |input1: &InputKind, input2: &InputKind| -> Option<f32> {
let mut out: Option<f32> = None;
if let Some(value1) = get_input_value(input1, source) {
out = Some(value1.abs());
}
if let Some(value2) = get_input_value(input2, source) {
match out {
Some(prev) if prev == 0.0 => {
// If first input is 0.0, override with second input
out = Some(value2.abs());
}
None => {
// No input from first, use second
out = Some(value2.abs());
}
// If first input is non-zero input, use it and ignore second.
Some(_) => {}
}
}
out
};
if let Some(left) = merge_inputs(&mapping.movement.left, &mapping.movement_alt.left) {
control.left = left.abs();
}
if let Some(right) = merge_inputs(&mapping.movement.right, &mapping.movement_alt.right)
{
control.right = right.abs();
}
if let Some(up) = merge_inputs(&mapping.movement.up, &mapping.movement_alt.up) {
control.up = up.abs();
}
if let Some(down) = merge_inputs(&mapping.movement.down, &mapping.movement_alt.down) {
control.down = down.abs();
}
};
for (source, control) in self.current_controls.iter_mut() {
apply_controls(
control,
source,
match source {
ControlSource::Keyboard1 => &mapping.keyboard1,
ControlSource::Keyboard2 => &mapping.keyboard2,
ControlSource::Gamepad(_) => &mapping.gamepad,
},
);
}
}
// TODO: Fix bones Trait definition, player_idx not relevant
fn get_control(&self, _player_idx: usize, control_source: ControlSource) -> &PlayerControl {
self.current_controls.get(&control_source).unwrap()
}
}
#[cfg(not(target_arch = "wasm32"))]
impl NetworkPlayerControl<DensePlayerControl> for PlayerControl {
fn get_dense_input(&self) -> DensePlayerControl {
let mut dense_control = DensePlayerControl::default();
dense_control.set_jump_pressed(self.jump_pressed);
dense_control.set_grab_pressed(self.grab_pressed);
dense_control.set_slide_pressed(self.slide_pressed);
dense_control.set_shoot_pressed(self.shoot_pressed);
dense_control.set_ragdoll_pressed(self.ragdoll_pressed);
dense_control.set_move_direction(proto::DenseMoveDirection(self.move_direction));
dense_control
}
fn update_from_dense(&mut self, new_control: &DensePlayerControl) {
let jump_pressed = new_control.jump_pressed();
self.jump_just_pressed = jump_pressed && !self.jump_pressed;
self.jump_pressed = jump_pressed;
let grab_pressed = new_control.grab_pressed();
self.grab_just_pressed = grab_pressed && !self.grab_pressed;
self.grab_pressed = grab_pressed;
let shoot_pressed = new_control.shoot_pressed();
self.shoot_just_pressed = shoot_pressed && !self.shoot_pressed;
self.shoot_pressed = shoot_pressed;
let ragdoll_pressed = new_control.ragdoll_pressed();
self.ragdoll_just_pressed = ragdoll_pressed && !self.ragdoll_pressed;
self.ragdoll_pressed = ragdoll_pressed;
let was_moving = self.move_direction.length_squared() > f32::MIN_POSITIVE;
self.move_direction = new_control.move_direction().0;
let is_moving = self.move_direction.length_squared() > f32::MIN_POSITIVE;
self.just_moved = !was_moving && is_moving;
}
}
#[cfg(not(target_arch = "wasm32"))]
bitfield::bitfield! {
/// A player's controller inputs densely packed into a single u32.
///
/// This is used when sending player inputs across the network.
#[derive(bytemuck::Pod, bytemuck::Zeroable, Copy, Clone, PartialEq, Eq)]//, Reflect)]
#[repr(transparent)]
pub struct DensePlayerControl(u32);
impl Debug;
pub jump_pressed, set_jump_pressed: 0;
pub shoot_pressed, set_shoot_pressed: 1;
pub grab_pressed, set_grab_pressed: 2;
pub slide_pressed, set_slide_pressed: 3;
pub ragdoll_pressed, set_ragdoll_pressed: 4;
pub from into DenseMoveDirection, move_direction, set_move_direction: 16, 5;
}
#[cfg(not(target_arch = "wasm32"))]
impl Default for DensePlayerControl {
fn default() -> Self {
let mut control = Self(0);
control.set_move_direction(default());
control
}
}
#[cfg(not(target_arch = "wasm32"))]
/// Used to implement input type config for bones networking.
pub struct NetworkInputConfig;
#[cfg(not(target_arch = "wasm32"))]
impl<'a> bones_framework::networking::input::NetworkInputConfig<'a> for NetworkInputConfig {
type Dense = DensePlayerControl;
type Control = PlayerControl;
type PlayerControls = MatchInputs;
type InputCollector = PlayerInputCollector;
}