Button.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "Base.h"
  2. #include "Button.h"
  3. namespace gameplay
  4. {
  5. Button::Button()
  6. {
  7. }
  8. Button::~Button()
  9. {
  10. }
  11. Button* Button::create(const char* id, Theme::Style* style)
  12. {
  13. Button* button = new Button();
  14. button->_id = id;
  15. button->_style = style;
  16. return button;
  17. }
  18. Button* Button::create(Theme::Style* style, Properties* properties)
  19. {
  20. Button* button = new Button();
  21. button->initialize(style, properties);
  22. return button;
  23. }
  24. bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  25. {
  26. if (!isEnabled())
  27. return false;
  28. switch (evt)
  29. {
  30. case Touch::TOUCH_PRESS:
  31. if (_contactIndex == INVALID_CONTACT_INDEX)
  32. {
  33. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  34. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  35. {
  36. _contactIndex = (int) contactIndex;
  37. setState(Control::ACTIVE);
  38. notifyListeners(Listener::PRESS);
  39. return _consumeInputEvents;
  40. }
  41. else
  42. {
  43. setState(Control::NORMAL);
  44. }
  45. }
  46. break;
  47. case Touch::TOUCH_RELEASE:
  48. if (_contactIndex == (int) contactIndex)
  49. {
  50. _contactIndex = INVALID_CONTACT_INDEX;
  51. notifyListeners(Listener::RELEASE);
  52. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  53. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  54. {
  55. setState(Control::FOCUS);
  56. notifyListeners(Listener::CLICK);
  57. }
  58. else
  59. {
  60. setState(Control::NORMAL);
  61. }
  62. return _consumeInputEvents;
  63. }
  64. break;
  65. case Touch::TOUCH_MOVE:
  66. if (_contactIndex == (int) contactIndex)
  67. return _consumeInputEvents;
  68. break;
  69. }
  70. return false;
  71. }
  72. const char* Button::getType() const
  73. {
  74. return "button";
  75. }
  76. }