Gamepad.cpp 9.2 KB

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