Button.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(Control::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(Control::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(Control::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. bool Button::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  80. {
  81. switch (evt)
  82. {
  83. case Gamepad::BUTTON_EVENT:
  84. if (_state == Control::FOCUS)
  85. {
  86. if (gamepad->isButtonDown(Gamepad::BUTTON_A) ||
  87. gamepad->isButtonDown(Gamepad::BUTTON_X))
  88. {
  89. notifyListeners(Control::Listener::PRESS);
  90. setState(Control::ACTIVE);
  91. return _consumeInputEvents;
  92. }
  93. }
  94. else if (_state == Control::ACTIVE)
  95. {
  96. if (!gamepad->isButtonDown(Gamepad::BUTTON_A) &&
  97. !gamepad->isButtonDown(Gamepad::BUTTON_X))
  98. {
  99. notifyListeners(Control::Listener::RELEASE);
  100. notifyListeners(Control::Listener::CLICK);
  101. setState(Control::FOCUS);
  102. return _consumeInputEvents;
  103. }
  104. }
  105. break;
  106. default:
  107. break;
  108. }
  109. return false;
  110. }
  111. const char* Button::getType() const
  112. {
  113. return "button";
  114. }
  115. const unsigned int Button::getDataBinding() const
  116. {
  117. return _dataBinding;
  118. }
  119. void Button::setDataBinding(unsigned int dataBinding)
  120. {
  121. _dataBinding = dataBinding;
  122. }
  123. }