Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands2/src/libraries/core/ConsoleCommand.cc @ 7621

Last change on this file since 7621 was 6454, checked in by rgrieder, 15 years ago

3 build fixes for MSVC.

  • Property svn:eol-style set to native
File size: 8.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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "ConsoleCommand.h"
30#include <cassert>
31
32namespace orxonox
33{
34    ConsoleCommand::ConsoleCommand(Functor* functor, const std::string& name) : Executor(functor, name)
35    {
36        this->accessLevel_ = AccessLevel::None;
37        this->argumentCompleter_[0] = 0;
38        this->argumentCompleter_[1] = 0;
39        this->argumentCompleter_[2] = 0;
40        this->argumentCompleter_[3] = 0;
41        this->argumentCompleter_[4] = 0;
42
43        this->keybindMode_ = KeybindMode::OnPress;
44        this->inputConfiguredParam_ = -1;
45    }
46
47    ConsoleCommand& ConsoleCommand::argumentCompleter(unsigned int param, ArgumentCompleter* completer)
48    {
49        if (param < 5)
50            this->argumentCompleter_[param] = completer;
51        else
52        {
53            COUT(2) << "Warning: Couldn't add autocompletion-function for param " << param << ": index out of bound." << std::endl;
54        }
55        return (*this);
56    }
57
58    ArgumentCompleter* ConsoleCommand::getArgumentCompleter(unsigned int param) const
59    {
60        if (param < 5)
61            return this->argumentCompleter_[param];
62        else
63            return 0;
64    }
65
66    void ConsoleCommand::createArgumentCompletionList(unsigned int param, const std::string& param1, const std::string& param2, const std::string& param3, const std::string& param4, const std::string& param5)
67    {
68        if (param < 5 && this->argumentCompleter_[param])
69            this->argumentList_ = (*this->argumentCompleter_[param])(param1, param2, param3, param4, param5);
70        else
71            this->argumentList_.clear();
72    }
73}
74
75#include "BaseObject.h" // remove this
76
77namespace orxonox
78{
79    _SetConsoleCommand("BaseObject", "setName", &BaseObject::setName, (BaseObject*)0);
80    _ConsoleCommand::_ConsoleCommandManipulator test(_ModifyConsoleCommand("BaseObject", "setName").setFunction(&BaseObject::setActive));
81
82    _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state) : Executor(functor, name), functionHeader_(functor->getHeaderIdentifier())
83    {
84        this->state_ = state;
85        _ConsoleCommand::registerCommand(group, name, this);
86    }
87
88    _ConsoleCommand& _ConsoleCommand::addShortcut()
89    {
90        _ConsoleCommand::registerCommand("", this->getName(), this);
91        return *this;
92    }
93
94    _ConsoleCommand& _ConsoleCommand::addShortcut(const std::string&  name)
95    {
96        _ConsoleCommand::registerCommand("", name, this);
97        return *this;
98    }
99
100    _ConsoleCommand& _ConsoleCommand::addGroup(const std::string& group)
101    {
102        _ConsoleCommand::registerCommand(group, this->getName(), this);
103        return *this;
104    }
105
106    _ConsoleCommand& _ConsoleCommand::addGroup(const std::string& group, const std::string&  name)
107    {
108        _ConsoleCommand::registerCommand(group, name, this);
109        return *this;
110    }
111
112    void _ConsoleCommand::setActive(bool bActive)
113    {
114        if (bActive)
115        {
116            if (this->state_ == State::Inactive)
117                this->state_ = State::Active;
118            else if (this->state_ == State::UninitializedInactive)
119                this->state_ = State::UninitializedActive;
120        }
121        else
122        {
123            if (this->state_ == State::Active)
124                this->state_ = State::Inactive;
125            else if (this->state_ == State::UninitializedActive)
126                this->state_ = State::UninitializedInactive;
127        }
128    }
129
130    void _ConsoleCommand::setInitialized(bool bInitialized)
131    {
132        if (bInitialized)
133        {
134            if (this->state_ == State::UninitializedActive)
135                this->state_ = State::Active;
136            else if (this->state_ == State::UninitializedInactive)
137                this->state_ = State::Inactive;
138        }
139        else
140        {
141            if (this->state_ == State::Active)
142                this->state_ = State::UninitializedActive;
143            else if (this->state_ == State::Inactive)
144                this->state_ = State::UninitializedInactive;
145        }
146    }
147
148    void _ConsoleCommand::setFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode)
149    {
150        if (!functor)
151        {
152            this->setInitialized(false);
153            return;
154        }
155
156        if (!this->functionHeaderMatches(functor))
157        {
158            COUT(1) << "Error: Couldn't assign new function to console command with name \"" << this->getName() << "\", headers don't match." << std::endl;
159            return;
160        }
161
162        switch (mode)
163        {
164            default:
165            case _ConsoleCommand::ObjectPointer::Null:
166            {
167                this->functor_ = functor;
168            }
169            break;
170
171            case _ConsoleCommand::ObjectPointer::RawCopy:
172            {
173                void* object = (this->functor_) ? this->functor_->getRawObjectPointer() : 0;
174
175                this->functor_ = functor;
176
177                if (!this->functor_->getBaseObject())
178                    this->functor_->setRawObjectPointer(object);
179            }
180            break;
181
182            case _ConsoleCommand::ObjectPointer::CastViaBaseObject:
183            {
184                BaseObject* object = (this->functor_) ? this->functor_->getBaseObject() : 0;
185
186                this->functor_ = functor;
187
188                if (!this->functor_->getBaseObject())
189                    this->functor_->setBaseObject(object);
190            }
191            break;
192        }
193    }
194
195    bool _ConsoleCommand::functionHeaderMatches(Functor* functor) const
196    {
197        if (!this->functor_)
198        {
199            assert(false);
200            return false;
201        }
202        return (functor->getHeaderIdentifier() == this->functor_->getHeaderIdentifier());
203    }
204
205    void _ConsoleCommand::setObject(void* object)
206    {
207        if (this->functor_)
208            this->functor_->setRawObjectPointer(object);
209    }
210
211    void _ConsoleCommand::setObject(BaseObject* object)
212    {
213        if (this->functor_)
214            this->functor_->setBaseObject(object);
215    }
216
217    /* static */ const _ConsoleCommand* _ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError)
218    {
219        std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = _ConsoleCommand::getCommandMap().find(group);
220        if (it_group != _ConsoleCommand::getCommandMap().end())
221        {
222            std::map<std::string, _ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
223            if (it_name != it_group->second.end())
224            {
225                return it_name->second;
226            }
227        }
228        if (bPrintError)
229        {
230            if (group == "")
231                COUT(0) << "Error: Couldn't find console command with shortcut \"" << name << "\"" << std::endl;
232            else
233                COUT(0) << "Error: Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << std::endl;
234        }
235        return 0;
236    }
237
238    /* static */ std::map<std::string, std::map<std::string, _ConsoleCommand*> >& _ConsoleCommand::getCommandMap()
239    {
240        static std::map<std::string, std::map<std::string, _ConsoleCommand*> > commandMap;
241        return commandMap;
242    }
243
244    /* static */ void _ConsoleCommand::registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command)
245    {
246        if (name == "")
247            return;
248
249        if (_ConsoleCommand::getCommand(group, name) != 0)
250        {
251            if (group == "")
252                COUT(2) << "Warning: A console command with shortcut name \"" << name << "\" already exists." << std::endl;
253            else
254                COUT(2) << "Warning: A console command with group \"" << group << "\" and name \"" << name << "\" already exists." << std::endl;
255        }
256        else
257        {
258            _ConsoleCommand::getCommandMap()[group][name] = command;
259        }
260    }
261}
Note: See TracBrowser for help on using the repository browser.