Button.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "Base.h"
  2. #include "Button.h"
  3. #include "Gamepad.h"
  4. namespace gameplay
  5. {
  6. Button::Button() : _dataBinding(0)
  7. {
  8. }
  9. Button::~Button()
  10. {
  11. }
  12. Button* Button::create(const char* id, Theme::Style* style)
  13. {
  14. Button* button = new Button();
  15. button->_id = id;
  16. button->_style = style;
  17. return button;
  18. }
  19. Button* Button::create(Theme::Style* style, Properties* properties)
  20. {
  21. Button* button = new Button();
  22. button->initialize(style, properties);
  23. // Different types of data bindings can be named differently in a button namespace.
  24. // Gamepad button mappings have the name "mapping" and correspond to Gamepad::ButtonMapping enums.
  25. const char* mapping = properties->getString("mapping");
  26. if (mapping)
  27. {
  28. button->_dataBinding = Gamepad::getButtonMappingFromString(mapping);
  29. }
  30. return button;
  31. }
  32. bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  33. {
  34. switch (evt)
  35. {
  36. case Touch::TOUCH_PRESS:
  37. if (_contactIndex == INVALID_CONTACT_INDEX)
  38. {
  39. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  40. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  41. {
  42. _contactIndex = (int) contactIndex;
  43. setState(Control::ACTIVE);
  44. notifyListeners(Listener::PRESS);
  45. return _consumeInputEvents;
  46. }
  47. else
  48. {
  49. setState(Control::NORMAL);
  50. }
  51. }
  52. break;
  53. case Touch::TOUCH_RELEASE:
  54. if (_contactIndex == (int) contactIndex)
  55. {
  56. _contactIndex = INVALID_CONTACT_INDEX;
  57. notifyListeners(Listener::RELEASE);
  58. if (!_parent->isScrolling() &&
  59. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  60. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  61. {
  62. setState(Control::FOCUS);
  63. notifyListeners(Listener::CLICK);
  64. }
  65. else
  66. {
  67. setState(Control::NORMAL);
  68. }
  69. return _consumeInputEvents;
  70. }
  71. break;
  72. case Touch::TOUCH_MOVE:
  73. if (_contactIndex == (int) contactIndex)
  74. return _consumeInputEvents;
  75. break;
  76. }
  77. return false;
  78. }
  79. const char* Button::getType() const
  80. {
  81. return "button";
  82. }
  83. const unsigned int Button::getDataBinding() const
  84. {
  85. return _dataBinding;
  86. }
  87. void Button::setDataBinding(unsigned int dataBinding)
  88. {
  89. _dataBinding = dataBinding;
  90. }
  91. }