Button.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "Base.h"
  2. #include "Button.h"
  3. namespace gameplay
  4. {
  5. Button::Button() : _callback(NULL)
  6. {
  7. }
  8. Button::~Button()
  9. {
  10. SAFE_DELETE(_callback);
  11. }
  12. Button* Button::create(Theme::Style* style, Properties* properties)
  13. {
  14. Button* button = new Button();
  15. button->init(style, properties);
  16. return button;
  17. }
  18. bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  19. {
  20. if (!isEnabled())
  21. {
  22. return false;
  23. }
  24. switch (evt)
  25. {
  26. case Touch::TOUCH_PRESS:
  27. _state = Control::ACTIVE;
  28. _dirty = true;
  29. return _consumeTouchEvents;
  30. case Touch::TOUCH_RELEASE:
  31. if (_callback &&
  32. x > 0 && x <= _bounds.width &&
  33. y > 0 && y <= _bounds.height)
  34. {
  35. // Button-clicked callback.
  36. _callback->trigger(this);
  37. setState(Control::NORMAL);
  38. _dirty = true;
  39. return _consumeTouchEvents;
  40. }
  41. _dirty = true;
  42. setState(Control::NORMAL);
  43. break;
  44. }
  45. return _consumeTouchEvents;
  46. }
  47. }