|
|
#include "joystickWrapper.h"
#define LOG( X ) m_pLog->Log( "JOYSTICK", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "JOYSTICK", this, _LOG_ERROR, X )
sdlJoystickWrapper::sdlJoystickWrapper(int x, int y, /*int w, int h,*/ sdlCoreWidget* parent):sdlCoreWidget(x,y,500,150,false,parent){ m_pLog = basicLogCollection::getInstance(); m_pJoystick = NULL; m_pHatTexture = NULL; m_pFlyHunter = NULL; m_bAnimationStage = 0; m_nNextAnimationStage = 0; m_pProtector = SDL_CreateMutex(); m_pJoyNameLabel = new sdlLabel((CSTR)"NO JOYSTICK", 5, 0, 250, 50, this, 20); m_pJoyNameLabel->setColor(0, 0, 0); m_pJoyNameLabel->setBgColor(0, 0, 0, 50);
memset(m_pButtonLabels, 0, sizeof(sdlLabel*) * JOYSTICK_MAX_BUTTONS);
m_pButtonLabels[0] = new sdlLabel((CSTR)"Main", 0, 70, 60, 15, this, 10 ); m_pButtonLabels[0]->setColor(0, 0, 0); m_pButtonLabels[0]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[1] = new sdlLabel((CSTR)"Auxiary", 00, 95, 60, 15, this, 10); m_pButtonLabels[1]->setColor(0, 0, 0); m_pButtonLabels[1]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[2] = new sdlLabel((CSTR)"3", 55, 120, 17, 15, this, 10); m_pButtonLabels[2]->setColor(0, 0, 0); m_pButtonLabels[2]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[3] = new sdlLabel((CSTR)"4", 80, 120, 17, 15, this, 10); m_pButtonLabels[3]->setColor(0, 0, 0); m_pButtonLabels[3]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[4] = new sdlLabel((CSTR)"5", 105, 120, 17, 15, this, 10); m_pButtonLabels[4]->setColor(0, 0, 0); m_pButtonLabels[4]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[5] = new sdlLabel((CSTR)"6", 130, 120, 17, 15, this, 10); m_pButtonLabels[5]->setColor(0, 0, 0); m_pButtonLabels[5]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[6] = new sdlLabel((CSTR)"7", 155, 120, 17, 15, this, 10); m_pButtonLabels[6]->setColor(0, 0, 0); m_pButtonLabels[6]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[7] = new sdlLabel((CSTR)"8", 180, 120, 17, 15, this, 10); m_pButtonLabels[7]->setColor(0, 0, 0); m_pButtonLabels[7]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[8] = new sdlLabel((CSTR)"9", 205, 120, 17, 15, this, 10); m_pButtonLabels[8]->setColor(0, 0, 0); m_pButtonLabels[8]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[9] = new sdlLabel((CSTR)"10", 230, 120, 17, 15, this, 10); m_pButtonLabels[9]->setColor(0, 0, 0); m_pButtonLabels[9]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[10] = new sdlLabel((CSTR)"11", 255, 120, 17, 15, this, 10); m_pButtonLabels[10]->setColor(0, 0, 0); m_pButtonLabels[10]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[11] = new sdlLabel((CSTR)"12", 280, 120, 17, 15, this, 10); m_pButtonLabels[11]->setColor(0, 0, 0); m_pButtonLabels[11]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[12] = new sdlLabel((CSTR)"13", 305, 120, 17, 15, this, 10); m_pButtonLabels[12]->setColor(0, 0, 0); m_pButtonLabels[12]->setBgColor(0, 0, 0, 50);
m_pButtonLabels[13] = new sdlLabel((CSTR)"14", 330, 120, 17, 15, this, 10); m_pButtonLabels[13]->setColor(0, 0, 0); m_pButtonLabels[13]->setBgColor(0, 0, 0, 50);
sdlGraphicElement::setType(GEL_PANEL); m_pGUI->registerGEL(this);};
sdlJoystickWrapper::~sdlJoystickWrapper() { if (m_pProtector != NULL) SDL_DestroyMutex(m_pProtector); if (m_pJoyNameLabel) delete(m_pJoyNameLabel);};
bool sdlJoystickWrapper::create(void) { m_pHatTexture = m_pGUI->loadTexture((CSTR)"joystick-hat.png"); return sdlCoreWidget::create();};
void sdlJoystickWrapper::setController(int id) { setController( SDL_JoystickOpen(id) );};
void sdlJoystickWrapper::setController(SDL_Joystick* joystick) { m_pJoystick = joystick; m_bAnimationStage = 0; m_nNextAnimationStage = getMilliseconds() + ANIMATION_DELAY; if (m_pJoystick != NULL) { m_pJoyNameLabel->setText((CSTR)SDL_JoystickName(m_pJoystick)); m_pJoyNameLabel->setColor(255, 255, 255); m_bNumAxes = SDL_JoystickNumAxes(m_pJoystick); m_bNumBalls = SDL_JoystickNumBalls(m_pJoystick); m_bNumButtons = SDL_JoystickNumButtons(m_pJoystick); m_bNumHats = SDL_JoystickNumHats(m_pJoystick); uint8_t i = 0; for (; i < m_bNumButtons; i++) { if (m_pButtonLabels[i] != NULL) { m_pButtonLabels[i]->setBgColor(255, 255, 255); }; }; for (; i < JOYSTICK_MAX_BUTTONS; i++) { if (m_pButtonLabels[i] != NULL) { m_pButtonLabels[i]->setBgColor(0, 0, 0, 50); }; }; } else { m_pJoyNameLabel->setText((CSTR)"NO JOYSTICK"); m_pJoyNameLabel->setColor(0, 0, 0); m_bNumAxes = 0; m_bNumBalls = 0; m_bNumButtons = 0; m_bNumHats = 0; if (Roll != NULL)Roll(0); if (Pitch != NULL)Pitch(0); if (Yaw != NULL)Yaw(0); for (uint8_t i = 0; i < JOYSTICK_MAX_BUTTONS; i++) { if (m_pButtonLabels[i] != NULL) { m_pButtonLabels[i]->setBgColor(0, 0, 0, 50); }; }; };};
void sdlJoystickWrapper::drawBefore(SDL_Renderer* pRenderer) { SDL_Rect tR; uint8_t i = 0; SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 200); //Stage 1
SDL_RenderDrawLine(pRenderer, 1, 45, 1, 54); SDL_RenderDrawLine(pRenderer, 1, 54, 259, 54); SDL_RenderDrawLine(pRenderer, 259, 54, 259, 45); SDL_RenderDrawLine(pRenderer, 0, 45, 0, 55); SDL_RenderDrawLine(pRenderer, 0, 55, 260, 55); SDL_RenderDrawLine(pRenderer, 260, 55, 260, 45); tR.x = 5; tR.y = 0; tR.w = 250; tR.h = 50; SDL_RenderDrawRect(pRenderer, &tR); for (i = 0; i < JOYSTICK_MAX_AXIS; i++) { int z = i * 10; int d = i * 3; int x = i * 2; //Stage 2
SDL_RenderDrawLine(pRenderer, 130 - d, 55, 130 - d, 60 + d - x); //Stage 3
SDL_RenderDrawLine(pRenderer, 130 - d, 60 + d - x, 135 - d + x, 65 + d ); //Stage 4
SDL_RenderDrawLine(pRenderer, 135 - d + x, 65 + d, 267 + z -0, 65 + d); //Stage 5
SDL_RenderDrawLine(pRenderer, 267 + z - 0, 65 + d, 272 + z, 60 + d); //Stage 6
SDL_RenderDrawLine(pRenderer, 272 + z, 60+d, 272 + z, 55); tR.x = 271 + z; tR.y = 52; tR.h = 3; tR.w = 3; SDL_RenderFillRect(pRenderer, &tR); }; for (i = 0; i < JOYSTICK_MAX_USED_BUTTONS; i++) { int d = i * 3; SDL_RenderDrawLine(pRenderer, 67 + d, 55, 67 + d, 60 ); };
tR.y = 0; tR.w = 5; tR.h = 50; i = 0; for (; i < m_bNumAxes; i++) { tR.x = 270 + i * 10; SDL_RenderFillRect(pRenderer, &tR); }; for (; i < JOYSTICK_MAX_AXIS; i++) { tR.x = 270 + i * 10; SDL_RenderDrawRect(pRenderer, &tR); }; if (m_pHatTexture != NULL) { SDL_Rect sR; tR.w = tR.h = sR.h = sR.w = 48; if (m_bNumHats > 0) { tR.y = 5; tR.x = 365; sR.y = 0; switch (m_hat[0]) { case SDL_HAT_CENTERED: sR.x = 48; break; case SDL_HAT_UP: sR.x = 96; break; case SDL_HAT_RIGHTUP: sR.x = 144; break; case SDL_HAT_RIGHT: sR.x = 192; break; case SDL_HAT_RIGHTDOWN: sR.x = 240; break; case SDL_HAT_DOWN: sR.x = 288; break; case SDL_HAT_LEFTDOWN: sR.x = 336; break; case SDL_HAT_LEFT: sR.x = 384; break; case SDL_HAT_LEFTUP: sR.x = 432; break; }; SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR); tR.y = 60; if (m_bNumHats == 1) { sR.x = 0; sR.y = 0; SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR); } else { switch (m_hat[0]) { case SDL_HAT_CENTERED: sR.x = 48; break; case SDL_HAT_UP: sR.x = 96; break; case SDL_HAT_RIGHTUP: sR.x = 144; break; case SDL_HAT_RIGHT: sR.x = 192; break; case SDL_HAT_RIGHTDOWN: sR.x = 240; break; case SDL_HAT_DOWN: sR.x = 288; break; case SDL_HAT_LEFTDOWN: sR.x = 336; break; case SDL_HAT_LEFT: sR.x = 384; break; case SDL_HAT_LEFTUP: sR.x = 432; break; }; SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR); }; } else { sR.x = 0; sR.y = 0; tR.x = 365; tR.y = 0; SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR); tR.y = 60; SDL_RenderCopy(pRenderer, m_pHatTexture->texture, &sR, &tR); }; };};void sdlJoystickWrapper::drawAfter(SDL_Renderer* pRenderer) { if (m_pJoystick != NULL) { SDL_Rect tR; if (m_bAnimationStage < 8) { uint64_t tVal = getMilliseconds(); if (tVal > m_nNextAnimationStage) { m_bAnimationStage++; m_nNextAnimationStage = tVal + ANIMATION_DELAY; }; }; SDL_SetRenderDrawColor(pRenderer, 255, 255, 255, 255); tR.x = 5; tR.y = 0; tR.w = 250; tR.h = 50; SDL_RenderDrawRect(pRenderer, &tR); if (m_bAnimationStage < 2) return; //Stage 1
SDL_RenderDrawLine(pRenderer, 1, 45, 1, 54); SDL_RenderDrawLine(pRenderer, 1, 54, 259, 54); SDL_RenderDrawLine(pRenderer, 259, 54, 259, 45); SDL_RenderDrawLine(pRenderer, 0, 45, 0, 55); SDL_RenderDrawLine(pRenderer, 0, 55, 260, 55); SDL_RenderDrawLine(pRenderer, 260, 55, 260, 45); if (m_bAnimationStage < 3) return; for (uint8_t i = 0; i < m_bNumAxes; i++) { int z = i * 10; int d = i * 3; int x = i * 2; //Stage 2
SDL_RenderDrawLine(pRenderer, 130 - d, 55, 130 - d, 60 + d - x); if (m_bAnimationStage < 4) continue; //Stage 3
SDL_RenderDrawLine(pRenderer, 130 - d, 60 + d - x, 135 - d + x, 65 + d); if (m_bAnimationStage < 5) continue; //Stage 4
SDL_RenderDrawLine(pRenderer, 135 - d + x, 65 + d, 267 + z - 0, 65 + d); if (m_bAnimationStage < 6) continue; //Stage 5
SDL_RenderDrawLine(pRenderer, 267 + z - 0, 65 + d, 272 + z, 60 + d); if (m_bAnimationStage < 7) continue; //Stage 6
SDL_RenderDrawLine(pRenderer, 272 + z, 60 + d, 272 + z, 55); tR.x = 271 + z; tR.y = 52; tR.h = 3; tR.w = 3; SDL_RenderFillRect(pRenderer, &tR); }; if (m_bAnimationStage < 8) return; tR.w = 5; for (uint8_t i = 0; i < m_bNumAxes; i++) { tR.x = 270 + i * 10; tR.y = 50 * (32768 - m_aAxis[i]) / 65535; tR.h = 50 - tR.y; SDL_RenderFillRect(pRenderer, &tR); }; };};
void sdlJoystickWrapper::dispatch(SDL_Event* pEvt) { switch (pEvt->type) { case SDL_JOYAXISMOTION: for (uint8_t i = 0; i < m_bNumAxes; i++) { m_aAxis[i] = SDL_JoystickGetAxis(m_pJoystick, i); if (Roll != NULL && i == 0)Roll(m_aAxis[i]); if (Pitch != NULL && i == 1)Pitch(m_aAxis[i]); if (m_bNumAxes > 3) { if (Yaw != NULL && i == 2)Yaw(m_aAxis[i]); if (Throttle != NULL && i == 3)Throttle(m_aAxis[i]); } else { if (Throttle != NULL && i == 2)Throttle(m_aAxis[i]); }; }; break; case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: for (uint8_t i = 0; i < m_bNumButtons; i++) { m_aButtons[i] = (bool)SDL_JoystickGetButton(m_pJoystick, i); if (m_pButtonLabels[i] != NULL) { if (m_aButtons[i]) { m_pButtonLabels[i]->setBgColor(255, 255, 0); } else { m_pButtonLabels[i]->setBgColor(255, 255, 255); }; }; }; break; case SDL_JOYHATMOTION: for (uint8_t i = 0; i < m_bNumHats; i++) m_hat[i] = SDL_JoystickGetHat(m_pJoystick, i); break; case SDL_JOYBALLMOTION: for (uint8_t i = 0; i < m_bNumBalls; i++) SDL_JoystickGetBall(m_pJoystick, i, &m_aBall_x[i], &m_aBall_y[i]); break; case SDL_JOYDEVICEREMOVED: SDL_JoystickClose(m_pJoystick); setController(NULL); break; };};
#undef LOG
#undef LOG_ERROR
|