Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/libraries/core/input/KeyBinderManager.cc @ 6182

Last change on this file since 6182 was 6182, checked in by rgrieder, 14 years ago

Singleton pointer initialisation should be part of the ManageScopedSingleton macro to ensure the order.

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