Keys.cpp 1.3 KB

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