1 module gfm.sdl2.keyboard;
2 
3 import bindbc.sdl;
4 import gfm.sdl2.sdl;
5 
6 
7 /// Holds SDL keyboard state.
8 /// Bugs: This class should disappear.
9 final class SDL2Keyboard
10 {
11     public
12     {
13         this(SDL2 sdl2)
14         {
15             _sdl2 = sdl2;
16             clear();            
17         }      
18 
19         /// Returns: true if a key is pressed.
20         bool isPressed(SDL_Keycode key)
21         {
22             SDL_Scancode scan = SDL_GetScancodeFromKey(key);
23             return (_state[scan] == PRESSED);
24         }
25 
26         /// Mark a key as unpressed.
27         /// Returns: true if a key was pressed.
28         bool testAndRelease(SDL_Keycode key)
29         {
30             SDL_Scancode scan = SDL_GetScancodeFromKey(key);
31             return markKeyAsReleased(scan);
32         }
33     }
34 
35     package
36     {
37         void clear()
38         {
39             _state[] = RELEASED;
40         }
41 
42         // Mark key as pressed and return previous state.
43         bool markKeyAsPressed(SDL_Scancode scancode)
44         {
45             bool oldState = _state[scancode];
46             _state[scancode] = PRESSED;
47             return oldState;
48         }
49 
50         // Mark key as released and return previous state.
51         bool markKeyAsReleased(SDL_Scancode scancode)
52         {
53             bool oldState = _state[scancode];
54             _state[scancode] = RELEASED;
55             return oldState;
56         }
57     }
58 
59     private
60     {
61         SDL2 _sdl2;
62         bool[SDL_NUM_SCANCODES] _state;
63 
64         static const PRESSED = true,
65                      RELEASED = false;
66     }
67 }