Gamepad.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "Base.h"
  2. #include "Gamepad.h"
  3. #include "Game.h"
  4. #include "Button.h"
  5. namespace gameplay
  6. {
  7. static std::vector<Gamepad*> __gamepads;
  8. Gamepad::Gamepad(const char* formPath)
  9. : _handle(0), _vendorId(0), _productId(0), _buttonCount(0), _joystickCount(0), _triggerCount(0), _form(NULL), _buttons(0)
  10. {
  11. GP_ASSERT(formPath);
  12. _form = Form::create(formPath);
  13. GP_ASSERT(_form);
  14. _form->setConsumeInputEvents(false);
  15. _vendorString = "None";
  16. _productString = "Virtual";
  17. for (int i = 0; i < 2; ++i)
  18. {
  19. _uiJoysticks[i] = NULL;
  20. }
  21. for (int i = 0; i < 20; ++i)
  22. {
  23. _uiButtons[i] = NULL;
  24. }
  25. bindGamepadControls(_form);
  26. }
  27. Gamepad::Gamepad(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
  28. unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString)
  29. : _handle(handle), _vendorId(vendorId), _productId(productId), _vendorString(vendorString), _productString(productString),
  30. _buttonCount(buttonCount), _joystickCount(joystickCount), _triggerCount(triggerCount), _form(NULL), _buttons(0)
  31. {
  32. }
  33. Gamepad::~Gamepad()
  34. {
  35. if (_form)
  36. {
  37. SAFE_RELEASE(_form);
  38. }
  39. }
  40. Gamepad* Gamepad::add(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
  41. unsigned int vendorId, unsigned int productId,
  42. const char* vendorString, const char* productString)
  43. {
  44. Gamepad* gamepad = new Gamepad(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
  45. __gamepads.push_back(gamepad);
  46. Game::getInstance()->gamepadEvent(CONNECTED_EVENT, gamepad);
  47. return gamepad;
  48. }
  49. Gamepad* Gamepad::add(const char* formPath)
  50. {
  51. Gamepad* gamepad = new Gamepad(formPath);
  52. __gamepads.push_back(gamepad);
  53. Game::getInstance()->gamepadEvent(CONNECTED_EVENT, gamepad);
  54. return gamepad;
  55. }
  56. void Gamepad::remove(GamepadHandle handle)
  57. {
  58. std::vector<Gamepad*>::iterator it = __gamepads.begin();
  59. do
  60. {
  61. Gamepad* gamepad = *it;
  62. if (gamepad->_handle == handle)
  63. {
  64. Game::getInstance()->gamepadEvent(DISCONNECTED_EVENT, gamepad);
  65. SAFE_DELETE(gamepad);
  66. it = __gamepads.erase(it);
  67. }
  68. else
  69. {
  70. it++;
  71. }
  72. } while (it != __gamepads.end());
  73. }
  74. void Gamepad::remove(Gamepad* gamepad)
  75. {
  76. std::vector<Gamepad*>::iterator it = __gamepads.begin();
  77. do
  78. {
  79. Gamepad* g = *it;
  80. if (g == gamepad)
  81. {
  82. Game::getInstance()->gamepadEvent(DISCONNECTED_EVENT, g);
  83. SAFE_DELETE(gamepad);
  84. it = __gamepads.erase(it);
  85. }
  86. else
  87. {
  88. it++;
  89. }
  90. } while (it != __gamepads.end());
  91. }
  92. void Gamepad::bindGamepadControls(Container* container)
  93. {
  94. std::vector<Control*> controls = container->getControls();
  95. std::vector<Control*>::iterator itr = controls.begin();
  96. for (; itr != controls.end(); itr++)
  97. {
  98. Control* control = *itr;
  99. GP_ASSERT(control);
  100. if (control->isContainer())
  101. {
  102. bindGamepadControls((Container*) control);
  103. }
  104. else if (std::strcmp("joystick", control->getType()) == 0)
  105. {
  106. Joystick* joystick = (Joystick*)control;
  107. _uiJoysticks[joystick->getIndex()] = joystick;
  108. _joystickCount++;
  109. }
  110. else if (std::strcmp("button", control->getType()) == 0)
  111. {
  112. Button* button = (Button*)control;
  113. _uiButtons[button->getDataBinding()] = button;
  114. _buttonCount++;
  115. }
  116. }
  117. }
  118. unsigned int Gamepad::getGamepadCount()
  119. {
  120. return __gamepads.size();
  121. }
  122. Gamepad* Gamepad::getGamepad(unsigned int index)
  123. {
  124. return __gamepads[index];
  125. }
  126. Gamepad::ButtonMapping Gamepad::getButtonMappingFromString(const char* string)
  127. {
  128. if (strcmp(string, "A") == 0 || strcmp(string, "BUTTON_A") == 0)
  129. return BUTTON_A;
  130. else if (strcmp(string, "B") == 0 || strcmp(string, "BUTTON_B") == 0)
  131. return BUTTON_B;
  132. else if (strcmp(string, "C") == 0 || strcmp(string, "BUTTON_C") == 0)
  133. return BUTTON_C;
  134. else if (strcmp(string, "X") == 0 || strcmp(string, "BUTTON_X") == 0)
  135. return BUTTON_X;
  136. else if (strcmp(string, "Y") == 0 || strcmp(string, "BUTTON_Y") == 0)
  137. return BUTTON_Y;
  138. else if (strcmp(string, "Z") == 0 || strcmp(string, "BUTTON_Z") == 0)
  139. return BUTTON_Z;
  140. else if (strcmp(string, "MENU1") == 0 || strcmp(string, "BUTTON_MENU1") == 0)
  141. return BUTTON_MENU1;
  142. else if (strcmp(string, "MENU2") == 0 || strcmp(string, "BUTTON_MENU2") == 0)
  143. return BUTTON_MENU2;
  144. else if (strcmp(string, "MENU3") == 0 || strcmp(string, "BUTTON_MENU3") == 0)
  145. return BUTTON_MENU3;
  146. else if (strcmp(string, "MENU4") == 0 || strcmp(string, "BUTTON_MENU4") == 0)
  147. return BUTTON_MENU4;
  148. else if (strcmp(string, "L1") == 0 || strcmp(string, "BUTTON_L1") == 0)
  149. return BUTTON_L1;
  150. else if (strcmp(string, "L2") == 0 || strcmp(string, "BUTTON_L2") == 0)
  151. return BUTTON_L2;
  152. else if (strcmp(string, "L3") == 0 || strcmp(string, "BUTTON_L3") == 0)
  153. return BUTTON_L3;
  154. else if (strcmp(string, "R1") == 0 || strcmp(string, "BUTTON_R1") == 0)
  155. return BUTTON_R1;
  156. else if (strcmp(string, "R2") == 0 || strcmp(string, "BUTTON_R2") == 0)
  157. return BUTTON_R2;
  158. else if (strcmp(string, "R3") == 0 || strcmp(string, "BUTTON_R3") == 0)
  159. return BUTTON_R3;
  160. else if (strcmp(string, "UP") == 0 || strcmp(string, "BUTTON_UP") == 0)
  161. return BUTTON_UP;
  162. else if (strcmp(string, "DOWN") == 0 || strcmp(string, "BUTTON_DOWN") == 0)
  163. return BUTTON_DOWN;
  164. else if (strcmp(string, "LEFT") == 0 || strcmp(string, "BUTTON_LEFT") == 0)
  165. return BUTTON_LEFT;
  166. else if (strcmp(string, "RIGHT") == 0 || strcmp(string, "BUTTON_RIGHT") == 0)
  167. return BUTTON_RIGHT;
  168. GP_WARN("Unknown GamepadButton string.");
  169. return BUTTON_A;
  170. }
  171. const unsigned int Gamepad::getVendorId() const
  172. {
  173. return _vendorId;
  174. }
  175. const unsigned int Gamepad::getProductId() const
  176. {
  177. return _productId;
  178. }
  179. const char* Gamepad::getVendorString() const
  180. {
  181. return _vendorString.c_str();
  182. }
  183. const char* Gamepad::getProductString() const
  184. {
  185. return _productString.c_str();
  186. }
  187. void Gamepad::update(float elapsedTime)
  188. {
  189. if (_form)
  190. {
  191. if (_form->isEnabled())
  192. {
  193. _form->update(elapsedTime);
  194. }
  195. }
  196. else
  197. {
  198. Platform::pollGamepadState(this);
  199. }
  200. }
  201. void Gamepad::draw()
  202. {
  203. if (_form && _form->isEnabled())
  204. {
  205. _form->draw();
  206. }
  207. }
  208. unsigned int Gamepad::getButtonCount() const
  209. {
  210. return _buttonCount;
  211. }
  212. bool Gamepad::isButtonDown(ButtonMapping mapping) const
  213. {
  214. if (_form)
  215. {
  216. Button* button = _uiButtons[mapping];
  217. if (button)
  218. {
  219. return (button->getState() == Control::ACTIVE);
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. else if (_buttons & (1 << mapping))
  227. {
  228. return true;
  229. }
  230. return false;
  231. }
  232. unsigned int Gamepad::getJoystickCount() const
  233. {
  234. return _joystickCount;
  235. }
  236. void Gamepad::getJoystickValues(unsigned int joystickId, Vector2* outValue) const
  237. {
  238. GP_ASSERT(joystickId < _joystickCount);
  239. if (_form)
  240. {
  241. Joystick* joystick = _uiJoysticks[joystickId];
  242. if (joystick)
  243. {
  244. const Vector2& value = joystick->getValue();
  245. outValue->set(value.x, value.y);
  246. }
  247. else
  248. {
  249. outValue->set(0.0f, 0.0f);
  250. }
  251. }
  252. else
  253. {
  254. outValue->set(_joysticks[joystickId]);
  255. }
  256. }
  257. unsigned int Gamepad::getTriggerCount() const
  258. {
  259. return _triggerCount;
  260. }
  261. float Gamepad::getTriggerValue(unsigned int triggerId) const
  262. {
  263. GP_ASSERT(triggerId < _triggerCount);
  264. if (_form)
  265. {
  266. // Triggers are currently not available for virtual gamepads.
  267. return 0.0f;
  268. }
  269. else
  270. {
  271. return _triggers[triggerId];
  272. }
  273. }
  274. bool Gamepad::isVirtual() const
  275. {
  276. return _form;
  277. }
  278. Form* Gamepad::getForm() const
  279. {
  280. return _form;
  281. }
  282. }