ABC_Game_Engine 0.1.2

A simple, fast, and flexible Game Engine written in Rust, with simplicity in mind.
Documentation
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
use crate::delta_time;
use fxhash::{FxHashMap, FxHashSet};
use gilrs::{Event, Gilrs};
use ABC_ECS::{EntitiesAndComponents, Resource};

// Stolen directly from winit, this is a list of all the keycodes that can be pressed on a keyboard.
// I copy pasted this because I don't want to depend on winit in this crate.
// The keycodes might also change in the future, so it's better to have a copy of them here.
/// Key codes for keyboard keys.
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
pub enum KeyCode {
    /// The '1' key over the letters.
    Key1,
    /// The '2' key over the letters.
    Key2,
    /// The '3' key over the letters.
    Key3,
    /// The '4' key over the letters.
    Key4,
    /// The '5' key over the letters.
    Key5,
    /// The '6' key over the letters.
    Key6,
    /// The '7' key over the letters.
    Key7,
    /// The '8' key over the letters.
    Key8,
    /// The '9' key over the letters.
    Key9,
    /// The '0' key over the 'O' and 'P' keys.
    Key0,

    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,

    /// The Escape key, next to F1.
    Escape,

    F1,
    F2,
    F3,
    F4,
    F5,
    F6,
    F7,
    F8,
    F9,
    F10,
    F11,
    F12,
    F13,
    F14,
    F15,
    F16,
    F17,
    F18,
    F19,
    F20,
    F21,
    F22,
    F23,
    F24,

    /// Print Screen/SysRq.
    Snapshot,
    /// Scroll Lock.
    Scroll,
    /// Pause/Break key, next to Scroll lock.
    Pause,

    /// `Insert`, next to Backspace.
    Insert,
    Home,
    Delete,
    End,
    PageDown,
    PageUp,

    Left,
    Up,
    Right,
    Down,

    /// The Backspace key, right over Enter.
    Backspace,
    /// The Enter key.
    Return,
    /// The space bar.
    Space,

    Numlock,
    Numpad0,
    Numpad1,
    Numpad2,
    Numpad3,
    Numpad4,
    Numpad5,
    Numpad6,
    Numpad7,
    Numpad8,
    Numpad9,
    NumpadAdd,
    NumpadDivide,
    NumpadDecimal,
    NumpadComma,
    NumpadEnter,
    NumpadEquals,
    NumpadMultiply,
    NumpadSubtract,

    Apostrophe,
    Asterisk,
    Backslash,
    Capital,
    Colon,
    Comma,
    Convert,
    Equals,
    Grave,
    Kana,
    Kanji,
    LAlt,
    LBracket,
    LControl,
    LShift,
    LWin,
    Mail,
    MediaSelect,
    MediaStop,
    Minus,
    Mute,
    MyComputer,
    // also called "Next"
    NavigateForward,
    // also called "Prior"
    NavigateBackward,
    NextTrack,
    NoConvert,
    OEM102,
    Period,
    PlayPause,
    Plus,
    Power,
    PrevTrack,
    RAlt,
    RBracket,
    RControl,
    RShift,
    RWin,
    Semicolon,
    Slash,
    Sleep,
    Stop,
    Sysrq,
    Tab,
    Underline,
    Unlabeled,
    VolumeDown,
    VolumeUp,
    Wake,
    WebBack,
    WebFavorites,
    WebForward,
    WebHome,
    WebRefresh,
    WebSearch,
    WebStop,
    Yen,
    Copy,
    Paste,
    Cut,
}

/// The buttons on a gamepad.
/// (note: if there is something wrong with this list or if something is missing, please let me know.)
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum GamepadButton {
    Start,
    Menu,
    Select,

    /// The north face button.
    ///
    /// * Nintendo: X
    /// * Playstation: Triangle
    /// * XBox: Y
    North,
    /// The south face button.
    ///
    /// * Nintendo: B
    /// * Playstation: X
    /// * XBox: A
    South,
    /// The east face button.
    ///
    /// * Nintendo: A
    /// * Playstation: Circle
    /// * XBox: B
    East,
    /// The west face button.
    ///
    /// * Nintendo: Y
    /// * Playstation: Square
    /// * XBox: X
    West,

    LeftStick,
    RightStick,

    LeftTrigger,
    RightTrigger,
    LeftBumper,
    RightBumper,

    LeftShoulder,
    RightShoulder,

    DPadUp,
    DPadDown,
    DPadLeft,
    DPadRight,
}

/// The state of a key relative to the previous frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd)]
pub enum KeyState {
    /// NotPressed is sent every frame that a key is not pressed. not including the first frame it is released, released is sent instead.
    NotPressed,
    /// Pressed is sent the frame that a key is pressed.
    Pressed,
    /// Held is sent every frame that a key is held down. including the first frame.
    Held,
    /// Released is sent the frame that a key is released.
    Released,
}

impl Ord for KeyState {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match (self, other) {
            (KeyState::NotPressed, KeyState::NotPressed) => std::cmp::Ordering::Equal,
            (KeyState::NotPressed, KeyState::Pressed) => std::cmp::Ordering::Less,
            (KeyState::NotPressed, KeyState::Held) => std::cmp::Ordering::Less,
            (KeyState::NotPressed, KeyState::Released) => std::cmp::Ordering::Less,

            (KeyState::Pressed, KeyState::NotPressed) => std::cmp::Ordering::Greater,
            (KeyState::Pressed, KeyState::Pressed) => std::cmp::Ordering::Equal,
            (KeyState::Pressed, KeyState::Held) => std::cmp::Ordering::Less,
            (KeyState::Pressed, KeyState::Released) => std::cmp::Ordering::Less,

            (KeyState::Held, KeyState::NotPressed) => std::cmp::Ordering::Greater,
            (KeyState::Held, KeyState::Pressed) => std::cmp::Ordering::Greater,
            (KeyState::Held, KeyState::Held) => std::cmp::Ordering::Equal,
            (KeyState::Held, KeyState::Released) => std::cmp::Ordering::Less,

            (KeyState::Released, KeyState::NotPressed) => std::cmp::Ordering::Greater,
            (KeyState::Released, KeyState::Pressed) => std::cmp::Ordering::Greater,
            (KeyState::Released, KeyState::Held) => std::cmp::Ordering::Greater,
            (KeyState::Released, KeyState::Released) => std::cmp::Ordering::Equal,
        }
    }
}

pub enum AxisEventDirection {
    /// left or down
    Negative,
    /// right or up
    Positive,
}

/// An axis event is an event that is triggered when an axis passes a certain threshold.
pub struct AxisEvent {
    direction: AxisEventDirection,
    axis: String,
    // how much the axis has to be pressed to be considered pressed.
    // this is a bit of a confusing name, because it's the same as the deadzone for gamepad axes.
    // but it's the same concept except not analog.
    // always positive
    threshold: f32,
}

impl AxisEvent {
    /// Creates a new axis event.
    pub fn new(direction: AxisEventDirection, axis: String, threshold: f32) -> Self {
        Self {
            direction,
            axis,
            threshold: threshold.abs(),
        }
    }
}

/// For either a key or a mouse button.
/// (note: This naming seems a bit off, if you have a better name, please suggest it.)
pub enum Key {
    KeyCode(KeyCode),
    MouseButton(MouseButton),
    GamepadButton((GamepadButton, Option<u32>)),
    AxisEvent(AxisEvent),
}

impl Into<Key> for KeyCode {
    fn into(self) -> Key {
        Key::KeyCode(self)
    }
}

impl Into<Key> for MouseButton {
    fn into(self) -> Key {
        Key::MouseButton(self)
    }
}

impl Into<Key> for (GamepadButton, Option<u32>) {
    fn into(self) -> Key {
        Key::GamepadButton(self)
    }
}

impl Into<Key> for GamepadButton {
    fn into(self) -> Key {
        Key::GamepadButton((self, None))
    }
}

impl Into<Key> for AxisEvent {
    fn into(self) -> Key {
        Key::AxisEvent(self)
    }
}

/// A button is a combination of keys that can be pressed to activate it or deactivate it.
/// This is essential for custom keybindings or for games that want to support multiple control schemes.
pub struct Button {
    /// if a single positive key is pressed, the button is pressed unless a negative key is also pressed.
    positive_keys: Vec<Key>,
    /// if a single negative key is pressed, the button is not pressed.
    negative_keys: Vec<Key>,
}

impl Button {
    /// Creates a new button.
    /// positive_keys are keys that when pressed, the button is pressed.
    /// negative_keys are keys that when pressed, the button is not pressed, even if a positive key is pressed.
    pub fn new(positive_keys: Vec<Key>, negative_keys: Vec<Key>) -> Self {
        Self {
            positive_keys,
            negative_keys,
        }
    }
}

/// The direction of an axis.
/// X is left and right, Y is up and down.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AxisDirection {
    X, // positive x is right, negative x is left.
    Y, // positive y is up, negative y is down.
}

impl From<AxisDirection> for usize {
    fn from(value: AxisDirection) -> Self {
        match value {
            AxisDirection::X => 0,
            AxisDirection::Y => 1,
        }
    }
}

/// A gamepad axis specifies a specific axis on a specific gamepad.
/// if the gamepad is not specified, the last gamepad is used.
#[derive(Debug, Clone)]
pub struct GamepadAxis {
    // if none, the last gamepad is used.
    id: Option<u32>,
    axis: u32,
    direction: AxisDirection,
    deadzone: f32,
}

impl GamepadAxis {
    /// Creates a new gamepad axis.
    pub fn new(id: Option<u32>, axis: u32, direction: AxisDirection, deadzone: f32) -> Self {
        Self {
            id,
            axis,
            direction,
            deadzone,
        }
    }
}

/// A 1D axis is a series of positive and negative keys that can be pressed to move the axis in either the positive or negative direction.
pub struct Axis {
    positive_keys: Vec<Key>,
    negative_keys: Vec<Key>,
    axes: Vec<GamepadAxis>,
    // the speed at which the axis moves back to 0 when no keys are pressed.
    // if this is None, the axis will be moved back to 0 instantly.
    // the higher the value, the faster the axis moves back to 0.
    gravity: Option<f32>,
    // the speed at which the axis moves when a key is pressed.
    // if this is None, the axis will be moved at a constant speed.
    // the higher the value, the faster the axis moves.
    sensitivity: Option<f32>,
    value: f32,
    presvious_value: f32,
}

impl Axis {
    /// Creates a new axis.
    pub fn new(positive_keys: Vec<Key>, negative_keys: Vec<Key>, axes: Vec<GamepadAxis>) -> Self {
        Self {
            positive_keys,
            negative_keys,
            axes,
            gravity: None,
            sensitivity: None,
            value: 0.0,
            presvious_value: 0.0,
        }
    }

    /// Creates a new axis with a gravity value.
    pub fn with_gravity(mut self, gravity: f32) -> Self {
        self.gravity = Some(gravity);
        self
    }

    /// Creates a new axis with a sensitivity value.
    pub fn with_sensitivity(mut self, sensitivity: f32) -> Self {
        self.sensitivity = Some(sensitivity);
        self
    }

    /// Creates a new axis with certain axes.
    pub fn with_axes(mut self, axes: Vec<GamepadAxis>) -> Self {
        self.axes = axes;
        self
    }

    /// Creates a new axis with certain positive keys.
    pub fn with_positive_keys(mut self, positive_keys: Vec<Key>) -> Self {
        self.positive_keys = positive_keys;
        self
    }

    /// Creates a new axis with certain negative keys.
    pub fn with_negative_keys(mut self, negative_keys: Vec<Key>) -> Self {
        self.negative_keys = negative_keys;
        self
    }

    /// Creates a new axis with a certain value.
    pub fn set_axis(&mut self, axes: Vec<GamepadAxis>) {
        self.axes = axes;
    }

    /// sets the positive keys of the axis.
    pub fn set_positive_keys(&mut self, positive_keys: Vec<Key>) {
        self.positive_keys = positive_keys;
    }

    /// sets the negative keys of the axis.
    pub fn set_negative_keys(&mut self, negative_keys: Vec<Key>) {
        self.negative_keys = negative_keys;
    }

    /// sets the gravity of the axis.
    pub fn set_gravity(&mut self, gravity: f32) {
        self.gravity = Some(gravity);
    }

    /// sets the sensitivity of the axis.
    pub fn set_sensitivity(&mut self, sensitivity: f32) {
        self.sensitivity = Some(sensitivity);
    }

    /// gets the value of the axis.
    pub fn update_value_from_raw(&mut self, raw_value: f32, delta_time: f32) {
        self.presvious_value = self.value;

        let difference = raw_value - self.value;

        let multiplier;
        if raw_value.abs() < 0.01 {
            if let Some(gravity) = self.gravity {
                multiplier = gravity;
            } else {
                self.value = raw_value;
                return;
            }
        } else {
            if let Some(sensitivity) = self.sensitivity {
                multiplier = sensitivity;
            } else {
                self.value = raw_value;
                return;
            }
        }

        let dir = difference.signum();

        let amount_to_move = multiplier * delta_time * dir;
        if amount_to_move.abs() > difference.abs() {
            self.value = raw_value;
        } else {
            self.value += amount_to_move;
        }
    }
}

/// The mouse buttons.
/// Other can be any other mouse button that isn't left, right or middle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum MouseButton {
    Left,
    Right,
    Middle,
    Other(u32),
}

/// all of the info neccessary set from the gamepad.
struct GamepadInputInfo {
    gamepad_axes: FxHashMap<u32, [f32; 2]>,
    gamepad_states: FxHashMap<GamepadButton, bool>,
    last_gamepad_states: FxHashMap<GamepadButton, bool>,
    buttons_awaiting_release: FxHashSet<GamepadButton>,
}

impl GamepadInputInfo {
    fn new() -> Self {
        Self {
            gamepad_axes: FxHashMap::default(),
            gamepad_states: FxHashMap::default(),
            last_gamepad_states: FxHashMap::default(),
            buttons_awaiting_release: FxHashSet::default(),
        }
    }

    /// sets a gamepad button as down
    pub fn set_gamepad_button_down(&mut self, button: GamepadButton) {
        self.gamepad_states.insert(button, true);
    }

    /// clears all gamepad states.
    pub fn clear_gamepad_states(&mut self) {
        self.last_gamepad_states = self.gamepad_states.clone();
        self.gamepad_states.clear();
    }

    /// gets the state of a gamepad button.
    pub fn get_gamepad_state(&self, button: GamepadButton) -> KeyState {
        let last = self
            .last_gamepad_states
            .get(&button)
            .copied()
            .unwrap_or(false);
        let current = self.gamepad_states.get(&button).copied().unwrap_or(false);
        if last {
            if current {
                KeyState::Held
            } else {
                KeyState::Released
            }
        } else {
            if current {
                KeyState::Pressed
            } else {
                KeyState::NotPressed
            }
        }
    }

    /// gets the axis of a gamepad. The axis is a value between -1 and 1.
    /// Assumes you want the first gamepad, otherwise you can use get_gamepad_axis_with_id.
    /// returns [0.0, 0.0] if no gamepad is found.
    pub fn get_gamepad_axis(&self) -> [f32; 2] {
        self.gamepad_axes.get(&0).copied().unwrap_or([0.0, 0.0])
    }

    /// sets the axis of a gamepad. The axis is a value between -1 and 1.
    pub fn set_gamepad_axis(&mut self, id: u32, axis: [f32; 2]) {
        self.gamepad_axes.insert(id, axis);
    }

    /// gets the axis of a gamepad. The axis is a value between -1 and 1.
    /// returns [0.0, 0.0] if the gamepad is not found.
    pub fn get_gamepad_axis_with_id(&self, id: u32) -> [f32; 2] {
        self.gamepad_axes.get(&id).copied().unwrap_or([0.0, 0.0])
    }
}

/// The input resource.
/// This is the resource that is used to get input from the user.
pub struct Input {
    last_key_states: FxHashMap<KeyCode, bool>,
    key_states: FxHashMap<KeyCode, bool>,
    last_mouse_states: FxHashMap<MouseButton, bool>,

    mouse_states: FxHashMap<MouseButton, bool>,
    mouse_position: [f32; 2],
    mouse_wheel: f32,

    gamepad_infos: FxHashMap<u32, GamepadInputInfo>,
    last_active_gamepad: u32,

    buttons: FxHashMap<String, Button>,
    axes: FxHashMap<String, Axis>,
    // gamepad input handling
    gilrs: Gilrs,
}

impl Input {
    /// Creates a new input resource.
    /// mostly don't need to call this, as the engine will create it for you.
    ///
    pub fn new() -> Self {
        Self {
            last_key_states: FxHashMap::default(),
            key_states: FxHashMap::default(),
            last_mouse_states: FxHashMap::default(),
            mouse_states: FxHashMap::default(),
            mouse_position: [0.0, 0.0],
            mouse_wheel: 0.0,
            gamepad_infos: FxHashMap::default(),
            last_active_gamepad: 0,
            buttons: FxHashMap::default(),
            axes: FxHashMap::default(),
            gilrs: Gilrs::new().unwrap(),
        }
    }

    /// gets the state of a key.
    pub fn get_key_state(&self, key: KeyCode) -> KeyState {
        let last = self.last_key_states.get(&key).copied().unwrap_or(false);
        let current = self.key_states.get(&key).copied().unwrap_or(false);
        if last {
            if current {
                KeyState::Held
            } else {
                KeyState::Released
            }
        } else {
            if current {
                KeyState::Pressed
            } else {
                KeyState::NotPressed
            }
        }
    }

    /// gets the position of the mouse.
    pub fn get_mouse_position(&self) -> [f32; 2] {
        self.mouse_position
    }

    /// sets the mouse wheel value. Unless you are implementing a rendering system, don't call this.
    pub fn set_mouse_position(&mut self, x: f32, y: f32) {
        self.mouse_position = [x, y];
    }

    /// gets the mouse wheel value.
    pub fn get_mouse_wheel(&self) -> f32 {
        self.mouse_wheel
    }

    /// sets the mouse wheel value. Unless you are implementing a rendering system, don't call this.
    pub fn set_mouse_wheel(&mut self, wheel: f32) {
        self.mouse_wheel = wheel;
    }

    /// sets the key state of a key. Unless you are implementing a rendering system, don't call this.
    pub fn set_key_down(&mut self, key: KeyCode) {
        self.key_states.insert(key, true);
    }

    /// Moves all current key states to previous key states. Unless you are implementing a rendering system, don't call this.
    /// if you are implementing a rendering system, call this before calling set_key_state.
    /// also keep in mind this will not clear the mouse states, that is done by clear_mouse_states.
    pub fn clear_key_states(&mut self) {
        self.last_key_states = self.key_states.clone();
        self.key_states.clear();
    }

    /// sets the mouse state of a mouse button. Unless you are implementing a rendering system, don't call this.
    pub fn set_mouse_down(&mut self, button: MouseButton) {
        self.mouse_states.insert(button, true);
    }

    /// Moves all current mouse states to previous mouse states. Unless you are implementing a rendering system, don't call this.
    pub fn clear_mouse_states(&mut self) {
        self.last_mouse_states = self.mouse_states.clone();
        self.mouse_states.clear();
    }

    /// gets the state of a mouse button.
    pub fn get_mouse_state(&self, button: MouseButton) -> KeyState {
        let last = self
            .last_mouse_states
            .get(&button)
            .copied()
            .unwrap_or(false);
        let current = self.mouse_states.get(&button).copied().unwrap_or(false);
        if last {
            if current {
                KeyState::Held
            } else {
                KeyState::Released
            }
        } else {
            if current {
                KeyState::Pressed
            } else {
                KeyState::NotPressed
            }
        }
    }

    /// Adds a button to the input resource.
    pub fn add_button(&mut self, name: &str, button: Button) {
        self.buttons.insert(name.to_string(), button);
    }

    /// gets the state of a button.
    pub fn get_button_state(&self, name: &str) -> KeyState {
        let button = self
            .buttons
            .get(name)
            .expect(format!("Button {} not found", name).as_str());

        let positive_keystate = self.find_highest_state(&button.positive_keys);

        let negative_keystate = self.find_highest_state(&button.negative_keys);
        let negative =
            negative_keystate == KeyState::Pressed || negative_keystate == KeyState::Held;

        if positive_keystate != KeyState::NotPressed {
            if positive_keystate == KeyState::Released || negative_keystate == KeyState::Pressed {
                return KeyState::Released; // doesn't matter if negative keys are pressed, if a positive key is released, the button is released.
            } else if negative {
                return KeyState::NotPressed; // if a negative key is pressed, the button is not pressed.
            } else {
                return positive_keystate; // no interference from negative keys.
            }
        } else {
            KeyState::NotPressed
        }
    }

    /// finds the highest state of a list of keys.
    fn find_highest_state(&self, keys: &[Key]) -> KeyState {
        let mut highest = KeyState::NotPressed;
        for key in keys {
            match key {
                Key::KeyCode(key) => {
                    let keystate = self.get_key_state(*key);

                    if (keystate == KeyState::Pressed
                        || keystate == KeyState::Held
                        || keystate == KeyState::Released)
                        && keystate > highest
                    {
                        highest = keystate;
                    }
                }
                Key::MouseButton(button) => {
                    let keystate = self.get_mouse_state(*button);

                    if (keystate == KeyState::Pressed
                        || keystate == KeyState::Held
                        || keystate == KeyState::Released)
                        && keystate > highest
                    {
                        highest = keystate;
                    }
                }
                Key::GamepadButton((button, id)) => {
                    let id = id.unwrap_or(self.last_active_gamepad);

                    let keystate = self
                        .gamepad_infos
                        .get(&id)
                        .map(|info| info.get_gamepad_state(*button))
                        .unwrap_or(KeyState::NotPressed);

                    if (keystate == KeyState::Pressed
                        || keystate == KeyState::Held
                        || keystate == KeyState::Released)
                        && keystate > highest
                    {
                        highest = keystate;
                    }
                }
                Key::AxisEvent(axis_event) => {
                    let axis = self.get_axis(&axis_event.axis);

                    let is_active = match axis_event.direction {
                        AxisEventDirection::Negative => axis < -axis_event.threshold,
                        AxisEventDirection::Positive => axis > axis_event.threshold,
                    };

                    // this isn't the greatest but currently there is no way to find the delta of the axis.
                    // which would be neccessary to determine if the axis was pressed or held or released.
                    /*if is_active && KeyState::Held > highest {
                        highest = KeyState::Held;
                    }*/

                    let previous_axis = self.get_previous_axis(&axis_event.axis);
                    let was_active = match axis_event.direction {
                        AxisEventDirection::Negative => previous_axis < -axis_event.threshold,
                        AxisEventDirection::Positive => previous_axis > axis_event.threshold,
                    };

                    let mut current_state = KeyState::NotPressed;
                    if was_active && !is_active {
                        current_state = KeyState::Released;
                    } else if is_active && !was_active {
                        current_state = KeyState::Pressed;
                    } else if is_active {
                        current_state = KeyState::Held;
                    }

                    if current_state > highest {
                        highest = current_state;
                    }
                }
            }
        }

        highest
    }

    /// gets the axis of a gamepad. The axis is a value between -1 and 1.
    /// Assumes you want the first gamepad, otherwise you can use get_gamepad_axis_with_id.
    /// returns [0.0, 0.0] if no gamepad is found.
    pub fn get_gamepad_axis(&self) -> [f32; 2] {
        self.gamepad_infos
            .get(&0)
            .map(|info| info.get_gamepad_axis())
            .unwrap_or([0.0, 0.0])
    }

    /// sets the axis of a gamepad. The axis is a value between -1 and 1.
    pub fn set_gamepad_axis(&mut self, gamepad_id: u32, axis_id: u32, axis: [f32; 2]) {
        self.gamepad_infos
            .entry(gamepad_id)
            .or_insert_with(GamepadInputInfo::new)
            .set_gamepad_axis(axis_id, axis);
    }

    /// sets the state of a gamepad button. unless you are implementing a rendering system, don't call this.
    pub fn set_gamepad_button_down(&mut self, gamepad_id: u32, button: GamepadButton) {
        self.gamepad_infos
            .entry(gamepad_id)
            .or_insert_with(GamepadInputInfo::new)
            .set_gamepad_button_down(button);
    }

    /// gets the state of a gamepad button.
    pub fn get_gamepad_state(&self, gamepad_id: u32, button: GamepadButton) -> KeyState {
        self.gamepad_infos
            .get(&gamepad_id)
            .map(|info| info.get_gamepad_state(button))
            .unwrap_or(KeyState::NotPressed)
    }

    /// gets the axis of a gamepad. The axis is a value between -1 and 1.
    /// returns [0.0, 0.0] if the gamepad is not found.
    pub fn get_gamepad_axis_with_id(&self, gamepad_id: u32, axis_id: u32) -> [f32; 2] {
        self.gamepad_infos
            .get(&gamepad_id)
            .map(|info| info.get_gamepad_axis_with_id(axis_id))
            .unwrap_or([0.0, 0.0])
    }

    fn gilrs_button_to_gamepad_button(button: gilrs::Button) -> Option<GamepadButton> {
        match button {
            gilrs::Button::South => Some(GamepadButton::South),
            gilrs::Button::East => Some(GamepadButton::East),
            gilrs::Button::North => Some(GamepadButton::North),
            gilrs::Button::West => Some(GamepadButton::West),
            gilrs::Button::C => Some(GamepadButton::RightShoulder),
            gilrs::Button::Z => Some(GamepadButton::LeftShoulder),
            gilrs::Button::LeftTrigger => Some(GamepadButton::LeftBumper),
            gilrs::Button::RightTrigger => Some(GamepadButton::RightBumper),
            gilrs::Button::LeftTrigger2 => Some(GamepadButton::LeftTrigger),
            gilrs::Button::RightTrigger2 => Some(GamepadButton::RightTrigger),
            gilrs::Button::LeftThumb => Some(GamepadButton::LeftStick),
            gilrs::Button::RightThumb => Some(GamepadButton::RightStick),
            gilrs::Button::Select => Some(GamepadButton::Select),
            gilrs::Button::Start => Some(GamepadButton::Start),
            gilrs::Button::DPadUp => Some(GamepadButton::DPadUp),
            gilrs::Button::DPadDown => Some(GamepadButton::DPadDown),
            gilrs::Button::DPadLeft => Some(GamepadButton::DPadLeft),
            gilrs::Button::DPadRight => Some(GamepadButton::DPadRight),
            _ => None,
        }
    }

    /// Adds an axis to the input resource.
    /// The axis can be accessed by name by calling get_axis.
    pub fn add_axis(&mut self, name: &str, axis: Axis) {
        self.axes.insert(name.to_string(), axis);
    }

    /// gets the raw value of an axis.
    /// This is the value of the axis before gravity and sensitivity are applied.
    /// Just as received from the controller, with no modifications.
    pub fn get_axis_raw(&self, name: &str) -> f32 {
        let axis = self.axes.get(name).expect(
            format!(
                "Axis {} not found... did you ever add it? the list of available axes are: {:?}",
                name,
                self.axes.keys()
            )
            .as_str(),
        );

        let mut is_keyboard_input = false;

        // not entirely necessary, but it should help if someone bumps a keybind for only one frame?
        // + it's more readable and consistent with the button code.
        let positive = self.find_highest_state(&axis.positive_keys);
        let negative = self.find_highest_state(&axis.negative_keys);

        let mut value = 0.0;

        if positive == KeyState::Held || positive == KeyState::Pressed {
            value += 1.0;
            is_keyboard_input = true;
        }

        if negative == KeyState::Held || negative == KeyState::Pressed {
            value -= 1.0;
            is_keyboard_input = true;
        }

        if is_keyboard_input {
            // no need to check gamepad input if keyboard input is found.
            // Note: this prioritizes keyboard input over gamepad input, maybe this should be configurable?
            return value;
        }

        for gamepad_axis in &axis.axes {
            let id = gamepad_axis.id.unwrap_or(self.last_active_gamepad);
            let axis = self.get_gamepad_axis_with_id(id, gamepad_axis.axis)
                [gamepad_axis.direction as usize];
            if axis > gamepad_axis.deadzone || axis < -gamepad_axis.deadzone {
                value += axis;
                return value;
            }
        }

        value
    }

    /// gets the value of an axis.
    pub fn get_axis(&self, name: &str) -> f32 {
        let axis = self.axes.get(name).expect(
            format!(
                "Axis {} not found... did you ever add it? the list of available axes are: {:?}",
                name,
                self.axes.keys()
            )
            .as_str(),
        );

        axis.value
    }

    /// gets the previous value of an axis.
    fn get_previous_axis(&self, name: &str) -> f32 {
        let axis = self.axes.get(name).expect(
            format!(
                "Axis {} not found... did you ever add it? the list of available axes are: {:?}",
                name,
                self.axes.keys()
            )
            .as_str(),
        );

        axis.presvious_value
    }
}

impl Resource for Input {
    fn update(&mut self) {} // update is handled by the InputUpdateSystem
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

pub(crate) struct InputUpdateSystem;

impl InputUpdateSystem {
    /// Creates a new input update system.
    pub(crate) fn new() -> Self {
        Self
    }
}

impl ABC_ECS::System for InputUpdateSystem {
    fn run(&mut self, entities_and_components: &mut EntitiesAndComponents) {
        let delta_time = entities_and_components
            .get_resource::<delta_time::DeltaTime>()
            .expect("DeltaTime not found")
            .get_delta_time();

        let input = entities_and_components
            .get_resource_mut::<Input>()
            .expect("Input not found");

        // handle gamepad input
        for info in input.gamepad_infos.values_mut() {
            info.clear_gamepad_states();
        }

        while let Some(Event { id, event, .. }) = input.gilrs.next_event() {
            let id = usize::from(id) as u32;

            match event {
                gilrs::EventType::ButtonRepeated(button, _) => {
                    input.last_active_gamepad = id;
                    let button: Option<GamepadButton> =
                        Input::gilrs_button_to_gamepad_button(button);
                    if let Some(button) = button {
                        input.set_gamepad_button_down(id, button);
                    }
                }
                gilrs::EventType::ButtonPressed(button, _) => {
                    input.last_active_gamepad = id;
                    let button = Input::gilrs_button_to_gamepad_button(button);

                    if let Some(button) = button {
                        input.set_gamepad_button_down(id, button);
                        input
                            .gamepad_infos
                            .get_mut(&id)
                            .unwrap()
                            .buttons_awaiting_release
                            .insert(button);
                    }
                }
                gilrs::EventType::ButtonReleased(button, _) => {
                    input.last_active_gamepad = id;
                    let button = Input::gilrs_button_to_gamepad_button(button);

                    if let Some(button) = button {
                        input
                            .gamepad_infos
                            .get_mut(&id)
                            .unwrap()
                            .buttons_awaiting_release
                            .remove(&button);
                    }
                }
                _ => {}
            }
        }

        let mut axis_data = vec![];

        for (id, gamepad) in input.gilrs.gamepads() {
            let id = usize::from(id) as u32;

            let left_stick_x = gamepad.value(gilrs::Axis::LeftStickX);
            let left_stick_y = gamepad.value(gilrs::Axis::LeftStickY);

            let left_stick = [left_stick_x, left_stick_y];

            let right_stick_x = gamepad.value(gilrs::Axis::RightStickX);
            let right_stick_y = gamepad.value(gilrs::Axis::RightStickY);

            let right_stick = [right_stick_x, right_stick_y];

            // has to be done for borrow checker
            axis_data.push((id, left_stick, right_stick));
        }

        for (id, left_stick, right_stick) in axis_data {
            input.set_gamepad_axis(id, 0, left_stick);
            input.set_gamepad_axis(id, 1, right_stick);
        }

        let mut all_buttons_awating_release = vec![];

        for (id, gamepad) in input.gamepad_infos.iter() {
            for button in gamepad.buttons_awaiting_release.iter() {
                all_buttons_awating_release.push((*id, *button));
            }
        }

        for (id, button) in all_buttons_awating_release {
            input.set_gamepad_button_down(id, button)
        }

        let all_axis_names = input.axes.keys().cloned().collect::<Vec<String>>();
        for axis_name in all_axis_names {
            let raw_value = input.get_axis_raw(&axis_name);

            let axis = input
                .axes
                .get_mut(&axis_name)
                .expect(format!("Axis {} not found", axis_name).as_str());

            axis.update_value_from_raw(raw_value, delta_time as f32);
        }

        input.gilrs.inc();
    }
}

#[cfg(test)]
mod tests {
    use crate::Scene;

    use super::*;
    use ABC_ECS::System;

    #[test]
    fn test_button() {
        let button = Button::new(
            vec![Key::KeyCode(KeyCode::A)],
            vec![Key::KeyCode(KeyCode::B)],
        );

        assert_eq!(button.positive_keys.len(), 1);
        assert_eq!(button.negative_keys.len(), 1);
    }

    #[test]
    fn test_key_state() {
        let key_state = KeyState::Pressed;

        assert!(key_state > KeyState::NotPressed);
        assert!(key_state < KeyState::Held);
        assert!(key_state < KeyState::Released);
    }

    #[test]
    fn test_axis() {
        let axis = Axis::new(
            vec![Key::KeyCode(KeyCode::A)],
            vec![Key::KeyCode(KeyCode::B)],
            vec![GamepadAxis::new(None, 0, AxisDirection::X, 0.5)],
        );

        assert_eq!(axis.positive_keys.len(), 1);
        assert_eq!(axis.negative_keys.len(), 1);
        assert_eq!(axis.axes.len(), 1);
    }

    #[test]
    fn test_gamepad_button() {
        let button: Key = GamepadButton::South.into();

        match button {
            Key::GamepadButton((GamepadButton::South, None)) => {}
            _ => panic!("Key not GamepadButton::South"),
        }
    }

    #[test]
    fn test_gamepad_axis() {
        let axis = GamepadAxis::new(None, 0, AxisDirection::X, 0.5);

        assert_eq!(axis.axis, 0);
    }

    #[test]
    fn test_releasing() {
        let mut input = Input::new();

        input.set_key_down(KeyCode::A);
        input.set_mouse_down(MouseButton::Left);

        input.clear_key_states();
        input.clear_mouse_states();

        assert_eq!(input.get_key_state(KeyCode::A), KeyState::Released);
        assert_eq!(input.get_mouse_state(MouseButton::Left), KeyState::Released);
    }

    #[test]
    fn test_pressed() {
        let mut input = Input::new();

        input.set_key_down(KeyCode::A);
        input.set_mouse_down(MouseButton::Left);

        assert_eq!(input.get_key_state(KeyCode::A), KeyState::Pressed);
        assert_eq!(input.get_mouse_state(MouseButton::Left), KeyState::Pressed);
    }

    #[test]
    fn test_held() {
        let mut input = Input::new();

        input.set_key_down(KeyCode::A);
        input.set_mouse_down(MouseButton::Left);

        input.clear_key_states();
        input.clear_mouse_states();

        input.set_key_down(KeyCode::A);
        input.set_mouse_down(MouseButton::Left);

        assert_eq!(input.get_key_state(KeyCode::A), KeyState::Held);
        assert_eq!(input.get_mouse_state(MouseButton::Left), KeyState::Held);
    }

    #[test]
    fn test_button_state() {
        let mut input = Input::new();

        input.add_button(
            "test",
            Button::new(
                vec![Key::KeyCode(KeyCode::A)],
                vec![Key::KeyCode(KeyCode::B)],
            ),
        );

        input.set_key_down(KeyCode::A);

        assert_eq!(input.get_button_state("test"), KeyState::Pressed);

        input.clear_key_states();

        assert_eq!(input.get_button_state("test"), KeyState::Released);
    }

    #[test]
    fn test_axis_value() {
        let mut input = Input::new();

        input.add_axis(
            "test",
            Axis::new(
                vec![Key::KeyCode(KeyCode::A)],
                vec![Key::KeyCode(KeyCode::B)],
                vec![GamepadAxis::new(None, 0, AxisDirection::X, 0.5)],
            ),
        );

        let mut scene = Scene::new();

        let mut input_system = InputUpdateSystem::new();

        let entities_and_components = &mut scene.world.entities_and_components;

        entities_and_components.add_resource(input);

        input_system.run(entities_and_components);

        let input = entities_and_components
            .get_resource_mut::<Input>()
            .expect("Input not found");

        assert_eq!(input.get_axis("test"), 0.0);

        input.set_key_down(KeyCode::A);

        input_system.run(entities_and_components);

        let input = entities_and_components
            .get_resource_mut::<Input>()
            .expect("Input not found");

        assert_eq!(input.get_axis("test"), 1.0);
    }

    #[test]
    fn test_axis_event() {
        let mut input = Input::new();

        input.add_axis(
            "test",
            Axis::new(vec![Key::KeyCode(KeyCode::A)], vec![], vec![]),
        );

        let axis_related_button = Button::new(
            vec![Key::AxisEvent(AxisEvent {
                direction: AxisEventDirection::Positive,
                axis: "test".to_string(),
                threshold: 0.5,
            })],
            vec![],
        );

        input.add_button("axis_related_button", axis_related_button);

        let mut scene = Scene::new();

        let mut input_system = InputUpdateSystem::new();

        let entities_and_components = &mut scene.world.entities_and_components;

        entities_and_components.add_resource(input);

        input_system.run(entities_and_components);

        let input = entities_and_components
            .get_resource_mut::<Input>()
            .expect("Input not found");

        assert_eq!(
            input.get_button_state("axis_related_button"),
            KeyState::NotPressed
        );

        input.set_key_down(KeyCode::A);

        input_system.run(entities_and_components);

        let input = entities_and_components
            .get_resource_mut::<Input>()
            .expect("Input not found");

        assert_eq!(
            input.get_button_state("axis_related_button"),
            KeyState::Pressed
        );
    }
}