Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands/src/libraries/core/input/KeyBinderManager.cc @ 6360

Last change on this file since 6360 was 6311, checked in by dafrick, 15 years ago

The KeyBindMenu now shows all Keybindings and allows for various manipulations.
For this the bookkeeping in KeyBinder has ben improved.
Also KeyEscape now can't be bound to other commands.

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