11use serde:: { Deserialize , Serialize } ;
22
3- use super :: { EventSource , GamepadEvent , KeyEvent , MouseEvent } ;
3+ use crate :: Direction ;
4+
5+ use super :: { EventSource , GamepadControlEvent , GamepadEvent , KeyEvent , MouseEvent } ;
46
57/// A user input event, as generated by the new frontend (LUNA).
68#[ derive( Debug , Serialize , Deserialize , PartialEq , Clone ) ]
@@ -20,6 +22,38 @@ impl InputEvent {
2022 InputEvent :: Gamepad ( GamepadEvent { source, .. } ) => source,
2123 }
2224 }
25+
26+ /// Parses the input event as an arbitrary direction.
27+ pub fn direction ( & self ) -> Option < Direction > {
28+ self . left_direction ( ) . or_else ( || self . right_direction ( ) )
29+ }
30+
31+ /// The direction if the input event represents a WASD key, D-pad or left stick.
32+ /// Commonly used e.g. for movement in games.
33+ pub fn left_direction ( & self ) -> Option < Direction > {
34+ match self {
35+ InputEvent :: Key ( key) => key. wasd_direction ( ) ,
36+ InputEvent :: Gamepad ( gamepad) => match & gamepad. control {
37+ GamepadControlEvent :: Button ( button) => button. d_pad_direction ( ) ,
38+ GamepadControlEvent :: Axis2D ( axis2d) if axis2d. index == 0 => axis2d. direction ( ) ,
39+ _ => None ,
40+ } ,
41+ _ => None ,
42+ }
43+ }
44+
45+ /// The direction if the input event represents an arrow key or right stick.
46+ /// Commonly used e.g. for camera control in games.
47+ pub fn right_direction ( & self ) -> Option < Direction > {
48+ match self {
49+ InputEvent :: Key ( key) => key. arrow_direction ( ) ,
50+ InputEvent :: Gamepad ( gamepad) => match & gamepad. control {
51+ GamepadControlEvent :: Axis2D ( axis2d) if axis2d. index == 1 => axis2d. direction ( ) ,
52+ _ => None ,
53+ } ,
54+ _ => None ,
55+ }
56+ }
2357}
2458
2559#[ cfg( test) ]
0 commit comments