1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-10 15:39:02 +00:00
OpenMW/apps/openmw/mwinput/inputmanagerimp.cpp

261 lines
7.6 KiB
C++
Raw Normal View History

#include "inputmanagerimp.hpp"
2010-07-17 17:58:15 +00:00
#include <osgViewer/ViewerEventHandlers>
2015-05-13 14:50:47 +00:00
#include <components/sdlutil/sdlinputwrapper.hpp>
2016-10-20 00:12:01 +00:00
#include <components/esm/esmwriter.hpp>
#include <components/esm/esmreader.hpp>
2015-05-13 14:50:47 +00:00
2012-08-12 18:45:02 +00:00
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/environment.hpp"
2020-06-26 22:58:33 +00:00
#include "../mwbase/world.hpp"
2014-02-23 19:11:05 +00:00
#include "../mwworld/esmstore.hpp"
#include "actionmanager.hpp"
#include "bindingsmanager.hpp"
#include "controllermanager.hpp"
#include "controlswitch.hpp"
#include "keyboardmanager.hpp"
#include "mousemanager.hpp"
2020-04-08 07:43:45 +00:00
#include "sdlmappings.hpp"
#include "sensormanager.hpp"
2010-07-17 17:58:15 +00:00
namespace MWInput
{
2015-05-03 15:24:35 +00:00
InputManager::InputManager(
2015-05-13 14:50:47 +00:00
SDL_Window* window,
osg::ref_ptr<osgViewer::Viewer> viewer,
osg::ref_ptr<osgViewer::ScreenCaptureHandler> screenCaptureHandler,
2017-11-09 17:26:27 +00:00
osgViewer::ScreenCaptureHandler::CaptureOperation *screenCaptureOperation,
const std::string& userFile, bool userFileExists, const std::string& userControllerBindingsFile,
const std::string& controllerBindingsFile, bool grab)
2020-05-26 07:24:47 +00:00
: mControlsDisabled(false)
2010-07-17 17:58:15 +00:00
{
mInputWrapper = new SDLUtil::InputWrapper(window, viewer, grab);
mInputWrapper->setWindowEventCallback(MWBase::Environment::get().getWindowManager());
mBindingsManager = new BindingsManager(userFile, userFileExists);
mControlSwitch = new ControlSwitch();
2014-12-20 20:46:11 +00:00
mActionManager = new ActionManager(mBindingsManager, screenCaptureOperation, viewer, screenCaptureHandler);
2014-12-20 20:46:11 +00:00
2020-04-17 11:59:37 +00:00
mKeyboardManager = new KeyboardManager(mBindingsManager);
mInputWrapper->setKeyboardEventCallback(mKeyboardManager);
mMouseManager = new MouseManager(mBindingsManager, mInputWrapper, window);
mInputWrapper->setMouseEventCallback(mMouseManager);
2014-12-20 20:46:11 +00:00
mControllerManager = new ControllerManager(mBindingsManager, mActionManager, mMouseManager, userControllerBindingsFile, controllerBindingsFile);
mInputWrapper->setControllerEventCallback(mControllerManager);
2015-05-14 22:41:21 +00:00
mSensorManager = new SensorManager();
mInputWrapper->setSensorEventCallback(mSensorManager);
}
void InputManager::clear()
{
// Enable all controls
mControlSwitch->clear();
}
2012-08-12 18:45:02 +00:00
InputManager::~InputManager()
{
delete mActionManager;
delete mControllerManager;
delete mKeyboardManager;
delete mMouseManager;
delete mSensorManager;
delete mControlSwitch;
delete mBindingsManager;
delete mInputWrapper;
}
void InputManager::setAttemptJump(bool jumping)
{
mActionManager->setAttemptJump(jumping);
}
void InputManager::update(float dt, bool disableControls, bool disableEvents)
{
2020-05-26 06:58:24 +00:00
mControlsDisabled = disableControls;
mInputWrapper->setMouseVisible(MWBase::Environment::get().getWindowManager()->getCursorVisible());
mInputWrapper->capture(disableEvents);
if (disableControls)
{
2020-05-26 07:24:47 +00:00
mMouseManager->updateCursorMode();
return;
}
mBindingsManager->update(dt);
2020-05-26 07:24:47 +00:00
mMouseManager->updateCursorMode();
2020-05-26 06:58:24 +00:00
bool controllerMove = mControllerManager->update(dt);
mMouseManager->update(dt);
2020-04-17 11:41:52 +00:00
mSensorManager->update(dt);
2020-04-16 12:36:32 +00:00
mActionManager->update(dt, controllerMove);
2020-06-26 22:58:33 +00:00
MWBase::Environment::get().getWorld()->applyDeferredPreviewRotationToPlayer(dt);
2012-08-12 18:45:02 +00:00
}
void InputManager::setDragDrop(bool dragDrop)
{
mBindingsManager->setDragDrop(dragDrop);
2010-07-17 17:58:15 +00:00
}
void InputManager::setGamepadGuiCursorEnabled(bool enabled)
{
mControllerManager->setGamepadGuiCursorEnabled(enabled);
}
2012-08-12 18:45:02 +00:00
void InputManager::changeInputMode(bool guiMode)
{
2020-04-17 11:41:52 +00:00
mControllerManager->setGuiCursorEnabled(guiMode);
mMouseManager->setGuiCursorEnabled(guiMode);
mSensorManager->setGuiCursorEnabled(guiMode);
mMouseManager->setMouseLookEnabled(!guiMode);
if (guiMode)
MWBase::Environment::get().getWindowManager()->showCrosshair(false);
2020-04-17 12:51:47 +00:00
bool isCursorVisible = guiMode && (!mControllerManager->joystickLastUsed() || mControllerManager->gamepadGuiCursorEnabled());
MWBase::Environment::get().getWindowManager()->setCursorVisible(isCursorVisible);
// if not in gui mode, the camera decides whether to show crosshair or not.
2012-08-12 18:45:02 +00:00
}
void InputManager::processChangedSettings(const Settings::CategorySettingVector& changed)
{
mMouseManager->processChangedSettings(changed);
mSensorManager->processChangedSettings(changed);
}
2021-11-06 00:25:20 +00:00
bool InputManager::getControlSwitch(std::string_view sw)
{
return mControlSwitch->get(sw);
}
2021-11-06 00:25:20 +00:00
void InputManager::toggleControlSwitch(std::string_view sw, bool value)
{
mControlSwitch->set(sw, value);
}
2012-08-17 21:31:57 +00:00
void InputManager::resetIdleTime()
{
2020-04-16 12:36:32 +00:00
mActionManager->resetIdleTime();
2012-08-17 21:31:57 +00:00
}
2021-06-26 21:10:24 +00:00
bool InputManager::isIdle() const
{
return mActionManager->getIdleTime() > 0.5;
}
std::string InputManager::getActionDescription(int action) const
2012-08-12 18:45:02 +00:00
{
return mBindingsManager->getActionDescription(action);
2012-08-12 18:45:02 +00:00
}
2021-06-26 21:10:24 +00:00
std::string InputManager::getActionKeyBindingName(int action) const
{
return mBindingsManager->getActionKeyBindingName(action);
}
2021-06-26 21:10:24 +00:00
std::string InputManager::getActionControllerBindingName(int action) const
2012-08-12 23:26:15 +00:00
{
return mBindingsManager->getActionControllerBindingName(action);
2014-12-09 03:57:32 +00:00
}
2021-06-26 21:10:24 +00:00
bool InputManager::actionIsActive(int action) const
{
return mBindingsManager->actionIsActive(action);
}
float InputManager::getActionValue(int action) const
{
return mBindingsManager->getActionValue(action);
}
2021-11-06 00:25:20 +00:00
bool InputManager::isControllerButtonPressed(SDL_GameControllerButton button) const
2021-06-26 21:10:24 +00:00
{
2021-11-06 00:25:20 +00:00
return mControllerManager->isButtonPressed(button);
2021-06-26 21:10:24 +00:00
}
2021-11-06 00:25:20 +00:00
float InputManager::getControllerAxisValue(SDL_GameControllerAxis axis) const
2021-06-26 21:10:24 +00:00
{
2021-11-06 00:25:20 +00:00
return mControllerManager->getAxisValue(axis);
2021-06-26 21:10:24 +00:00
}
int InputManager::getMouseMoveX() const
{
return mMouseManager->getMouseMoveX();
}
int InputManager::getMouseMoveY() const
{
return mMouseManager->getMouseMoveY();
}
2014-12-09 03:57:32 +00:00
std::vector<int> InputManager::getActionKeySorting()
2012-08-12 23:26:15 +00:00
{
return mBindingsManager->getActionKeySorting();
2012-08-12 23:26:15 +00:00
}
std::vector<int> InputManager::getActionControllerSorting()
2012-08-12 23:26:15 +00:00
{
return mBindingsManager->getActionControllerSorting();
2014-12-09 03:57:32 +00:00
}
2012-08-12 23:26:15 +00:00
void InputManager::enableDetectingBindingMode(int action, bool keyboard)
2014-12-09 03:57:32 +00:00
{
mBindingsManager->enableDetectingBindingMode(action, keyboard);
2012-08-12 23:26:15 +00:00
}
2016-10-20 00:12:01 +00:00
int InputManager::countSavedGameRecords() const
{
return mControlSwitch->countSavedGameRecords();
2016-10-20 00:12:01 +00:00
}
void InputManager::write(ESM::ESMWriter& writer, Loading::Listener& progress)
2016-10-20 00:12:01 +00:00
{
mControlSwitch->write(writer, progress);
2016-10-20 00:12:01 +00:00
}
void InputManager::readRecord(ESM::ESMReader& reader, uint32_t type)
{
if (type == ESM::REC_INPU)
{
mControlSwitch->readRecord(reader, type);
2016-10-20 00:12:01 +00:00
}
}
2014-12-09 03:57:32 +00:00
void InputManager::resetToDefaultKeyBindings()
{
mBindingsManager->loadKeyDefaults(true);
}
2014-12-09 03:57:32 +00:00
void InputManager::resetToDefaultControllerBindings()
{
mBindingsManager->loadControllerDefaults(true);
2014-12-09 03:57:32 +00:00
}
void InputManager::setJoystickLastUsed(bool enabled)
{
mControllerManager->setJoystickLastUsed(enabled);
}
bool InputManager::joystickLastUsed()
{
return mControllerManager->joystickLastUsed();
}
void InputManager::executeAction(int action)
{
mActionManager->executeAction(action);
}
2010-07-17 17:58:15 +00:00
}