Keys.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <GLFW/glfw3.h>
  2. #include "client/input/Keys.h"
  3. bool Keys::Key::isDown() const
  4. {
  5. return down;
  6. }
  7. bool Keys::Key::isReleased() const
  8. {
  9. return shouldRelease;
  10. }
  11. u32 Keys::Key::getDownTime() const
  12. {
  13. return downTime;
  14. }
  15. std::ostream& operator <<(std::ostream& os, const Keys::Key& k)
  16. {
  17. os << "Key(down: " << k.isDown() << ", release: " << k.isReleased() <<
  18. ", time: " << k.getDownTime() << ")";
  19. return os;
  20. }
  21. Keys::Keys() : left(keys[0]), right(keys[1]), up(keys[2]), down(keys[3]),
  22. jump(keys[4]), sneak(keys[5]),
  23. camLeft(keys[6]), camRight(keys[7]), camUp(keys[8]), camDown(keys[9]),
  24. test(keys[10])
  25. {
  26. keys[0].glfwKey = GLFW_KEY_A;
  27. keys[1].glfwKey = GLFW_KEY_D;
  28. keys[2].glfwKey = GLFW_KEY_W;
  29. keys[3].glfwKey = GLFW_KEY_S;
  30. keys[4].glfwKey = GLFW_KEY_SPACE;
  31. keys[5].glfwKey = GLFW_KEY_LEFT_SHIFT;
  32. keys[6].glfwKey = GLFW_KEY_LEFT;
  33. keys[7].glfwKey = GLFW_KEY_RIGHT;
  34. keys[8].glfwKey = GLFW_KEY_UP;
  35. keys[9].glfwKey = GLFW_KEY_DOWN;
  36. keys[10].glfwKey = GLFW_KEY_T;
  37. }
  38. void Keys::release(int key)
  39. {
  40. for(Key& k : keys)
  41. {
  42. if(k.glfwKey == key)
  43. {
  44. k.shouldRelease = true;
  45. }
  46. }
  47. }
  48. void Keys::press(int key)
  49. {
  50. for(Key& k : keys)
  51. {
  52. if(k.glfwKey == key)
  53. {
  54. k.down = true;
  55. k.shouldRelease = false;
  56. }
  57. }
  58. }
  59. void Keys::tick()
  60. {
  61. for(Key& k : keys)
  62. {
  63. k.downTime += k.down;
  64. k.down = k.down && !k.shouldRelease;
  65. k.downTime *= !k.shouldRelease;
  66. k.shouldRelease = false;
  67. }
  68. }