1 /**
2  * Copyright:       Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3  * Authors:         $(Gabriel Gheorghe)
4  * License:         $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5  * Source:          $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/input/keyboard/impl.d)
6  * Documentation:
7  * Coverage:
8 **/
9 module liberty.input.keyboard.impl;
10 
11 import bindbc.glfw;
12 
13 import liberty.core.platform;
14 import liberty.input.keyboard.constants;
15 
16 /**
17  *
18 **/
19 final class Keyboard {
20   private {
21     bool[KEYBOARD_BUTTONS] buttonsState;
22   }
23 
24   /**
25    * Returns true if keyboard button was just pressed in an event loop.
26   **/
27   bool isButtonDown(KeyboardButton button)  {
28     return isButtonHold(button) && !buttonsState[button];
29   }
30 
31   /**
32    * Returns true if keyboard button was just released in an event loop.
33   **/
34   bool isButtonUp(KeyboardButton button)  {
35     return !isButtonHold(button) && buttonsState[button];
36   }
37 
38   /**
39    * Returns true if keyboard button is still pressed in an event loop.
40    * Use case: player movement.
41   **/
42   bool isButtonHold(KeyboardButton button)  {
43     return glfwGetKey(Platform.getWindow().getHandle(), button) == GLFW_PRESS;
44   }
45 
46   /**
47    * Returns true if keyboard button is still pressed in an event loop after a while since pressed.
48    * Strongly recommended for text input.
49   **/
50   bool isButtonRepeat(KeyboardButton button)  {
51     return glfwGetKey(Platform.getWindow().getHandle(), button) == GLFW_REPEAT;
52   }
53 
54   /**
55    * Returns true if keyboard button has no input action in an event loop.
56   **/
57   bool isButtonNone(KeyboardButton button)  {
58     return glfwGetKey(Platform.getWindow().getHandle(), button) == GLFW_RELEASE;
59   }
60 
61   /**
62    *
63   **/
64   bool isUnfolding(KeyboardButton button, KeyboardAction action)  {
65     final switch (action) with (KeyboardAction) {
66       case NONE:
67         return isButtonNone(button);
68       case DOWN:
69         return isButtonDown(button);
70       case UP:
71         return isButtonUp(button);
72       case HOLD:
73         return isButtonHold(button);
74       case REPEAT:
75         return isButtonRepeat(button);
76     }
77   }
78 
79   package(liberty.input) void update()  {
80     static foreach (i; 0..KEYBOARD_BUTTONS)
81       buttonsState[i] = isButtonHold(cast(KeyboardButton)i);
82   }
83 }