Button.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  32. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  33. {
  34. _contactIndex = (int) contactIndex;
  35. setState(Control::ACTIVE);
  36. notifyListeners(Listener::PRESS);
  37. return _consumeInputEvents;
  38. }
  39. else
  40. {
  41. setState(Control::NORMAL);
  42. }
  43. break;
  44. case Touch::TOUCH_RELEASE:
  45. _contactIndex = INVALID_CONTACT_INDEX;
  46. notifyListeners(Listener::RELEASE);
  47. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  48. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  49. {
  50. setState(Control::FOCUS);
  51. notifyListeners(Listener::CLICK);
  52. }
  53. else
  54. {
  55. setState(Control::NORMAL);
  56. }
  57. return _consumeInputEvents;
  58. case Touch::TOUCH_MOVE:
  59. return _consumeInputEvents;
  60. }
  61. return false;
  62. }
  63. const char* Button::getType() const
  64. {
  65. return "button";
  66. }
  67. }