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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
use super::*;
use serde_derive::{Deserialize, Serialize};

/// Input device types, will resolve to a InputDevice
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub enum InputDeviceType {
    /// Nintendo Entertainment System style controller
    Nes,
    /// A modern controller, similar to a XBox 360 controller
    Controller,
    /// A computer keyboard
    Keyboard,
}

/// Trait for converting from one input type to another
pub trait InputConvert {
    /// Returns how closely this device matches the type of another, for example a standard
    /// controller is closely to a NES style controller than a keyboard is. Lower values are closer
    /// fits. None = cant be converted at all.
    fn affinity(&self, device_type: InputDeviceType) -> Option<i32>;

    //Convert this device into another device, None = can't be converted.
    fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice>;
}

/// Trait for combining two inputs together
pub trait InputCombine {
    /// Combine this input with another one, usually this means any pressed buttons from either
    /// device will be down in the new one
    fn combine(&self, with: &Self) -> Self;
}

/// Enumeration over all input types
#[derive(Serialize, Deserialize, Clone)]
pub enum InputDevice {
    Nes(Nes),
    Controller(Controller),
    Keyboard(Keyboard),
}

impl InputCombine for InputDevice {
    fn combine(&self, with: &Self) -> Self {
        match self {
            InputDevice::Nes(nes) => {
                let conversion = with.convert(InputDeviceType::Nes);
                if let Some(conversion) = conversion {
                    if let InputDevice::Nes(with) = conversion {
                        return InputDevice::Nes(nes.combine(&with));
                    }
                }
            }
            InputDevice::Controller(standard_controller) => {
                let conversion = with.convert(InputDeviceType::Controller);
                if let Some(conversion) = conversion {
                    if let InputDevice::Controller(with) = conversion {
                        return InputDevice::Controller(standard_controller.combine(&with));
                    }
                }
            }
            InputDevice::Keyboard(keyboard) => {
                let conversion = with.convert(InputDeviceType::Keyboard);
                if let Some(conversion) = conversion {
                    if let InputDevice::Keyboard(with) = conversion {
                        return InputDevice::Keyboard(keyboard.combine(&with));
                    }
                }
            }
        }

        self.clone()
    }
}

impl InputConvert for InputDevice {
    fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
        match self {
            InputDevice::Nes(nes) => nes.convert(device_type),
            InputDevice::Controller(standard_controller) => {
                standard_controller.convert(device_type)
            }
            InputDevice::Keyboard(keyboard) => keyboard.convert(device_type),
        }
    }
    fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
        match self {
            InputDevice::Nes(nes) => nes.affinity(device_type),
            InputDevice::Controller(standard_controller) => {
                standard_controller.affinity(device_type)
            }
            InputDevice::Keyboard(keyboard) => keyboard.affinity(device_type),
        }
    }
}

/// Collection of many inputs
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct InputCollection {
    inputs: Vec<InputDevice>,
}

impl InputCollection {
    pub fn new() -> Self {
        Self { inputs: Vec::new() }
    }

    /// Add a new input to the collection
    ///
    /// # Arguments
    /// * `device` - Device to add
    pub fn add_input(&mut self, device: InputDevice) {
        self.inputs.push(device)
    }

    /// Distribute all of the inputs in the collection amongst all of the players mentioned in the
    /// info argument and return a InputArgument suitable for passing to Game::Step()
    ///
    /// # Arguments
    /// * `info` - The game info
    pub fn get_input_arguments(&self, info: &Info) -> InputArguments {
        let devices: Vec<InputDeviceType> = info
            .players
            .iter()
            .map(|player| player.input.clone())
            .collect();

        let (dist, mut remaining) = self.split(&devices);
        let mut result: Vec<Option<PlayerInputArguments>> = dist
            .iter()
            .map(|input| match input {
                Some(input) => Some(PlayerInputArguments {
                    input: input.clone(),
                }),
                None => None,
            })
            .collect();

        //TODO: HORRID LOOP TO COMBINE ALL POSSIBLE INPUTS:
        loop {
            let (new_dist, new_remaining) = remaining.split(&devices);
            if new_remaining.inputs.len() == remaining.inputs.len() {
                break;
            }

            for (result_index, result_player) in result.iter_mut().enumerate() {
                if let Some(player) = result_player {
                    if let Some(device) = &new_dist[result_index] {
                        player.input = player.input.combine(device);
                    }
                }
            }

            remaining = new_remaining;
        }

        InputArguments::new(result)
    }

    /// Splits this collection up into into separate inputs
    ///
    /// # Arguments
    /// * `into` - a slice of inputs type to split into.
    /// Returns a tuple with the split inputs and a collection of remaining ones.
    fn split(&self, into: &[InputDeviceType]) -> (Vec<Option<InputDevice>>, InputCollection) {
        let mut remaining = self.inputs.clone();
        let mut found = Vec::new();

        //TODO: PREFER DEVICES THAT ARE THE RIGHT TYPE:

        for input_type in into {
            let mut found_index = None;
            let mut found_affinity = None;
            let mut found_for = None;
            for (index, input) in remaining.iter().enumerate() {
                let affinity = input.affinity(input_type.clone());
                match affinity {
                    Some(affinity) => {
                        if let Some(fa) = found_affinity {
                            if affinity >= fa {
                                continue;
                            }
                        }

                        let found_new = input.convert(input_type.clone());
                        if let Some(found_new) = found_new {
                            found_affinity = Some(affinity);
                            found_index = Some(index);
                            found_for = Some(found_new);
                        }
                    }
                    None => continue,
                }
            }

            found.push(found_for);

            if let Some(index) = found_index {
                remaining.remove(index);
            }
        }

        (found, InputCollection { inputs: remaining })
    }
}

impl InputConvert for InputCollection {
    fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
        let mut successfully_converted = Vec::new();
        for input in &self.inputs {
            if let Some(input) = input.convert(device_type.clone()) {
                successfully_converted.push(input);
            }
        }

        if successfully_converted.is_empty() {
            None
        } else {
            let mut result = successfully_converted.pop().unwrap();
            while let Some(converted) = successfully_converted.pop() {
                result = result.combine(&converted);
            }

            Some(result)
        }
    }
    fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
        let mut affinity = None;
        for input in &self.inputs {
            match affinity {
                Some(current) => {
                    if let Some(test) = input.affinity(device_type.clone()) {
                        if test < current {
                            affinity = Some(test);
                        }
                    }
                }
                None => affinity = input.affinity(device_type.clone()),
            }
        }
        affinity
    }
}

impl InputCombine for InputCollection {
    fn combine(&self, with: &Self) -> Self {
        let mut inputs = self.inputs.clone();
        inputs.extend(with.inputs.clone());
        Self { inputs }
    }
}

/// An input type similar to a Nintendo Entertainment System controller, has a dpad and 2 primary
/// buttons. Also has start + select.
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct Nes {
    a: bool,
    b: bool,
    up: bool,
    down: bool,
    left: bool,
    right: bool,
    start: bool,
    select: bool,
}

impl Nes {
    /// Is the a button down
    pub fn a(&self) -> bool {
        self.a
    }

    /// Is the b button down
    pub fn b(&self) -> bool {
        self.b
    }

    /// Is the up button down
    pub fn up(&self) -> bool {
        self.up
    }

    /// Is the down button down
    pub fn down(&self) -> bool {
        self.down
    }

    /// Is the left button down
    pub fn left(&self) -> bool {
        self.left
    }

    /// Is the right button down
    pub fn right(&self) -> bool {
        self.right
    }

    /// Is the start button down
    pub fn start(&self) -> bool {
        self.start
    }

    /// Is the select button down
    pub fn select(&self) -> bool {
        self.select
    }

    /// Sets the state of the a button
    pub fn set_a(&mut self, value: bool) {
        self.a = value;
    }

    /// Sets the state of the b button
    pub fn set_b(&mut self, value: bool) {
        self.b = value;
    }

    /// Sets the state of the up button
    pub fn set_up(&mut self, value: bool) {
        self.up = value;
    }

    /// Sets the state of the down button
    pub fn set_down(&mut self, value: bool) {
        self.down = value;
    }

    /// Sets the state of the left button
    pub fn set_left(&mut self, value: bool) {
        self.left = value;
    }

    /// Sets the state of the right button
    pub fn set_right(&mut self, value: bool) {
        self.right = value;
    }

    /// Sets the state of the start button
    pub fn set_start(&mut self, value: bool) {
        self.start = value;
    }

    /// Sets the state of the select button
    pub fn set_select(&mut self, value: bool) {
        self.select = value;
    }
}

impl InputConvert for Nes {
    fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
        match device_type {
            InputDeviceType::Nes => Some(InputDevice::Nes(self.clone())),
            _ => None,
        }
    }
    fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
        match device_type {
            InputDeviceType::Nes => Some(0),
            _ => None,
        }
    }
}

impl InputCombine for Nes {
    fn combine(&self, with: &Self) -> Self {
        Self {
            a: self.a || with.a,
            b: self.b || with.b,
            up: self.up || with.up,
            down: self.down || with.down,
            left: self.left || with.left,
            right: self.right || with.right,
            start: self.start || with.start,
            select: self.select || with.select,
        }
    }
}

/// A standard controller, similar to one used for a XBox 360
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct Controller {
    a: bool,
    b: bool,
    x: bool,
    y: bool,
    up: bool,
    down: bool,
    left: bool,
    right: bool,
    start: bool,
    select: bool,
    guide: bool,
    left_shoulder: bool,
    right_shoulder: bool,
    left_stick: bool,
    right_stick: bool,
    left_stick_x: f32,
    left_stick_y: f32,
    right_stick_x: f32,
    right_stick_y: f32,
    left_trigger: f32,
    right_trigger: f32,
}

/// Structure for initializing a controller
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct ControllerInit {
    pub a: bool,
    pub b: bool,
    pub x: bool,
    pub y: bool,
    pub up: bool,
    pub down: bool,
    pub left: bool,
    pub right: bool,
    pub start: bool,
    pub select: bool,
    pub guide: bool,
    pub left_shoulder: bool,
    pub right_shoulder: bool,
    pub left_stick: bool,
    pub right_stick: bool,
    pub left_stick_x: f32,
    pub left_stick_y: f32,
    pub right_stick_x: f32,
    pub right_stick_y: f32,
    pub left_trigger: f32,
    pub right_trigger: f32,
}

impl Controller {
    pub fn new(init: ControllerInit) -> Self {
        Controller {
            a: init.a,
            b: init.b,
            x: init.x,
            y: init.y,
            up: init.up,
            down: init.down,
            left: init.left,
            right: init.right,
            start: init.start,
            select: init.select,
            guide: init.guide,
            left_shoulder: init.left_shoulder,
            right_shoulder: init.right_shoulder,
            left_stick: init.left_stick,
            right_stick: init.right_stick,
            left_stick_x: init.left_stick_x,
            left_stick_y: init.left_stick_y,
            right_stick_x: init.right_stick_x,
            right_stick_y: init.right_stick_y,
            left_trigger: init.left_trigger,
            right_trigger: init.right_trigger,
        }
    }

    /// Is the a button down
    pub fn a(&self) -> bool {
        self.a
    }

    /// Is the b button down
    pub fn b(&self) -> bool {
        self.b
    }

    /// Is the x button down
    pub fn x(&self) -> bool {
        self.x
    }

    /// Is the y button down
    pub fn y(&self) -> bool {
        self.y
    }

    /// Is the up button down
    pub fn up(&self) -> bool {
        self.up
    }

    /// Is the down button down
    pub fn down(&self) -> bool {
        self.down
    }

    /// Is the left button down
    pub fn left(&self) -> bool {
        self.left
    }

    /// Is the right button down
    pub fn right(&self) -> bool {
        self.right
    }

    /// Is the start button down
    pub fn start(&self) -> bool {
        self.start
    }

    /// Is the select button down
    pub fn select(&self) -> bool {
        self.select
    }

    /// Is the guide button down
    pub fn guide(&self) -> bool {
        self.guide
    }

    /// Is the left shoulder button down
    pub fn left_shoulder(&self) -> bool {
        self.left_shoulder
    }

    /// Is the right shoulder button down
    pub fn right_shoulder(&self) -> bool {
        self.right_shoulder
    }

    /// Is the left stick button down
    pub fn left_stick(&self) -> bool {
        self.left_stick
    }

    /// Is the right stick button down
    pub fn right_stick(&self) -> bool {
        self.right_stick
    }

    /// Left stick position x [-1 - +1] -1 left +1 right
    pub fn left_stick_x(&self) -> f32 {
        self.left_stick_x
    }

    /// Left stick position y [-1 - +1] -1 up +1 down
    pub fn left_stick_y(&self) -> f32 {
        self.left_stick_y
    }

    /// Right stick position x [-1 - +1] -1 left +1 right
    pub fn right_stick_x(&self) -> f32 {
        self.right_stick_x
    }

    /// Right stick position y [-1 - +1] -1 up +1 down
    pub fn right_stick_y(&self) -> f32 {
        self.right_stick_y
    }

    /// Left trigger position [0 - +1] +1 fully down
    pub fn left_trigger(&self) -> f32 {
        self.left_trigger
    }

    /// Right trigger position [0 - +1] +1 fully down
    pub fn right_trigger(&self) -> f32 {
        self.right_trigger
    }

    /// Sets the state of the a button
    pub fn set_a(&mut self, value: bool) {
        self.a = value;
    }

    /// Sets the state of the b button
    pub fn set_b(&mut self, value: bool) {
        self.b = value;
    }

    /// Sets the state of the x button
    pub fn set_x(&mut self, value: bool) {
        self.x = value;
    }

    /// Sets the state of the y button
    pub fn set_y(&mut self, value: bool) {
        self.y = value;
    }

    /// Sets the state of the up button
    pub fn set_up(&mut self, value: bool) {
        self.up = value;
    }

    /// Sets the state of the down button
    pub fn set_down(&mut self, value: bool) {
        self.down = value;
    }

    /// Sets the state of the left button
    pub fn set_left(&mut self, value: bool) {
        self.left = value;
    }

    /// Sets the state of the right button
    pub fn set_right(&mut self, value: bool) {
        self.right = value;
    }

    /// Sets the state of the start button
    pub fn set_start(&mut self, value: bool) {
        self.start = value;
    }

    /// Sets the state of the select button
    pub fn set_select(&mut self, value: bool) {
        self.select = value;
    }

    /// Sets the state of the guide button
    pub fn set_guide(&mut self, value: bool) {
        self.guide = value;
    }

    /// Sets the state of the left shoulder button
    pub fn set_left_shoulder(&mut self, value: bool) {
        self.left_shoulder = value;
    }

    /// Sets the state of the right shoulder button
    pub fn set_right_shoulder(&mut self, value: bool) {
        self.right_shoulder = value;
    }

    /// Sets the state of the left stick button
    pub fn set_left_stick(&mut self, value: bool) {
        self.left_stick = value;
    }

    /// Sets the state of the right stick button
    pub fn set_right_stick(&mut self, value: bool) {
        self.right_stick = value;
    }

    /// Sets the state of the left stick x axis
    pub fn set_left_stick_x(&mut self, value: f32) {
        self.left_stick_x = value;
    }
    /// Sets the state of the left stick y axis
    pub fn set_left_stick_y(&mut self, value: f32) {
        self.left_stick_y = value;
    }
    /// Sets the state of the right stick x axis
    pub fn set_right_stick_x(&mut self, value: f32) {
        self.right_stick_x = value;
    }
    /// Sets the state of the right stick y axis
    pub fn set_right_stick_y(&mut self, value: f32) {
        self.right_stick_y = value;
    }

    /// Sets the state of the left trigger
    pub fn set_left_trigger(&mut self, value: f32) {
        self.left_trigger = value;
    }
    /// Sets the state of the right trigger
    pub fn set_right_trigger(&mut self, value: f32) {
        self.right_trigger = value;
    }
}

impl InputConvert for Controller {
    fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
        match device_type {
            InputDeviceType::Nes => Some(InputDevice::Nes(self.to_nes())),
            InputDeviceType::Controller => Some(InputDevice::Controller(self.clone())),
            _ => None,
        }
    }
    fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
        match device_type {
            InputDeviceType::Nes => Some(1),
            InputDeviceType::Controller => Some(0),
            _ => None,
        }
    }
}

impl InputCombine for Controller {
    fn combine(&self, with: &Self) -> Self {
        Self {
            a: self.a || with.a,
            b: self.b || with.b,
            x: self.x || with.x,
            y: self.y || with.y,
            up: self.up || with.up,
            down: self.down || with.down,
            left: self.left || with.left,
            right: self.right || with.right,
            start: self.start || with.start,
            select: self.select || with.select,
            guide: self.guide || with.guide,
            left_shoulder: self.left_shoulder || with.left_shoulder,
            right_shoulder: self.right_shoulder || with.right_shoulder,
            left_stick: self.left_stick || with.left_stick,
            right_stick: self.right_stick || with.right_stick,
            left_stick_x: self.left_stick_x.max(with.left_stick_x),
            left_stick_y: self.left_stick_y.max(with.left_stick_y),
            right_stick_x: self.right_stick_x.max(with.right_stick_x),
            right_stick_y: self.right_stick_y.max(with.right_stick_y),
            left_trigger: self.left_trigger.max(with.left_trigger),
            right_trigger: self.right_trigger.max(with.right_trigger),
        }
    }
}

impl Controller {
    fn to_nes(&self) -> Nes {
        let stick_sensitivity = 0.5;

        Nes {
            a: self.a,
            b: self.b,
            up: self.up || self.left_stick_y < -stick_sensitivity,
            down: self.down || self.left_stick_y >= stick_sensitivity,
            left: self.left || self.left_stick_x < -stick_sensitivity,
            right: self.right || self.left_stick_x >= stick_sensitivity,
            start: self.start,
            select: self.select,
        }
    }
}

/// A input for a computer keyboard
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Keyboard {
    pressed: Vec<Key>,
}

impl Keyboard {
    /// Add a key down state
    ///
    /// # Arguments
    /// * `key` - the key that is down
    pub fn key_down(&mut self, key_code: KeyCode) {
        self.key_up(key_code);
        self.pressed.push(Key::new(key_code));
    }

    /// Return a key to the up state
    ///
    /// # Arguments
    /// * `scan_code` - the scan code for the key that is now up
    pub fn key_up(&mut self, key_code: KeyCode) {
        self.pressed.retain(|key| key.key_code != key_code);
    }

    /// Get the pressed state of a key
    ///
    /// # Arguments
    /// * `scan_code` - The scan code to look up
    pub fn is_down(&self, key_code: KeyCode) -> bool {
        for key in &self.pressed {
            if key.key_code == key_code {
                return true;
            }
        }

        false
    }

    fn to_nes(&self) -> Nes {
        Nes {
            a: self.is_down(KeyCode::X)
                || self.is_down(KeyCode::K)
                || self.is_down(KeyCode::E),
            b: self.is_down(KeyCode::Z)
                || self.is_down(KeyCode::J)
                || self.is_down(KeyCode::N),
            up: self.is_down(KeyCode::W)
                || self.is_down(KeyCode::Up)
                || self.is_down(KeyCode::F),
            down: self.is_down(KeyCode::S) || self.is_down(KeyCode::Down),
            left: self.is_down(KeyCode::A)
                || self.is_down(KeyCode::Left)
                || self.is_down(KeyCode::R),
            right: self.is_down(KeyCode::D)
                || self.is_down(KeyCode::Right)
                || self.is_down(KeyCode::T),
            start: self.is_down(KeyCode::Enter),
            select: self.is_down(KeyCode::Tab),
        }
    }
}

impl InputConvert for Keyboard {
    fn convert(&self, device_type: InputDeviceType) -> Option<InputDevice> {
        match device_type {
            InputDeviceType::Nes => Some(InputDevice::Nes(self.to_nes())),
            InputDeviceType::Keyboard => Some(InputDevice::Keyboard(self.clone())),
            _ => None,
        }
    }
    fn affinity(&self, device_type: InputDeviceType) -> Option<i32> {
        match device_type {
            InputDeviceType::Nes => Some(2),
            InputDeviceType::Keyboard => Some(0),
            _ => None,
        }
    }
}

impl InputCombine for Keyboard {
    fn combine(&self, with: &Self) -> Self {
        let pressed = self.pressed.clone();

        let mut result = Self { pressed };

        for key in &with.pressed {
            result.key_down(key.key_code);
        }

        result
    }
}

/// A key that can be pressed by a Keyboard
#[derive(Serialize, Deserialize, Clone)]
pub struct Key {
    key_code: KeyCode,
}

impl Key {
    /// Create new key from a scan and key code
    ///
    /// # Arguments
    /// * `scan_code` - The scan code of the key, not effected by the locale
    /// * `key_code` - The key code of the key, effected by the locale
    pub fn new(key_code: KeyCode) -> Self {
        Self {
            key_code,
        }
    }
}

/// Key/scan codes
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq)]
pub enum KeyCode {
    _1,
    _2,
    _3,
    _4,
    _5,
    _6,
    _7,
    _8,
    _9,
    _0,
    A,
    B,
    C,
    D,
    E,
    F,
    G,
    H,
    I,
    J,
    K,
    L,
    M,
    N,
    O,
    P,
    Q,
    R,
    S,
    T,
    U,
    V,
    W,
    X,
    Y,
    Z,
    Up,
    Down,
    Left,
    Right,
    Enter,
    Tab,
    LeftBracket,
    RightBracket,
    Slash,
    Backslash,
    Comma,
    Period,
    Semicolon,
    Quote,
}