Keys.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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]) {
  19. keys[0].glfwKey = GLFW_KEY_A;
  20. keys[1].glfwKey = GLFW_KEY_D;
  21. keys[2].glfwKey = GLFW_KEY_W;
  22. keys[3].glfwKey = GLFW_KEY_S;
  23. keys[4].glfwKey = GLFW_KEY_SPACE;
  24. keys[5].glfwKey = GLFW_KEY_LEFT_SHIFT;
  25. keys[6].glfwKey = GLFW_KEY_LEFT;
  26. keys[7].glfwKey = GLFW_KEY_RIGHT;
  27. keys[8].glfwKey = GLFW_KEY_UP;
  28. keys[9].glfwKey = GLFW_KEY_DOWN;
  29. keys[10].glfwKey = GLFW_KEY_T;
  30. }
  31. void Keys::release(int key) {
  32. for(Key& k : keys) {
  33. if(k.glfwKey == key) {
  34. k.shouldRelease = true;
  35. }
  36. }
  37. }
  38. void Keys::press(int key) {
  39. for(Key& k : keys) {
  40. if(k.glfwKey == key) {
  41. k.down = true;
  42. k.shouldRelease = false;
  43. }
  44. }
  45. }
  46. void Keys::tick() {
  47. for(Key& k : keys) {
  48. k.downTime += k.down;
  49. k.down = k.down && !k.shouldRelease;
  50. k.downTime *= !k.shouldRelease;
  51. k.shouldRelease = false;
  52. }
  53. }