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 <boost/shared_ptr.hpp> |
---|
37 | #include <CEGUIForwardRefs.h> |
---|
38 | |
---|
39 | #include "util/Singleton.h" |
---|
40 | #include "core/OrxonoxClass.h" |
---|
41 | |
---|
42 | namespace orxonox //tolua_export |
---|
43 | { //tolua_export |
---|
44 | /** |
---|
45 | @brief |
---|
46 | Handles the KeyBinders and supplies them throughout the game. |
---|
47 | |
---|
48 | This interface merely serves to provide a static "keybind" command that always |
---|
49 | maps to the currently active KeyBinder. You can set that with setCurrent(). |
---|
50 | There is also a default one, retrieved with getDefault(). The idea is that |
---|
51 | mostly the default KeyBinder is active except for special situations (mini-game for inst). |
---|
52 | @remarks |
---|
53 | You are not forced to use the KeyBinder imposed by getCurrent(). But be aware that "keybind" |
---|
54 | will not work as expected! |
---|
55 | */ |
---|
56 | class _CoreExport KeyBinderManager //tolua_export |
---|
57 | : public Singleton<KeyBinderManager>, public OrxonoxClass |
---|
58 | { //tolua_export |
---|
59 | friend class Singleton<KeyBinderManager>; |
---|
60 | public: |
---|
61 | KeyBinderManager(); |
---|
62 | ~KeyBinderManager(); |
---|
63 | void setConfigValues(); |
---|
64 | |
---|
65 | static KeyBinderManager& getInstance() { return Singleton<KeyBinderManager>::getInstance(); } //tolua_export |
---|
66 | //! Returns the currently selected KeyBinder |
---|
67 | KeyBinder* getCurrent() { return this->currentBinder_; } //tolua_export |
---|
68 | //! Like getCurrent(), but returns it as InputHandler* (so you don't have to include KeyBinder.h) |
---|
69 | InputHandler* getCurrentAsHandler(); |
---|
70 | //! Selects the current KeyBinder and creates it if not yet loaded. |
---|
71 | void setCurrent(const std::string& filename); |
---|
72 | |
---|
73 | //! Returns the default KeyBinder |
---|
74 | KeyBinder* getDefault() |
---|
75 | { return binders_[this->defaultFilename_]; } |
---|
76 | //! Returns the default KeyBinder as InputHandler* (so you don't have to include KeyBinder.h) |
---|
77 | InputHandler* getDefaultAsHandler(); |
---|
78 | //! Returns the filename of the default key bindings |
---|
79 | const std::string& getDefaultFilename() |
---|
80 | { return defaultFilename_; } |
---|
81 | //! Selects the default KeyBinder as current one |
---|
82 | void setToDefault() |
---|
83 | { this->setCurrent(this->defaultFilename_); } |
---|
84 | |
---|
85 | void subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function); //tolua_export |
---|
86 | |
---|
87 | //! Returns a pointer to a KeyBinder (creates it if not yet loaded) |
---|
88 | KeyBinder* get(const std::string& name); |
---|
89 | //! Like get() but return value is of type InputHandler* (so you don't have to include KeyBinder.h) |
---|
90 | InputHandler* getAsHandler(const std::string& name); |
---|
91 | |
---|
92 | //! Loads a KeyBinder by creating it (no different from get() except for the return value) |
---|
93 | void load(const std::string& filename); |
---|
94 | //! Destroys a KeyBinder completely (does nothing if not yet loaded) |
---|
95 | void unload(const std::string& filename); |
---|
96 | |
---|
97 | //! Bind 'command' to any key pressed after this call (use with care!) |
---|
98 | inline void keybind(const std::string& command) { this->keybindInternal(command, false); } //tolua_export |
---|
99 | //! Bind 'command' to any key pressed after this call (use with care!), but temporarily (no file save) |
---|
100 | inline void tkeybind(const std::string& command) |
---|
101 | { this->keybindInternal(command, true); } |
---|
102 | inline void registerKeybindCallback(Functor* function) { this->callbackFunction_.reset(function); } // tolua_export |
---|
103 | |
---|
104 | private: |
---|
105 | KeyBinderManager(const KeyBinderManager&); |
---|
106 | void keybindInternal(const std::string& command, bool bTemporary); |
---|
107 | void keybindKeyPressed(const std::string& keyName); |
---|
108 | void defaultFilenameChanged(); |
---|
109 | |
---|
110 | // KeyBinder management |
---|
111 | KeyBinder* currentBinder_; //! Currently selected KeyBinder (never NULL!) |
---|
112 | std::map<std::string, KeyBinder*> binders_; //! All loaded KeyBinders |
---|
113 | bool bDefaultFileLoaded_; //! Tells whether the default one is loaded |
---|
114 | std::string defaultFilename_; //! Name of the file with the default key bindings |
---|
115 | |
---|
116 | // keybind command related |
---|
117 | shared_ptr<Functor> callbackFunction_; //! Function to be called when key was pressed after "keybind" command |
---|
118 | bool bBinding_; //! Tells whether a key binding process is active |
---|
119 | bool bTemporary_; //! Stores tkeybind/keybind value |
---|
120 | std::string command_; //! Stores the command received by (t)keybind |
---|
121 | |
---|
122 | static KeyBinderManager* singletonPtr_s; |
---|
123 | }; //tolua_export |
---|
124 | } //tolua_export |
---|
125 | |
---|
126 | #endif /* _KeyBinderManager_H__ */ |
---|