Beispiel #1
0
void SDL2Manager::pluginUpdate(float deltaTime, bool jointsCaptured) {
#ifdef HAVE_SDL2
    if (_isInitialized) {
        auto userInputMapper = DependencyManager::get<UserInputMapper>();
        for (auto joystick : _openJoysticks) {
            joystick->update(deltaTime, jointsCaptured);
        }
        
        PerformanceTimer perfTimer("SDL2Manager::update");
        SDL_GameControllerUpdate();
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_CONTROLLERAXISMOTION) {
                Joystick* joystick = _openJoysticks[event.caxis.which];
                if (joystick) {
                    joystick->handleAxisEvent(event.caxis);
                }
            } else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) {
                Joystick* joystick = _openJoysticks[event.cbutton.which];
                if (joystick) {
                    joystick->handleButtonEvent(event.cbutton);
                }
                
                if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK) {
                    // this will either start or stop a global back event
                    QEvent::Type backType = (event.type == SDL_CONTROLLERBUTTONDOWN)
                    ? HFBackEvent::startType()
                    : HFBackEvent::endType();
                    HFBackEvent backEvent(backType);
                    
                    qApp->sendEvent(qApp, &backEvent);
                }
                
            } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
                SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which);
                
                SDL_JoystickID id = getInstanceId(controller);
                if (!_openJoysticks.contains(id)) {
                    Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller);
                    _openJoysticks[id] = joystick;
                    joystick->registerToUserInputMapper(*userInputMapper);
                    joystick->assignDefaultInputMapping(*userInputMapper);
                    emit joystickAdded(joystick);
                }
            } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) {
                Joystick* joystick = _openJoysticks[event.cdevice.which];
                _openJoysticks.remove(event.cdevice.which);
                userInputMapper->removeDevice(joystick->getDeviceID());
                emit joystickRemoved(joystick);
            }
        }
    }
#endif
}
void JoystickScriptingInterface::update() {
#ifdef HAVE_SDL2
    if (_isInitialized) {
        PerformanceTimer perfTimer("JoystickScriptingInterface::update");
        SDL_GameControllerUpdate();
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_CONTROLLERAXISMOTION) {
                Joystick* joystick = _openJoysticks[event.caxis.which];
                if (joystick) {
                    joystick->handleAxisEvent(event.caxis);
                }
            } else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) {
                Joystick* joystick = _openJoysticks[event.cbutton.which];
                if (joystick) {
                    joystick->handleButtonEvent(event.cbutton);
                }
                
                if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK) {
                    // this will either start or stop a global back event
                    QEvent::Type backType = (event.type == SDL_CONTROLLERBUTTONDOWN)
                        ? HFBackEvent::startType()
                        : HFBackEvent::endType();
                    HFBackEvent backEvent(backType);
                    
                    qApp->sendEvent(qApp, &backEvent);
                } else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
                    // this will either start or stop a global action event
                    QEvent::Type actionType = (event.type == SDL_CONTROLLERBUTTONDOWN)
                        ? HFActionEvent::startType()
                        : HFActionEvent::endType();
                    
                    // global action events fire in the center of the screen
                    HFActionEvent actionEvent(actionType,
                                              Application::getInstance()->getCamera()->computeViewPickRay(0.5f, 0.5f));
                    qApp->sendEvent(qApp, &actionEvent);
                }
                
            } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
                SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which);

                SDL_JoystickID id = getInstanceId(controller);
                Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller);
                _openJoysticks[id] = joystick;
                emit joystickAdded(joystick);
            } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) {
                Joystick* joystick = _openJoysticks[event.cdevice.which];
                _openJoysticks.remove(event.cdevice.which);
                emit joystickRemoved(joystick);
            }
        }
    }
#endif
}