Gamepad.cpp 9.0 KB

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