Keys.cpp 1.7 KB

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