Keys.cpp 998 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <GLFW/glfw3.h>
  2. #include "input/Keys.h"
  3. Keys::Key::Key() : glfwKey(-1), down(false), downTime(0) {
  4. }
  5. uint Keys::searchKey(int glfwKey) const {
  6. for(uint i = 1; i < keys.size(); i++) {
  7. if(keys[i].glfwKey == glfwKey) {
  8. return i;
  9. }
  10. }
  11. return 0;
  12. }
  13. uint Keys::getIndex(uint key) const {
  14. return key * (key < keys.size());
  15. }
  16. int Keys::add(int glfwKey) {
  17. int index = searchKey(-1);
  18. keys[index].glfwKey = glfwKey;
  19. return index;
  20. }
  21. bool Keys::isDown(uint key) const {
  22. return keys[getIndex(key)].down;
  23. }
  24. uint Keys::getDownTime(uint key) const {
  25. return keys[getIndex(key)].downTime;
  26. }
  27. void Keys::release(int glfwKey) {
  28. uint index = searchKey(glfwKey);
  29. keys[index].down = false;
  30. keys[index].downTime = 0;
  31. }
  32. void Keys::press(int glfwKey) {
  33. uint index = searchKey(glfwKey);
  34. keys[index].down = true;
  35. }
  36. void Keys::tick() {
  37. for(uint i = 1; i < keys.size(); i++) {
  38. keys[i].downTime += keys[i].down;
  39. }
  40. }