Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/input/KeyBinderManager.cc @ 6858

Last change on this file since 6858 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

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