| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Reto Grieder | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #ifndef _KeyBinderManager_H__ | 
|---|
| 30 | #define _KeyBinderManager_H__ | 
|---|
| 31 |  | 
|---|
| 32 | #include "InputPrereqs.h" | 
|---|
| 33 |  | 
|---|
| 34 | #include <map> | 
|---|
| 35 | #include <string> | 
|---|
| 36 | #include "util/Singleton.h" | 
|---|
| 37 | #include "core/OrxonoxClass.h" | 
|---|
| 38 |  | 
|---|
| 39 | namespace orxonox | 
|---|
| 40 | { | 
|---|
| 41 |     /** | 
|---|
| 42 |     @brief | 
|---|
| 43 |         Handles the KeyBinders and supplies them throughout the game. | 
|---|
| 44 |  | 
|---|
| 45 |         This interface merely serves to provide a static "keybind" command that always | 
|---|
| 46 |         maps to the currently active KeyBinder. You can set that with setCurrent(). | 
|---|
| 47 |         There is also a default one, retrieved with getDefault(). The idea is that | 
|---|
| 48 |         mostly the default KeyBinder is active except for special situtations (mini-game for inst). | 
|---|
| 49 |     @remarks | 
|---|
| 50 |         You are not forced to use the KeyBinder imposed by getCurrent(). But be aware that "keybind" | 
|---|
| 51 |         will not work as expected! | 
|---|
| 52 |     */ | 
|---|
| 53 |     class _CoreExport KeyBinderManager : public Singleton<KeyBinderManager>, public OrxonoxClass | 
|---|
| 54 |     { | 
|---|
| 55 |         friend class Singleton<KeyBinderManager>; | 
|---|
| 56 |     public: | 
|---|
| 57 |         KeyBinderManager(); | 
|---|
| 58 |         ~KeyBinderManager(); | 
|---|
| 59 |         void setConfigValues(); | 
|---|
| 60 |  | 
|---|
| 61 |         //! Returns the currently selected KeyBinder | 
|---|
| 62 |         KeyBinder*    getCurrent() | 
|---|
| 63 |             { return this->currentBinder_; } | 
|---|
| 64 |         //! Like getCurrent(), but returns it as InputHandler* (so you don't have to include KeyBinder.h) | 
|---|
| 65 |         InputHandler* getCurrentAsHandler(); | 
|---|
| 66 |         //! Selects the current KeyBinder and creates it if not yet loaded. | 
|---|
| 67 |         void setCurrent(const std::string& filename); | 
|---|
| 68 |  | 
|---|
| 69 |         //! Returns the default KeyBinder | 
|---|
| 70 |         KeyBinder*    getDefault() | 
|---|
| 71 |             { return binders_[this->defaultFilename_]; } | 
|---|
| 72 |         //! Returns the default KeyBinder as InputHandler* (so you don't have to include KeyBinder.h) | 
|---|
| 73 |         InputHandler* getDefaultAsHandler(); | 
|---|
| 74 |         //! Returns the filename of the default key bindings | 
|---|
| 75 |         const std::string& getDefaultFilename() | 
|---|
| 76 |             { return defaultFilename_; } | 
|---|
| 77 |         //! Selects the default KeyBinder as current one | 
|---|
| 78 |         void setToDefault() | 
|---|
| 79 |             { this->setCurrent(this->defaultFilename_); } | 
|---|
| 80 |  | 
|---|
| 81 |         //! Returns a pointer to a KeyBinder (creates it if not yet loaded) | 
|---|
| 82 |         KeyBinder*    get(const std::string& name); | 
|---|
| 83 |         //! Like get() but return value is of type InputHandler* (so you don't have to include KeyBinder.h) | 
|---|
| 84 |         InputHandler* getAsHandler(const std::string& name); | 
|---|
| 85 |  | 
|---|
| 86 |         //! Loads a KeyBinder by creating it (no different from get() except for the return value) | 
|---|
| 87 |         void load  (const std::string& filename); | 
|---|
| 88 |         //! Destroys a KeyBinder completely (does nothing if not yet loaded) | 
|---|
| 89 |         void unload(const std::string& filename); | 
|---|
| 90 |  | 
|---|
| 91 |         //! Bind 'command' to any key pressed after this call (use with care!) | 
|---|
| 92 |         inline void keybind(const std::string& command) | 
|---|
| 93 |             { this->keybindInternal(command, false); } | 
|---|
| 94 |         //! Bind 'command' to any key pressed after this call (use with care!), but temporarily (no file save) | 
|---|
| 95 |         inline void tkeybind(const std::string& command) | 
|---|
| 96 |             { this->keybindInternal(command, true); } | 
|---|
| 97 |  | 
|---|
| 98 |     private: | 
|---|
| 99 |         KeyBinderManager(const KeyBinderManager&); | 
|---|
| 100 |         void keybindInternal(const std::string& command, bool bTemporary); | 
|---|
| 101 |         void callback(const std::string& keyName); | 
|---|
| 102 |         void defaultFilenameChanged(); | 
|---|
| 103 |  | 
|---|
| 104 |         // KeyBinder management | 
|---|
| 105 |         KeyBinder* currentBinder_;                   //! Currently selected KeyBinder (never NULL!) | 
|---|
| 106 |         std::map<std::string, KeyBinder*> binders_;  //! All loaded KeyBinders | 
|---|
| 107 |         bool bDefaultFileLoaded_;                    //! Tells whether the default one is loaded | 
|---|
| 108 |         std::string defaultFilename_;                //! Name of the file with the default key bindings | 
|---|
| 109 |  | 
|---|
| 110 |         // keybind command related | 
|---|
| 111 |         Functor* callbackFunction_;                  //! Function to be called when key was pressed after "keybind" command | 
|---|
| 112 |         bool bBinding_;                              //! Tells whether a key binding process is active | 
|---|
| 113 |         bool bTemporary_;                            //! Stores tkeybind/keybind value | 
|---|
| 114 |         std::string command_;                        //! Stores the command received by (t)keybind | 
|---|
| 115 |  | 
|---|
| 116 |         static KeyBinderManager* singletonPtr_s; | 
|---|
| 117 |     }; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | #endif /* _KeyBinderManager_H__ */ | 
|---|