1use super::*;
2use serde_derive::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
6pub enum InputDeviceType {
7 Nes,
9 Controller,
11 Keyboard,
13}
14
15pub trait InputConvert {
17 fn affinity(&self, device_type: InputDeviceType) -> Option<i32>;
21
22 fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice>;
24}
25
26pub trait InputCombine {
28 fn combine(&self, with: &Self) -> Self;
31}
32
33#[derive(Serialize, Deserialize, Clone)]
35pub enum InputDevice {
36 Nes(Nes),
37 Controller(Controller),
38 Keyboard(Keyboard),
39}
40
41impl InputCombine for InputDevice {
42 fn combine(&self, with: &Self) -> Self {
43 match self {
44 InputDevice::Nes(nes) => {
45 let conversion = with.convert(InputDeviceType::Nes);
46 if let Some(conversion) = conversion {
47 if let InputDevice::Nes(with) = conversion {
48 return InputDevice::Nes(nes.combine(&with));
49 }
50 }
51 }
52 InputDevice::Controller(standard_controller) => {
53 let conversion = with.convert(InputDeviceType::Controller);
54 if let Some(conversion) = conversion {
55 if let InputDevice::Controller(with) = conversion {
56 return InputDevice::Controller(standard_controller.combine(&with));
57 }
58 }
59 }
60 InputDevice::Keyboard(keyboard) => {
61 let conversion = with.convert(InputDeviceType::Keyboard);
62 if let Some(conversion) = conversion {
63 if let InputDevice::Keyboard(with) = conversion {
64 return InputDevice::Keyboard(keyboard.combine(&with));
65 }
66 }
67 }
68 }
69
70 self.clone()
71 }
72}
73
74impl InputConvert for InputDevice {
75 fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
76 match self {
77 InputDevice::Nes(nes) => nes.convert(device_type),
78 InputDevice::Controller(standard_controller) => {
79 standard_controller.convert(device_type)
80 }
81 InputDevice::Keyboard(keyboard) => keyboard.convert(device_type),
82 }
83 }
84 fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
85 match self {
86 InputDevice::Nes(nes) => nes.affinity(device_type),
87 InputDevice::Controller(standard_controller) => {
88 standard_controller.affinity(device_type)
89 }
90 InputDevice::Keyboard(keyboard) => keyboard.affinity(device_type),
91 }
92 }
93}
94
95#[derive(Serialize, Deserialize, Default, Clone)]
97pub struct InputCollection {
98 inputs: Vec<InputDevice>,
99}
100
101impl InputCollection {
102 pub fn new() -> Self {
103 Self { inputs: Vec::new() }
104 }
105
106 pub fn add_input(&mut self, device: InputDevice) {
111 self.inputs.push(device)
112 }
113
114 pub fn get_input_arguments(&self, info: &Info) -> InputArguments {
120 let devices: Vec<InputDeviceType> = info
121 .players
122 .iter()
123 .map(|player| player.input.clone())
124 .collect();
125
126 let (dist, mut remaining) = self.split(&devices);
127 let mut result: Vec<Option<PlayerInputArguments>> = dist
128 .iter()
129 .map(|input| match input {
130 Some(input) => Some(PlayerInputArguments {
131 input: input.clone(),
132 }),
133 None => None,
134 })
135 .collect();
136
137 loop {
139 let (new_dist, new_remaining) = remaining.split(&devices);
140 if new_remaining.inputs.len() == remaining.inputs.len() {
141 break;
142 }
143
144 for (result_index, result_player) in result.iter_mut().enumerate() {
145 if let Some(player) = result_player {
146 if let Some(device) = &new_dist[result_index] {
147 player.input = player.input.combine(device);
148 }
149 }
150 }
151
152 remaining = new_remaining;
153 }
154
155 InputArguments::new(result)
156 }
157
158 fn split(&self, into: &[InputDeviceType]) -> (Vec<Option<InputDevice>>, InputCollection) {
164 let mut remaining = self.inputs.clone();
165 let mut found = Vec::new();
166
167 for input_type in into {
170 let mut found_index = None;
171 let mut found_affinity = None;
172 let mut found_for = None;
173 for (index, input) in remaining.iter().enumerate() {
174 let affinity = input.affinity(input_type.clone());
175 match affinity {
176 Some(affinity) => {
177 if let Some(fa) = found_affinity {
178 if affinity >= fa {
179 continue;
180 }
181 }
182
183 let found_new = input.convert(input_type.clone());
184 if let Some(found_new) = found_new {
185 found_affinity = Some(affinity);
186 found_index = Some(index);
187 found_for = Some(found_new);
188 }
189 }
190 None => continue,
191 }
192 }
193
194 found.push(found_for);
195
196 if let Some(index) = found_index {
197 remaining.remove(index);
198 }
199 }
200
201 (found, InputCollection { inputs: remaining })
202 }
203}
204
205impl InputConvert for InputCollection {
206 fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
207 let mut successfully_converted = Vec::new();
208 for input in &self.inputs {
209 if let Some(input) = input.convert(device_type.clone()) {
210 successfully_converted.push(input);
211 }
212 }
213
214 if successfully_converted.is_empty() {
215 None
216 } else {
217 let mut result = successfully_converted.pop().unwrap();
218 while let Some(converted) = successfully_converted.pop() {
219 result = result.combine(&converted);
220 }
221
222 Some(result)
223 }
224 }
225 fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
226 let mut affinity = None;
227 for input in &self.inputs {
228 match affinity {
229 Some(current) => {
230 if let Some(test) = input.affinity(device_type.clone()) {
231 if test < current {
232 affinity = Some(test);
233 }
234 }
235 }
236 None => affinity = input.affinity(device_type.clone()),
237 }
238 }
239 affinity
240 }
241}
242
243impl InputCombine for InputCollection {
244 fn combine(&self, with: &Self) -> Self {
245 let mut inputs = self.inputs.clone();
246 inputs.extend(with.inputs.clone());
247 Self { inputs }
248 }
249}
250
251#[derive(Serialize, Deserialize, Default, Clone)]
254pub struct Nes {
255 a: bool,
256 b: bool,
257 up: bool,
258 down: bool,
259 left: bool,
260 right: bool,
261 start: bool,
262 select: bool,
263}
264
265impl Nes {
266 pub fn a(&self) -> bool {
268 self.a
269 }
270
271 pub fn b(&self) -> bool {
273 self.b
274 }
275
276 pub fn up(&self) -> bool {
278 self.up
279 }
280
281 pub fn down(&self) -> bool {
283 self.down
284 }
285
286 pub fn left(&self) -> bool {
288 self.left
289 }
290
291 pub fn right(&self) -> bool {
293 self.right
294 }
295
296 pub fn start(&self) -> bool {
298 self.start
299 }
300
301 pub fn select(&self) -> bool {
303 self.select
304 }
305
306 pub fn set_a(&mut self, value: bool) {
308 self.a = value;
309 }
310
311 pub fn set_b(&mut self, value: bool) {
313 self.b = value;
314 }
315
316 pub fn set_up(&mut self, value: bool) {
318 self.up = value;
319 }
320
321 pub fn set_down(&mut self, value: bool) {
323 self.down = value;
324 }
325
326 pub fn set_left(&mut self, value: bool) {
328 self.left = value;
329 }
330
331 pub fn set_right(&mut self, value: bool) {
333 self.right = value;
334 }
335
336 pub fn set_start(&mut self, value: bool) {
338 self.start = value;
339 }
340
341 pub fn set_select(&mut self, value: bool) {
343 self.select = value;
344 }
345}
346
347impl InputConvert for Nes {
348 fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
349 match device_type {
350 InputDeviceType::Nes => Some(InputDevice::Nes(self.clone())),
351 _ => None,
352 }
353 }
354 fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
355 match device_type {
356 InputDeviceType::Nes => Some(0),
357 _ => None,
358 }
359 }
360}
361
362impl InputCombine for Nes {
363 fn combine(&self, with: &Self) -> Self {
364 Self {
365 a: self.a || with.a,
366 b: self.b || with.b,
367 up: self.up || with.up,
368 down: self.down || with.down,
369 left: self.left || with.left,
370 right: self.right || with.right,
371 start: self.start || with.start,
372 select: self.select || with.select,
373 }
374 }
375}
376
377#[derive(Serialize, Deserialize, Default, Clone)]
379pub struct Controller {
380 a: bool,
381 b: bool,
382 x: bool,
383 y: bool,
384 up: bool,
385 down: bool,
386 left: bool,
387 right: bool,
388 start: bool,
389 select: bool,
390 guide: bool,
391 left_shoulder: bool,
392 right_shoulder: bool,
393 left_stick: bool,
394 right_stick: bool,
395 left_stick_x: f32,
396 left_stick_y: f32,
397 right_stick_x: f32,
398 right_stick_y: f32,
399 left_trigger: f32,
400 right_trigger: f32,
401}
402
403#[derive(Serialize, Deserialize, Default, Clone)]
405pub struct ControllerInit {
406 pub a: bool,
407 pub b: bool,
408 pub x: bool,
409 pub y: bool,
410 pub up: bool,
411 pub down: bool,
412 pub left: bool,
413 pub right: bool,
414 pub start: bool,
415 pub select: bool,
416 pub guide: bool,
417 pub left_shoulder: bool,
418 pub right_shoulder: bool,
419 pub left_stick: bool,
420 pub right_stick: bool,
421 pub left_stick_x: f32,
422 pub left_stick_y: f32,
423 pub right_stick_x: f32,
424 pub right_stick_y: f32,
425 pub left_trigger: f32,
426 pub right_trigger: f32,
427}
428
429impl Controller {
430 pub fn new(init: ControllerInit) -> Self {
431 Controller {
432 a: init.a,
433 b: init.b,
434 x: init.x,
435 y: init.y,
436 up: init.up,
437 down: init.down,
438 left: init.left,
439 right: init.right,
440 start: init.start,
441 select: init.select,
442 guide: init.guide,
443 left_shoulder: init.left_shoulder,
444 right_shoulder: init.right_shoulder,
445 left_stick: init.left_stick,
446 right_stick: init.right_stick,
447 left_stick_x: init.left_stick_x,
448 left_stick_y: init.left_stick_y,
449 right_stick_x: init.right_stick_x,
450 right_stick_y: init.right_stick_y,
451 left_trigger: init.left_trigger,
452 right_trigger: init.right_trigger,
453 }
454 }
455
456 pub fn a(&self) -> bool {
458 self.a
459 }
460
461 pub fn b(&self) -> bool {
463 self.b
464 }
465
466 pub fn x(&self) -> bool {
468 self.x
469 }
470
471 pub fn y(&self) -> bool {
473 self.y
474 }
475
476 pub fn up(&self) -> bool {
478 self.up
479 }
480
481 pub fn down(&self) -> bool {
483 self.down
484 }
485
486 pub fn left(&self) -> bool {
488 self.left
489 }
490
491 pub fn right(&self) -> bool {
493 self.right
494 }
495
496 pub fn start(&self) -> bool {
498 self.start
499 }
500
501 pub fn select(&self) -> bool {
503 self.select
504 }
505
506 pub fn guide(&self) -> bool {
508 self.guide
509 }
510
511 pub fn left_shoulder(&self) -> bool {
513 self.left_shoulder
514 }
515
516 pub fn right_shoulder(&self) -> bool {
518 self.right_shoulder
519 }
520
521 pub fn left_stick(&self) -> bool {
523 self.left_stick
524 }
525
526 pub fn right_stick(&self) -> bool {
528 self.right_stick
529 }
530
531 pub fn left_stick_x(&self) -> f32 {
533 self.left_stick_x
534 }
535
536 pub fn left_stick_y(&self) -> f32 {
538 self.left_stick_y
539 }
540
541 pub fn right_stick_x(&self) -> f32 {
543 self.right_stick_x
544 }
545
546 pub fn right_stick_y(&self) -> f32 {
548 self.right_stick_y
549 }
550
551 pub fn left_trigger(&self) -> f32 {
553 self.left_trigger
554 }
555
556 pub fn right_trigger(&self) -> f32 {
558 self.right_trigger
559 }
560
561 pub fn set_a(&mut self, value: bool) {
563 self.a = value;
564 }
565
566 pub fn set_b(&mut self, value: bool) {
568 self.b = value;
569 }
570
571 pub fn set_x(&mut self, value: bool) {
573 self.x = value;
574 }
575
576 pub fn set_y(&mut self, value: bool) {
578 self.y = value;
579 }
580
581 pub fn set_up(&mut self, value: bool) {
583 self.up = value;
584 }
585
586 pub fn set_down(&mut self, value: bool) {
588 self.down = value;
589 }
590
591 pub fn set_left(&mut self, value: bool) {
593 self.left = value;
594 }
595
596 pub fn set_right(&mut self, value: bool) {
598 self.right = value;
599 }
600
601 pub fn set_start(&mut self, value: bool) {
603 self.start = value;
604 }
605
606 pub fn set_select(&mut self, value: bool) {
608 self.select = value;
609 }
610
611 pub fn set_guide(&mut self, value: bool) {
613 self.guide = value;
614 }
615
616 pub fn set_left_shoulder(&mut self, value: bool) {
618 self.left_shoulder = value;
619 }
620
621 pub fn set_right_shoulder(&mut self, value: bool) {
623 self.right_shoulder = value;
624 }
625
626 pub fn set_left_stick(&mut self, value: bool) {
628 self.left_stick = value;
629 }
630
631 pub fn set_right_stick(&mut self, value: bool) {
633 self.right_stick = value;
634 }
635
636 pub fn set_left_stick_x(&mut self, value: f32) {
638 self.left_stick_x = value;
639 }
640 pub fn set_left_stick_y(&mut self, value: f32) {
642 self.left_stick_y = value;
643 }
644 pub fn set_right_stick_x(&mut self, value: f32) {
646 self.right_stick_x = value;
647 }
648 pub fn set_right_stick_y(&mut self, value: f32) {
650 self.right_stick_y = value;
651 }
652
653 pub fn set_left_trigger(&mut self, value: f32) {
655 self.left_trigger = value;
656 }
657 pub fn set_right_trigger(&mut self, value: f32) {
659 self.right_trigger = value;
660 }
661}
662
663impl InputConvert for Controller {
664 fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
665 match device_type {
666 InputDeviceType::Nes => Some(InputDevice::Nes(self.to_nes())),
667 InputDeviceType::Controller => Some(InputDevice::Controller(self.clone())),
668 _ => None,
669 }
670 }
671 fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
672 match device_type {
673 InputDeviceType::Nes => Some(1),
674 InputDeviceType::Controller => Some(0),
675 _ => None,
676 }
677 }
678}
679
680impl InputCombine for Controller {
681 fn combine(&self, with: &Self) -> Self {
682 Self {
683 a: self.a || with.a,
684 b: self.b || with.b,
685 x: self.x || with.x,
686 y: self.y || with.y,
687 up: self.up || with.up,
688 down: self.down || with.down,
689 left: self.left || with.left,
690 right: self.right || with.right,
691 start: self.start || with.start,
692 select: self.select || with.select,
693 guide: self.guide || with.guide,
694 left_shoulder: self.left_shoulder || with.left_shoulder,
695 right_shoulder: self.right_shoulder || with.right_shoulder,
696 left_stick: self.left_stick || with.left_stick,
697 right_stick: self.right_stick || with.right_stick,
698 left_stick_x: self.left_stick_x.max(with.left_stick_x),
699 left_stick_y: self.left_stick_y.max(with.left_stick_y),
700 right_stick_x: self.right_stick_x.max(with.right_stick_x),
701 right_stick_y: self.right_stick_y.max(with.right_stick_y),
702 left_trigger: self.left_trigger.max(with.left_trigger),
703 right_trigger: self.right_trigger.max(with.right_trigger),
704 }
705 }
706}
707
708impl Controller {
709 fn to_nes(&self) -> Nes {
710 let stick_sensitivity = 0.5;
711
712 Nes {
713 a: self.a,
714 b: self.b,
715 up: self.up || self.left_stick_y < -stick_sensitivity,
716 down: self.down || self.left_stick_y >= stick_sensitivity,
717 left: self.left || self.left_stick_x < -stick_sensitivity,
718 right: self.right || self.left_stick_x >= stick_sensitivity,
719 start: self.start,
720 select: self.select,
721 }
722 }
723}
724
725#[derive(Serialize, Deserialize, Clone, Default)]
727pub struct Keyboard {
728 pressed: Vec<Key>,
729}
730
731impl Keyboard {
732 pub fn key_down(&mut self, key_code: KeyCode) {
737 self.key_up(key_code);
738 self.pressed.push(Key::new(key_code));
739 }
740
741 pub fn key_up(&mut self, key_code: KeyCode) {
746 self.pressed.retain(|key| key.key_code != key_code);
747 }
748
749 pub fn is_down(&self, key_code: KeyCode) -> bool {
754 for key in &self.pressed {
755 if key.key_code == key_code {
756 return true;
757 }
758 }
759
760 false
761 }
762
763 fn to_nes(&self) -> Nes {
764 Nes {
765 a: self.is_down(KeyCode::X)
766 || self.is_down(KeyCode::K)
767 || self.is_down(KeyCode::E),
768 b: self.is_down(KeyCode::Z)
769 || self.is_down(KeyCode::J)
770 || self.is_down(KeyCode::N),
771 up: self.is_down(KeyCode::W)
772 || self.is_down(KeyCode::Up)
773 || self.is_down(KeyCode::F),
774 down: self.is_down(KeyCode::S) || self.is_down(KeyCode::Down),
775 left: self.is_down(KeyCode::A)
776 || self.is_down(KeyCode::Left)
777 || self.is_down(KeyCode::R),
778 right: self.is_down(KeyCode::D)
779 || self.is_down(KeyCode::Right)
780 || self.is_down(KeyCode::T),
781 start: self.is_down(KeyCode::Enter),
782 select: self.is_down(KeyCode::Tab),
783 }
784 }
785}
786
787impl InputConvert for Keyboard {
788 fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
789 match device_type {
790 InputDeviceType::Nes => Some(InputDevice::Nes(self.to_nes())),
791 InputDeviceType::Keyboard => Some(InputDevice::Keyboard(self.clone())),
792 _ => None,
793 }
794 }
795 fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
796 match device_type {
797 InputDeviceType::Nes => Some(2),
798 InputDeviceType::Keyboard => Some(0),
799 _ => None,
800 }
801 }
802}
803
804impl InputCombine for Keyboard {
805 fn combine(&self, with: &Self) -> Self {
806 let pressed = self.pressed.clone();
807
808 let mut result = Self { pressed };
809
810 for key in &with.pressed {
811 result.key_down(key.key_code);
812 }
813
814 result
815 }
816}
817
818#[derive(Serialize, Deserialize, Clone)]
820pub struct Key {
821 key_code: KeyCode,
822}
823
824impl Key {
825 pub fn new(key_code: KeyCode) -> Self {
831 Self {
832 key_code,
833 }
834 }
835}
836
837#[derive(Serialize, Deserialize, Copy, Clone, PartialEq)]
839pub enum KeyCode {
840 _1,
841 _2,
842 _3,
843 _4,
844 _5,
845 _6,
846 _7,
847 _8,
848 _9,
849 _0,
850 A,
851 B,
852 C,
853 D,
854 E,
855 F,
856 G,
857 H,
858 I,
859 J,
860 K,
861 L,
862 M,
863 N,
864 O,
865 P,
866 Q,
867 R,
868 S,
869 T,
870 U,
871 V,
872 W,
873 X,
874 Y,
875 Z,
876 Up,
877 Down,
878 Left,
879 Right,
880 Enter,
881 Tab,
882 LeftBracket,
883 RightBracket,
884 Slash,
885 Backslash,
886 Comma,
887 Period,
888 Semicolon,
889 Quote,
890}