Keys.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. {
  25. keys[0].glfwKey = GLFW_KEY_A;
  26. keys[1].glfwKey = GLFW_KEY_D;
  27. keys[2].glfwKey = GLFW_KEY_W;
  28. keys[3].glfwKey = GLFW_KEY_S;
  29. keys[4].glfwKey = GLFW_KEY_SPACE;
  30. keys[5].glfwKey = GLFW_KEY_LEFT_SHIFT;
  31. keys[6].glfwKey = GLFW_KEY_LEFT;
  32. keys[7].glfwKey = GLFW_KEY_RIGHT;
  33. keys[8].glfwKey = GLFW_KEY_UP;
  34. keys[9].glfwKey = GLFW_KEY_DOWN;
  35. }
  36. void Keys::release(int key)
  37. {
  38. for(Key& k : keys)
  39. {
  40. if(k.glfwKey == key)
  41. {
  42. k.shouldRelease = true;
  43. }
  44. }
  45. }
  46. void Keys::press(int key)
  47. {
  48. for(Key& k : keys)
  49. {
  50. if(k.glfwKey == key)
  51. {
  52. k.down = true;
  53. k.shouldRelease = false;
  54. }
  55. }
  56. }
  57. void Keys::tick()
  58. {
  59. for(Key& k : keys)
  60. {
  61. k.downTime += k.down;
  62. k.down = k.down && !k.shouldRelease;
  63. k.downTime *= !k.shouldRelease;
  64. k.shouldRelease = false;
  65. }
  66. }