From 8512133bb1df6388b730c6f9ee518a67d9cafc8f Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Thu, 16 Apr 2020 18:08:55 +0400 Subject: [PATCH] Move control switches to the separate file --- apps/openmw/CMakeLists.txt | 2 +- apps/openmw/mwinput/controlswitch.cpp | 58 +++++++++++++++++++++++++ apps/openmw/mwinput/controlswitch.hpp | 21 +++++++++ apps/openmw/mwinput/inputmanagerimp.cpp | 43 ++++-------------- apps/openmw/mwinput/inputmanagerimp.hpp | 3 +- 5 files changed, 90 insertions(+), 37 deletions(-) create mode 100644 apps/openmw/mwinput/controlswitch.cpp create mode 100644 apps/openmw/mwinput/controlswitch.hpp diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 97cc9b035f..d907091bda 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -25,7 +25,7 @@ add_openmw_dir (mwrender ) add_openmw_dir (mwinput - actions actionmanager controllermanager inputmanagerimp mousemanager keyboardmanager sdlmappings sensormanager + actions actionmanager controllermanager controlswitch inputmanagerimp mousemanager keyboardmanager sdlmappings sensormanager ) add_openmw_dir (mwgui diff --git a/apps/openmw/mwinput/controlswitch.cpp b/apps/openmw/mwinput/controlswitch.cpp new file mode 100644 index 0000000000..6ea51064fc --- /dev/null +++ b/apps/openmw/mwinput/controlswitch.cpp @@ -0,0 +1,58 @@ +#include "controlswitch.hpp" + +#include "../mwbase/environment.hpp" +#include "../mwbase/world.hpp" + +#include "../mwworld/player.hpp" + +namespace MWInput +{ + ControlSwitch::ControlSwitch() + { + clear(); + } + + void ControlSwitch::clear() + { + mSwitches["playercontrols"] = true; + mSwitches["playerfighting"] = true; + mSwitches["playerjumping"] = true; + mSwitches["playerlooking"] = true; + mSwitches["playermagic"] = true; + mSwitches["playerviewswitch"] = true; + mSwitches["vanitymode"] = true; + } + + bool ControlSwitch::get(const std::string& key) + { + return mSwitches[key]; + } + + void ControlSwitch::set(const std::string& key, bool value) + { + MWWorld::Player& player = MWBase::Environment::get().getWorld()->getPlayer(); + + /// \note 7 switches at all, if-else is relevant + if (key == "playercontrols" && !value) + { + player.setLeftRight(0); + player.setForwardBackward(0); + player.setAutoMove(false); + player.setUpDown(0); + } + else if (key == "playerjumping" && !value) + { + /// \fixme maybe crouching at this time + player.setUpDown(0); + } + else if (key == "vanitymode") + { + MWBase::Environment::get().getWorld()->allowVanityMode(value); + } + else if (key == "playerlooking" && !value) + { + MWBase::Environment::get().getWorld()->rotateObject(player.getPlayer(), 0.f, 0.f, 0.f); + } + mSwitches[key] = value; + } +} diff --git a/apps/openmw/mwinput/controlswitch.hpp b/apps/openmw/mwinput/controlswitch.hpp new file mode 100644 index 0000000000..5fa475e770 --- /dev/null +++ b/apps/openmw/mwinput/controlswitch.hpp @@ -0,0 +1,21 @@ +#ifndef MWINPUT_CONTROLSWITCH_H +#define MWINPUT_CONTROLSWITCH_H + +#include + +namespace MWInput +{ + class ControlSwitch + { + public: + ControlSwitch(); + + bool get(const std::string& key); + void set(const std::string& key, bool value); + void clear(); + + private: + std::map mSwitches; + }; +} +#endif diff --git a/apps/openmw/mwinput/inputmanagerimp.cpp b/apps/openmw/mwinput/inputmanagerimp.cpp index 96806845e7..b9b9ecf62f 100644 --- a/apps/openmw/mwinput/inputmanagerimp.cpp +++ b/apps/openmw/mwinput/inputmanagerimp.cpp @@ -31,6 +31,7 @@ #include "actionmanager.hpp" #include "controllermanager.hpp" +#include "controlswitch.hpp" #include "keyboardmanager.hpp" #include "mousemanager.hpp" #include "sdlmappings.hpp" @@ -66,13 +67,7 @@ namespace MWInput mInputBinder->getChannel (i)->addListener (this); } - mControlSwitch["playercontrols"] = true; - mControlSwitch["playerfighting"] = true; - mControlSwitch["playerjumping"] = true; - mControlSwitch["playerlooking"] = true; - mControlSwitch["playermagic"] = true; - mControlSwitch["playerviewswitch"] = true; - mControlSwitch["vanitymode"] = true; + mControlSwitch = new ControlSwitch(); mActionManager = new ActionManager(mInputBinder, screenCaptureOperation, viewer, screenCaptureHandler); @@ -92,8 +87,7 @@ namespace MWInput void InputManager::clear() { // Enable all controls - for (std::map::iterator it = mControlSwitch.begin(); it != mControlSwitch.end(); ++it) - it->second = true; + mControlSwitch->clear(); mActionManager->clear(); mControllerManager->clear(); @@ -109,6 +103,8 @@ namespace MWInput delete mMouseManager; delete mSensorManager; + delete mControlSwitch; + mInputBinder->save(mUserFile); delete mInputBinder; @@ -159,7 +155,7 @@ namespace MWInput return; } - if (mControlSwitch["playercontrols"]) + if (mControlSwitch->get("playercontrols")) { bool joystickUsed = mControllerManager->joystickLastUsed(); if (action == A_Use) @@ -278,35 +274,12 @@ namespace MWInput bool InputManager::getControlSwitch (const std::string& sw) { - return mControlSwitch[sw]; + return mControlSwitch->get(sw); } void InputManager::toggleControlSwitch (const std::string& sw, bool value) { - MWWorld::Player& player = MWBase::Environment::get().getWorld()->getPlayer(); - - /// \note 7 switches at all, if-else is relevant - if (sw == "playercontrols" && !value) - { - player.setLeftRight(0); - player.setForwardBackward(0); - player.setAutoMove(false); - player.setUpDown(0); - } - else if (sw == "playerjumping" && !value) - { - /// \fixme maybe crouching at this time - player.setUpDown(0); - } - else if (sw == "vanitymode") - { - MWBase::Environment::get().getWorld()->allowVanityMode(value); - } - else if (sw == "playerlooking" && !value) - { - MWBase::Environment::get().getWorld()->rotateObject(player.getPlayer(), 0.f, 0.f, 0.f); - } - mControlSwitch[sw] = value; + mControlSwitch->set(sw, value); } void InputManager::resetIdleTime() diff --git a/apps/openmw/mwinput/inputmanagerimp.hpp b/apps/openmw/mwinput/inputmanagerimp.hpp index 51561feac9..ac056809e4 100644 --- a/apps/openmw/mwinput/inputmanagerimp.hpp +++ b/apps/openmw/mwinput/inputmanagerimp.hpp @@ -21,6 +21,7 @@ namespace MWInput { + class ControlSwitch; class ActionManager; class ControllerManager; class KeyboardManager; @@ -151,7 +152,7 @@ namespace MWInput bool mDetectingKeyboard; - std::map mControlSwitch; + ControlSwitch* mControlSwitch; ActionManager* mActionManager; ControllerManager* mControllerManager;