2
0

Gamepad.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "Base.h"
  2. #include "Gamepad.h"
  3. #include "Game.h"
  4. namespace gameplay
  5. {
  6. Gamepad::Gamepad(unsigned int handle, const char* formPath)
  7. : _id(""), _handle(handle), _buttonCount(0), _joystickCount(0), _triggerCount(0), _gamepadForm(NULL),
  8. _uiJoysticks(NULL), _uiButtons(NULL)
  9. {
  10. GP_ASSERT(formPath);
  11. _gamepadForm = Form::create(formPath);
  12. GP_ASSERT(_gamepadForm);
  13. _gamepadForm->setConsumeInputEvents(false);
  14. _id = _gamepadForm->getId();
  15. bindGamepadControls(_gamepadForm);
  16. }
  17. Gamepad::Gamepad(const char* id, unsigned int handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount)
  18. : _id(id), _handle(handle), _buttonCount(buttonCount), _joystickCount(joystickCount), _triggerCount(triggerCount),
  19. _gamepadForm(NULL), _uiJoysticks(NULL), _uiButtons(NULL)
  20. {
  21. }
  22. void Gamepad::bindGamepadControls(Container* container)
  23. {
  24. std::vector<Control*> controls = container->getControls();
  25. std::vector<Control*>::iterator itr = controls.begin();
  26. for (; itr != controls.end(); itr++)
  27. {
  28. Control* control = *itr;
  29. GP_ASSERT(control);
  30. if (control->isContainer())
  31. {
  32. bindGamepadControls((Container*) control);
  33. }
  34. else if (std::strcmp("joystick", control->getType()) == 0)
  35. {
  36. control->addRef();
  37. if (!_uiJoysticks)
  38. _uiJoysticks = new std::vector<Joystick*>;
  39. _uiJoysticks->push_back((Joystick*) control);
  40. _joystickCount++;
  41. }
  42. else if (std::strcmp("button", control->getType()) == 0)
  43. {
  44. control->addRef();
  45. if (!_uiButtons)
  46. _uiButtons = new std::vector<Button*>;
  47. _uiButtons->push_back((Button*) control);
  48. _buttonCount++;
  49. }
  50. }
  51. }
  52. Gamepad::~Gamepad()
  53. {
  54. if (_gamepadForm)
  55. {
  56. if (_uiJoysticks)
  57. {
  58. for (std::vector<Joystick*>::iterator itr = _uiJoysticks->begin(); itr != _uiJoysticks->end(); itr++)
  59. {
  60. SAFE_RELEASE((*itr));
  61. }
  62. _uiJoysticks->clear();
  63. SAFE_DELETE(_uiJoysticks);
  64. }
  65. if (_uiButtons)
  66. {
  67. for (std::vector<Button*>::iterator itr = _uiButtons->begin(); itr!= _uiButtons->end(); itr++)
  68. {
  69. SAFE_RELEASE((*itr));
  70. }
  71. _uiButtons->clear();
  72. SAFE_DELETE(_uiButtons);
  73. }
  74. SAFE_RELEASE(_gamepadForm);
  75. }
  76. }
  77. const char* Gamepad::getId() const
  78. {
  79. return _id.c_str();
  80. }
  81. void Gamepad::update(float elapsedTime)
  82. {
  83. if (_gamepadForm && _gamepadForm->isEnabled())
  84. {
  85. _gamepadForm->update(elapsedTime);
  86. }
  87. else
  88. {
  89. isConnected();
  90. }
  91. }
  92. void Gamepad::draw()
  93. {
  94. if (_gamepadForm && _gamepadForm->isEnabled())
  95. {
  96. _gamepadForm->draw();
  97. }
  98. }
  99. unsigned int Gamepad::getButtonCount() const
  100. {
  101. return _buttonCount;
  102. }
  103. Gamepad::ButtonState Gamepad::getButtonState(unsigned int buttonId) const
  104. {
  105. GP_ASSERT(buttonId < _buttonCount);
  106. if (_gamepadForm)
  107. {
  108. if (_uiButtons)
  109. return _uiButtons->at(buttonId)->getState() == Control::ACTIVE ? BUTTON_PRESSED : BUTTON_RELEASED;
  110. else
  111. return BUTTON_RELEASED;
  112. }
  113. else
  114. return Platform::getGamepadButtonState(_handle, buttonId) ? BUTTON_PRESSED : BUTTON_RELEASED;
  115. }
  116. unsigned int Gamepad::getJoystickCount() const
  117. {
  118. return _joystickCount;
  119. }
  120. bool Gamepad::isJoystickActive(unsigned int joystickId) const
  121. {
  122. GP_ASSERT(joystickId < _joystickCount);
  123. if (_gamepadForm)
  124. {
  125. if (_uiJoysticks)
  126. return !_uiJoysticks->at(joystickId)->getValue().isZero();
  127. else
  128. return false;
  129. }
  130. else
  131. {
  132. return Platform::isGamepadJoystickActive(_handle, joystickId);
  133. }
  134. }
  135. void Gamepad::getJoystickAxisValues(unsigned int joystickId, Vector2* outValue) const
  136. {
  137. GP_ASSERT(joystickId < _joystickCount);
  138. if (_gamepadForm)
  139. {
  140. if (_uiJoysticks)
  141. {
  142. const Vector2& value = _uiJoysticks->at(joystickId)->getValue();
  143. outValue->set(value.x, value.y);
  144. }
  145. else
  146. {
  147. outValue->set(0.0f, 0.0f);
  148. }
  149. }
  150. else
  151. {
  152. Platform::getGamepadJoystickAxisValues(_handle, joystickId, outValue);
  153. }
  154. }
  155. float Gamepad::getJoystickAxisX(unsigned int joystickId) const
  156. {
  157. return Platform::getGamepadJoystickAxisX(_handle, joystickId);
  158. }
  159. float Gamepad::getJoystickAxisY(unsigned int joystickId) const
  160. {
  161. return Platform::getGamepadJoystickAxisY(_handle, joystickId);
  162. }
  163. bool Gamepad::isVirtual() const
  164. {
  165. return _gamepadForm;
  166. }
  167. Form* Gamepad::getForm() const
  168. {
  169. return _gamepadForm;
  170. }
  171. bool Gamepad::isConnected() const
  172. {
  173. if (_gamepadForm)
  174. {
  175. return true;
  176. }
  177. else
  178. {
  179. return Platform::isGamepadConnected(_handle);
  180. }
  181. }
  182. }