void Controller::update(){ if (!joystick&&SDL_NumJoysticks()==1){ //initialize controller joystick = SDL_JoystickOpen(0); if (joystick) joystickAnalogs.resize(SDL_JoystickNumAxes(joystick)); } if (joystick&&SDL_NumJoysticks() == 0){ //possible data loss. joystick is just a pointer, make sure //what it's referencing is actually freed joystick = NULL; } if (getRightTrigger() > 0.75&&lastRightTriggerValue < 0.75){ rightTriggerHoldClock = clock(); } lastRightTriggerValue= getRightTrigger(); }
double Controller::getRightTriggerDuration(){ if (getRightTrigger() < 0.75)return 0; return (clock() - rightTriggerHoldClock) / (CLOCKS_PER_SEC / 1000); }
static void onButtonEvent(const std::string& vendorName, int controllerID, AndroidControllerCode btnCode, bool isPressed, float value, bool isAnalog) { auto iter = findController(vendorName, controllerID); if (iter == Controller::_controllers.end()) { onConnected(vendorName, controllerID); iter = findController(vendorName, controllerID); } auto gamepad = (*iter)->getGamepad(); auto thiz = (*iter)->getImpl(); switch(btnCode) { case AndroidControllerCode::BUTTON_A: { thiz->sendEventButton(gamepad->getButtonA(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_B: { thiz->sendEventButton(gamepad->getButtonB(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_X: { thiz->sendEventButton(gamepad->getButtonX(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_Y: { thiz->sendEventButton(gamepad->getButtonY(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_LEFT_SHOULDER: { thiz->sendEventButton(gamepad->getLeftShoulder(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_RIGHT_SHOULDER: { thiz->sendEventButton(gamepad->getRightShoulder(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_LEFT_TRIGGER: { thiz->sendEventButton(gamepad->getLeftTrigger(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_RIGHT_TRIGGER: { thiz->sendEventButton(gamepad->getRightTrigger(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_DPAD_UP: { thiz->sendEventButton(gamepad->getDirectionPad()->getUp(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_DPAD_DOWN: { thiz->sendEventButton(gamepad->getDirectionPad()->getDown(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_DPAD_LEFT: { thiz->sendEventButton(gamepad->getDirectionPad()->getLeft(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_DPAD_RIGHT: { thiz->sendEventButton(gamepad->getDirectionPad()->getRight(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_START: { thiz->sendEventButton(gamepad->getButtonStart(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_SELECT: { thiz->sendEventButton(gamepad->getButtonSelect(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_LEFT_THUMBSTICK: { thiz->sendEventButton(gamepad->getLeftThumbstick()->getButton(), isPressed, value, isAnalog); } break; case AndroidControllerCode::BUTTON_RIGHT_THUMBSTICK: { thiz->sendEventButton(gamepad->getRightThumbstick()->getButton(), isPressed, value, isAnalog); } break; default: // CCASSERT(false, "Invalid controller button code!"); break; } }
void GameController::handleAnalog() { if (!isActive()) { return; } //Right analog stick = look. if (!shootmode || gamePaused) { int rightx = getRawRightXMove() / gamepad_menux_sensitivity; int righty = getRawRightYMove() / gamepad_menuy_sensitivity; //The right stick's inversion and the menu's inversion should be independent of eachother. This just undoes any inversion. if (gamepad_rightx_invert) { rightx = -rightx; } if (gamepad_righty_invert) { righty = -righty; } if (gamepad_menux_invert) { rightx = -rightx; } if (gamepad_menuy_invert) { righty = -righty; } if (rightx || righty) { SDL_WarpMouseInWindow(screen, std::max(0, std::min(camera.winw, mousex + rightx)), std::max(0, std::min(camera.winh, mousey + righty))); } } else { int rightx = getRightXMove(); int righty = getRightYMove(); if (rightx || righty) { SDL_Event e; e.type = SDL_MOUSEMOTION; e.motion.x = mousex + rightx; e.motion.y = mousey + righty; e.motion.xrel = rightx; e.motion.yrel = righty; SDL_PushEvent(&e); } } if (getLeftTrigger()) { if ( !oldLeftTrigger ) { oldLeftTrigger = 1; joy_trigger_status[0] = 1; lastkeypressed = 299; } } else { oldLeftTrigger = 0; joy_trigger_status[0] = 0; } if (getRightTrigger()) { if ( !oldRightTrigger ) { oldRightTrigger = 1; joy_trigger_status[1] = 1; lastkeypressed = 300; } } else { oldRightTrigger = 0; joy_trigger_status[1] = 0; } }