Keys.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <GLFW/glfw3.h>
  2. #include "client/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. {
  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. }
  28. void Keys::release(int key)
  29. {
  30. for(Key& k : keys)
  31. {
  32. if(k.glfwKey == key)
  33. {
  34. k.shouldRelease = true;
  35. }
  36. }
  37. }
  38. void Keys::press(int key)
  39. {
  40. for(Key& k : keys)
  41. {
  42. if(k.glfwKey == key)
  43. {
  44. k.down = true;
  45. k.shouldRelease = false;
  46. }
  47. }
  48. }
  49. void Keys::tick()
  50. {
  51. for(Key& k : keys)
  52. {
  53. k.downTime += k.down;
  54. k.down = k.down && !k.shouldRelease;
  55. k.downTime *= !k.shouldRelease;
  56. k.shouldRelease = false;
  57. }
  58. }