Button.cpp 1.9 KB

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