Gamepad.cpp 10 KB

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