MouseManager.cpp 1.5 KB

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