You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "joystickWrapper.h"
  2. #define LOG( X ) m_pLog->Log( "JOYSTICK", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "JOYSTICK", this, _LOG_ERROR, X )
  4. sdlJoystickWrapper::sdlJoystickWrapper(int x, int y, /*int w, int h,*/ sdlCoreWidget* parent):sdlCoreWidget(x,y,500,150,false,parent){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_pJoystick = NULL;
  7. m_pHatTexture = NULL;
  8. m_pFlyHunter = NULL;
  9. m_bAnimationStage = 0;
  10. m_nNextAnimationStage = 0;
  11. m_pProtector = SDL_CreateMutex();
  12. m_pJoyNameLabel = new sdlLabel((CSTR)"NO JOYSTICK", 5, 0, 250, 50, this, 20);
  13. m_pJoyNameLabel->setColor(0, 0, 0);
  14. m_pJoyNameLabel->setBgColor(0, 0, 0, 50);
  15. memset(m_pButtonLabels, 0, sizeof(sdlLabel*) * JOYSTICK_MAX_BUTTONS);
  16. m_pButtonLabels[0] = new sdlLabel((CSTR)"Main", 0, 70, 60, 15, this, 10 );
  17. m_pButtonLabels[0]->setColor(0, 0, 0);
  18. m_pButtonLabels[0]->setBgColor(0, 0, 0, 50);
  19. m_pButtonLabels[1] = new sdlLabel((CSTR)"Auxiary", 00, 95, 60, 15, this, 10);
  20. m_pButtonLabels[1]->setColor(0, 0, 0);
  21. m_pButtonLabels[1]->setBgColor(0, 0, 0, 50);
  22. m_pButtonLabels[2] = new sdlLabel((CSTR)"3", 55, 120, 17, 15, this, 10);
  23. m_pButtonLabels[2]->setColor(0, 0, 0);
  24. m_pButtonLabels[2]->setBgColor(0, 0, 0, 50);
  25. m_pButtonLabels[3] = new sdlLabel((CSTR)"4", 80, 120, 17, 15, this, 10);
  26. m_pButtonLabels[3]->setColor(0, 0, 0);
  27. m_pButtonLabels[3]->setBgColor(0, 0, 0, 50);
  28. m_pButtonLabels[4] = new sdlLabel((CSTR)"5", 105, 120, 17, 15, this, 10);
  29. m_pButtonLabels[4]->setColor(0, 0, 0);
  30. m_pButtonLabels[4]->setBgColor(0, 0, 0, 50);
  31. m_pButtonLabels[5] = new sdlLabel((CSTR)"6", 130, 120, 17, 15, this, 10);
  32. m_pButtonLabels[5]->setColor(0, 0, 0);
  33. m_pButtonLabels[5]->setBgColor(0, 0, 0, 50);
  34. m_pButtonLabels[6] = new sdlLabel((CSTR)"7", 155, 120, 17, 15, this, 10);
  35. m_pButtonLabels[6]->setColor(0, 0, 0);
  36. m_pButtonLabels[6]->setBgColor(0, 0, 0, 50);
  37. m_pButtonLabels[7] = new sdlLabel((CSTR)"8", 180, 120, 17, 15, this, 10);
  38. m_pButtonLabels[7]->setColor(0, 0, 0);
  39. m_pButtonLabels[7]->setBgColor(0, 0, 0, 50);
  40. m_pButtonLabels[8] = new sdlLabel((CSTR)"9", 205, 120, 17, 15, this, 10);
  41. m_pButtonLabels[8]->setColor(0, 0, 0);
  42. m_pButtonLabels[8]->setBgColor(0, 0, 0, 50);
  43. m_pButtonLabels[9] = new sdlLabel((CSTR)"10", 230, 120, 17, 15, this, 10);
  44. m_pButtonLabels[9]->setColor(0, 0, 0);
  45. m_pButtonLabels[9]->setBgColor(0, 0, 0, 50);
  46. m_pButtonLabels[10] = new sdlLabel((CSTR)"11", 255, 120, 17, 15, this, 10);
  47. m_pButtonLabels[10]->setColor(0, 0, 0);
  48. m_pButtonLabels[10]->setBgColor(0, 0, 0, 50);
  49. m_pButtonLabels[11] = new sdlLabel((CSTR)"12", 280, 120, 17, 15, this, 10);
  50. m_pButtonLabels[11]->setColor(0, 0, 0);
  51. m_pButtonLabels[11]->setBgColor(0, 0, 0, 50);
  52. m_pButtonLabels[12] = new sdlLabel((CSTR)"13", 305, 120, 17, 15, this, 10);
  53. m_pButtonLabels[12]->setColor(0, 0, 0);
  54. m_pButtonLabels[12]->setBgColor(0, 0, 0, 50);
  55. m_pButtonLabels[13] = new sdlLabel((CSTR)"14", 330, 120, 17, 15, this, 10);
  56. m_pButtonLabels[13]->setColor(0, 0, 0);
  57. m_pButtonLabels[13]->setBgColor(0, 0, 0, 50);
  58. sdlGraphicElement::setType(GEL_PANEL);
  59. m_pGUI->registerGEL(this);
  60. };
  61. sdlJoystickWrapper::~sdlJoystickWrapper() {
  62. if (m_pProtector != NULL) SDL_DestroyMutex(m_pProtector);
  63. if (m_pJoyNameLabel) delete(m_pJoyNameLabel);
  64. };
  65. bool sdlJoystickWrapper::create(void) {
  66. m_pHatTexture = m_pGUI->loadTexture((CSTR)"joystick-hat.png");
  67. return sdlCoreWidget::create();
  68. };
  69. void sdlJoystickWrapper::setController(int id) {
  70. setController( SDL_JoystickOpen(id) );
  71. };
  72. void sdlJoystickWrapper::setController(SDL_Joystick* joystick) {
  73. m_pJoystick = joystick;
  74. m_bAnimationStage = 0;
  75. m_nNextAnimationStage = getMilliseconds() + ANIMATION_DELAY;
  76. if (m_pJoystick != NULL) {
  77. m_pJoyNameLabel->setText((CSTR)SDL_JoystickName(m_pJoystick));
  78. m_pJoyNameLabel->setColor(255, 255, 255);
  79. m_bNumAxes = SDL_JoystickNumAxes(m_pJoystick);
  80. m_bNumBalls = SDL_JoystickNumBalls(m_pJoystick);
  81. m_bNumButtons = SDL_JoystickNumButtons(m_pJoystick);
  82. m_bNumHats = SDL_JoystickNumHats(m_pJoystick);
  83. uint8_t i = 0;
  84. for (; i < m_bNumButtons; i++) {
  85. if (m_pButtonLabels[i] != NULL) {
  86. m_pButtonLabels[i]->setBgColor(255, 255, 255);
  87. };
  88. };
  89. for (; i < JOYSTICK_MAX_BUTTONS; i++) {
  90. if (m_pButtonLabels[i] != NULL) {
  91. m_pButtonLabels[i]->setBgColor(0, 0, 0, 50);
  92. };
  93. };
  94. } else {
  95. m_pJoyNameLabel->setText((CSTR)"NO JOYSTICK");
  96. m_pJoyNameLabel->setColor(0, 0, 0);
  97. m_bNumAxes = 0;
  98. m_bNumBalls = 0;
  99. m_bNumButtons = 0;
  100. m_bNumHats = 0;
  101. if (Roll != NULL)Roll(0);
  102. if (Pitch != NULL)Pitch(0);
  103. if (Yaw != NULL)Yaw(0);
  104. for (uint8_t i = 0; i < JOYSTICK_MAX_BUTTONS; i++) {
  105. if (m_pButtonLabels[i] != NULL) {
  106. m_pButtonLabels[i]->setBgColor(0, 0, 0, 50);
  107. };
  108. };
  109. };
  110. };
  111. void sdlJoystickWrapper::drawBefore(SDL_Renderer* pRenderer) {
  112. SDL_Rect tR;
  113. uint8_t i = 0;
  114. SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 200);
  115. //Stage 1
  116. SDL_RenderDrawLine(pRenderer, 1, 45, 1, 54);
  117. SDL_RenderDrawLine(pRenderer, 1, 54, 259, 54);
  118. SDL_RenderDrawLine(pRenderer, 259, 54, 259, 45);
  119. SDL_RenderDrawLine(pRenderer, 0, 45, 0, 55);
  120. SDL_RenderDrawLine(pRenderer, 0, 55, 260, 55);
  121. SDL_RenderDrawLine(pRenderer, 260, 55, 260, 45);
  122. tR.x = 5;
  123. tR.y = 0;
  124. tR.w = 250;
  125. tR.h = 50;
  126. SDL_RenderDrawRect(pRenderer, &tR);
  127. for (i = 0; i < JOYSTICK_MAX_AXIS; i++) {
  128. int z = i * 10;
  129. int d = i * 3;
  130. int x = i * 2;
  131. //Stage 2
  132. SDL_RenderDrawLine(pRenderer, 130 - d, 55, 130 - d, 60 + d - x);
  133. //Stage 3
  134. SDL_RenderDrawLine(pRenderer, 130 - d, 60 + d - x, 135 - d + x, 65 + d );
  135. //Stage 4
  136. SDL_RenderDrawLine(pRenderer, 135 - d + x, 65 + d, 267 + z -0, 65 + d);
  137. //Stage 5
  138. SDL_RenderDrawLine(pRenderer, 267 + z - 0, 65 + d, 272 + z, 60 + d);
  139. //Stage 6
  140. SDL_RenderDrawLine(pRenderer, 272 + z, 60+d, 272 + z, 55);
  141. tR.x = 271 + z; tR.y = 52; tR.h = 3; tR.w = 3;
  142. SDL_RenderFillRect(pRenderer, &tR);
  143. };
  144. for (i = 0; i < JOYSTICK_MAX_USED_BUTTONS; i++) {
  145. int d = i * 3;
  146. SDL_RenderDrawLine(pRenderer, 67 + d, 55, 67 + d, 60 );
  147. };
  148. tR.y = 0;
  149. tR.w = 5;
  150. tR.h = 50;
  151. i = 0;
  152. for (; i < m_bNumAxes; i++) {
  153. tR.x = 270 + i * 10;
  154. SDL_RenderFillRect(pRenderer, &tR);
  155. };
  156. for (; i < JOYSTICK_MAX_AXIS; i++) {
  157. tR.x = 270 + i * 10;
  158. SDL_RenderDrawRect(pRenderer, &tR);
  159. };
  160. if (m_pHatTexture != NULL) {
  161. SDL_Rect sR;
  162. tR.w = tR.h = sR.h = sR.w = 48;
  163. if (m_bNumHats > 0) {
  164. tR.y = 5;
  165. tR.x = 365;
  166. sR.y = 0;
  167. switch (m_hat[0]) {
  168. case SDL_HAT_CENTERED: sR.x = 48; break;
  169. case SDL_HAT_UP: sR.x = 96; break;
  170. case SDL_HAT_RIGHTUP: sR.x = 144; break;
  171. case SDL_HAT_RIGHT: sR.x = 192; break;
  172. case SDL_HAT_RIGHTDOWN: sR.x = 240; break;
  173. case SDL_HAT_DOWN: sR.x = 288; break;
  174. case SDL_HAT_LEFTDOWN: sR.x = 336; break;
  175. case SDL_HAT_LEFT: sR.x = 384; break;
  176. case SDL_HAT_LEFTUP: sR.x = 432; break;
  177. };
  178. SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR);
  179. tR.y = 60;
  180. if (m_bNumHats == 1) {
  181. sR.x = 0;
  182. sR.y = 0;
  183. SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR);
  184. } else {
  185. switch (m_hat[0]) {
  186. case SDL_HAT_CENTERED: sR.x = 48; break;
  187. case SDL_HAT_UP: sR.x = 96; break;
  188. case SDL_HAT_RIGHTUP: sR.x = 144; break;
  189. case SDL_HAT_RIGHT: sR.x = 192; break;
  190. case SDL_HAT_RIGHTDOWN: sR.x = 240; break;
  191. case SDL_HAT_DOWN: sR.x = 288; break;
  192. case SDL_HAT_LEFTDOWN: sR.x = 336; break;
  193. case SDL_HAT_LEFT: sR.x = 384; break;
  194. case SDL_HAT_LEFTUP: sR.x = 432; break;
  195. };
  196. SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR);
  197. };
  198. } else {
  199. sR.x = 0;
  200. sR.y = 0;
  201. tR.x = 365; tR.y = 0;
  202. SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR);
  203. tR.y = 60;
  204. SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR);
  205. };
  206. };
  207. };
  208. void sdlJoystickWrapper::drawAfter(SDL_Renderer* pRenderer) {
  209. if (m_pJoystick != NULL) {
  210. SDL_Rect tR;
  211. if (m_bAnimationStage < 8) {
  212. uint64_t tVal = getMilliseconds();
  213. if (tVal > m_nNextAnimationStage) {
  214. m_bAnimationStage++;
  215. m_nNextAnimationStage = tVal + ANIMATION_DELAY;
  216. };
  217. };
  218. SDL_SetRenderDrawColor(pRenderer, 255, 255, 255, 255);
  219. tR.x = 5;
  220. tR.y = 0;
  221. tR.w = 250;
  222. tR.h = 50;
  223. SDL_RenderDrawRect(pRenderer, &tR);
  224. if (m_bAnimationStage < 2) return;
  225. //Stage 1
  226. SDL_RenderDrawLine(pRenderer, 1, 45, 1, 54);
  227. SDL_RenderDrawLine(pRenderer, 1, 54, 259, 54);
  228. SDL_RenderDrawLine(pRenderer, 259, 54, 259, 45);
  229. SDL_RenderDrawLine(pRenderer, 0, 45, 0, 55);
  230. SDL_RenderDrawLine(pRenderer, 0, 55, 260, 55);
  231. SDL_RenderDrawLine(pRenderer, 260, 55, 260, 45);
  232. if (m_bAnimationStage < 3) return;
  233. for (uint8_t i = 0; i < m_bNumAxes; i++) {
  234. int z = i * 10;
  235. int d = i * 3;
  236. int x = i * 2;
  237. //Stage 2
  238. SDL_RenderDrawLine(pRenderer, 130 - d, 55, 130 - d, 60 + d - x);
  239. if (m_bAnimationStage < 4) continue;
  240. //Stage 3
  241. SDL_RenderDrawLine(pRenderer, 130 - d, 60 + d - x, 135 - d + x, 65 + d);
  242. if (m_bAnimationStage < 5) continue;
  243. //Stage 4
  244. SDL_RenderDrawLine(pRenderer, 135 - d + x, 65 + d, 267 + z - 0, 65 + d);
  245. if (m_bAnimationStage < 6) continue;
  246. //Stage 5
  247. SDL_RenderDrawLine(pRenderer, 267 + z - 0, 65 + d, 272 + z, 60 + d);
  248. if (m_bAnimationStage < 7) continue;
  249. //Stage 6
  250. SDL_RenderDrawLine(pRenderer, 272 + z, 60 + d, 272 + z, 55);
  251. tR.x = 271 + z; tR.y = 52; tR.h = 3; tR.w = 3;
  252. SDL_RenderFillRect(pRenderer, &tR);
  253. };
  254. if (m_bAnimationStage < 8) return;
  255. tR.w = 5;
  256. for (uint8_t i = 0; i < m_bNumAxes; i++) {
  257. tR.x = 270 + i * 10;
  258. tR.y = 50 * (32768 - m_aAxis[i]) / 65535;
  259. tR.h = 50 - tR.y;
  260. SDL_RenderFillRect(pRenderer, &tR);
  261. };
  262. };
  263. };
  264. void sdlJoystickWrapper::dispatch(SDL_Event* pEvt) {
  265. switch (pEvt->type) {
  266. case SDL_JOYAXISMOTION:
  267. for (uint8_t i = 0; i < m_bNumAxes; i++) {
  268. m_aAxis[i] = SDL_JoystickGetAxis(m_pJoystick, i);
  269. if (Roll != NULL && i == 0)Roll(m_aAxis[i]);
  270. if (Pitch != NULL && i == 1)Pitch(m_aAxis[i]);
  271. if (m_bNumAxes > 3) {
  272. if (Yaw != NULL && i == 2)Yaw(m_aAxis[i]);
  273. if (Throttle != NULL && i == 3)Throttle(m_aAxis[i]);
  274. } else {
  275. if (Throttle != NULL && i == 2)Throttle(m_aAxis[i]);
  276. };
  277. };
  278. break;
  279. case SDL_JOYBUTTONUP:
  280. case SDL_JOYBUTTONDOWN:
  281. for (uint8_t i = 0; i < m_bNumButtons; i++) {
  282. m_aButtons[i] = (bool)SDL_JoystickGetButton(m_pJoystick, i);
  283. if (m_pButtonLabels[i] != NULL) {
  284. if (m_aButtons[i]) {
  285. m_pButtonLabels[i]->setBgColor(255, 255, 0);
  286. } else {
  287. m_pButtonLabels[i]->setBgColor(255, 255, 255);
  288. };
  289. };
  290. };
  291. break;
  292. case SDL_JOYHATMOTION:
  293. for (uint8_t i = 0; i < m_bNumHats; i++) m_hat[i] = SDL_JoystickGetHat(m_pJoystick, i);
  294. break;
  295. case SDL_JOYBALLMOTION:
  296. for (uint8_t i = 0; i < m_bNumBalls; i++) SDL_JoystickGetBall(m_pJoystick, i, &m_aBall_x[i], &m_aBall_y[i]);
  297. break;
  298. case SDL_JOYDEVICEREMOVED:
  299. SDL_JoystickClose(m_pJoystick);
  300. setController(NULL);
  301. break;
  302. };
  303. };
  304. #undef LOG
  305. #undef LOG_ERROR