Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6266 was 6266, checked in by dafrick, 14 years ago

KeyBindMenu is working now. (Apart from some minor faults)
Found some commandExecutor("show/hideGUI …") in DecisionPopup, and another possible bug.
Added an InfoPopup

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