[Orxonox-commit 1142] r5863 - in code/branches/core5: data/defaultConfig src/libraries/core src/libraries/core/input src/orxonox/gamestates
rgrieder at orxonox.net
rgrieder at orxonox.net
Sat Oct 3 17:33:32 CEST 2009
Author: rgrieder
Date: 2009-10-03 17:33:31 +0200 (Sat, 03 Oct 2009)
New Revision: 5863
Added:
code/branches/core5/src/libraries/core/input/KeyBinderManager.cc
code/branches/core5/src/libraries/core/input/KeyBinderManager.h
Removed:
code/branches/core5/data/defaultConfig/masterKeybindings.ini
Modified:
code/branches/core5/data/defaultConfig/keybindings.ini
code/branches/core5/src/libraries/core/Core.cc
code/branches/core5/src/libraries/core/Core.h
code/branches/core5/src/libraries/core/CorePrereqs.h
code/branches/core5/src/libraries/core/input/CMakeLists.txt
code/branches/core5/src/libraries/core/input/InputManager.cc
code/branches/core5/src/libraries/core/input/InputManager.h
code/branches/core5/src/libraries/core/input/KeyBinder.cc
code/branches/core5/src/libraries/core/input/KeyBinder.h
code/branches/core5/src/orxonox/gamestates/GSGraphics.cc
code/branches/core5/src/orxonox/gamestates/GSGraphics.h
code/branches/core5/src/orxonox/gamestates/GSLevel.cc
code/branches/core5/src/orxonox/gamestates/GSLevel.h
code/branches/core5/src/orxonox/gamestates/GSMainMenu.cc
Log:
New class: KeyBinderManager (yes, it really was necessary, I'm not such a Fan of zillions of classes as well) and moved the keybind command to it from GSLevel.
This new Singleton simply maps the keybind command to the right KeyBinder, selected by KeyBinderManager::setCurrent().
There is also a default KeyBinder (with keybindings.ini as file), which should do the Trick for now. Other Keybinders should only server special purposes (like in mini games or so).
====== DELETE YOUR keybindings.ini FILE! =======
Modified: code/branches/core5/data/defaultConfig/keybindings.ini
===================================================================
--- code/branches/core5/data/defaultConfig/keybindings.ini 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/data/defaultConfig/keybindings.ini 2009-10-03 15:33:31 UTC (rev 5863)
@@ -24,7 +24,7 @@
KeyEquals=
KeyEscape="exit"
KeyF="scale -1 moveUpDown"
-KeyF1=
+KeyF1="OverlayGroup toggleVisibility Debug"
KeyF10=
KeyF11=
KeyF12=
@@ -40,7 +40,7 @@
KeyF8=
KeyF9=
KeyG=greet
-KeyGrave=
+KeyGrave="openConsole"
KeyH=
KeyHome=showGUI
KeyI=
@@ -125,7 +125,7 @@
KeyTab="cycleNavigationFocus"
KeyU=
KeyUP="scale 1 moveFrontBack"
-KeyUnassigned=
+KeyUnassigned="openConsole"
KeyUnderline=
KeyUnlabeled=
KeyV=
Deleted: code/branches/core5/data/defaultConfig/masterKeybindings.ini
===================================================================
--- code/branches/core5/data/defaultConfig/masterKeybindings.ini 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/data/defaultConfig/masterKeybindings.ini 2009-10-03 15:33:31 UTC (rev 5863)
@@ -1,167 +0,0 @@
-[Keys]
-KeyA=
-KeyABNT_C1=
-KeyABNT_C2=
-KeyAT=
-KeyAX=
-KeyApostrophe=
-KeyApps=
-KeyB=
-KeyBack=
-KeyBackslash=
-KeyC=
-KeyCalculator=
-KeyCapsLock=
-KeyColon=
-KeyComma=
-KeyConvert=
-KeyD=
-KeyDelete=
-KeyDivide=
-KeyDown=
-KeyE=
-KeyEnd=
-KeyEquals=
-KeyEscape=
-KeyF=
-KeyF1="OverlayGroup toggleVisibility Debug"
-KeyF10=
-KeyF11=
-KeyF12=
-KeyF13=
-KeyF14=
-KeyF15=
-KeyF2=
-KeyF3=
-KeyF4=
-KeyF5=
-KeyF6=
-KeyF7=
-KeyF8=
-KeyF9=
-KeyG=
-KeyGrave="openConsole"
-KeyH=
-KeyHome=
-KeyI=
-KeyInsert=
-KeyJ=
-KeyK=
-KeyKana=
-KeyKanji=
-KeyL=
-KeyLeft=
-KeyLeftAlt=
-KeyLeftBracket=
-KeyLeftControl=
-KeyLeftShift=
-KeyLeftWindows=
-KeyLessThan=
-KeyM=
-KeyMail=
-KeyMediaSelect=
-KeyMediaStop=
-KeyMinus=
-KeyMultiply=
-KeyMute=
-KeyMyComputer=
-KeyN=
-KeyNextTrack=
-KeyNoConvert=
-KeyNumLock=
-KeyNumRow0=
-KeyNumRow1=
-KeyNumRow2=
-KeyNumRow3=
-KeyNumRow4=
-KeyNumRow5=
-KeyNumRow6=
-KeyNumRow7=
-KeyNumRow8=
-KeyNumRow9=
-KeyNumpad0=
-KeyNumpad1=
-KeyNumpad2=
-KeyNumpad3=
-KeyNumpad4=
-KeyNumpad5=
-KeyNumpad6=
-KeyNumpad7=
-KeyNumpad8=
-KeyNumpad9=
-KeyNumpadAdd=
-KeyNumpadComma=
-KeyNumpadEnter=
-KeyNumpadEquals=
-KeyNumpadPeriod=
-KeyNumpadSubtract=
-KeyO=
-KeyP=
-KeyPageDown=
-KeyPageUp=
-KeyPause=
-KeyPeriod=
-KeyPlayPause=
-KeyPower=
-KeyPreviousTrack=
-KeyQ=
-KeyR=
-KeyReturn=
-KeyRight=
-KeyRightAlt=
-KeyRightBracket=
-KeyRightControl=
-KeyRightShift=
-KeyRightWindows=
-KeyS=
-KeyScrollLock=
-KeySemicolon=
-KeySlash=
-KeySleep=
-KeySpace=
-KeyStop=
-KeySystemRequest=
-KeyT=
-KeyTab=
-KeyU=
-KeyUP=
-KeyUnassigned="openConsole"
-KeyUnderline=
-KeyUnlabeled=
-KeyV=
-KeyVolumeDown=
-KeyVolumeUp=
-KeyW=
-KeyWake=
-KeyWebBack=
-KeyWebFavorites=
-KeyWebForward=
-KeyWebHome=
-KeyWebRefresh=
-KeyWebSearch=
-KeyWebStop=
-KeyX=
-KeyY=
-KeyYen=
-KeyZ=
-
-[MouseButtons]
-MouseButton3=
-MouseButton4=
-MouseButton5=
-MouseButton6=
-MouseButton7=
-MouseLeft=
-MouseMiddle=
-MouseRight=
-MouseWheel1Down=
-MouseWheel1Up=
-MouseWheel2Down=
-MouseWheel2Up=
-
-[MouseAxes]
-MouseXNeg=
-MouseXPos=
-MouseYNeg=
-MouseYPos=
-
Modified: code/branches/core5/src/libraries/core/Core.cc
===================================================================
--- code/branches/core5/src/libraries/core/Core.cc 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/libraries/core/Core.cc 2009-10-03 15:33:31 UTC (rev 5863)
@@ -68,6 +68,7 @@
#include "TclBind.h"
#include "TclThreadManager.h"
#include "input/InputManager.h"
+#include "input/KeyBinderManager.h"
namespace orxonox
{
@@ -287,6 +288,9 @@
// Calls the InputManager which sets up the input devices.
inputManager_.reset(new InputManager());
+ // Manages KeyBinders and makes them available
+ keyBinderManager_.reset(new KeyBinderManager());
+
// load the CEGUI interface
guiManager_.reset(new GUIManager(graphicsManager_->getRenderWindow(),
inputManager_->getMousePosition(), graphicsManager_->isFullScreen()));
@@ -303,6 +307,7 @@
{
this->graphicsScope_.reset();
this->guiManager_.reset();
+ this->keyBinderManager_.reset();
this->inputManager_.reset();
this->graphicsManager_.reset();
Modified: code/branches/core5/src/libraries/core/Core.h
===================================================================
--- code/branches/core5/src/libraries/core/Core.h 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/libraries/core/Core.h 2009-10-03 15:33:31 UTC (rev 5863)
@@ -99,6 +99,7 @@
// graphical
scoped_ptr<GraphicsManager> graphicsManager_; //!< Interface to OGRE
scoped_ptr<InputManager> inputManager_; //!< Interface to OIS
+ scoped_ptr<KeyBinderManager> keyBinderManager_; //!< Manages all KeyBinders
scoped_ptr<GUIManager> guiManager_; //!< Interface to GUI
scoped_ptr<Scope<ScopeID::Root> > rootScope_;
scoped_ptr<Scope<ScopeID::Graphics> > graphicsScope_;
Modified: code/branches/core5/src/libraries/core/CorePrereqs.h
===================================================================
--- code/branches/core5/src/libraries/core/CorePrereqs.h 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/libraries/core/CorePrereqs.h 2009-10-03 15:33:31 UTC (rev 5863)
@@ -211,6 +211,7 @@
class JoyStickQuantityListener;
class JoyStick;
class KeyBinder;
+ class KeyBinderManager;
class Keyboard;
class KeyDetector;
class KeyEvent;
Modified: code/branches/core5/src/libraries/core/input/CMakeLists.txt
===================================================================
--- code/branches/core5/src/libraries/core/input/CMakeLists.txt 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/libraries/core/input/CMakeLists.txt 2009-10-03 15:33:31 UTC (rev 5863)
@@ -8,6 +8,7 @@
JoyStick.cc
JoyStickQuantityListener.cc
KeyBinder.cc
+ KeyBinderManager.cc
Keyboard.cc
KeyDetector.cc
Mouse.cc
Modified: code/branches/core5/src/libraries/core/input/InputManager.cc
===================================================================
--- code/branches/core5/src/libraries/core/input/InputManager.cc 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/libraries/core/input/InputManager.cc 2009-10-03 15:33:31 UTC (rev 5863)
@@ -263,11 +263,6 @@
JoyStickQuantityListener::changeJoyStickQuantity(joyStickList);
}
- void InputManager::setKeyDetectorCallback(const std::string& command)
- {
- this->keyDetector_->setCallbackCommand(command);
- }
-
// ############################################################
// ##### Destruction #####
// ########## ##########
Modified: code/branches/core5/src/libraries/core/input/InputManager.h
===================================================================
--- code/branches/core5/src/libraries/core/input/InputManager.h 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/libraries/core/input/InputManager.h 2009-10-03 15:33:31 UTC (rev 5863)
@@ -160,8 +160,6 @@
//-------------------------------
// Various getters and setters
//-------------------------------
- //! Sets the the name of the command used by the KeyDetector as callback.
- void setKeyDetectorCallback(const std::string& command);
//! Returns the number of joy stick that have been created since the c'tor or last call to reload().
unsigned int getJoyStickQuantity() const
{ return devices_.size() - InputDeviceEnumerator::FirstJoyStick; }
Modified: code/branches/core5/src/libraries/core/input/KeyBinder.cc
===================================================================
--- code/branches/core5/src/libraries/core/input/KeyBinder.cc 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/libraries/core/input/KeyBinder.cc 2009-10-03 15:33:31 UTC (rev 5863)
@@ -26,15 +26,11 @@
*
*/
-/**
- @file
- @brief Implementation of the different input handlers.
- */
-
#include "KeyBinder.h"
#include "util/Convert.h"
#include "util/Debug.h"
+#include "util/Exception.h"
#include "core/ConfigValueIncludes.h"
#include "core/CoreIncludes.h"
#include "core/ConfigFileManager.h"
@@ -47,8 +43,9 @@
@brief
Constructor that does as little as necessary.
*/
- KeyBinder::KeyBinder()
+ KeyBinder::KeyBinder(const std::string& filename)
: deriveTime_(0.0f)
+ , filename_(filename)
{
mouseRelative_[0] = 0;
mouseRelative_[1] = 0;
@@ -102,6 +99,10 @@
// set them here to use allHalfAxes_
setConfigValues();
+
+ // Load the bindings if filename was given
+ if (!this->filename_.empty())
+ this->loadBindings();
}
/**
@@ -239,26 +240,17 @@
/**
@brief
Loads the key and button bindings.
- @return
- True if loading succeeded.
*/
- void KeyBinder::loadBindings(const std::string& filename)
+ void KeyBinder::loadBindings()
{
COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
- if (filename.empty())
- return;
+ // Get a new ConfigFileType from the ConfigFileManager
+ this->configFile_ = ConfigFileManager::getInstance().getNewConfigFileType();
- if (this->configFile_ == ConfigFileType::NoType)
- {
- // Get a new ConfigFileType from the ConfigFileManager
- this->configFile_ = ConfigFileManager::getInstance().getNewConfigFileType();
- }
+ ConfigFileManager::getInstance().setFilename(this->configFile_, this->filename_);
- ConfigFileManager::getInstance().setFilename(this->configFile_, filename);
-
// Parse bindings and create the ConfigValueContainers if necessary
- clearBindings();
for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
it->second->readConfigValue(this->configFile_);
Modified: code/branches/core5/src/libraries/core/input/KeyBinder.h
===================================================================
--- code/branches/core5/src/libraries/core/input/KeyBinder.h 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/libraries/core/input/KeyBinder.h 2009-10-03 15:33:31 UTC (rev 5863)
@@ -26,12 +26,6 @@
*
*/
-/**
- at file
- at brief
- Different definitions of input processing.
-*/
-
#ifndef _KeyBinder_H__
#define _KeyBinder_H__
@@ -52,34 +46,40 @@
{
/**
@brief
- Handles mouse, keyboard and joy stick input while in the actual game mode.
- Manages the key bindings.
+ Maps mouse, keyboard and joy stick input to command strings and executes them.
+
+ The bindings are stored in ini-files (like the one for configValues) in the config Path.
+ @remarks
+ You cannot change the filename because the KeyBinderManager maps these filenames to the
+ KeyBinders. If you need to load other bindings, just create a new one.
*/
class _CoreExport KeyBinder : public InputHandler, public JoyStickQuantityListener
{
public:
- KeyBinder ();
+ KeyBinder (const std::string& filename);
virtual ~KeyBinder();
- void loadBindings(const std::string& filename);
void clearBindings();
bool setBinding(const std::string& binding, const std::string& name, bool bTemporary = false);
+ const std::string& getBindingsFilename()
+ { return this->filename_; }
void setConfigValues();
void resetJoyStickAxes();
protected: // functions
+ void loadBindings();
+ void buttonThresholdChanged();
+ void initialiseJoyStickBindings();
+ void compilePointerLists();
+ // from JoyStickQuantityListener interface
+ virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList);
+
void allDevicesUpdated(float dt);
void mouseUpdated(float dt);
void joyStickUpdated(unsigned int joyStick, float dt);
// internal
void tickHalfAxis(HalfAxis& halfAxis);
- void buttonThresholdChanged();
- // from JoyStickQuantityListener interface
- virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList);
- void initialiseJoyStickBindings();
- void compilePointerLists();
-
void buttonPressed (const KeyEvent& evt);
void buttonReleased(const KeyEvent& evt);
void buttonHeld (const KeyEvent& evt);
@@ -143,6 +143,8 @@
int mouseRelative_[2];
float deriveTime_;
+ //! Name of the file used in this KeyBinder (constant!)
+ const std::string filename_;
//! Config file used. ConfigFileType::NoType in case of KeyDetector. Also indicates whether we've already loaded.
ConfigFileType configFile_;
@@ -170,6 +172,7 @@
static const int mouseClippingSize_ = 1024;
};
+
inline void KeyBinder::buttonPressed (const KeyEvent& evt)
{ assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnPress); }
Added: code/branches/core5/src/libraries/core/input/KeyBinderManager.cc
===================================================================
--- code/branches/core5/src/libraries/core/input/KeyBinderManager.cc (rev 0)
+++ code/branches/core5/src/libraries/core/input/KeyBinderManager.cc 2009-10-03 15:33:31 UTC (rev 5863)
@@ -0,0 +1,165 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Reto Grieder
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "KeyBinderManager.h"
+
+#include "util/Debug.h"
+#include "util/Exception.h"
+#include "core/ConfigValueIncludes.h"
+#include "core/ConsoleCommand.h"
+#include "core/CoreIncludes.h"
+#include "InputManager.h"
+#include "KeyDetector.h"
+
+namespace orxonox
+{
+ KeyBinderManager* KeyBinderManager::singletonPtr_s = 0;
+
+ KeyBinderManager::KeyBinderManager()
+ : currentBinder_(NULL)
+ , bDefaultFileLoaded_(true)
+ , bBinding_(false)
+ {
+ this->callbackFunction_ = createFunctor(&KeyBinderManager::callback, this);
+
+ RegisterObject(KeyBinderManager);
+ this->setConfigValues();
+
+ // keybind console commands
+ CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&KeyBinderManager::keybind, this), "keybind" ));
+ CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&KeyBinderManager::tkeybind, this), "tkeybind"));
+
+ // Load default key binder
+ this->setCurrent(defaultFilename_);
+ }
+
+ KeyBinderManager::~KeyBinderManager()
+ {
+ // Delete all remaining KeyBinders
+ for (std::map<std::string, KeyBinder*>::const_iterator it = this->binders_.begin(); it != this->binders_.end(); ++it)
+ delete it->second;
+ delete this->callbackFunction_;
+ }
+
+ void KeyBinderManager::setConfigValues()
+ {
+ SetConfigValue(defaultFilename_, "keybindings.ini")
+ .description("Filename for the default keybindings file.")
+ .callback(this, &KeyBinderManager::defaultFilenameChanged);
+ }
+
+ void KeyBinderManager::defaultFilenameChanged()
+ {
+ if (this->bDefaultFileLoaded_)
+ this->setCurrent(this->defaultFilename_);
+ }
+
+ void KeyBinderManager::setCurrent(const std::string& filename)
+ {
+ this->currentBinder_ = this->get(filename);
+ if (filename == this->defaultFilename_)
+ this->bDefaultFileLoaded_ = true;
+ else
+ this->bDefaultFileLoaded_ = false;
+ }
+
+ void KeyBinderManager::load(const std::string& filename)
+ {
+ std::map<std::string, KeyBinder*>::const_iterator it = this->binders_.find(filename);
+ if (it != this->binders_.end())
+ return;
+
+ KeyBinder* binder = new KeyBinder(filename);
+ this->binders_[filename] = binder;
+ }
+
+ void KeyBinderManager::unload(const std::string& filename)
+ {
+ if (filename == this->defaultFilename_)
+ ThrowException(General, "KeyBinderManager: Cannot unload the default file");
+ if (filename == this->currentBinder_->getBindingsFilename())
+ {
+ // unloading current file --> set default file
+ this->setCurrent(this->defaultFilename_);
+ }
+ std::map<std::string, KeyBinder*>::iterator it = this->binders_.find(filename);
+ if (it != this->binders_.end())
+ {
+ delete it->second;
+ this->binders_.erase(it);
+ }
+ }
+
+ KeyBinder* KeyBinderManager::get(const std::string& name)
+ {
+ this->load(name);
+ return this->binders_[name];
+ }
+
+ InputHandler* KeyBinderManager::getCurrentAsHandler()
+ {
+ return this->getCurrent();
+ }
+
+ InputHandler* KeyBinderManager::getDefaultAsHandler()
+ {
+ return this->getDefault();
+ }
+
+ InputHandler* KeyBinderManager::getAsHandler(const std::string& name)
+ {
+ return this->get(name);
+ }
+
+ void KeyBinderManager::keybindInternal(const std::string& command, bool bTemporary)
+ {
+ if (!this->bBinding_)
+ {
+ COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
+ KeyDetector::getInstance().setCallback(callbackFunction_);
+ InputManager::getInstance().enterState("detector");
+ this->command_ = command;
+ this->bTemporary_ = bTemporary;
+ this->bBinding_ = true;
+ }
+ // else: We're still in a keybind command. Ignore this call.
+ }
+
+ // Gets called by the KeyDetector (registered with a Functor)
+ void KeyBinderManager::callback(const std::string& keyName)
+ {
+ if (this->bBinding_)
+ {
+ COUT(0) << "Binding string \"" << command_ << "\" on key '" << keyName << "'" << std::endl;
+ this->currentBinder_->setBinding(command_, keyName, bTemporary_);
+ InputManager::getInstance().leaveState("detector");
+ this->bBinding_ = false;
+ }
+ // else: A key was probably pressed within the same tick, ignore it.
+ }
+}
Property changes on: code/branches/core5/src/libraries/core/input/KeyBinderManager.cc
___________________________________________________________________
Added: svn:eol-style
+ native
Added: code/branches/core5/src/libraries/core/input/KeyBinderManager.h
===================================================================
--- code/branches/core5/src/libraries/core/input/KeyBinderManager.h (rev 0)
+++ code/branches/core5/src/libraries/core/input/KeyBinderManager.h 2009-10-03 15:33:31 UTC (rev 5863)
@@ -0,0 +1,120 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Reto Grieder
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _KeyBinderManager_H__
+#define _KeyBinderManager_H__
+
+#include "InputPrereqs.h"
+
+#include <map>
+#include <string>
+#include "util/Singleton.h"
+#include "core/OrxonoxClass.h"
+
+namespace orxonox
+{
+ /**
+ @brief
+ Handles the KeyBinders and supplies them throughout the game.
+
+ This interface merely serves to provide a static "keybind" command that always
+ maps to the currently active KeyBinder. You can set that with setCurrent().
+ There is also a default one, retrieved with getDefault(). The idea is that
+ mostly the default KeyBinder is active except for special situtations (mini-game for inst).
+ @remarks
+ You are not forced to use the KeyBinder imposed by getCurrent(). But be aware that "keybind"
+ will not work as expected!
+ */
+ class _CoreExport KeyBinderManager : public Singleton<KeyBinderManager>, public OrxonoxClass
+ {
+ friend class Singleton<KeyBinderManager>;
+ public:
+ KeyBinderManager();
+ ~KeyBinderManager();
+ void setConfigValues();
+
+ //! Returns the currently selected KeyBinder
+ KeyBinder* getCurrent()
+ { return this->currentBinder_; }
+ //! Like getCurrent(), but returns it as InputHandler* (so you don't have to include KeyBinder.h)
+ InputHandler* getCurrentAsHandler();
+ //! Selects the current KeyBinder and creates it if not yet loaded.
+ void setCurrent(const std::string& filename);
+
+ //! Returns the default KeyBinder
+ KeyBinder* getDefault()
+ { return binders_[this->defaultFilename_]; }
+ //! Returns the default KeyBinder as InputHandler* (so you don't have to include KeyBinder.h)
+ InputHandler* getDefaultAsHandler();
+ //! Returns the filename of the default key bindings
+ const std::string& getDefaultFilename()
+ { return defaultFilename_; }
+ //! Selects the default KeyBinder as current one
+ void setToDefault()
+ { this->setCurrent(this->defaultFilename_); }
+
+ //! Returns a pointer to a KeyBinder (creates it if not yet loaded)
+ KeyBinder* get(const std::string& name);
+ //! Like get() but return value is of type InputHandler* (so you don't have to include KeyBinder.h)
+ InputHandler* getAsHandler(const std::string& name);
+
+ //! Loads a KeyBinder by creating it (no different from get() except for the return value)
+ void load (const std::string& filename);
+ //! Destroys a KeyBinder completely (does nothing if not yet loaded)
+ void unload(const std::string& filename);
+
+ //! Bind 'command' to any key pressed after this call (use with care!)
+ inline void keybind(const std::string& command)
+ { this->keybindInternal(command, false); }
+ //! Bind 'command' to any key pressed after this call (use with care!), but temporarily (no file save)
+ inline void tkeybind(const std::string& command)
+ { this->keybindInternal(command, true); }
+
+ private:
+ KeyBinderManager(const KeyBinderManager&);
+ void keybindInternal(const std::string& command, bool bTemporary);
+ void callback(const std::string& keyName);
+ void defaultFilenameChanged();
+
+ // KeyBinder management
+ KeyBinder* currentBinder_; //! Currently selected KeyBinder (never NULL!)
+ std::map<std::string, KeyBinder*> binders_; //! All loaded KeyBinders
+ bool bDefaultFileLoaded_; //! Tells whether the default one is loaded
+ std::string defaultFilename_; //! Name of the file with the default key bindings
+
+ // keybind command related
+ Functor* callbackFunction_; //! Function to be called when key was pressed after "keybind" command
+ bool bBinding_; //! Tells whether a key binding process is active
+ bool bTemporary_; //! Stores tkeybind/keybind value
+ std::string command_; //! Stores the command received by (t)keybind
+
+ static KeyBinderManager* singletonPtr_s;
+ };
+}
+
+#endif /* _KeyBinderManager_H__ */
Property changes on: code/branches/core5/src/libraries/core/input/KeyBinderManager.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: code/branches/core5/src/orxonox/gamestates/GSGraphics.cc
===================================================================
--- code/branches/core5/src/orxonox/gamestates/GSGraphics.cc 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/orxonox/gamestates/GSGraphics.cc 2009-10-03 15:33:31 UTC (rev 5863)
@@ -40,9 +40,6 @@
#include "core/ConsoleCommand.h"
#include "core/Game.h"
#include "core/GUIManager.h"
-#include "core/input/InputManager.h"
-#include "core/input/KeyBinder.h"
-#include "core/input/InputState.h"
#include "core/Loader.h"
#include "core/XMLFile.h"
@@ -55,20 +52,12 @@
GSGraphics::GSGraphics(const GameStateInfo& info)
: GameState(info)
- , masterKeyBinder_(0)
- , masterInputState_(0)
, debugOverlay_(0)
{
- // load master key bindings
- masterInputState_ = InputManager::getInstance().createInputState("master", true);
- masterKeyBinder_ = new KeyBinder();
- masterInputState_->setKeyHandler(masterKeyBinder_);
}
GSGraphics::~GSGraphics()
{
- InputManager::getInstance().destroyState("master");
- this->masterKeyBinder_->destroy();
}
/**
@@ -93,14 +82,9 @@
this->debugOverlay_ = new XMLFile("debug.oxo");
Loader::open(debugOverlay_);
- masterKeyBinder_->loadBindings("masterKeybindings.ini");
-
// add console command to toggle GUI
this->ccToggleGUI_ = createConsoleCommand(createFunctor(&GSGraphics::toggleGUI, this), "toggleGUI");
CommandExecutor::addConsoleCommandShortcut(this->ccToggleGUI_);
-
- // enable master input
- InputManager::getInstance().enterState("master");
}
/**
Modified: code/branches/core5/src/orxonox/gamestates/GSGraphics.h
===================================================================
--- code/branches/core5/src/orxonox/gamestates/GSGraphics.h 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/orxonox/gamestates/GSGraphics.h 2009-10-03 15:33:31 UTC (rev 5863)
@@ -59,8 +59,6 @@
void toggleGUI();
private:
- KeyBinder* masterKeyBinder_; //!< Key binder for master key bindings
- InputState* masterInputState_; //!< Special input state for master input
XMLFile* debugOverlay_;
ConsoleCommand* ccToggleGUI_; //!< Console command to toggle GUI
};
Modified: code/branches/core5/src/orxonox/gamestates/GSLevel.cc
===================================================================
--- code/branches/core5/src/orxonox/gamestates/GSLevel.cc 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/orxonox/gamestates/GSLevel.cc 2009-10-03 15:33:31 UTC (rev 5863)
@@ -34,7 +34,7 @@
#include "util/Clock.h"
#include "core/input/InputManager.h"
#include "core/input/InputState.h"
-#include "core/input/KeyBinder.h"
+#include "core/input/KeyBinderManager.h"
#include "core/ConsoleCommand.h"
#include "core/ConfigValueIncludes.h"
#include "core/CoreIncludes.h"
@@ -56,15 +56,11 @@
GSLevel::GSLevel(const GameStateInfo& info)
: GameState(info)
- , keyBinder_(0)
, gameInputState_(0)
, guiMouseOnlyInputState_(0)
, guiKeysOnlyInputState_(0)
{
RegisterObject(GSLevel);
-
- this->ccKeybind_ = 0;
- this->ccTkeybind_ = 0;
}
GSLevel::~GSLevel()
@@ -73,7 +69,6 @@
void GSLevel::setConfigValues()
{
- SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
}
void GSLevel::activate()
@@ -83,9 +78,8 @@
if (GameMode::showsGraphics())
{
gameInputState_ = InputManager::getInstance().createInputState("game");
- keyBinder_ = new KeyBinder();
- keyBinder_->loadBindings("keybindings.ini");
- gameInputState_->setHandler(keyBinder_);
+ gameInputState_->setHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
+ KeyBinderManager::getInstance().setToDefault();
guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr());
@@ -101,14 +95,6 @@
if (GameMode::showsGraphics())
{
- // keybind console command
- ccKeybind_ = createConsoleCommand(createFunctor(&GSLevel::keybind, this), "keybind");
- CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
- ccTkeybind_ = createConsoleCommand(createFunctor(&GSLevel::tkeybind, this), "tkeybind");
- CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
- // set our console command as callback for the key detector
- InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
-
// level is loaded: we can start capturing the input
InputManager::getInstance().enterState("game");
@@ -135,20 +121,6 @@
void GSLevel::deactivate()
{
-/*
- // destroy console commands
- if (this->ccKeybind_)
- {
- delete this->ccKeybind_;
- this->ccKeybind_ = 0;
- }
- if (this->ccTkeybind_)
- {
- delete this->ccTkeybind_;
- this->ccTkeybind_ = 0;
- }
-*/
-
if (GameMode::showsGraphics())
{
// disconnect the HumanPlayer
@@ -180,11 +152,6 @@
InputManager::getInstance().destroyState("game");
InputManager::getInstance().destroyState("guiKeysOnly");
InputManager::getInstance().destroyState("guiMouseOnly");
- if (this->keyBinder_)
- {
- this->keyBinder_->destroy();
- this->keyBinder_ = 0;
- }
}
}
@@ -210,59 +177,4 @@
delete startFile_s;
}
-
- void GSLevel::keybind(const std::string &command)
- {
- this->keybindInternal(command, false);
- }
-
- void GSLevel::tkeybind(const std::string &command)
- {
- this->keybindInternal(command, true);
- }
-
- /**
- @brief
- Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
- @param command
- Command string that can be executed by the CommandExecutor
- OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
- the key/button/axis that has been activated. This is configured above in activate().
- */
- void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
- {
- if (GameMode::showsGraphics())
- {
- static std::string bindingString = "";
- static bool bTemporarySaved = false;
- static bool bound = true;
- // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
- // Howerver there will be no real issue if it happens anyway.
- if (command.find(keyDetectorCallbackCode_) != 0)
- {
- if (bound)
- {
- COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
- InputManager::getInstance().enterState("detector");
- bindingString = command;
- bTemporarySaved = bTemporary;
- bound = false;
- }
- //else: We're still in a keybind command. ignore this call.
- }
- else
- {
- if (!bound)
- {
- // user has pressed the key
- std::string name = command.substr(this->keyDetectorCallbackCode_.size());
- COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
- this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
- InputManager::getInstance().leaveState("detector");
- bound = true;
- }
- // else: A key was pressed within the same tick, ignore it.
- }
- }
- }
}
Modified: code/branches/core5/src/orxonox/gamestates/GSLevel.h
===================================================================
--- code/branches/core5/src/orxonox/gamestates/GSLevel.h 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/orxonox/gamestates/GSLevel.h 2009-10-03 15:33:31 UTC (rev 5863)
@@ -56,22 +56,9 @@
void loadLevel();
void unloadLevel();
- // console commands
- void keybind(const std::string& command);
- void tkeybind(const std::string& command);
- void keybindInternal(const std::string& command, bool bTemporary);
-
- KeyBinder* keyBinder_; //!< tool that loads and manages the input bindings
InputState* gameInputState_; //!< input state for normal ingame playing
InputState* guiMouseOnlyInputState_; //!< input state if we only need the mouse to use the GUI
InputState* guiKeysOnlyInputState_; //!< input state if we only need the keys to use the GUI
-
- //##### ConfigValues #####
- std::string keyDetectorCallbackCode_;
-
- // console commands
- ConsoleCommand* ccKeybind_;
- ConsoleCommand* ccTkeybind_;
};
}
Modified: code/branches/core5/src/orxonox/gamestates/GSMainMenu.cc
===================================================================
--- code/branches/core5/src/orxonox/gamestates/GSMainMenu.cc 2009-10-02 19:11:35 UTC (rev 5862)
+++ code/branches/core5/src/orxonox/gamestates/GSMainMenu.cc 2009-10-03 15:33:31 UTC (rev 5863)
@@ -33,6 +33,7 @@
#include "util/Clock.h"
#include "core/input/InputManager.h"
#include "core/input/InputState.h"
+#include "core/input/KeyBinderManager.h"
#include "core/Game.h"
#include "core/ConsoleCommand.h"
#include "core/GraphicsManager.h"
@@ -50,6 +51,7 @@
{
inputState_ = InputManager::getInstance().createInputState("mainMenu");
inputState_->setHandler(GUIManager::getInstancePtr());
+ inputState_->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
inputState_->setJoyStickHandler(&InputHandler::EMPTY);
inputState_->setIsExclusiveMouse(false);
@@ -85,6 +87,7 @@
this->ccStartMainMenu_ = createConsoleCommand(createFunctor(&GSMainMenu::startMainMenu, this), "startMainMenu");
CommandExecutor::addConsoleCommandShortcut(this->ccStartMainMenu_);
+ KeyBinderManager::getInstance().setToDefault();
InputManager::getInstance().enterState("mainMenu");
this->ambient_ = new SoundMainMenu();
More information about the Orxonox-commit
mailing list