Button.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "Base.h"
  2. #include "Button.h"
  3. namespace gameplay
  4. {
  5. static std::vector<Button*> __buttons;
  6. Button::Button() : _callback(NULL)
  7. {
  8. }
  9. Button::~Button()
  10. {
  11. SAFE_DELETE(_callback);
  12. }
  13. Button* Button::create(Theme::Style* style, Properties* properties)
  14. {
  15. Button* button = new Button();
  16. button->init(style, properties);
  17. __buttons.push_back(button);
  18. return button;
  19. }
  20. Button* Button::create(const char* id, unsigned int x, unsigned int y, unsigned int width, unsigned int height)
  21. {
  22. Button* button = new Button();
  23. button->_id = id;
  24. button->_position.set(x, y);
  25. button->_size.set(width, height);
  26. __buttons.push_back(button);
  27. return button;
  28. }
  29. Button* Button::getButton(const char* id)
  30. {
  31. std::vector<Button*>::const_iterator it;
  32. for (it = __buttons.begin(); it < __buttons.end(); it++)
  33. {
  34. Button* b = *it;
  35. if (strcmp(id, b->getID()) == 0)
  36. {
  37. return b;
  38. }
  39. }
  40. return NULL;
  41. }
  42. bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  43. {
  44. if (!isEnabled())
  45. {
  46. return false;
  47. }
  48. switch (evt)
  49. {
  50. case Touch::TOUCH_PRESS:
  51. _state = Control::STATE_ACTIVE;
  52. _dirty = true;
  53. return _consumeTouchEvents;
  54. case Touch::TOUCH_RELEASE:
  55. if (_callback &&
  56. x > 0 && x <= _size.x &&
  57. y > 0 && y <= _size.y)
  58. {
  59. // Button-clicked callback.
  60. _callback->trigger(this);
  61. setState(Control::STATE_NORMAL);
  62. _dirty = true;
  63. return _consumeTouchEvents;
  64. }
  65. _dirty = true;
  66. setState(Control::STATE_NORMAL);
  67. break;
  68. }
  69. return _consumeTouchEvents;
  70. }
  71. }