Gamepad.cpp 5.0 KB

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