Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/input/KeyBinderManager.cc @ 7204

Last change on this file since 7204 was 7204, checked in by landauf, 15 years ago

adjusted includes in all other files

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