Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added LuaFunctor that can execute arbitrary lua code.
Also added LuaState::createLuaFunctor(std::string) and implemented that for the keybindings menu.

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