leafwing-input-manager 0.20.0

A powerful, flexible and ergonomic way to manage action-input keybindings for the Bevy game engine.
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
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
//! This module contains [`ActionState`] and its supporting methods and impls.

use crate::buttonlike::ButtonValue;
use crate::input_map::UpdatedValue;
use crate::{Actionlike, InputControlKind};
use crate::{action_diff::ActionDiff, input_map::UpdatedActions};

use bevy::platform::{collections::HashMap, time::Instant};
use bevy::prelude::Resource;
use bevy::reflect::Reflect;
use bevy::{ecs::component::Component, prelude::ReflectComponent};
use bevy::{
    math::{Vec2, Vec3},
    prelude::ReflectResource,
};
#[cfg(feature = "timing")]
use core::time::Duration;
use serde::{Deserialize, Serialize};

mod action_data;
pub use action_data::*;

/// Stores the canonical input-method-agnostic representation of the inputs received
///
/// Can be used as either a resource or as a [`Component`] on entities that you wish to control directly from player input.
///
/// # Disabling actions
///
/// Actions can be disabled in four different ways, with increasing granularity:
///
/// 1. By disabling updates to all actions using a run condition on [`InputManagerSystem::Update`](crate::plugin::InputManagerSystem::Update).
/// 2. By disabling updates to all actions of type `A` using a run condition on [`TickActionStateSystem::<A>`](crate::plugin::TickActionStateSystem).
/// 3. By setting a specific action state to disabled using [`ActionState::disable`].
/// 4. By disabling a specific action using [`ActionState::disable_action`].
///
/// More general mechanisms of disabling actions will cause specific mechanisms to be ignored.
/// For example, if an entire action state is disabled, then enabling or disabling individual actions will have no effect.
///
/// Actions that are disabled will report as released (but not just released), and their values will be zero.
/// Under the hood, their values are still updated to avoid surprising behavior when re-enabled,
/// but they are not reported to the user using standard methods like [`ActionState::pressed`].
/// To check the underlying values, access their [`ActionData`] directly.
///
/// # Example
///
/// ```rust
/// use bevy::reflect::Reflect;
/// use leafwing_input_manager::prelude::*;
/// use bevy::platform::time::Instant;
///
/// #[derive(Actionlike, PartialEq, Eq, Hash, Clone, Copy, Debug, Reflect)]
/// enum Action {
///     Left,
///     Right,
///     Jump,
/// }
///
/// let mut action_state = ActionState::<Action>::default();
///
/// // Typically, this is done automatically by the `InputManagerPlugin` from user inputs
/// // using the `ActionState::update` method
/// action_state.press(&Action::Jump);
///
/// assert!(action_state.pressed(&Action::Jump));
/// assert!(action_state.just_pressed(&Action::Jump));
/// assert!(action_state.released(&Action::Left));
///
/// // Resets just_pressed and just_released
/// let t0 = Instant::now();
/// let t1 = Instant::now();
///
///  action_state.tick(t1, t0);
/// assert!(action_state.pressed(&Action::Jump));
/// assert!(!action_state.just_pressed(&Action::Jump));
///
/// action_state.release(&Action::Jump);
/// assert!(!action_state.pressed(&Action::Jump));
/// assert!(action_state.released(&Action::Jump));
/// assert!(action_state.just_released(&Action::Jump));
///
/// let t2 = Instant::now();
/// action_state.tick(t2, t1);
/// assert!(action_state.released(&Action::Jump));
/// assert!(!action_state.just_released(&Action::Jump));
/// ```
#[derive(Resource, Component, Clone, Debug, PartialEq, Serialize, Deserialize, Reflect)]
#[reflect(Resource, Component)]
pub struct ActionState<A: Actionlike> {
    /// Whether or not all of the actions are disabled.
    disabled: bool,
    /// The shared action data for each action
    action_data: HashMap<A, ActionData>,
}

// The derive does not work unless A: Default,
// so we have to implement it manually
impl<A: Actionlike> Default for ActionState<A> {
    fn default() -> Self {
        Self {
            disabled: false,
            action_data: HashMap::default(),
        }
    }
}

impl<A: Actionlike> ActionState<A> {
    /// Returns a reference to the complete [`ActionData`] for all actions.
    #[inline]
    #[must_use]
    pub fn all_action_data(&self) -> &HashMap<A, ActionData> {
        &self.action_data
    }

    /// We are about to enter the `Main` schedule, so we:
    /// - save all the changes applied to `state` into the `fixed_update_state`
    /// - switch to loading the `update_state`
    pub(crate) fn swap_to_update_state(&mut self) {
        for action_datum in self.action_data.values_mut() {
            action_datum.kind_data.swap_to_update_state();
        }
    }

    /// We are about to enter the `FixedMain` schedule, so we:
    /// - save all the changes applied to `state` into the `update_state`
    /// - switch to loading the `fixed_update_state`
    pub(crate) fn swap_to_fixed_update_state(&mut self) {
        for action_datum in self.action_data.values_mut() {
            action_datum.kind_data.swap_to_fixed_update_state();
        }
    }

    /// Function for advanced users to override the `state` from the `update_state`
    pub fn set_update_state_from_state(&mut self) {
        for action_datum in self.action_data.values_mut() {
            action_datum.kind_data.set_update_state_from_state();
        }
    }

    /// Function for advanced users to override the `state` from the `fixed_update_state`
    pub fn set_fixed_update_state_from_state(&mut self) {
        for action_datum in self.action_data.values_mut() {
            action_datum.kind_data.set_fixed_update_state_from_state();
        }
    }

    /// Updates the [`ActionState`] based on the provided [`UpdatedActions`].
    ///
    /// The `action_data` is typically constructed from [`InputMap::process_actions`](crate::input_map::InputMap::process_actions),
    /// which reads from the assorted [`ButtonInput`](bevy::input::ButtonInput) resources.
    ///
    /// Actions that are disabled will still be updated: instead, their values will be read as released / zero.
    /// You can see their underlying values by checking their [`ActionData`] directly.
    pub fn update(&mut self, updated_actions: UpdatedActions<A>) {
        for (action, updated_value) in updated_actions.iter() {
            match updated_value {
                UpdatedValue::Button(ButtonValue { pressed, value }) => {
                    if *pressed {
                        self.press(action);
                    } else {
                        self.release(action);
                    }
                    self.set_button_value(action, *value);
                }
                UpdatedValue::Axis(value) => {
                    self.set_value(action, *value);
                }
                UpdatedValue::DualAxis(pair) => {
                    self.set_axis_pair(action, *pair);
                }
                UpdatedValue::TripleAxis(triple) => {
                    self.set_axis_triple(action, *triple);
                }
            }
        }
    }

    /// Advances the time for all actions,
    /// transitioning them from `just_pressed` to `pressed`, and `just_released` to `released`.
    ///
    /// If the `timing` feature flag is enabled, the underlying timing and action data will be advanced according to the `current_instant`.
    /// - if no [`Instant`] is set, the `current_instant` will be set as the initial time at which the button was pressed / released
    /// - the [`Duration`] will advance to reflect elapsed time
    ///
    ///
    /// # Example
    /// ```rust
    /// use bevy::prelude::Reflect;
    /// use leafwing_input_manager::prelude::*;
    /// use leafwing_input_manager::buttonlike::ButtonState;
    /// use bevy::platform::time::Instant;
    ///
    /// #[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, Debug, Reflect)]
    /// enum Action {
    ///     Run,
    ///     Jump,
    /// }
    ///
    /// let mut action_state = ActionState::<Action>::default();
    ///
    /// // Actions start released
    /// assert!(action_state.released(&Action::Jump));
    /// assert!(!action_state.just_released(&Action::Run));
    ///
    /// // Ticking time moves causes buttons just released to no longer be just released
    /// let t0 = Instant::now();
    /// let t1 = Instant::now();
    ///
    /// action_state.tick(t1, t0);
    /// assert!(action_state.released(&Action::Jump));
    /// assert!(!action_state.just_released(&Action::Jump));
    ///
    /// action_state.press(&Action::Jump);
    /// assert!(action_state.just_pressed(&Action::Jump));
    ///
    /// // Ticking time moves causes buttons just pressed to no longer be just pressed
    /// let t2 = Instant::now();
    ///
    /// action_state.tick(t2, t1);
    /// assert!(action_state.pressed(&Action::Jump));
    /// assert!(!action_state.just_pressed(&Action::Jump));
    /// ```
    pub fn tick(&mut self, _current_instant: Instant, _previous_instant: Instant) {
        // Advanced the action states
        self.action_data
            .values_mut()
            .for_each(|action_datum| action_datum.tick(_current_instant, _previous_instant));
    }

    /// A reference to the [`ActionData`] corresponding to the `action`.
    #[inline]
    #[must_use]
    pub fn action_data(&self, action: &A) -> Option<&ActionData> {
        self.action_data.get(action)
    }

    /// A mutable reference to the [`ActionData`] corresponding to the `action`.
    ///
    /// To initialize the [`ActionData`] if it has not yet been triggered,
    /// use [`action_data_mut_or_default`](Self::action_data_mut_or_default) method.
    #[inline]
    #[must_use]
    pub fn action_data_mut(&mut self, action: &A) -> Option<&mut ActionData> {
        self.action_data.get_mut(action)
    }

    /// A mutable reference to the [`ActionData`] corresponding to the `action`, initializing it if needed.
    ///
    /// If the `action` has no data yet (because the `action` has not been triggered),
    /// this method will create and insert a default [`ActionData`] for you,
    /// avoiding potential errors from unwrapping [`None`].
    pub fn action_data_mut_or_default(&mut self, action: &A) -> &mut ActionData {
        if self.action_data.contains_key(action) {
            // Safe to unwrap because we just checked
            self.action_data.get_mut(action).unwrap()
        } else {
            self.action_data.insert(
                action.clone(),
                ActionData::from_kind(action.input_control_kind()),
            );
            // Safe to unwrap because we just inserted
            self.action_data_mut(action).unwrap()
        }
    }

    /// A reference of the [`ButtonData`] corresponding to the `action`.
    ///
    /// Generally, it'll be clearer to call `pressed` or so on directly on the [`ActionState`].
    /// However, accessing the raw data directly allows you to examine detailed metadata holistically.
    ///
    /// # Caution
    ///
    /// To access the [`ButtonData`] regardless of whether the `action` has been triggered,
    /// use [`unwrap_or_default`](Option::unwrap_or_default) on the returned [`Option`].
    ///
    /// # Returns
    ///
    /// - `Some(ButtonData)` if it exists.
    /// - `None` if the `action` has never been triggered (pressed, clicked, etc.).
    #[inline]
    #[must_use]
    pub fn button_data(&self, action: &A) -> Option<&ButtonData> {
        match self.action_data(action) {
            Some(action_data) => match action_data.kind_data {
                ActionKindData::Button(ref button_data) => Some(button_data),
                _ => None,
            },
            None => None,
        }
    }

    /// A mutable reference of the [`ButtonData`] corresponding to the `action`.
    ///
    /// Generally, it'll be clearer to call `pressed` or so on directly on the [`ActionState`].
    /// However, accessing the raw data directly allows you to examine detailed metadata holistically.
    ///
    /// # Caution
    ///
    /// To access the [`ButtonData`] regardless of whether the `action` has been triggered,
    /// use [`unwrap_or_default`](Option::unwrap_or_default) on the returned [`Option`].
    ///
    /// To insert a default [`ButtonData`] if it doesn't exist,
    /// use [`button_data_mut_or_default`](Self::button_data_mut_or_default) method.
    ///
    /// # Returns
    ///
    /// - `Some(ButtonData)` if it exists.
    /// - `None` if the `action` has never been triggered (pressed, clicked, etc.).
    #[inline]
    #[must_use]
    pub fn button_data_mut(&mut self, action: &A) -> Option<&mut ButtonData> {
        match self.action_data_mut(action) {
            Some(action_data) => match &mut action_data.kind_data {
                ActionKindData::Button(button_data) => Some(button_data),
                _ => None,
            },
            None => None,
        }
    }

    /// A mutable reference of the [`ButtonData`] corresponding to the `action`, initializing it if needed.
    ///
    /// If the `action` has no data yet (because the `action` has not been triggered),
    /// this method will create and insert a default [`ButtonData`] for you,
    /// avoiding potential errors from unwrapping [`None`].
    ///
    /// Generally, it'll be clearer to call `pressed` or so on directly on the [`ActionState`].
    /// However, accessing the raw data directly allows you to examine detailed metadata holistically.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn button_data_mut_or_default(&mut self, action: &A) -> &mut ButtonData {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        let action_data = self.action_data_mut_or_default(action);
        let ActionKindData::Button(ref mut button_data) = action_data.kind_data else {
            panic!("{action:?} is not a Button");
        };
        button_data
    }

    /// A reference of the [`AxisData`] corresponding to the `action`.
    ///
    /// # Caution
    ///
    /// To access the [`AxisData`] regardless of whether the `action` has been triggered,
    /// use [`unwrap_or_default`](Option::unwrap_or_default) on the returned [`Option`].
    ///
    /// # Returns
    ///
    /// - `Some(AxisData)` if it exists.
    /// - `None` if the `action` has never been triggered (pressed, clicked, etc.).
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn axis_data(&self, action: &A) -> Option<&AxisData> {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Axis);

        match self.action_data(action) {
            Some(action_data) => match action_data.kind_data {
                ActionKindData::Axis(ref axis_data) => Some(axis_data),
                _ => None,
            },
            None => None,
        }
    }

    /// A mutable reference of the [`AxisData`] corresponding to the `action`.
    ///
    /// # Caution
    ///
    /// To insert a default [`AxisData`] if it doesn't exist,
    /// use [`axis_data_mut_or_default`](Self::axis_data_mut_or_default) method.
    ///
    /// # Returns
    ///
    /// - `Some(AxisData)` if it exists.
    /// - `None` if the `action` has never been triggered (pressed, clicked, etc.).
    #[inline]
    #[must_use]
    pub fn axis_data_mut(&mut self, action: &A) -> Option<&mut AxisData> {
        match self.action_data_mut(action) {
            Some(action_data) => match &mut action_data.kind_data {
                ActionKindData::Axis(axis_data) => Some(axis_data),
                _ => None,
            },
            None => None,
        }
    }

    /// A mutable reference of the [`AxisData`] corresponding to the `action`, initializing it if needed..
    ///
    /// If the `action` has no data yet (because the `action` has not been triggered),
    /// this method will create and insert a default [`AxisData`] for you,
    /// avoiding potential errors from unwrapping [`None`].
    ///
    /// Generally, it'll be clearer to call `pressed` or so on directly on the [`ActionState`].
    /// However, accessing the raw data directly allows you to examine detailed metadata holistically.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn axis_data_mut_or_default(&mut self, action: &A) -> &mut AxisData {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Axis);

        let action_data = self.action_data_mut_or_default(action);
        let ActionKindData::Axis(ref mut axis_data) = action_data.kind_data else {
            panic!("{action:?} is not an Axis");
        };
        axis_data
    }

    /// A reference of the [`DualAxisData`] corresponding to the `action`.
    ///
    /// # Caution
    ///
    /// To access the [`DualAxisData`] regardless of whether the `action` has been triggered,
    /// use [`unwrap_or_default`](Option::unwrap_or_default) on the returned [`Option`].
    ///
    /// # Returns
    ///
    /// - `Some(DualAxisData)` if it exists.
    /// - `None` if the `action` has never been triggered (pressed, clicked, etc.).
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn dual_axis_data(&self, action: &A) -> Option<&DualAxisData> {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::DualAxis);

        match self.action_data(action) {
            Some(action_data) => match action_data.kind_data {
                ActionKindData::DualAxis(ref dual_axis_data) => Some(dual_axis_data),
                _ => None,
            },
            None => None,
        }
    }

    /// A mutable reference of the [`DualAxisData`] corresponding to the `action`.
    ///
    /// # Caution
    ///
    /// To insert a default [`DualAxisData`] if it doesn't exist,
    /// use [`dual_axis_data_mut_or_default`](Self::dual_axis_data_mut_or_default) method.
    ///
    /// # Returns
    ///
    /// - `Some(DualAxisData)` if it exists.
    /// - `None` if the `action` has never been triggered (pressed, clicked, etc.).
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn dual_axis_data_mut(&mut self, action: &A) -> Option<&mut DualAxisData> {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::DualAxis);

        match self.action_data_mut(action) {
            Some(action_data) => match &mut action_data.kind_data {
                ActionKindData::DualAxis(dual_axis_data) => Some(dual_axis_data),
                _ => None,
            },
            None => None,
        }
    }

    /// A mutable reference of the [`DualAxisData`] corresponding to the `action` initializing it if needed.
    ///
    /// If the `action` has no data yet (because the `action` has not been triggered),
    /// this method will create and insert a default [`DualAxisData`] for you,
    /// avoiding potential errors from unwrapping [`None`].
    ///
    /// Generally, it'll be clearer to call `pressed` or so on directly on the [`ActionState`].
    /// However, accessing the raw data directly allows you to examine detailed metadata holistically.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn dual_axis_data_mut_or_default(&mut self, action: &A) -> &mut DualAxisData {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::DualAxis);

        let action_data = self.action_data_mut_or_default(action);
        let ActionKindData::DualAxis(ref mut dual_axis_data) = action_data.kind_data else {
            panic!("{action:?} is not a DualAxis");
        };
        dual_axis_data
    }

    /// A reference of the [`TripleAxisData`] corresponding to the `action`.
    ///
    /// # Caution
    ///
    /// To access the [`TripleAxisData`] regardless of whether the `action` has been triggered,
    /// use [`unwrap_or_default`](Option::unwrap_or_default) on the returned [`Option`].
    ///
    /// # Returns
    ///
    /// - `Some(TripleAxisData)` if it exists.
    /// - `None` if the `action` has never been triggered (pressed, clicked, etc.).
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn triple_axis_data(&self, action: &A) -> Option<&TripleAxisData> {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::TripleAxis);

        match self.action_data(action) {
            Some(action_data) => match action_data.kind_data {
                ActionKindData::TripleAxis(ref triple_axis_data) => Some(triple_axis_data),
                _ => None,
            },
            None => None,
        }
    }

    /// A mutable reference of the [`TripleAxisData`] corresponding to the `action`.
    ///
    /// # Caution
    ///
    /// To insert a default [`TripleAxisData`] if it doesn't exist,
    /// use [`triple_axis_data_mut_or_default`](Self::dual_axis_data_mut_or_default) method.
    ///
    /// # Returns
    ///
    /// - `Some(ButtonData)` if it exists.
    /// - `None` if the `action` has never been triggered (pressed, clicked, etc.).
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn triple_axis_data_mut(&mut self, action: &A) -> Option<&mut TripleAxisData> {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::TripleAxis);

        match self.action_data_mut(action) {
            Some(action_data) => match &mut action_data.kind_data {
                ActionKindData::TripleAxis(triple_axis_data) => Some(triple_axis_data),
                _ => None,
            },
            None => None,
        }
    }

    /// A mutable reference of the [`TripleAxisData`] corresponding to the `action` initializing it if needed.
    ///
    /// If the `action` has no data yet (because the `action` has not been triggered),
    /// this method will create and insert a default [`TripleAxisData`] for you,
    /// avoiding potential errors from unwrapping [`None`].
    ///
    /// Generally, it'll be clearer to call `pressed` or so on directly on the [`ActionState`].
    /// However, accessing the raw data directly allows you to examine detailed metadata holistically.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn triple_axis_data_mut_or_default(&mut self, action: &A) -> &mut TripleAxisData {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::TripleAxis);

        let action_data = self.action_data_mut_or_default(action);
        let ActionKindData::TripleAxis(ref mut triple_axis_data) = action_data.kind_data else {
            panic!("{action:?} is not a TripleAxis");
        };
        triple_axis_data
    }

    /// Get the value associated with the corresponding buttonlike `action` if present.
    ///
    /// # Warnings
    ///
    /// This value may not be bounded as you might expect.
    /// Consider clamping this to account for multiple triggering inputs,
    /// typically using the [`clamped_button_value`](Self::clamped_button_value) method instead.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn button_value(&self, action: &A) -> f32 {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        if self.action_disabled(action) {
            return 0.0;
        }

        let action_data = self.button_data(action);
        action_data.map_or(0.0, |action_data| action_data.value)
    }

    /// Sets the value of the buttonlike `action` to the provided `value`.
    /// A threshold of `0.02` must be overcome for the button to count as "pressed"
    /// This is used to account for a semi-frequent issue with analog inputs
    /// (e.g., gamepad triggers) reporting very small non-zero values when
    /// not physically pressed, due to sensor imprecision.
    ///
    /// Also updates the state of the button based on the `value`:
    /// - If `value > 0.02`, the button will be pressed.
    /// - If `value <= 0.0`, the button will be released.
    #[track_caller]
    pub fn set_button_value(&mut self, action: &A, value: f32) {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);
        const BUTTON_PRESS_THRESHOLD: f32 = 0.02;

        let button_data = self.button_data_mut_or_default(action);
        button_data.value = value;

        if value > BUTTON_PRESS_THRESHOLD {
            #[cfg(feature = "timing")]
            if button_data.state.released() {
                button_data.timing.flip();
            }

            button_data.state.press();
        } else {
            #[cfg(feature = "timing")]
            if button_data.state.pressed() {
                button_data.timing.flip();
            }

            button_data.state.release();
        }
    }

    /// Get the value associated with the corresponding `action`, clamped to `[0.0, 1.0]`.
    ///
    /// # Warning
    ///
    /// This value will be 0. by default,
    /// even if the action is not a buttonlike action.
    pub fn clamped_button_value(&self, action: &A) -> f32 {
        self.button_value(action).clamp(0., 1.)
    }

    /// Get the value associated with the corresponding axislike `action` if present.
    ///
    /// # Warnings
    ///
    /// This value may not be bounded as you might expect.
    /// Consider clamping this to account for multiple triggering inputs,
    /// typically using the [`clamped_value`](Self::clamped_value) method instead.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn value(&self, action: &A) -> f32 {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Axis);

        if self.action_disabled(action) {
            return 0.0;
        }

        let action_data = self.axis_data(action);
        action_data.map_or(0.0, |action_data| action_data.value)
    }

    /// Sets the value of the axislike `action` to the provided `value`.
    #[track_caller]
    pub fn set_value(&mut self, action: &A, value: f32) {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Axis);

        let axis_data = self.axis_data_mut_or_default(action);
        axis_data.value = value;
    }

    /// Get the value associated with the corresponding `action`, clamped to `[-1.0, 1.0]`.
    ///
    /// # Warning
    ///
    /// This value will be 0. by default,
    /// even if the action is not an axislike action.
    pub fn clamped_value(&self, action: &A) -> f32 {
        self.value(action).clamp(-1., 1.)
    }

    /// Get the [`Vec2`] from the binding that triggered the corresponding `action`.
    ///
    /// Only messages that represent dual-axis control provide a [`Vec2`],
    /// and this will return [`None`] for other messages.
    ///
    /// If multiple inputs with an axis pair trigger the same game action at the same time, the
    /// value of each axis pair will be added together.
    ///
    /// # Warning
    ///
    /// This value will be [`Vec2::ZERO`] by default,
    /// even if the action is not a dual-axislike action.
    ///
    /// These values may not be bounded as you might expect.
    /// Consider clamping this to account for multiple triggering inputs,
    /// typically using the [`clamped_axis_pair`](Self::clamped_axis_pair) method instead.
    #[must_use]
    #[track_caller]
    pub fn axis_pair(&self, action: &A) -> Vec2 {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::DualAxis);

        if self.action_disabled(action) {
            return Vec2::ZERO;
        }

        let action_data = self.dual_axis_data(action);
        action_data.map_or(Vec2::ZERO, |action_data| action_data.pair)
    }

    /// Sets the [`Vec2`] of the `action` to the provided `pair`.
    #[track_caller]
    pub fn set_axis_pair(&mut self, action: &A, pair: Vec2) {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::DualAxis);

        let dual_axis_data = self.dual_axis_data_mut_or_default(action);
        dual_axis_data.pair = pair;
    }

    /// Get the [`Vec2`] associated with the corresponding `action`, clamped to `[-1.0, 1.0]`.
    ///  
    /// # Warning
    ///
    /// This value will be [`Vec2::ZERO`] by default,
    /// even if the action is not a dual-axislike action.
    pub fn clamped_axis_pair(&self, action: &A) -> Vec2 {
        let pair = self.axis_pair(action);
        pair.clamp(Vec2::NEG_ONE, Vec2::ONE)
    }

    /// Get the [`Vec3`] from the binding that triggered the corresponding `action`.
    ///
    /// Only messages that represent triple-axis control provide a [`Vec3`],
    /// and this will return [`None`] for other messages.
    ///
    /// If multiple inputs with an axis triple trigger the same game action at the same time, the
    /// value of each axis triple will be added together.
    ///
    /// # Warning
    ///
    /// This value will be [`Vec3::ZERO`] by default,
    /// even if the action is not a triple-axislike action.
    ///
    /// These values may not be bounded as you might expect.
    /// Consider clamping this to account for multiple triggering inputs,
    /// typically using the [`clamped_axis_triple`](Self::clamped_axis_triple) method instead.
    #[must_use]
    #[track_caller]
    pub fn axis_triple(&self, action: &A) -> Vec3 {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::TripleAxis);

        if self.action_disabled(action) {
            return Vec3::ZERO;
        }

        let action_data = self.triple_axis_data(action);
        action_data.map_or(Vec3::ZERO, |action_data| action_data.triple)
    }

    /// Sets the [`Vec2`] of the `action` to the provided `pair`.
    #[track_caller]
    pub fn set_axis_triple(&mut self, action: &A, triple: Vec3) {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::TripleAxis);

        let triple_axis_data = self.triple_axis_data_mut_or_default(action);
        triple_axis_data.triple = triple;
    }

    /// Get the [`Vec3`] associated with the corresponding `action`, clamped to the cube of values bounded by -1 and 1 on all axes.
    ///
    /// # Warning
    ///
    /// This value will be [`Vec3::ZERO`] by default,
    /// even if the action is not a dual-axislike action.
    pub fn clamped_axis_triple(&self, action: &A) -> Vec3 {
        let triple = self.axis_triple(action);
        triple.clamp(Vec3::NEG_ONE, Vec3::ONE)
    }

    /// Manually sets the [`ButtonData`] of the corresponding `action`
    ///
    /// You should almost always use more direct methods, as they are simpler and less error-prone.
    ///
    /// However, this method can be useful for testing,
    /// or when transferring [`ButtonData`] between action states.
    ///
    /// # Example
    /// ```rust
    /// use bevy::prelude::Reflect;
    /// use leafwing_input_manager::prelude::*;
    ///
    /// #[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, Debug, Reflect)]
    /// enum AbilitySlot {
    ///     Slot1,
    ///     Slot2,
    /// }
    ///
    /// #[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, Debug, Reflect)]
    /// enum Action {
    ///     Run,
    ///     Jump,
    /// }
    ///
    /// let mut ability_slot_state = ActionState::<AbilitySlot>::default();
    /// let mut action_state = ActionState::<Action>::default();
    ///
    /// // Extract the state from the ability slot
    /// let slot_1_state = ability_slot_state.button_data(&AbilitySlot::Slot1);
    ///
    /// // And transfer it to the actual ability that we care about
    /// // without losing timing information
    /// if let Some(state) = slot_1_state {
    ///    action_state.set_button_data(Action::Run, state.clone());
    /// }
    /// ```
    #[inline]
    #[track_caller]
    pub fn set_button_data(&mut self, action: A, data: ButtonData) {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        let button_data = self.button_data_mut_or_default(&action);
        *button_data = data;
    }

    /// Press the `action`
    ///
    /// No initial instant or reasons why the button was pressed will be recorded.
    /// Instead, this is set through [`ActionState::tick()`]
    #[inline]
    #[track_caller]
    pub fn press(&mut self, action: &A) {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        let action_data = self.button_data_mut_or_default(action);

        #[cfg(feature = "timing")]
        if action_data.update_state.released() {
            action_data.timing.flip();
        }

        action_data.state.press();
        action_data.value = 1.0;
    }

    /// Release the `action`
    ///
    /// No initial instant will be recorded.
    /// Instead, this is set through [`ActionState::tick()`]
    #[inline]
    pub fn release(&mut self, action: &A) {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        let action_data = self.button_data_mut_or_default(action);

        #[cfg(feature = "timing")]
        if action_data.update_state.pressed() {
            action_data.timing.flip();
        }

        action_data.state.release();
        action_data.value = 0.0;
    }

    /// Resets an action to its default state.
    ///
    /// Buttons will be released, and axes will be set to 0.
    pub fn reset(&mut self, action: &A) {
        match action.input_control_kind() {
            InputControlKind::Button => self.release(action),
            InputControlKind::Axis => {
                self.set_value(action, 0.0);
            }
            InputControlKind::DualAxis => {
                self.set_axis_pair(action, Vec2::ZERO);
            }
            InputControlKind::TripleAxis => {
                self.set_axis_triple(action, Vec3::ZERO);
            }
        }
    }

    /// Releases all [`Buttonlike`](crate::user_input::Buttonlike) actions,
    /// sets all [`Axislike`](crate::user_input::Axislike) actions to 0,
    /// sets all [`DualAxislike`](crate::user_input::DualAxislike) actions to [`Vec2::ZERO`],
    /// and sets all [`TripleAxislike`](crate::user_input::TripleAxislike) actions to [`Vec3::ZERO`].
    pub fn reset_all(&mut self) {
        // Collect out to avoid angering the borrow checker
        let all_actions = self.action_data.keys().cloned().collect::<Vec<A>>();
        for action in all_actions.into_iter() {
            self.reset(&action);
        }
    }

    /// Is the entire [`ActionState`] currently disabled?
    pub fn disabled(&self) -> bool {
        self.disabled
    }

    /// Is this `action` currently disabled?
    #[inline]
    #[must_use]
    pub fn action_disabled(&self, action: &A) -> bool {
        if self.disabled {
            return true;
        }

        match self.action_data(action) {
            Some(action_data) => action_data.disabled,
            None => false,
        }
    }

    /// Disables the entire [`ActionState`].
    ///
    /// All values will be reset to their default state.
    #[inline]
    pub fn disable(&mut self) {
        self.disabled = true;
        self.reset_all();
    }

    /// Disables the `action`.
    ///
    /// The action's value will be reset to its default state.
    #[inline]
    pub fn disable_action(&mut self, action: &A) {
        let action_data = self.action_data_mut_or_default(action);

        action_data.disabled = true;
        self.reset(action);
    }

    /// Disables all actions
    #[inline]
    pub fn disable_all_actions(&mut self) {
        for action in self.keys() {
            self.disable_action(&action);
        }
    }

    /// Enables the entire [`ActionState`]
    #[inline]
    pub fn enable(&mut self) {
        self.disabled = false;
    }

    /// Enables the `action`
    #[inline]
    pub fn enable_action(&mut self, action: &A) {
        let action_data = self.action_data_mut_or_default(action);

        action_data.disabled = false;
    }

    /// Enables all actions
    #[inline]
    pub fn enable_all_actions(&mut self) {
        for action in self.keys() {
            self.enable_action(&action);
        }
    }

    /// Is this `action` currently pressed?
    ///
    /// # Warning
    ///
    /// This value will be `false` by default,
    /// even if the action is not a buttonlike action.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn pressed(&self, action: &A) -> bool {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        if self.action_disabled(action) {
            return false;
        }

        match self.button_data(action) {
            Some(button_data) => button_data.pressed(),
            None => false,
        }
    }

    /// Was this `action` pressed since the last time [tick](ActionState::tick) was called?
    ///
    /// # Warning
    ///
    /// This value will be `false` by default,
    /// even if the action is not a buttonlike action.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn just_pressed(&self, action: &A) -> bool {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        if self.action_disabled(action) {
            return false;
        }

        match self.button_data(action) {
            Some(button_data) => button_data.just_pressed(),
            None => false,
        }
    }

    /// Is this `action` currently released?
    ///
    /// This is always the logical negation of [pressed](ActionState::pressed)
    ///
    /// # Warning
    ///
    /// This value will be `true` by default,
    /// even if the action is not a buttonlike action.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn released(&self, action: &A) -> bool {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        if self.action_disabled(action) {
            return true;
        }

        match self.button_data(action) {
            Some(button_data) => button_data.released(),
            None => true,
        }
    }

    /// Was this `action` released since the last time [tick](ActionState::tick) was called?
    ///
    /// # Warning
    ///
    /// This value will be `false` by default,
    /// even if the action is not a buttonlike action.
    #[inline]
    #[must_use]
    #[track_caller]
    pub fn just_released(&self, action: &A) -> bool {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        if self.action_disabled(action) {
            return false;
        }

        match self.button_data(action) {
            Some(button_data) => button_data.just_released(),
            None => false,
        }
    }

    #[must_use]
    /// Which actions are currently pressed?
    pub fn get_pressed(&self) -> Vec<A> {
        let all_actions = self.action_data.keys().cloned();

        all_actions
            .into_iter()
            .filter(|action| action.input_control_kind() == InputControlKind::Button)
            .filter(|action| self.pressed(action))
            .collect()
    }

    #[must_use]
    /// Which actions were just pressed?
    pub fn get_just_pressed(&self) -> Vec<A> {
        let all_actions = self.action_data.keys().cloned();

        all_actions
            .into_iter()
            .filter(|action| action.input_control_kind() == InputControlKind::Button)
            .filter(|action| self.just_pressed(action))
            .collect()
    }

    #[must_use]
    /// Which actions are currently released?
    pub fn get_released(&self) -> Vec<A> {
        let all_actions = self.action_data.keys().cloned();

        all_actions
            .into_iter()
            .filter(|action| action.input_control_kind() == InputControlKind::Button)
            .filter(|action| self.released(action))
            .collect()
    }

    #[must_use]
    /// Which actions were just released?
    pub fn get_just_released(&self) -> Vec<A> {
        let all_actions = self.action_data.keys().cloned();

        all_actions
            .into_iter()
            .filter(|action| action.input_control_kind() == InputControlKind::Button)
            .filter(|action| self.just_released(action))
            .collect()
    }

    /// The [`Instant`] that the action was last pressed or released
    ///
    ///
    ///
    /// If the action was pressed or released since the last time [`ActionState::tick`] was called
    /// the value will be [`None`].
    /// This ensures that all our actions are assigned a timing and duration
    /// that corresponds exactly to the start of a frame, rather than relying on idiosyncratic timing.
    ///
    /// This will also be [`None`] if the action was never pressed or released.
    #[cfg(feature = "timing")]
    #[must_use]
    #[track_caller]
    pub fn instant_started(&self, action: &A) -> Option<Instant> {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        let button_data = self.button_data(action)?;
        button_data.timing.instant_started
    }

    /// The [`Duration`] for which the action has been held or released
    ///
    /// This will be [`Duration::ZERO`] if the action was never pressed or released.
    #[cfg(feature = "timing")]
    #[must_use]
    #[track_caller]
    pub fn current_duration(&self, action: &A) -> Duration {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        self.button_data(action)
            .map(|data| data.timing.current_duration)
            .unwrap_or_default()
    }

    /// The [`Duration`] for which the action was last held or released
    ///
    /// This is a snapshot of the [`ActionState::current_duration`] state at the time
    /// the action was last pressed or released.
    ///
    /// This will be [`Duration::ZERO`] if the action was never pressed or released.
    #[cfg(feature = "timing")]
    #[must_use]
    #[track_caller]
    pub fn previous_duration(&self, action: &A) -> Duration {
        debug_assert_eq!(action.input_control_kind(), InputControlKind::Button);

        self.button_data(action)
            .map(|data| data.timing.previous_duration)
            .unwrap_or_default()
    }

    /// Applies an [`ActionDiff`] (usually received over the network) to the [`ActionState`].
    ///
    /// This lets you reconstruct an [`ActionState`] from a stream of [`ActionDiff`]s
    pub fn apply_diff(&mut self, action_diff: &ActionDiff<A>) {
        match action_diff {
            ActionDiff::Pressed { action, value } => {
                self.set_button_value(action, *value);
            }
            ActionDiff::Released { action } => {
                self.release(action);
            }
            ActionDiff::AxisChanged { action, value } => {
                self.set_value(action, *value);
            }
            ActionDiff::DualAxisChanged { action, axis_pair } => {
                self.set_axis_pair(action, *axis_pair);
            }
            ActionDiff::TripleAxisChanged {
                action,
                axis_triple,
            } => {
                self.set_axis_triple(action, *axis_triple);
            }
        };
    }

    /// Returns an owned list of the [`Actionlike`] keys in this [`ActionState`].
    #[inline]
    #[must_use]
    pub fn keys(&self) -> Vec<A> {
        self.action_data.keys().cloned().collect()
    }
}

#[cfg(test)]
mod tests {
    use crate as leafwing_input_manager;
    use crate::action_diff::ActionDiff;
    use crate::action_state::{
        ActionData, ActionKindData, ActionState, AxisData, ButtonData, DualAxisData,
    };
    use crate::buttonlike::{ButtonState, ButtonValue};
    use crate::input_map::UpdatedActions;
    #[cfg(any(feature = "gamepad", feature = "keyboard"))]
    use crate::prelude::{ButtonlikeChord, InputMap};
    #[cfg(feature = "gamepad")]
    use bevy::input::gamepad::GamepadButton;
    #[cfg(feature = "keyboard")]
    use bevy::input::keyboard::KeyCode;
    use bevy::platform::collections::HashMap;
    use bevy::prelude::*;
    use leafwing_input_manager_macros::Actionlike;

    #[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, Debug, Reflect)]
    enum TestAction {
        Trigger,
        Run,
        Jump,
        Hide,
        One,
        Two,
        OneAndTwo,
        #[actionlike(Axis)]
        Axis,
        #[actionlike(DualAxis)]
        DualAxis,
        #[actionlike(TripleAxis)]
        TripleAxis,
    }

    #[cfg(any(feature = "keyboard", feature = "gamepad"))]
    struct TestContext {
        pub app: App,
        pub input_map: InputMap<TestAction>,
    }

    #[cfg(any(feature = "keyboard", feature = "gamepad"))]
    impl TestContext {
        pub fn new() -> Self {
            use bevy::input::InputPlugin;

            use crate::plugin::InputManagerPlugin;

            let mut app = App::new();
            app.add_plugins((
                MinimalPlugins,
                InputPlugin,
                InputManagerPlugin::<TestAction>::default(),
            ));

            let mut input_map = InputMap::default();
            #[cfg(feature = "gamepad")]
            input_map.insert(TestAction::Trigger, GamepadButton::RightTrigger);

            #[cfg(feature = "keyboard")]
            {
                input_map.insert(TestAction::One, KeyCode::Digit1);
                input_map.insert(TestAction::Two, KeyCode::Digit2);
                input_map.insert(
                    TestAction::OneAndTwo,
                    ButtonlikeChord::new([KeyCode::Digit1, KeyCode::Digit2]),
                );
                input_map.insert(TestAction::Run, KeyCode::KeyR);
            }

            app.insert_resource(input_map.clone())
                .init_resource::<ActionState<TestAction>>();

            app.update();

            Self { app, input_map }
        }

        #[cfg(feature = "gamepad")]
        pub fn send_gamepad_connection_event(&mut self, gamepad: Option<Entity>) -> Entity {
            use bevy::input::gamepad::{GamepadConnection, GamepadConnectionEvent};

            let gamepad = gamepad.unwrap_or_else(|| self.app.world_mut().spawn_empty().id());
            self.app
                .world_mut()
                .resource_mut::<Messages<GamepadConnectionEvent>>()
                .write(GamepadConnectionEvent::new(
                    gamepad,
                    GamepadConnection::Connected {
                        name: "TestController".to_string(),
                        vendor_id: None,
                        product_id: None,
                    },
                ));
            gamepad
        }

        pub fn update(&mut self) {
            self.app.update();
        }
    }

    #[test]
    fn action_state_default_state() {
        let action_state = ActionState::<TestAction>::default();

        assert!(!action_state.disabled);
        assert_eq!(action_state.action_data, HashMap::default());
    }

    #[test]
    fn action_state_all_action_data() {
        let action_state = ActionState::<TestAction>::default();

        assert_eq!(action_state.all_action_data(), &action_state.action_data);
    }

    #[test]
    fn action_state_button() {
        let mut action_state = ActionState::<TestAction>::default();

        assert!(!action_state.pressed(&TestAction::Jump));
        assert_eq!(action_state.button_value(&TestAction::Jump), 0.0);

        let mut updated_actions = UpdatedActions::<TestAction>::default();
        updated_actions.0.insert(
            TestAction::Jump,
            crate::input_map::UpdatedValue::Button(ButtonValue {
                pressed: true,
                value: 0.5,
            }),
        );

        action_state.update(updated_actions);

        assert!(action_state.pressed(&TestAction::Jump));
        assert_eq!(action_state.button_value(&TestAction::Jump), 0.5);
    }

    #[test]
    fn action_state_axis() {
        let mut action_state = ActionState::<TestAction>::default();

        assert_eq!(action_state.value(&TestAction::Axis), 0.0);

        let mut updated_actions = UpdatedActions::<TestAction>::default();
        updated_actions
            .0
            .insert(TestAction::Axis, crate::input_map::UpdatedValue::Axis(0.5));

        action_state.update(updated_actions);

        assert_eq!(action_state.value(&TestAction::Axis), 0.5);
    }

    #[test]
    fn action_state_dual_axis() {
        let mut action_state = ActionState::<TestAction>::default();

        assert_eq!(
            action_state.axis_pair(&TestAction::DualAxis),
            Vec2::new(0.0, 0.0)
        );

        let mut updated_actions = UpdatedActions::<TestAction>::default();
        updated_actions.0.insert(
            TestAction::DualAxis,
            crate::input_map::UpdatedValue::DualAxis(Vec2::new(0.5, 0.5)),
        );

        action_state.update(updated_actions);

        assert_eq!(
            action_state.axis_pair(&TestAction::DualAxis),
            Vec2::new(0.5, 0.5)
        );
    }

    #[test]
    fn action_state_triple_axis() {
        let mut action_state = ActionState::<TestAction>::default();

        assert_eq!(
            action_state.axis_triple(&TestAction::TripleAxis),
            Vec3::new(0.0, 0.0, 0.0)
        );

        let mut updated_actions = UpdatedActions::<TestAction>::default();
        updated_actions.0.insert(
            TestAction::TripleAxis,
            crate::input_map::UpdatedValue::TripleAxis(Vec3::new(0.5, 0.5, 0.5)),
        );

        action_state.update(updated_actions);

        assert_eq!(
            action_state.axis_triple(&TestAction::TripleAxis),
            Vec3::new(0.5, 0.5, 0.5)
        );
    }

    #[cfg(feature = "keyboard")]
    #[test]
    fn press_lifecycle() {
        use std::time::{Duration, Instant};

        use crate::prelude::Buttonlike;
        use crate::prelude::ClashStrategy;
        use crate::prelude::updating::CentralInputStore;

        let ctx = TestContext::new();
        let mut app = ctx.app;
        let input_map = ctx.input_map;

        // Action state
        let mut action_state = ActionState::<TestAction>::default();
        println!(
            "Default button data: {:?}",
            action_state.button_data(&TestAction::Run)
        );

        // Starting state
        let input_store = app.world().resource::<CentralInputStore>();
        action_state.update(input_map.process_actions(None, input_store, ClashStrategy::PressAll));

        println!(
            "Initialized button data: {:?}",
            action_state.button_data(&TestAction::Run)
        );

        assert!(!action_state.pressed(&TestAction::Run));
        assert!(!action_state.just_pressed(&TestAction::Run));
        assert!(action_state.released(&TestAction::Run));
        assert!(!action_state.just_released(&TestAction::Run));

        // Pressing
        KeyCode::KeyR.press(app.world_mut());
        // Process the input messages into Input<KeyCode> data
        app.update();
        let input_store = app.world().resource::<CentralInputStore>();

        action_state.update(input_map.process_actions(None, input_store, ClashStrategy::PressAll));

        assert!(action_state.pressed(&TestAction::Run));
        assert!(action_state.just_pressed(&TestAction::Run));
        assert!(!action_state.released(&TestAction::Run));
        assert!(!action_state.just_released(&TestAction::Run));

        // Waiting
        action_state.tick(Instant::now(), Instant::now() - Duration::from_micros(1));
        action_state.update(input_map.process_actions(None, input_store, ClashStrategy::PressAll));

        assert!(action_state.pressed(&TestAction::Run));
        assert!(!action_state.just_pressed(&TestAction::Run));
        assert!(!action_state.released(&TestAction::Run));
        assert!(!action_state.just_released(&TestAction::Run));

        // Releasing
        KeyCode::KeyR.release(app.world_mut());
        app.update();
        let input_store = app.world().resource::<CentralInputStore>();

        action_state.update(input_map.process_actions(None, input_store, ClashStrategy::PressAll));

        assert!(!action_state.pressed(&TestAction::Run));
        assert!(!action_state.just_pressed(&TestAction::Run));
        assert!(action_state.released(&TestAction::Run));
        assert!(action_state.just_released(&TestAction::Run));

        // Waiting
        action_state.tick(Instant::now(), Instant::now() - Duration::from_micros(1));
        action_state.update(input_map.process_actions(None, input_store, ClashStrategy::PressAll));

        assert!(!action_state.pressed(&TestAction::Run));
        assert!(!action_state.just_pressed(&TestAction::Run));
        assert!(action_state.released(&TestAction::Run));
        assert!(!action_state.just_released(&TestAction::Run));
    }

    #[test]
    fn synthetic_press() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.press(&TestAction::One);
        dbg!(&action_state);

        assert!(action_state.pressed(&TestAction::One));
        assert!(action_state.just_pressed(&TestAction::One));
        assert!(!action_state.released(&TestAction::One));
        assert!(!action_state.just_released(&TestAction::One));

        assert!(!action_state.pressed(&TestAction::Two));
        assert!(!action_state.just_pressed(&TestAction::Two));
        assert!(action_state.released(&TestAction::Two));
        assert!(!action_state.just_released(&TestAction::Two));
    }

    #[cfg(feature = "keyboard")]
    #[test]
    #[ignore = "Clashing inputs for non-buttonlike inputs is broken."]
    fn update_with_clashes_prioritizing_longest() {
        use std::time::{Duration, Instant};

        use crate::prelude::ClashStrategy;
        use crate::prelude::updating::CentralInputStore;
        use crate::user_input::Buttonlike;
        use bevy::prelude::KeyCode::*;

        let ctx = TestContext::new();
        let mut app = ctx.app;
        let input_map = ctx.input_map;

        // Action state
        let mut action_state = ActionState::<TestAction>::default();

        // Starting state
        let input_store = app.world().resource::<CentralInputStore>();
        action_state.update(input_map.process_actions(
            None,
            input_store,
            ClashStrategy::PrioritizeLongest,
        ));
        assert!(action_state.released(&TestAction::One));
        assert!(action_state.released(&TestAction::Two));
        assert!(action_state.released(&TestAction::OneAndTwo));

        // Pressing One
        Digit1.press(app.world_mut());
        app.update();
        let input_store = app.world().resource::<CentralInputStore>();

        action_state.update(input_map.process_actions(
            None,
            input_store,
            ClashStrategy::PrioritizeLongest,
        ));

        assert!(action_state.pressed(&TestAction::One));
        assert!(action_state.released(&TestAction::Two));
        assert!(action_state.released(&TestAction::OneAndTwo));

        // Waiting
        action_state.tick(Instant::now(), Instant::now() - Duration::from_micros(1));
        action_state.update(input_map.process_actions(
            None,
            input_store,
            ClashStrategy::PrioritizeLongest,
        ));

        assert!(action_state.pressed(&TestAction::One));
        assert!(action_state.released(&TestAction::Two));
        assert!(action_state.released(&TestAction::OneAndTwo));

        // Pressing Two
        Digit2.press(app.world_mut());
        app.update();
        let input_store = app.world().resource::<CentralInputStore>();

        action_state.update(input_map.process_actions(
            None,
            input_store,
            ClashStrategy::PrioritizeLongest,
        ));

        // Now only the longest OneAndTwo has been pressed,
        // while both One and Two have been released
        assert!(action_state.released(&TestAction::One));
        assert!(action_state.released(&TestAction::Two));
        assert!(action_state.pressed(&TestAction::OneAndTwo));

        // Waiting
        action_state.tick(Instant::now(), Instant::now() - Duration::from_micros(1));
        action_state.update(input_map.process_actions(
            None,
            input_store,
            ClashStrategy::PrioritizeLongest,
        ));

        assert!(action_state.released(&TestAction::One));
        assert!(action_state.released(&TestAction::Two));
        assert!(action_state.pressed(&TestAction::OneAndTwo));
    }

    #[test]
    fn test_set_update_state_from_state() {
        let mut action_state = ActionState::<TestAction>::default();

        // Initial state
        assert!(action_state.released(&TestAction::Run));
        assert!(!action_state.just_released(&TestAction::Run));
        assert!(!action_state.pressed(&TestAction::Run));
        assert!(!action_state.just_pressed(&TestAction::Run));

        // Update the state manually
        action_state.action_data.insert(
            TestAction::Run,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::Button(ButtonData {
                    state: ButtonState::Pressed,
                    update_state: ButtonState::Pressed,
                    fixed_update_state: ButtonState::Pressed,
                    value: 1.0,
                    update_value: 1.0,
                    fixed_update_value: 1.0,
                    #[cfg(feature = "timing")]
                    timing: Default::default(),
                }),
            },
        );
        action_state.set_update_state_from_state();

        // Check the state
        assert!(action_state.pressed(&TestAction::Run));
        assert!(!action_state.just_pressed(&TestAction::Run));
        assert!(!action_state.released(&TestAction::Run));
        assert!(!action_state.just_released(&TestAction::Run));
    }

    #[test]
    fn test_set_fixed_update_state_from_state() {
        let mut action_state = ActionState::<TestAction>::default();

        // Initial state
        assert!(action_state.released(&TestAction::Run));
        assert!(!action_state.just_released(&TestAction::Run));
        assert!(!action_state.pressed(&TestAction::Run));
        assert!(!action_state.just_pressed(&TestAction::Run));

        // Update the state manually
        action_state.action_data.insert(
            TestAction::Run,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::Button(ButtonData {
                    state: ButtonState::Pressed,
                    update_state: ButtonState::Pressed,
                    fixed_update_state: ButtonState::Pressed,
                    value: 1.0,
                    update_value: 1.0,
                    fixed_update_value: 1.0,
                    #[cfg(feature = "timing")]
                    timing: Default::default(),
                }),
            },
        );
        action_state.set_fixed_update_state_from_state();

        assert!(action_state.pressed(&TestAction::Run));
        assert!(!action_state.just_pressed(&TestAction::Run));
        assert!(!action_state.released(&TestAction::Run));
        assert!(!action_state.just_released(&TestAction::Run));
    }

    #[test]
    fn test_button_data_for_button_action_without_data() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.remove(&TestAction::Run);
        assert!(action_state.button_data(&TestAction::Run).is_none());
    }

    #[test]
    fn test_button_data_for_non_button_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::Axis,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::Axis(AxisData {
                    value: 0.5,
                    update_value: 0.5,
                    fixed_update_value: 0.5,
                }),
            },
        );
        assert!(action_state.button_data(&TestAction::Axis).is_none());
    }

    #[test]
    fn test_button_data_mut_for_button_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::Run,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::Button(ButtonData {
                    state: ButtonState::Released,
                    update_state: ButtonState::Released,
                    fixed_update_state: ButtonState::Released,
                    value: 0.0,
                    update_value: 0.0,
                    fixed_update_value: 0.0,
                    #[cfg(feature = "timing")]
                    timing: Default::default(),
                }),
            },
        );

        assert!(action_state.button_data_mut(&TestAction::Run).is_some());
    }

    #[test]
    fn test_button_data_mut_for_button_action_without_data() {
        let mut action_state = ActionState::<TestAction>::default();

        assert!(action_state.button_data_mut(&TestAction::Run).is_none());
    }

    #[test]
    fn test_button_data_mut_for_non_button_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::Axis,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::Axis(AxisData {
                    value: 0.5,
                    update_value: 0.5,
                    fixed_update_value: 0.5,
                }),
            },
        );
        assert!(action_state.button_data_mut(&TestAction::Axis).is_none());
    }

    #[test]
    #[should_panic(expected = "assertion `left == right` failed\n  left: Axis\n right: Button")]
    fn test_button_data_mut_or_default_for_non_button_action() {
        let mut action_state = ActionState::<TestAction>::default();
        let _ = action_state.button_data_mut_or_default(&TestAction::Axis);
    }

    #[test]
    fn test_axis_data_for_axis_action_without_data() {
        let action_state = ActionState::<TestAction>::default();
        assert!(action_state.axis_data(&TestAction::Axis).is_none());
    }

    #[test]
    #[should_panic(expected = "assertion `left == right` failed\n  left: Button\n right: Axis")]
    fn test_axis_data_for_non_axis_action() {
        let action_state = ActionState::<TestAction>::default();
        assert!(action_state.axis_data(&TestAction::Run).is_none());
    }

    #[test]
    fn test_axis_data_mut_for_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::Axis,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::Axis(AxisData {
                    value: 0.5,
                    update_value: 0.5,
                    fixed_update_value: 0.5,
                }),
            },
        );
        assert!(action_state.axis_data_mut(&TestAction::Axis).is_some());
    }

    #[test]
    fn test_axis_data_mut_for_axis_action_without_data() {
        let mut action_state = ActionState::<TestAction>::default();
        assert!(action_state.axis_data_mut(&TestAction::Axis).is_none());
    }

    #[test]
    fn test_axis_data_mut_for_non_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::Run,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::Button(ButtonData {
                    state: ButtonState::Released,
                    update_state: ButtonState::Released,
                    fixed_update_state: ButtonState::Released,
                    value: 0.0,
                    update_value: 0.0,
                    fixed_update_value: 0.0,
                    #[cfg(feature = "timing")]
                    timing: Default::default(),
                }),
            },
        );
        assert!(action_state.axis_data_mut(&TestAction::Run).is_none());
    }

    #[test]
    #[should_panic(expected = "assertion `left == right` failed\n  left: Button\n right: Axis")]
    fn test_axis_data_mut_or_default_for_non_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        let _ = action_state.axis_data_mut_or_default(&TestAction::Run);
    }

    #[test]
    fn test_dual_axis_data_for_dual_axis_action_without_data() {
        let action_state = ActionState::<TestAction>::default();
        assert!(action_state.dual_axis_data(&TestAction::DualAxis).is_none());
    }

    #[test]
    fn test_dual_axis_data_mut_for_dual_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::DualAxis,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::DualAxis(DualAxisData {
                    pair: Vec2::new(0.5, 0.5),
                    update_pair: Vec2::new(0.5, 0.5),
                    fixed_update_pair: Vec2::new(0.5, 0.5),
                }),
            },
        );
        assert!(
            action_state
                .dual_axis_data_mut(&TestAction::DualAxis)
                .is_some()
        );
    }

    #[test]
    fn test_dual_axis_data_mut_for_dual_axis_action_without_data() {
        let mut action_state = ActionState::<TestAction>::default();
        assert!(
            action_state
                .dual_axis_data_mut(&TestAction::DualAxis)
                .is_none()
        );
    }

    #[test]
    #[should_panic(expected = "assertion `left == right` failed\n  left: Button\n right: DualAxis")]
    fn test_dual_axis_data_mut_for_non_dual_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        assert!(action_state.dual_axis_data_mut(&TestAction::Run).is_none());
    }

    #[test]
    fn test_triple_axis_data_for_triple_axis_action_without_data() {
        let action_state = ActionState::<TestAction>::default();
        assert!(
            action_state
                .triple_axis_data(&TestAction::TripleAxis)
                .is_none()
        );
    }

    #[test]
    fn test_triple_axis_data_mut_for_triple_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::TripleAxis,
            ActionData {
                disabled: false,
                kind_data: ActionKindData::TripleAxis(crate::action_state::TripleAxisData {
                    triple: Vec3::new(0.5, 0.5, 0.5),
                    update_triple: Vec3::new(0.5, 0.5, 0.5),
                    fixed_update_triple: Vec3::new(0.5, 0.5, 0.5),
                }),
            },
        );
        assert!(
            action_state
                .triple_axis_data_mut(&TestAction::TripleAxis)
                .is_some()
        );
    }

    #[test]
    fn test_button_value_for_disabled_button_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::Run,
            ActionData {
                disabled: true,
                kind_data: ActionKindData::Button(ButtonData {
                    state: ButtonState::Pressed,
                    update_state: ButtonState::Pressed,
                    fixed_update_state: ButtonState::Pressed,
                    value: 1.0,
                    update_value: 1.0,
                    fixed_update_value: 1.0,
                    #[cfg(feature = "timing")]
                    timing: Default::default(),
                }),
            },
        );
        action_state.disable_action(&TestAction::Run);
        assert_eq!(action_state.button_value(&TestAction::Run), 0.0);
    }

    #[test]
    fn test_clamped_button_value_less_than_zero() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_button_value(&TestAction::Run, -0.5);
        assert_eq!(action_state.clamped_button_value(&TestAction::Run), 0.0);
    }

    #[test]
    fn test_clamped_button_value_greater_than_zero() {
        let mut action_state: ActionState<TestAction> = ActionState::<TestAction>::default();
        action_state.set_button_value(&TestAction::Run, 1.5);
        assert_eq!(action_state.clamped_button_value(&TestAction::Run), 1.0);
    }

    #[test]
    fn test_value_for_disabled_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.action_data.insert(
            TestAction::Axis,
            ActionData {
                disabled: true,
                kind_data: ActionKindData::Axis(AxisData {
                    value: 1.0,
                    update_value: 1.0,
                    fixed_update_value: 1.0,
                }),
            },
        );
        action_state.disable_action(&TestAction::Axis);
        assert_eq!(action_state.value(&TestAction::Axis), 0.0);
    }

    #[test]
    fn test_clamped_value_less_than_negative_one() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_value(&TestAction::Axis, -2.0);
        assert_eq!(action_state.clamped_value(&TestAction::Axis), -1.0);
    }

    #[test]
    fn test_clamped_value_greater_than_one() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_value(&TestAction::Axis, 2.0);
        assert_eq!(action_state.clamped_value(&TestAction::Axis), 1.0);
    }

    #[test]
    fn test_axis_pair_for_disabled_dual_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable_action(&TestAction::DualAxis);
        assert_eq!(action_state.axis_pair(&TestAction::DualAxis), Vec2::ZERO);
    }

    #[test]
    fn test_clamped_axis_pair_greater_than_vec2_one() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_axis_pair(&TestAction::DualAxis, Vec2::new(2.0, 2.0));
        assert_eq!(
            action_state.clamped_axis_pair(&TestAction::DualAxis),
            Vec2::ONE
        );
    }

    #[test]
    fn test_clamped_axis_pair_less_than_vec2_negative_one() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_axis_pair(&TestAction::DualAxis, Vec2::new(-2.0, -2.0));
        assert_eq!(
            action_state.clamped_axis_pair(&TestAction::DualAxis),
            Vec2::NEG_ONE
        );
    }

    #[test]
    fn test_axis_triple_for_disabled_triple_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable_action(&TestAction::TripleAxis);
        assert_eq!(
            action_state.axis_triple(&TestAction::TripleAxis),
            Vec3::ZERO
        );
    }

    #[test]
    fn test_clamped_axis_triple_greater_than_vec3_one() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_axis_triple(&TestAction::TripleAxis, Vec3::new(2.0, 2.0, 2.0));
        assert_eq!(
            action_state.clamped_axis_triple(&TestAction::TripleAxis),
            Vec3::ONE
        );
    }

    #[test]
    fn test_clamped_axis_triple_less_than_vec3_negative_one() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_axis_triple(&TestAction::TripleAxis, Vec3::new(-2.0, -2.0, -2.0));
        assert_eq!(
            action_state.clamped_axis_triple(&TestAction::TripleAxis),
            Vec3::NEG_ONE
        );
    }

    #[test]
    fn test_set_button_data_for_button_action() {
        let mut action_state = ActionState::<TestAction>::default();
        let button_data = ButtonData {
            state: ButtonState::Pressed,
            update_state: ButtonState::Pressed,
            fixed_update_state: ButtonState::Pressed,
            value: 1.0,
            update_value: 1.0,
            fixed_update_value: 1.0,
            #[cfg(feature = "timing")]
            timing: Default::default(),
        };
        action_state.set_button_data(TestAction::Run, button_data);

        let returned_data = action_state.button_data(&TestAction::Run).unwrap();
        assert_eq!(returned_data.state, ButtonState::Pressed);
        assert_eq!(returned_data.value, 1.0);
    }

    #[test]
    #[should_panic(expected = "assertion `left == right` failed\n  left: Axis\n right: Button")]
    fn test_set_button_data_for_non_button_action() {
        let mut action_state = ActionState::<TestAction>::default();
        let button_data = ButtonData {
            state: ButtonState::Pressed,
            update_state: ButtonState::Pressed,
            fixed_update_state: ButtonState::Pressed,
            value: 1.0,
            update_value: 1.0,
            fixed_update_value: 1.0,
            #[cfg(feature = "timing")]
            timing: Default::default(),
        };
        action_state.set_button_data(TestAction::Axis, button_data);
    }

    #[test]
    fn test_reset_button_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_button_value(&TestAction::Run, 1.0);
        action_state.reset(&TestAction::Run);

        assert!(!action_state.pressed(&TestAction::Run));
        assert_eq!(action_state.button_value(&TestAction::Run), 0.0);
    }

    #[test]
    fn test_reset_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_value(&TestAction::Axis, 1.0);
        action_state.reset(&TestAction::Axis);

        assert_eq!(action_state.value(&TestAction::Axis), 0.0);
    }

    #[test]
    fn test_reset_dual_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_axis_pair(&TestAction::DualAxis, Vec2::ONE);
        action_state.reset(&TestAction::DualAxis);

        assert_eq!(action_state.axis_pair(&TestAction::DualAxis), Vec2::ZERO);
    }

    #[test]
    fn test_reset_triple_axis_action() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_axis_triple(&TestAction::TripleAxis, Vec3::ONE);
        action_state.reset(&TestAction::TripleAxis);

        assert_eq!(
            action_state.axis_triple(&TestAction::TripleAxis),
            Vec3::ZERO
        );
    }

    #[test]
    fn test_action_disabled_when_action_state_disabled() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable();

        assert!(action_state.action_disabled(&TestAction::Run));
    }

    #[test]
    fn test_action_disabled_when_action_state_not_disabled() {
        let mut action_state = ActionState::<TestAction>::default();
        assert!(!action_state.action_disabled(&TestAction::Run));

        action_state.disable_action(&TestAction::Run);
        assert!(action_state.action_disabled(&TestAction::Run));
    }

    #[test]
    fn test_disable() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable();

        assert!(action_state.disabled);
    }

    #[test]
    fn test_enable() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable();
        action_state.enable();
        assert!(!action_state.disabled);
    }

    #[test]
    fn test_just_pressed_when_action_disabled() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable_action(&TestAction::Run);

        assert!(!action_state.just_pressed(&TestAction::Run));
    }

    #[test]
    fn test_released_when_action_disabled() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable_action(&TestAction::Run);

        assert!(action_state.released(&TestAction::Run));
    }

    #[test]
    fn test_just_released_when_action_disabled() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable_action(&TestAction::Run);

        assert!(!action_state.just_released(&TestAction::Run));
    }

    #[test]
    fn test_pressed_when_action_disabled() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.disable();

        assert!(!action_state.pressed(&TestAction::Run));
    }

    #[test]
    fn apply_diff_triple_axis() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_axis_triple(&TestAction::TripleAxis, Vec3::new(1.0, 1.0, 1.0));

        let diff = ActionDiff::TripleAxisChanged {
            action: TestAction::TripleAxis,
            axis_triple: Vec3::new(0.5, 1.0, 1.5),
        };
        action_state.apply_diff(&diff);

        assert_eq!(
            action_state.axis_triple(&TestAction::TripleAxis),
            Vec3::new(0.5, 1.0, 1.5)
        );
    }

    #[test]
    fn test_get_pressed() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_button_value(&TestAction::Run, 1.0);

        let pressed_actions: Vec<TestAction> = action_state.get_pressed();
        assert_eq!(pressed_actions.len(), 1);
        assert!(pressed_actions.contains(&TestAction::Run));
    }

    #[test]
    fn test_get_just_pressed() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_button_value(&TestAction::Run, 1.0);

        let just_pressed_actions: Vec<TestAction> = action_state.get_just_pressed();
        assert_eq!(just_pressed_actions.len(), 1);
        assert!(just_pressed_actions.contains(&TestAction::Run));
    }

    #[test]
    fn test_get_released() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_button_value(&TestAction::Run, 0.0);

        let released_actions: Vec<TestAction> = action_state.get_released();
        assert_eq!(released_actions.len(), 1);
        assert!(released_actions.contains(&TestAction::Run));
    }

    #[test]
    fn test_get_just_released() {
        let mut action_state = ActionState::<TestAction>::default();
        action_state.set_button_value(&TestAction::Run, 1.0);
        action_state.set_button_value(&TestAction::Run, 0.0);

        let just_released_actions: Vec<TestAction> = action_state.get_just_released();
        assert_eq!(just_released_actions.len(), 1);
        assert!(just_released_actions.contains(&TestAction::Run));
    }

    #[cfg(feature = "gamepad")]
    #[test]
    fn test_triggerlikes() {
        use crate::prelude::Buttonlike;

        let mut ctx = TestContext::new();
        let _gamepad = ctx.send_gamepad_connection_event(None);
        ctx.update();

        let action_state = ctx.app.world().resource::<ActionState<TestAction>>();
        assert!(action_state.released(&TestAction::Trigger));

        // App Context
        let mut ctx = TestContext::new();
        let gamepad = ctx.send_gamepad_connection_event(None);
        ctx.update();

        GamepadButton::RightTrigger.set_value_as_gamepad(ctx.app.world_mut(), 0.8, Some(gamepad));
        ctx.update();

        let action_state = ctx.app.world().resource::<ActionState<TestAction>>();
        assert!(action_state.pressed(&TestAction::Trigger));
        assert!(action_state.just_pressed(&TestAction::Trigger));
        assert_eq!(action_state.button_value(&TestAction::Trigger), 0.8);
    }
}