Button.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. return Control::touchEvent(evt, x, y, contactIndex);
  74. }
  75. return false;
  76. }
  77. bool Button::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  78. {
  79. switch (evt)
  80. {
  81. case Gamepad::BUTTON_EVENT:
  82. if (_state == Control::FOCUS)
  83. {
  84. if (gamepad->isButtonDown(Gamepad::BUTTON_A) ||
  85. gamepad->isButtonDown(Gamepad::BUTTON_X))
  86. {
  87. notifyListeners(Control::Listener::PRESS);
  88. setState(Control::ACTIVE);
  89. return _consumeInputEvents;
  90. }
  91. }
  92. else if (_state == Control::ACTIVE)
  93. {
  94. if (!gamepad->isButtonDown(Gamepad::BUTTON_A) &&
  95. !gamepad->isButtonDown(Gamepad::BUTTON_X))
  96. {
  97. notifyListeners(Control::Listener::RELEASE);
  98. notifyListeners(Control::Listener::CLICK);
  99. setState(Control::FOCUS);
  100. return _consumeInputEvents;
  101. }
  102. }
  103. break;
  104. default:
  105. break;
  106. }
  107. return false;
  108. }
  109. bool Button::keyEvent(Keyboard::KeyEvent evt, int key)
  110. {
  111. if (evt == Keyboard::KEY_PRESS && key == Keyboard::KEY_RETURN)
  112. {
  113. notifyListeners(Control::Listener::PRESS);
  114. setState(Control::ACTIVE);
  115. return _consumeInputEvents;
  116. }
  117. else if (_state == ACTIVE && evt == Keyboard::KEY_RELEASE && key == Keyboard::KEY_RETURN)
  118. {
  119. notifyListeners(Control::Listener::RELEASE);
  120. notifyListeners(Control::Listener::CLICK);
  121. setState(Control::FOCUS);
  122. return _consumeInputEvents;
  123. }
  124. return false;
  125. }
  126. const char* Button::getType() const
  127. {
  128. return "button";
  129. }
  130. const unsigned int Button::getDataBinding() const
  131. {
  132. return _dataBinding;
  133. }
  134. void Button::setDataBinding(unsigned int dataBinding)
  135. {
  136. _dataBinding = dataBinding;
  137. }
  138. }