MouseButtons.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <GLFW/glfw3.h>
  2. #include "client/input/MouseButtons.h"
  3. MouseButtons::MouseButton::MouseButton() : down(false), shouldRelease(false), downTime(0) {
  4. }
  5. bool MouseButtons::MouseButton::isDown() const {
  6. return down;
  7. }
  8. bool MouseButtons::MouseButton::isReleased() const {
  9. return shouldRelease;
  10. }
  11. u32 MouseButtons::MouseButton::getDownTime() const {
  12. return downTime;
  13. }
  14. std::ostream& operator<<(std::ostream& os, const MouseButtons::MouseButton& m) {
  15. os << "Mouse(down: " << m.isDown() << ", release: " << m.isReleased() <<
  16. ", time: " << m.getDownTime() << ")";
  17. return os;
  18. }
  19. MouseButtons::MouseButtons() : primary(buttons[0]), secondary(buttons[1]), middle(buttons[2]), lastX(0), lastY(0),
  20. x(0), y(0), updateLast(true) {
  21. buttons[0].glfwButton = GLFW_MOUSE_BUTTON_1;
  22. buttons[1].glfwButton = GLFW_MOUSE_BUTTON_2;
  23. buttons[2].glfwButton = GLFW_MOUSE_BUTTON_3;
  24. }
  25. double MouseButtons::getLastX() const {
  26. return lastX;
  27. }
  28. double MouseButtons::getLastY() const {
  29. return lastY;
  30. }
  31. double MouseButtons::getX() const {
  32. return x;
  33. }
  34. double MouseButtons::getY() const {
  35. return y;
  36. }
  37. void MouseButtons::release(int button) {
  38. for(MouseButton& k : buttons) {
  39. if(k.glfwButton == button) {
  40. k.shouldRelease = true;
  41. }
  42. }
  43. }
  44. void MouseButtons::press(int button) {
  45. for(MouseButton& k : buttons) {
  46. if(k.glfwButton == button) {
  47. k.down = true;
  48. k.shouldRelease = false;
  49. }
  50. }
  51. }
  52. void MouseButtons::move(double x, double y) {
  53. MouseButtons::x = x;
  54. MouseButtons::y = y;
  55. updateLast = false;
  56. }
  57. void MouseButtons::tick() {
  58. if(updateLast) {
  59. lastX = x;
  60. lastY = y;
  61. }
  62. updateLast = true;
  63. for(MouseButton& k : buttons) {
  64. k.downTime += k.down;
  65. k.down = k.down && !k.shouldRelease;
  66. k.downTime *= !k.shouldRelease;
  67. k.shouldRelease = false;
  68. }
  69. }