Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

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