Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/input/KeyBinderManager.cc @ 5863

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

New class: KeyBinderManager (yes, it really was necessary, I'm not such a Fan of zillions of classes as well) and moved the keybind command to it from GSLevel.
This new Singleton simply maps the keybind command to the right KeyBinder, selected by KeyBinderManager::setCurrent().
There is also a default KeyBinder (with keybindings.ini as file), which should do the Trick for now. Other Keybinders should only server special purposes (like in mini games or so).

DELETE YOUR keybindings.ini FILE! =
  • Property svn:eol-style set to native
File size: 5.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 "InputManager.h"
37#include "KeyDetector.h"
38
39namespace orxonox
40{
41    KeyBinderManager* KeyBinderManager::singletonPtr_s = 0;
42
43    KeyBinderManager::KeyBinderManager()
44        : currentBinder_(NULL)
45        , bDefaultFileLoaded_(true)
46        , bBinding_(false)
47    {
48        this->callbackFunction_ = createFunctor(&KeyBinderManager::callback, this);
49
50        RegisterObject(KeyBinderManager);
51        this->setConfigValues();
52
53        // keybind console commands
54        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&KeyBinderManager::keybind,  this), "keybind" ));
55        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&KeyBinderManager::tkeybind, this), "tkeybind"));
56
57        // Load default key binder
58        this->setCurrent(defaultFilename_);
59    }
60
61    KeyBinderManager::~KeyBinderManager()
62    {
63        // Delete all remaining KeyBinders
64        for (std::map<std::string, KeyBinder*>::const_iterator it = this->binders_.begin(); it != this->binders_.end(); ++it)
65            delete it->second;
66        delete this->callbackFunction_;
67    }
68
69    void KeyBinderManager::setConfigValues()
70    {
71        SetConfigValue(defaultFilename_, "keybindings.ini")
72            .description("Filename for the default keybindings file.")
73            .callback(this, &KeyBinderManager::defaultFilenameChanged);
74    }
75
76    void KeyBinderManager::defaultFilenameChanged()
77    {
78        if (this->bDefaultFileLoaded_)
79            this->setCurrent(this->defaultFilename_);
80    }
81
82    void KeyBinderManager::setCurrent(const std::string& filename)
83    {
84        this->currentBinder_ = this->get(filename);
85        if (filename == this->defaultFilename_)
86            this->bDefaultFileLoaded_ = true;
87        else
88            this->bDefaultFileLoaded_ = false;
89    }
90
91    void KeyBinderManager::load(const std::string& filename)
92    {
93        std::map<std::string, KeyBinder*>::const_iterator it = this->binders_.find(filename);
94        if (it != this->binders_.end())
95            return;
96
97        KeyBinder* binder = new KeyBinder(filename);
98        this->binders_[filename] = binder;
99    }
100
101    void KeyBinderManager::unload(const std::string& filename)
102    {
103        if (filename == this->defaultFilename_)
104            ThrowException(General, "KeyBinderManager: Cannot unload the default file");
105        if (filename == this->currentBinder_->getBindingsFilename())
106        {
107            // unloading current file --> set default file
108            this->setCurrent(this->defaultFilename_);
109        }
110        std::map<std::string, KeyBinder*>::iterator it = this->binders_.find(filename);
111        if (it != this->binders_.end())
112        {
113            delete it->second;
114            this->binders_.erase(it);
115        }
116    }
117
118    KeyBinder* KeyBinderManager::get(const std::string& name)
119    {
120        this->load(name);
121        return this->binders_[name];
122    }
123
124    InputHandler* KeyBinderManager::getCurrentAsHandler()
125    {
126        return this->getCurrent();
127    }
128
129    InputHandler* KeyBinderManager::getDefaultAsHandler()
130    {
131        return this->getDefault();
132    }
133
134    InputHandler* KeyBinderManager::getAsHandler(const std::string& name)
135    {
136        return this->get(name);
137    }
138
139    void KeyBinderManager::keybindInternal(const std::string& command, bool bTemporary)
140    {
141        if (!this->bBinding_)
142        {
143            COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
144            KeyDetector::getInstance().setCallback(callbackFunction_);
145            InputManager::getInstance().enterState("detector");
146            this->command_ = command;
147            this->bTemporary_ = bTemporary;
148            this->bBinding_ = true;
149        }
150        // else: We're still in a keybind command. Ignore this call.
151    }
152
153    // Gets called by the KeyDetector (registered with a Functor)
154    void KeyBinderManager::callback(const std::string& keyName)
155    {
156        if (this->bBinding_)
157        {
158            COUT(0) << "Binding string \"" << command_ << "\" on key '" << keyName << "'" << std::endl;
159            this->currentBinder_->setBinding(command_, keyName, bTemporary_);
160            InputManager::getInstance().leaveState("detector");
161            this->bBinding_ = false;
162        }
163        // else: A key was probably pressed within the same tick, ignore it.
164    }
165}
Note: See TracBrowser for help on using the repository browser.