Button.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 (!_parent->isScrolling() &&
  53. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  54. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  55. {
  56. setState(Control::FOCUS);
  57. notifyListeners(Listener::CLICK);
  58. }
  59. else
  60. {
  61. setState(Control::NORMAL);
  62. }
  63. return _consumeInputEvents;
  64. }
  65. break;
  66. case Touch::TOUCH_MOVE:
  67. if (_contactIndex == (int) contactIndex)
  68. return _consumeInputEvents;
  69. break;
  70. }
  71. return false;
  72. }
  73. const char* Button::getType() const
  74. {
  75. return "button";
  76. }
  77. }