Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/core/Executor.cc @ 1340

Last change on this file since 1340 was 1340, checked in by rgrieder, 16 years ago
  • tried a more object oriented approach for the KeyBinder
  • things work as far as I can tell
  • tested slomo command on joy stick slider: I was able to steer the time factor with the slider.
  • more infos to come..
File size: 8.9 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 *   Inspiration: Executor by Benjamin Grauer
28 */
29
30#include "Executor.h"
31#include "util/Math.h"
32#include "Language.h"
33
34namespace orxonox
35{
36    Executor::Executor(Functor* functor, const std::string& name, AccessLevel::Level level)
37    {
38        this->functor_ = functor;
39        this->name_ = name;
40        this->accessLevel_ = level;
41        this->keybindMode_ = KeybindMode::OnPress;
42        this->axisParamIndex_ = -1;
43        this->bAxisRelative_ = false;
44
45        this->bAddedDescription_ = false;
46        this->bAddedDescriptionReturnvalue_ = false;
47
48        this->bAddedDescriptionParam_[0] = false;
49        this->bAddedDescriptionParam_[1] = false;
50        this->bAddedDescriptionParam_[2] = false;
51        this->bAddedDescriptionParam_[3] = false;
52        this->bAddedDescriptionParam_[4] = false;
53
54        this->bAddedDefaultValue_[0] = false;
55        this->bAddedDefaultValue_[1] = false;
56        this->bAddedDefaultValue_[2] = false;
57        this->bAddedDefaultValue_[3] = false;
58        this->bAddedDefaultValue_[4] = false;
59    }
60
61    Executor::~Executor()
62    {
63        delete this->functor_;
64    }
65
66    bool Executor::parse(const std::string& params, const std::string& delimiter) const
67    {
68        EXECUTOR_PARSE(normal);
69    }
70
71    bool Executor::evaluate(const std::string& params, MultiTypeMath param[5], const std::string& delimiter) const
72    {
73        unsigned int paramCount = this->functor_->getParamCount();
74
75        if (paramCount == 1)
76        {
77            // only one param: check if there are params given, otherwise try to use default values
78            std::string temp = getStripped(params);
79            if ((temp != "") && (temp.size() != 0))
80            {
81                param[0] = params;
82                this->functor_->evaluateParam(0, param[0]);
83                return true;
84            }
85            else if (this->bAddedDefaultValue_[0])
86            {
87                param[0] = this->defaultValue_[0];
88                this->functor_->evaluateParam(0, param[0]);
89                return true;
90            }
91            return false;
92        }
93        else
94        {
95            // more than one param
96            SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0');
97
98            // if there are not enough params given, check if there are default values
99            for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++)
100                if (!this->bAddedDefaultValue_[i])
101                    return false;
102
103            // assign all given arguments to the multitypes
104            for (unsigned int i = 0; i < min(tokens.size(), (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++)
105                param[i] = tokens[i];
106
107            // fill the remaining multitypes with default values
108            for (unsigned int i = tokens.size(); i < min(paramCount, (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++)
109                param[i] = this->defaultValue_[i];
110
111            // evaluate the param types through the functor
112            for (unsigned int i = 0; i < min(paramCount, (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++)
113                this->functor_->evaluateParam(i, param[i]);
114
115            return true;
116        }
117    }
118
119    Executor& Executor::setDescription(const std::string& description)
120    {
121        if (!this->bAddedDescription_)
122        {
123            this->description_ = std::string("ExecutorDescription::" + this->name_ + "::function");
124            AddLanguageEntry(this->description_, description);
125            this->bAddedDescription_ = true;
126        }
127        return (*this);
128    }
129
130    const std::string& Executor::getDescription() const
131    {
132        return GetLocalisation(this->description_);
133    }
134
135    Executor& Executor::setDescriptionParam(int param, const std::string& description)
136    {
137        if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS)
138        {
139            if (!this->bAddedDescriptionParam_[param])
140            {
141                std::string paramnumber;
142                if (!Convert::ToString(&paramnumber, param))
143                    return (*this);
144
145                this->descriptionParam_[param] = std::string("ExecutorDescription::" + this->name_ + "::param" + paramnumber);
146                AddLanguageEntry(this->descriptionParam_[param], description);
147                this->bAddedDescriptionParam_[param] = true;
148            }
149        }
150        return (*this);
151    }
152
153    const std::string& Executor::getDescriptionParam(int param) const
154    {
155        if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS)
156            return GetLocalisation(this->descriptionParam_[param]);
157
158        return this->descriptionParam_[0];
159    }
160
161    Executor& Executor::setDescriptionReturnvalue(const std::string& description)
162    {
163        if (!this->bAddedDescriptionReturnvalue_)
164        {
165            this->descriptionReturnvalue_ = std::string("ExecutorDescription::" + this->name_ + "::returnvalue");
166            AddLanguageEntry(this->descriptionReturnvalue_, description);
167            this->bAddedDescriptionReturnvalue_ = true;
168        }
169        return (*this);
170    }
171
172    const std::string& Executor::getDescriptionReturnvalue(int param) const
173    {
174        return GetLocalisation(this->descriptionReturnvalue_);
175    }
176
177    Executor& Executor::setDefaultValues(const MultiTypeMath& param1)
178    {
179        this->defaultValue_[0] = param1;
180        this->bAddedDefaultValue_[0] = true;
181
182        return (*this);
183    }
184
185    Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2)
186    {
187        this->defaultValue_[0] = param1;
188        this->bAddedDefaultValue_[0] = true;
189        this->defaultValue_[1] = param2;
190        this->bAddedDefaultValue_[1] = true;
191
192        return (*this);
193    }
194
195    Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3)
196    {
197        this->defaultValue_[0] = param1;
198        this->bAddedDefaultValue_[0] = true;
199        this->defaultValue_[1] = param2;
200        this->bAddedDefaultValue_[1] = true;
201        this->defaultValue_[2] = param3;
202        this->bAddedDefaultValue_[2] = true;
203
204        return (*this);
205    }
206
207    Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4)
208    {
209        this->defaultValue_[0] = param1;
210        this->bAddedDefaultValue_[0] = true;
211        this->defaultValue_[1] = param2;
212        this->bAddedDefaultValue_[1] = true;
213        this->defaultValue_[2] = param3;
214        this->bAddedDefaultValue_[2] = true;
215        this->defaultValue_[3] = param4;
216        this->bAddedDefaultValue_[3] = true;
217
218        return (*this);
219    }
220
221    Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5)
222    {
223        this->defaultValue_[0] = param1;
224        this->bAddedDefaultValue_[0] = true;
225        this->defaultValue_[1] = param2;
226        this->bAddedDefaultValue_[1] = true;
227        this->defaultValue_[2] = param3;
228        this->bAddedDefaultValue_[2] = true;
229        this->defaultValue_[3] = param4;
230        this->bAddedDefaultValue_[3] = true;
231        this->defaultValue_[4] = param5;
232        this->bAddedDefaultValue_[4] = true;
233
234        return (*this);
235    }
236
237    Executor& Executor::setDefaultValue(unsigned int index, const MultiTypeMath& param)
238    {
239        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
240        {
241            this->defaultValue_[index] = param;
242            this->bAddedDefaultValue_[index] = true;
243        }
244        return (*this);
245    }
246
247    bool Executor::allDefaultValuesSet() const
248    {
249        for (unsigned int i = 0; i < this->functor_->getParamCount(); i++)
250            if (!this->bAddedDefaultValue_[i])
251                return false;
252
253        return true;
254    }
255}
Note: See TracBrowser for help on using the repository browser.