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