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         debug {
25             if (Core.debugger.console.open) {
26                 // suppress input if console open
27                 return;
28             }
29         }
30         foreach (input; virtual_inputs) {
31             input.update();
32         }
33         // update input pipeline
34         immutable auto current_mouse_pos = mouse_position();
35         mouse_delta = current_mouse_pos - last_mouse_position;
36         last_mouse_position = current_mouse_pos;
37     }
38 
39     // - keyboard
40 
41     /// if a key was pressed this frame
42     public static bool is_key_pressed(Keys key) {
43         return raylib.IsKeyPressed(key);
44     }
45 
46     /// if a key is currently down
47     public static bool is_key_down(Keys key) {
48         return raylib.IsKeyDown(key);
49     }
50 
51     /// if a key was released this frame
52     public static bool is_key_released(Keys key) {
53         return raylib.IsKeyReleased(key);
54     }
55 
56     /// if a key is currently up
57     public static bool is_key_up(Keys key) {
58         return raylib.IsKeyUp(key);
59     }
60 
61     // - gamepad
62     // TODO
63 
64     // - mouse
65 
66     /// if the mouse was pressed this frame
67     public static bool is_mouse_pressed(MouseButton button) {
68         return raylib.IsMouseButtonPressed(button);
69     }
70 
71     /// if the mouse is currently down
72     public static bool is_mouse_down(MouseButton button) {
73         return raylib.IsMouseButtonDown(button);
74     }
75 
76     /// if the mouse was released this frame
77     public static bool is_mouse_released(MouseButton button) {
78         return raylib.IsMouseButtonReleased(button);
79     }
80 
81     /// if the mouse is currently up
82     public static bool is_mouse_up(MouseButton button) {
83         return raylib.IsMouseButtonUp(button);
84     }
85 
86     /// the position of the mouse as a vector
87     @property public static Vector2 mouse_position() {
88         return raylib.GetMousePosition();
89     }
90 
91     /// the mouse scroll wheel delta
92     @property public static int scroll_delta() {
93         return raylib.GetMouseWheelMove();
94     }
95 
96     private static bool _cursor_locked = false;
97     @property public static bool is_cursor_locked() {
98         return _cursor_locked;
99     }
100 
101     /// lock the cursor
102     public static void lock_cursor() {
103         raylib.DisableCursor();
104         _cursor_locked = true;
105     }
106 
107     /// unlock the cursor
108     public static void unlock_cursor() {
109         raylib.EnableCursor();
110         _cursor_locked = false;
111     }
112 }