KeyManager.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "client/engine/KeyManager.h"
  2. KeyManager::KeyManager()
  3. {
  4. for(int i = 0; i < NUMBER_OF_KEYS; i++)
  5. {
  6. mappingArray[i] = 0;
  7. }
  8. }
  9. bool KeyManager::isDown(int mapping)
  10. {
  11. return isInRange(mapping) && keyArray[mappingArray[mapping]].down;
  12. }
  13. bool KeyManager::isReleased(int mapping)
  14. {
  15. return isInRange(mapping) && keyArray[mappingArray[mapping]].shouldRelease;
  16. }
  17. unsigned int KeyManager::getDownTime(int mapping)
  18. {
  19. bool m = isInRange(mapping);
  20. return m * keyArray[mappingArray[mapping * m]].downTime;
  21. }
  22. void KeyManager::resetDownTime(int mapping)
  23. {
  24. keyArray[mappingArray[mapping * isInRange(mapping)]].downTime = 0;
  25. }
  26. bool KeyManager::map(int mapping, int key)
  27. {
  28. bool b = isInRange(mapping) && isInRange(key);
  29. mappingArray[mapping * b] = b * key;
  30. return b;
  31. }
  32. bool KeyManager::isInRange(int i)
  33. {
  34. return i >= 0 && i < NUMBER_OF_KEYS;
  35. }
  36. void KeyManager::tick()
  37. {
  38. for(int i = 0; i < NUMBER_OF_KEYS; i++)
  39. {
  40. keyArray[i].downTime += keyArray[i].down;
  41. keyArray[i].down = keyArray[i].down && !keyArray[i].shouldRelease;
  42. keyArray[i].downTime *= !keyArray[i].shouldRelease;
  43. keyArray[i].shouldRelease = false;
  44. }
  45. }
  46. void KeyManager::press(int key)
  47. {
  48. int index = isInRange(key) * key;
  49. keyArray[index].down = true;
  50. keyArray[index].shouldRelease = false;
  51. }
  52. void KeyManager::release(int key)
  53. {
  54. int index = isInRange(key) * key;
  55. keyArray[index].shouldRelease = true;
  56. }