Gamepad.cpp 9.3 KB

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