1 module rpui.input;
2 
3 import derelict.sdl2.sdl;
4 import std..string;
5 
6 /// break the dependence on SFML Mouse
7 enum MouseButton {
8     mouseNone = -1,
9     mouseLeft = SDL_BUTTON_LEFT,
10     mouseRight = SDL_BUTTON_RIGHT,
11     mouseMiddle = SDL_BUTTON_MIDDLE,
12 }
13 
14 MouseButton createMouseButtonFromButtonSdlEvent(in SDL_Event event) {
15     switch (event.button.button) {
16         case SDL_BUTTON_LEFT:
17             return MouseButton.mouseLeft;
18 
19         case SDL_BUTTON_RIGHT:
20             return MouseButton.mouseRight;
21 
22         case SDL_BUTTON_MIDDLE:
23             return MouseButton.mouseMiddle;
24 
25         default:
26             return MouseButton.mouseNone;
27     }
28 }
29 
30 MouseButton createMouseButtonFromSdlState(in Uint32 state) {
31     if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) {
32         return MouseButton.mouseLeft;
33     }
34     else if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
35         return MouseButton.mouseRight;
36     }
37     else if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) {
38         return MouseButton.mouseMiddle;
39     }
40     else {
41         return MouseButton.mouseNone;
42     }
43 }
44 
45 /// break the dependence on SDL Keyboard
46 enum KeyCode {
47     A = cast(int) SDLK_a,
48     B = cast(int) SDLK_b,
49     C = cast(int) SDLK_c,
50     D = cast(int) SDLK_d,
51     E = cast(int) SDLK_e,
52     F = cast(int) SDLK_f,
53     G = cast(int) SDLK_g,
54     H = cast(int) SDLK_h,
55     I = cast(int) SDLK_i,
56     J = cast(int) SDLK_j,
57     K = cast(int) SDLK_k,
58     L = cast(int) SDLK_l,
59     M = cast(int) SDLK_m,
60     N = cast(int) SDLK_n,
61     O = cast(int) SDLK_o,
62     P = cast(int) SDLK_p,
63     Q = cast(int) SDLK_q,
64     R = cast(int) SDLK_r,
65     S = cast(int) SDLK_s,
66     T = cast(int) SDLK_t,
67     U = cast(int) SDLK_u,
68     V = cast(int) SDLK_v,
69     W = cast(int) SDLK_w,
70     X = cast(int) SDLK_x,
71     Y = cast(int) SDLK_y,
72     Z = cast(int) SDLK_z,
73     Num0 = cast(int) SDLK_0,
74     Num1 = cast(int) SDLK_1,
75     Num2 = cast(int) SDLK_2,
76     Num3 = cast(int) SDLK_3,
77     Num4 = cast(int) SDLK_4,
78     Num5 = cast(int) SDLK_5,
79     Num6 = cast(int) SDLK_6,
80     Num7 = cast(int) SDLK_7,
81     Num8 = cast(int) SDLK_8,
82     Num9 = cast(int) SDLK_9,
83     Escape = cast(int) SDLK_ESCAPE,
84     LControl = cast(int) SDLK_LCTRL,
85     LShift = cast(int) SDLK_LSHIFT,
86     LAlt = cast(int) SDLK_LALT,
87     LSystem = cast(int) SDLK_LGUI,
88     RControl = cast(int) SDLK_RCTRL,
89     RShift = cast(int) SDLK_RSHIFT,
90     RAlt = cast(int) SDLK_RALT,
91     RSystem = cast(int) SDLK_RGUI,
92     Menu = cast(int) SDLK_MENU,
93     LBracket = cast(int) SDLK_LEFTBRACKET,
94     RBracket = cast(int) SDLK_RIGHTBRACKET,
95     SemiColon = cast(int) SDLK_SEMICOLON,
96     Comma = cast(int) SDLK_COMMA,
97     Period = cast(int) SDLK_PERIOD,
98     Quote = cast(int) SDLK_QUOTE,
99     Slash = cast(int) SDLK_SLASH,
100     BackSlash = cast(int) SDLK_BACKSLASH,
101     Tilde = cast(int) SDLK_BACKQUOTE,
102     Equal = cast(int) SDLK_EQUALS,
103     Space = cast(int) SDLK_SPACE,
104     Return = cast(int) SDLK_RETURN,
105     BackSpace = cast(int) SDLK_BACKSPACE,
106     Tab = cast(int) SDLK_TAB,
107     PageUp = cast(int) SDLK_PAGEUP,
108     PageDown = cast(int) SDLK_PAGEDOWN,
109     End = cast(int) SDLK_END,
110     Home = cast(int) SDLK_HOME,
111     Insert = cast(int) SDLK_INSERT,
112     Delete = cast(int) SDLK_DELETE,
113     Add = cast(int) SDLK_PLUS,
114     Subtract = cast(int) SDLK_MINUS,
115     Left = cast(int) SDLK_LEFT,
116     Right = cast(int) SDLK_RIGHT,
117     Up = cast(int) SDLK_UP,
118     Down = cast(int) SDLK_DOWN,
119     Numpad0 = cast(int) SDLK_KP_0,
120     Numpad1 = cast(int) SDLK_KP_1,
121     Numpad2 = cast(int) SDLK_KP_2,
122     Numpad3 = cast(int) SDLK_KP_3,
123     Numpad4 = cast(int) SDLK_KP_4,
124     Numpad5 = cast(int) SDLK_KP_5,
125     Numpad6 = cast(int) SDLK_KP_6,
126     Numpad7 = cast(int) SDLK_KP_7,
127     Numpad8 = cast(int) SDLK_KP_8,
128     Numpad9 = cast(int) SDLK_KP_9,
129     Super = cast(int) SDLK_APPLICATION,
130     F1 = cast(int) SDLK_F1,
131     F2 = cast(int) SDLK_F2,
132     F3 = cast(int) SDLK_F3,
133     F4 = cast(int) SDLK_F4,
134     F5 = cast(int) SDLK_F5,
135     F6 = cast(int) SDLK_F6,
136     F7 = cast(int) SDLK_F7,
137     F8 = cast(int) SDLK_F8,
138     F9 = cast(int) SDLK_F9,
139     F10 = cast(int) SDLK_F10,
140     F11 = cast(int) SDLK_F11,
141     F12 = cast(int) SDLK_F12,
142     F13 = cast(int) SDLK_F13,
143     F14 = cast(int) SDLK_F14,
144     F15 = cast(int) SDLK_F15,
145     Pause = cast(int) SDLK_PAUSE,
146     Shift,
147     Ctrl,
148     Alt,
149     Unspecified = cast(int) SDLK_UNKNOWN,
150 }
151 
152 private static bool[KeyCode] keyPressed;
153 
154 void setKeyPressed(in KeyCode key, in bool pressed) {
155     keyPressed[key] = pressed;
156 
157     with (KeyCode) {
158         keyPressed[Shift] = isKeyPressed(LShift) || isKeyPressed(RShift);
159         keyPressed[Ctrl] = isKeyPressed(LControl) || isKeyPressed(RControl);
160         keyPressed[Alt] = isKeyPressed(LAlt) || isKeyPressed(RAlt);
161     }
162 }
163 
164 bool isClickKey(in KeyCode key) {
165     return key == KeyCode.Return;
166 }
167 
168 bool isKeyPressed(in KeyCode key) {
169     if (key !in keyPressed)
170         return false;
171 
172     return keyPressed[key];
173 }
174 
175 bool testKeyState(in KeyCode key, in bool state) {
176     if (key !in keyPressed)
177         return false;
178 
179     return keyPressed[key] == state;
180 }