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

272 lines
9.0 KiB
C++
Raw Normal View History

#include "inputmanagerimp.hpp"
2010-07-17 19:58:15 +02:00
#include <osgViewer/ViewerEventHandlers>
2015-05-13 16:50:47 +02:00
#include <components/sdlutil/sdlinputwrapper.hpp>
2016-10-20 02:12:01 +02:00
#include <components/esm/esmwriter.hpp>
#include <components/esm/esmreader.hpp>
#include <components/esm/controlsstate.hpp>
2015-05-13 16:50:47 +02:00
2012-08-12 20:45:02 +02:00
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/environment.hpp"
2014-02-23 20:11:05 +01: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 11:43:45 +04:00
#include "sdlmappings.hpp"
#include "sensormanager.hpp"
2010-07-17 19:58:15 +02:00
namespace MWInput
{
2015-05-03 17:24:35 +02:00
InputManager::InputManager(
2015-05-13 16:50:47 +02:00
SDL_Window* window,
osg::ref_ptr<osgViewer::Viewer> viewer,
osg::ref_ptr<osgViewer::ScreenCaptureHandler> screenCaptureHandler,
2017-11-09 18:26:27 +01:00
osgViewer::ScreenCaptureHandler::CaptureOperation *screenCaptureOperation,
const std::string& userFile, bool userFileExists, const std::string& userControllerBindingsFile,
const std::string& controllerBindingsFile, bool grab)
: mGrabCursor(Settings::Manager::getBool("grab cursor", "Input"))
2010-07-17 19:58:15 +02: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 14:46:11 -06:00
mActionManager = new ActionManager(mBindingsManager, screenCaptureOperation, viewer, screenCaptureHandler);
2014-12-20 14:46:11 -06:00
2020-04-17 15:59:37 +04:00
mKeyboardManager = new KeyboardManager(mBindingsManager);
mInputWrapper->setKeyboardEventCallback(mKeyboardManager);
mMouseManager = new MouseManager(mBindingsManager, mInputWrapper, window);
mInputWrapper->setMouseEventCallback(mMouseManager);
2014-12-20 14:46:11 -06:00
mControllerManager = new ControllerManager(mBindingsManager, mActionManager, mMouseManager, userControllerBindingsFile, controllerBindingsFile);
mInputWrapper->setControllerEventCallback(mControllerManager);
2015-05-15 00:41:21 +02:00
mSensorManager = new SensorManager();
mInputWrapper->setSensorEventCallback(mSensorManager);
}
void InputManager::clear()
{
// Enable all controls
mControlSwitch->clear();
}
2012-08-12 20:45:02 +02: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::updateCursorMode()
2010-07-17 19:58:15 +02:00
{
2015-05-13 16:50:47 +02:00
bool grab = !MWBase::Environment::get().getWindowManager()->containsMode(MWGui::GM_MainMenu)
&& !MWBase::Environment::get().getWindowManager()->isConsoleMode();
2020-04-17 16:51:47 +04:00
bool wasRelative = mInputWrapper->getMouseRelative();
bool isRelative = !MWBase::Environment::get().getWindowManager()->isGuiMode();
// don't keep the pointer away from the window edge in gui mode
// stop using raw mouse motions and switch to system cursor movements
2020-04-17 16:51:47 +04:00
mInputWrapper->setMouseRelative(isRelative);
//we let the mouse escape in the main menu
2020-04-17 16:51:47 +04:00
mInputWrapper->setGrabPointer(grab && (mGrabCursor || isRelative));
//we switched to non-relative mode, move our cursor to where the in-game
//cursor is
2020-04-17 16:51:47 +04:00
if (!isRelative && wasRelative != isRelative)
{
mMouseManager->warpMouse();
}
}
void InputManager::update(float dt, bool disableControls, bool disableEvents)
{
mInputWrapper->setMouseVisible(MWBase::Environment::get().getWindowManager()->getCursorVisible());
mInputWrapper->capture(disableEvents);
2020-04-16 19:41:55 +04:00
mKeyboardManager->setControlsDisabled(disableControls);
if (disableControls)
{
updateCursorMode();
return;
}
mBindingsManager->update(dt);
updateCursorMode();
2020-04-16 16:36:32 +04:00
bool controllerMove = mControllerManager->update(dt, disableControls);
mMouseManager->update(dt, disableControls);
2020-04-17 15:41:52 +04:00
mSensorManager->update(dt);
2020-04-16 16:36:32 +04:00
mActionManager->update(dt, controllerMove);
2012-08-12 20:45:02 +02:00
}
void InputManager::setDragDrop(bool dragDrop)
{
mBindingsManager->setDragDrop(dragDrop);
2010-07-17 19:58:15 +02:00
}
void InputManager::setGamepadGuiCursorEnabled(bool enabled)
{
mControllerManager->setGamepadGuiCursorEnabled(enabled);
}
2012-08-12 20:45:02 +02:00
void InputManager::changeInputMode(bool guiMode)
{
2020-04-17 15:41:52 +04:00
mControllerManager->setGuiCursorEnabled(guiMode);
mMouseManager->setGuiCursorEnabled(guiMode);
mSensorManager->setGuiCursorEnabled(guiMode);
mMouseManager->setMouseLookEnabled(!guiMode);
if (guiMode)
MWBase::Environment::get().getWindowManager()->showCrosshair(false);
2020-04-17 16:51:47 +04: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 20:45:02 +02:00
}
void InputManager::processChangedSettings(const Settings::CategorySettingVector& changed)
{
for (const auto& setting : changed)
2012-08-12 20:45:02 +02:00
{
if (setting.first == "Input" && setting.second == "grab cursor")
mGrabCursor = Settings::Manager::getBool("grab cursor", "Input");
}
mMouseManager->processChangedSettings(changed);
mSensorManager->processChangedSettings(changed);
}
bool InputManager::getControlSwitch(const std::string& sw)
{
return mControlSwitch->get(sw);
}
void InputManager::toggleControlSwitch(const std::string& sw, bool value)
{
mControlSwitch->set(sw, value);
}
2012-08-18 01:31:57 +04:00
void InputManager::resetIdleTime()
{
2020-04-16 16:36:32 +04:00
mActionManager->resetIdleTime();
2012-08-18 01:31:57 +04:00
}
std::string InputManager::getActionDescription(int action)
2012-08-12 20:45:02 +02:00
{
return mBindingsManager->getActionDescription(action);
2012-08-12 20:45:02 +02:00
}
std::string InputManager::getActionKeyBindingName(int action)
{
return mBindingsManager->getActionKeyBindingName(action);
}
std::string InputManager::getActionControllerBindingName(int action)
2012-08-13 01:26:15 +02:00
{
return mBindingsManager->getActionControllerBindingName(action);
2014-12-08 21:57:32 -06:00
}
std::vector<int> InputManager::getActionKeySorting()
2012-08-13 01:26:15 +02:00
{
return mBindingsManager->getActionKeySorting();
2012-08-13 01:26:15 +02:00
}
std::vector<int> InputManager::getActionControllerSorting()
2012-08-13 01:26:15 +02:00
{
return mBindingsManager->getActionControllerSorting();
2014-12-08 21:57:32 -06:00
}
2012-08-13 01:26:15 +02:00
void InputManager::enableDetectingBindingMode(int action, bool keyboard)
2014-12-08 21:57:32 -06:00
{
mBindingsManager->enableDetectingBindingMode(action, keyboard);
2012-08-13 01:26:15 +02:00
}
2016-10-20 02:12:01 +02:00
int InputManager::countSavedGameRecords() const
{
return 1;
}
void InputManager::write(ESM::ESMWriter& writer, Loading::Listener& /*progress*/)
{
ESM::ControlsState controls;
controls.mViewSwitchDisabled = !getControlSwitch("playerviewswitch");
controls.mControlsDisabled = !getControlSwitch("playercontrols");
controls.mJumpingDisabled = !getControlSwitch("playerjumping");
controls.mLookingDisabled = !getControlSwitch("playerlooking");
controls.mVanityModeDisabled = !getControlSwitch("vanitymode");
controls.mWeaponDrawingDisabled = !getControlSwitch("playerfighting");
controls.mSpellDrawingDisabled = !getControlSwitch("playermagic");
writer.startRecord (ESM::REC_INPU);
controls.save(writer);
writer.endRecord (ESM::REC_INPU);
}
void InputManager::readRecord(ESM::ESMReader& reader, uint32_t type)
{
if (type == ESM::REC_INPU)
{
ESM::ControlsState controls;
controls.load(reader);
toggleControlSwitch("playerviewswitch", !controls.mViewSwitchDisabled);
toggleControlSwitch("playercontrols", !controls.mControlsDisabled);
toggleControlSwitch("playerjumping", !controls.mJumpingDisabled);
toggleControlSwitch("playerlooking", !controls.mLookingDisabled);
toggleControlSwitch("vanitymode", !controls.mVanityModeDisabled);
toggleControlSwitch("playerfighting", !controls.mWeaponDrawingDisabled);
toggleControlSwitch("playermagic", !controls.mSpellDrawingDisabled);
}
}
2014-12-08 21:57:32 -06:00
void InputManager::resetToDefaultKeyBindings()
{
mBindingsManager->loadKeyDefaults(true);
}
2014-12-08 21:57:32 -06:00
void InputManager::resetToDefaultControllerBindings()
{
mBindingsManager->loadControllerDefaults(true);
2014-12-08 21:57:32 -06: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 19:58:15 +02:00
}