1 module re.input.input;
2 
3 import re.core;
4 import re.math;
5 import re.input;
6 static import raylib;
7 
8 alias Keys = raylib.KeyboardKey;
9 alias MouseButton = raylib.MouseButton;
10 alias GamepadButtons = raylib.GamepadButton;
11 alias Axes = raylib.GamepadAxis;
12 
13 /// input helper
14 public static class Input {
15     /// global virtual input list
16     public static VirtualInput[] virtual_inputs;
17     private static Vector2 last_mouse_position;
18     public static Vector2 mouse_delta;
19 
20     static this() {
21     }
22 
23     public static void update() {
24         // update core input pipeline
25         immutable auto current_mouse_pos = mouse_position();
26         mouse_delta = current_mouse_pos - last_mouse_position;
27         last_mouse_position = current_mouse_pos;
28         // update virtual inputs
29         debug {
30             // skip virtual input update if console open
31             if (Core.debugger.console.open) {
32                 return;
33             }
34         }
35         foreach (input; virtual_inputs) {
36             input.update();
37         }
38     }
39 
40     // - keyboard
41 
42     /// if a key was pressed this frame
43     public static bool is_key_pressed(Keys key) {
44         return raylib.IsKeyPressed(key);
45     }
46 
47     /// if a key is currently down
48     public static bool is_key_down(Keys key) {
49         return raylib.IsKeyDown(key);
50     }
51 
52     /// if a key was released this frame
53     public static bool is_key_released(Keys key) {
54         return raylib.IsKeyReleased(key);
55     }
56 
57     /// if a key is currently up
58     public static bool is_key_up(Keys key) {
59         return raylib.IsKeyUp(key);
60     }
61 
62     // - gamepad
63     // TODO
64 
65     // - mouse
66 
67     /// if the mouse was pressed this frame
68     public static bool is_mouse_pressed(MouseButton button) {
69         return raylib.IsMouseButtonPressed(button);
70     }
71 
72     /// if the mouse is currently down
73     public static bool is_mouse_down(MouseButton button) {
74         return raylib.IsMouseButtonDown(button);
75     }
76 
77     /// if the mouse was released this frame
78     public static bool is_mouse_released(MouseButton button) {
79         return raylib.IsMouseButtonReleased(button);
80     }
81 
82     /// if the mouse is currently up
83     public static bool is_mouse_up(MouseButton button) {
84         return raylib.IsMouseButtonUp(button);
85     }
86 
87     /// the position of the mouse as a vector
88     @property public static Vector2 mouse_position() {
89         return raylib.GetMousePosition();
90     }
91 
92     /// the mouse scroll wheel delta
93     @property public static float scroll_delta() {
94         return raylib.GetMouseWheelMove();
95     }
96 
97     private static bool _cursor_locked = false;
98     @property public static bool is_cursor_locked() {
99         return _cursor_locked;
100     }
101 
102     /// lock the cursor
103     public static void lock_cursor() {
104         raylib.DisableCursor();
105         _cursor_locked = true;
106     }
107 
108     /// unlock the cursor
109     public static void unlock_cursor() {
110         raylib.EnableCursor();
111         _cursor_locked = false;
112     }
113 }