Keys.cpp 1.8 KB

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