Gamepad.cpp 9.2 KB

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