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 | #include "KeyBinderManager.h" |
---|
30 | |
---|
31 | #include "util/Output.h" |
---|
32 | #include "util/Exception.h" |
---|
33 | #include "core/singleton/ScopedSingletonIncludes.h" |
---|
34 | #include "core/config/ConfigValueIncludes.h" |
---|
35 | #include "core/CoreIncludes.h" |
---|
36 | #include "core/LuaState.h" |
---|
37 | #include "core/command/ConsoleCommandIncludes.h" |
---|
38 | #include "InputManager.h" |
---|
39 | #include "KeyDetector.h" |
---|
40 | |
---|
41 | namespace orxonox |
---|
42 | { |
---|
43 | ManageScopedSingleton(KeyBinderManager, ScopeID::GRAPHICS, false); |
---|
44 | |
---|
45 | static const std::string __CC_keybind_name = "keybind"; |
---|
46 | static const std::string __CC_tkeybind_name = "tkeybind"; |
---|
47 | static const std::string __CC_unbind_name = "unbind"; |
---|
48 | static const std::string __CC_tunbind_name = "tunbind"; |
---|
49 | |
---|
50 | SetConsoleCommand(__CC_keybind_name, &KeyBinderManager::keybind).defaultValues("").argumentCompleter(0, autocompletion::command()); |
---|
51 | SetConsoleCommand(__CC_tkeybind_name, &KeyBinderManager::tkeybind).defaultValues("").argumentCompleter(0, autocompletion::command()); |
---|
52 | SetConsoleCommand(__CC_unbind_name, &KeyBinderManager::unbind).defaultValues(""); |
---|
53 | SetConsoleCommand(__CC_tunbind_name, &KeyBinderManager::tunbind).defaultValues(""); |
---|
54 | |
---|
55 | RegisterAbstractClass(KeyBinderManager).inheritsFrom<Configurable>(); |
---|
56 | |
---|
57 | KeyBinderManager::KeyBinderManager() |
---|
58 | : currentBinder_(NULL) |
---|
59 | , bDefaultFileLoaded_(true) |
---|
60 | , bBinding_(false) |
---|
61 | { |
---|
62 | RegisterObject(KeyBinderManager); |
---|
63 | this->setConfigValues(); |
---|
64 | |
---|
65 | // keybind console commands |
---|
66 | ModifyConsoleCommand(__CC_keybind_name ).setObject(this); |
---|
67 | ModifyConsoleCommand(__CC_tkeybind_name).setObject(this); |
---|
68 | ModifyConsoleCommand(__CC_unbind_name ).setObject(this); |
---|
69 | ModifyConsoleCommand(__CC_tunbind_name ).setObject(this); |
---|
70 | |
---|
71 | // Load default key binder |
---|
72 | this->setCurrent(defaultFilename_); |
---|
73 | } |
---|
74 | |
---|
75 | KeyBinderManager::~KeyBinderManager() |
---|
76 | { |
---|
77 | // Delete all remaining KeyBinders |
---|
78 | for (std::map<std::string, KeyBinder*>::const_iterator it = this->binders_.begin(); it != this->binders_.end(); ++it) |
---|
79 | delete it->second; |
---|
80 | |
---|
81 | // Reset console commands |
---|
82 | ModifyConsoleCommand(__CC_keybind_name ).setObject(0); |
---|
83 | ModifyConsoleCommand(__CC_tkeybind_name).setObject(0); |
---|
84 | ModifyConsoleCommand(__CC_unbind_name ).setObject(0); |
---|
85 | ModifyConsoleCommand(__CC_tunbind_name ).setObject(0); |
---|
86 | } |
---|
87 | |
---|
88 | void KeyBinderManager::setConfigValues() |
---|
89 | { |
---|
90 | SetConfigValue(defaultFilename_, "keybindings.ini") |
---|
91 | .description("Filename for the default keybindings file.") |
---|
92 | .callback(this, &KeyBinderManager::defaultFilenameChanged); |
---|
93 | } |
---|
94 | |
---|
95 | void KeyBinderManager::defaultFilenameChanged() |
---|
96 | { |
---|
97 | if (this->bDefaultFileLoaded_) |
---|
98 | this->setCurrent(this->defaultFilename_); |
---|
99 | } |
---|
100 | |
---|
101 | void KeyBinderManager::setCurrent(const std::string& filename) |
---|
102 | { |
---|
103 | this->currentBinder_ = this->get(filename); |
---|
104 | if (filename == this->defaultFilename_) |
---|
105 | this->bDefaultFileLoaded_ = true; |
---|
106 | else |
---|
107 | this->bDefaultFileLoaded_ = false; |
---|
108 | } |
---|
109 | |
---|
110 | inline void KeyBinderManager::unbind(const std::string& binding) |
---|
111 | { |
---|
112 | this->currentBinder_->setBinding("", binding, false); |
---|
113 | } |
---|
114 | |
---|
115 | inline void KeyBinderManager::tunbind(const std::string& binding) |
---|
116 | { |
---|
117 | this->currentBinder_->setBinding("", binding, true); |
---|
118 | } |
---|
119 | |
---|
120 | void KeyBinderManager::load(const std::string& filename) |
---|
121 | { |
---|
122 | std::map<std::string, KeyBinder*>::const_iterator it = this->binders_.find(filename); |
---|
123 | if (it != this->binders_.end()) |
---|
124 | return; |
---|
125 | |
---|
126 | KeyBinder* binder = new KeyBinder(filename); |
---|
127 | this->binders_[filename] = binder; |
---|
128 | } |
---|
129 | |
---|
130 | void KeyBinderManager::unload(const std::string& filename) |
---|
131 | { |
---|
132 | if (filename == this->defaultFilename_) |
---|
133 | ThrowException(General, "KeyBinderManager: Cannot unload the default file"); |
---|
134 | if (filename == this->currentBinder_->getBindingsFilename()) |
---|
135 | { |
---|
136 | // unloading current file --> set default file |
---|
137 | this->setCurrent(this->defaultFilename_); |
---|
138 | } |
---|
139 | std::map<std::string, KeyBinder*>::iterator it = this->binders_.find(filename); |
---|
140 | if (it != this->binders_.end()) |
---|
141 | { |
---|
142 | delete it->second; |
---|
143 | this->binders_.erase(it); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | KeyBinder* KeyBinderManager::get(const std::string& name) |
---|
148 | { |
---|
149 | this->load(name); |
---|
150 | return this->binders_[name]; |
---|
151 | } |
---|
152 | |
---|
153 | InputHandler* KeyBinderManager::getCurrentAsHandler() |
---|
154 | { |
---|
155 | return this->getCurrent(); |
---|
156 | } |
---|
157 | |
---|
158 | InputHandler* KeyBinderManager::getDefaultAsHandler() |
---|
159 | { |
---|
160 | return this->getDefault(); |
---|
161 | } |
---|
162 | |
---|
163 | InputHandler* KeyBinderManager::getAsHandler(const std::string& name) |
---|
164 | { |
---|
165 | return this->get(name); |
---|
166 | } |
---|
167 | |
---|
168 | void KeyBinderManager::keybindInternal(const std::string& command, bool bTemporary) |
---|
169 | { |
---|
170 | if (!this->bBinding_) |
---|
171 | { |
---|
172 | orxout(message) << "Press any button/key or move a mouse/joystick axis" << endl; |
---|
173 | KeyDetector::getInstance().setCallback(createFunctor(&KeyBinderManager::keybindKeyPressed, this)); |
---|
174 | InputManager::getInstance().enterState("detector"); |
---|
175 | this->command_ = command; |
---|
176 | this->bTemporary_ = bTemporary; |
---|
177 | this->bBinding_ = true; |
---|
178 | } |
---|
179 | // else: We're still in a keybind command. Ignore this call. |
---|
180 | } |
---|
181 | |
---|
182 | // Gets called by the KeyDetector (registered with a Functor) |
---|
183 | void KeyBinderManager::keybindKeyPressed(const std::string& keyName) |
---|
184 | { |
---|
185 | if (this->bBinding_) |
---|
186 | { |
---|
187 | if (keyName == "Keys.KeyEscape") |
---|
188 | { |
---|
189 | orxout(message) << "Keybinding aborted." << endl; |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | orxout(message) << "Binding string \"" << command_ << "\" on key '" << keyName << "'" << endl; |
---|
194 | this->currentBinder_->setBinding(command_, keyName, bTemporary_); |
---|
195 | } |
---|
196 | InputManager::getInstance().leaveState("detector"); |
---|
197 | // inform whatever was calling the command |
---|
198 | if (this->callbackFunction_) |
---|
199 | (*this->callbackFunction_)(); |
---|
200 | this->bBinding_ = false; |
---|
201 | } |
---|
202 | // else: A key was probably pressed within the same tick, ignore it. |
---|
203 | } |
---|
204 | |
---|
205 | void KeyBinderManager::registerKeybindCallback(LuaFunctor* function) |
---|
206 | { |
---|
207 | this->callbackFunction_ = function; |
---|
208 | } |
---|
209 | } |
---|