[1505] | 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 | |
---|
[7401] | 29 | /** |
---|
| 30 | @defgroup ConsoleCommand Console commands |
---|
| 31 | @ingroup Command |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | @file |
---|
| 36 | @ingroup Command ConsoleCommand |
---|
| 37 | @brief Declaration of the orxonox::ConsoleCommand class and the SetConsoleCommand() macro. |
---|
| 38 | |
---|
| 39 | @anchor ConsoleCommandExample |
---|
| 40 | |
---|
| 41 | Console commands can be used to write scripts, use key-bindings or simply to be |
---|
| 42 | entered into the shell by the user. Instances of orxonox::ConsoleCommand define |
---|
| 43 | the function of a command, and also more information like, for example, if it is |
---|
| 44 | active, default values, and possible arguments. |
---|
| 45 | |
---|
| 46 | Commands need to be registered to the system statically on startup by using the |
---|
| 47 | SetConsoleCommand() or DeclareConsoleCommand() macros outside of a function. |
---|
| 48 | This ensures that commands are known to the system at any time, so they can be |
---|
| 49 | evaluated (see orxonox::CommandExecutor::evaluate()), for example for key-bindings. |
---|
| 50 | |
---|
| 51 | Example: |
---|
| 52 | @code |
---|
| 53 | void myCoutFunction(const std::string& text) // Define a static function |
---|
| 54 | { |
---|
| 55 | COUT(0) << "Text: " << text << std::endl; // Print the text to the console |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | SetConsoleCommand("cout", &myCoutFunction); // Register the function as command with name "cout" |
---|
| 59 | @endcode |
---|
| 60 | |
---|
| 61 | Now you can open the shell and execute the command: |
---|
| 62 | @code |
---|
| 63 | $ cout Hello World |
---|
| 64 | @endcode |
---|
| 65 | |
---|
| 66 | Internally this command is now passed to orxonox::CommandExecutor::execute(): |
---|
| 67 | @code |
---|
| 68 | CommandExecutor::execute("cout HelloWorld"); |
---|
| 69 | @endcode |
---|
| 70 | |
---|
| 71 | CommandExecutor searches for a command with name "cout" and passes the arguments |
---|
| 72 | "Hello World" to it. Because we registered myCoutFunction() with this command, |
---|
| 73 | as a result the following text will be printed to the console: |
---|
| 74 | @code |
---|
| 75 | Text: Hello World |
---|
| 76 | @endcode |
---|
| 77 | |
---|
| 78 | You can add more attributes to the ConsoleCommand, by using the command-chain feature |
---|
| 79 | of SetConsoleCommand(). For example like this: |
---|
| 80 | @code |
---|
| 81 | SetConsoleCommand("cout", &myCoutFunction) |
---|
| 82 | .addGroup("output", "text") |
---|
| 83 | .accessLevel(AccessLevel::Offline) |
---|
| 84 | .defaultValues("no text"); |
---|
| 85 | @endcode |
---|
| 86 | |
---|
| 87 | Open the shell again and try it: |
---|
| 88 | @code |
---|
| 89 | $ cout Hello World |
---|
| 90 | Text: Hello World |
---|
| 91 | $ output text Hello World |
---|
| 92 | Text: Hello World |
---|
| 93 | $ cout |
---|
| 94 | Text: no text |
---|
| 95 | @endcode |
---|
| 96 | |
---|
| 97 | If you execute it online (note: the access level is "Offline"), you will see the |
---|
| 98 | following (or something similar): |
---|
| 99 | @code |
---|
| 100 | $ cout Hello World |
---|
| 101 | Error: Can't execute command "cout", access denied. |
---|
| 102 | @endcode |
---|
| 103 | |
---|
| 104 | If a command is executed, the arguments are passed to an underlying function, |
---|
| 105 | whitch is wrapped by an orxonox::Functor which again is wrapped by an orxonox::Executor. |
---|
| 106 | The Functor contains the function-pointer, as well as the object-pointer in |
---|
| 107 | case of a non-static member-function. The executor stores possible default-values |
---|
| 108 | for each argument of the function. |
---|
| 109 | |
---|
| 110 | The function of a command can be changed at any time. It's possible to just exchange |
---|
| 111 | the function-pointer of the underlying Functor if the headers of the functions are |
---|
| 112 | exactly the same. But you can also exchange the Functor itself or even completely |
---|
| 113 | replace the Executor. Also the other attributes of a ConsoleCommand can be modified |
---|
| 114 | during the game, for example it can be activated or deactivated. |
---|
| 115 | |
---|
| 116 | To do so, the function ModifyConsoleCommand() has to be used. It returns an instance |
---|
| 117 | of orxonox::ConsoleCommand::ConsoleCommandManipulator which has an interface similar to |
---|
| 118 | orxonox::ConsoleCommand, but with slight differences. You can use it the same way like |
---|
| 119 | SetConsoleCommand(), meaning you can use command-chains to change different attributes at |
---|
| 120 | the same time. ModifyConsoleCommand() must not be executed statically, but rather in a |
---|
| 121 | function at some point of the execution of the program. |
---|
| 122 | |
---|
| 123 | Example: |
---|
| 124 | @code |
---|
| 125 | void myOtherCoutFunction(const std::string& text) // Define a new static function |
---|
| 126 | { |
---|
| 127 | COUT(0) << "Uppercase: " << getUppercase(text) << std::endl; // Print the text in uppercase to the console |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | { |
---|
| 131 | // ... // somewhere in the code |
---|
| 132 | |
---|
| 133 | ModifyConsoleCommand("cout").setFunction(&myOtherCoutFunction); // Modify the underlying function of the command |
---|
| 134 | |
---|
| 135 | // ... |
---|
| 136 | } |
---|
| 137 | @endcode |
---|
| 138 | |
---|
| 139 | If you now enter the command into the shell, you'll see a different behavior: |
---|
| 140 | @code |
---|
| 141 | $ cout Hello World |
---|
| 142 | Uppercase: HELLO WORLD |
---|
| 143 | $ cout |
---|
| 144 | Uppercase: NO TEXT |
---|
| 145 | @endcode |
---|
| 146 | |
---|
| 147 | A few important notes about changing functions: |
---|
| 148 | |
---|
| 149 | Instead of changing the function with setFunction(), you can also create a command-stack |
---|
| 150 | by using pushFunction() and popFunction(). It's important to note a few things about that, |
---|
| 151 | because the underlying structure of Executor and Functor has a few pitfalls: |
---|
| 152 | - If you push a new function-pointer, the same executor as before will be used (and, if |
---|
| 153 | the headers match, even the same functor can be used, which is very fast) |
---|
| 154 | - If you push a new Functor, the same executor as before will be used |
---|
| 155 | - If you push a new Executor, everything is changed |
---|
| 156 | |
---|
| 157 | Note that the executor contains the @b default @b values, so if you just exchange the |
---|
| 158 | Functor, the default values remain the same. However if you decide to change the default |
---|
| 159 | values at any point of the stack, <b>this will also change the default values on all |
---|
| 160 | other stack-levels</b> that share the same executor. If you don't like this behavior, |
---|
| 161 | you have to explicitly push a new executor before changing the default values, either by |
---|
| 162 | calling pushFunction(executor) or by calling pushFunction(void) which pushes a copy of |
---|
| 163 | the current executor to the stack. |
---|
| 164 | |
---|
| 165 | Another important point are object pointers in case of non-static member-functions. |
---|
| 166 | Whenever you set or push a new function, <b>you must add the object pointer again</b> |
---|
| 167 | because objects are stored in the Functor which is usually exchanged if you change |
---|
| 168 | the function. |
---|
| 169 | |
---|
| 170 | You can also use a stack for objects, but note that this <b>object-stack is different for each |
---|
| 171 | function</b> - so if you set a new function, the object-stack will be cleared. If you push |
---|
| 172 | a new function, the old object-stack is stored in the stack, so it can be restored if |
---|
| 173 | you pop the function. |
---|
| 174 | |
---|
| 175 | %DeclareConsoleCommand(): |
---|
| 176 | |
---|
| 177 | Appart from SetConsoleCommand() you can also call DeclareConsoleCommand(). In contrast |
---|
| 178 | to SetConsoleCommand(), this doesn't assign a function to the command. Indeed you have |
---|
| 179 | to pass a function-pointer to DeclareConsoleCommand(), but it is only used to determine |
---|
| 180 | the header of the future command-function. This allows to declare a command statically, |
---|
| 181 | thus it's possible to evaluate key-bindings of this command, but the actual function |
---|
| 182 | can be assigned at a later point. |
---|
| 183 | |
---|
| 184 | Example: |
---|
| 185 | @code |
---|
| 186 | DeclareConsoleCommand("cout", &prototype::void__string); |
---|
| 187 | @endcode |
---|
| 188 | |
---|
| 189 | If you try to execute the command now, you see the following (or something similar): |
---|
| 190 | @code |
---|
| 191 | $ cout Hello World |
---|
| 192 | Error: Can't execute command "cout", command is not active. |
---|
| 193 | @endcode |
---|
| 194 | |
---|
| 195 | You first have to assign a function to use the command: |
---|
| 196 | @code |
---|
| 197 | { |
---|
| 198 | // ... |
---|
| 199 | |
---|
| 200 | ModifyConsoleCommand("cout").setFunction(&myCoutFunction); |
---|
| 201 | |
---|
| 202 | // ... |
---|
| 203 | } |
---|
| 204 | @endcode |
---|
| 205 | |
---|
| 206 | Now you can use it: |
---|
| 207 | @code |
---|
| 208 | $ cout Hello World |
---|
| 209 | Text: Hello World |
---|
| 210 | @endcode |
---|
| 211 | |
---|
| 212 | Note that the initial function prototype::void__string is defined in the namespace |
---|
| 213 | orxonox::prototype. If there's no function with the desired header, you can extend |
---|
| 214 | the collection of functions or simply use another function that has the same header. |
---|
| 215 | */ |
---|
| 216 | |
---|
[1505] | 217 | #ifndef _ConsoleCommand_H__ |
---|
| 218 | #define _ConsoleCommand_H__ |
---|
| 219 | |
---|
[7203] | 220 | #include "core/CorePrereqs.h" |
---|
[1505] | 221 | |
---|
[7185] | 222 | #include <stack> |
---|
[7272] | 223 | #include <vector> |
---|
[3304] | 224 | #include <boost/preprocessor/cat.hpp> |
---|
[7179] | 225 | #include <boost/preprocessor/facilities/expand.hpp> |
---|
[3304] | 226 | |
---|
[7179] | 227 | #include "util/VA_NARGS.h" |
---|
[3196] | 228 | #include "ArgumentCompletionFunctions.h" |
---|
[1505] | 229 | #include "Executor.h" |
---|
| 230 | |
---|
| 231 | |
---|
[7401] | 232 | /** |
---|
| 233 | @brief Defines a console command. The macro is overloaded for 2-4 parameters. |
---|
| 234 | |
---|
| 235 | This is an overloaded macro. Depending on the number of arguments a different |
---|
| 236 | overloaded implementation of the macro will be chosen. |
---|
| 237 | |
---|
| 238 | Console commands created with SetConsoleCommand() become active immediately and |
---|
| 239 | the given function-pointer (and optionally the object) will be used to execute |
---|
| 240 | the command. |
---|
| 241 | */ |
---|
[7236] | 242 | #define SetConsoleCommand(...) \ |
---|
| 243 | BOOST_PP_EXPAND(BOOST_PP_CAT(SetConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)) |
---|
[7401] | 244 | /** |
---|
| 245 | @brief This macro is executed if you call SetConsoleCommand() with 2 arguments. |
---|
| 246 | @param name The name (string) of the console command |
---|
| 247 | @param functionpointer The function-pointer of the corresponding command-function |
---|
| 248 | */ |
---|
[7236] | 249 | #define SetConsoleCommand2(name, functionpointer) \ |
---|
| 250 | SetConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer)) |
---|
[7401] | 251 | /** |
---|
| 252 | @brief This macro is executed if you call SetConsoleCommand() with 3 arguments. |
---|
| 253 | @param group The group (string) of the console command |
---|
| 254 | @param name The name (string) of the console command |
---|
| 255 | @param functionpointer The function-pointer of the corresponding command-function |
---|
| 256 | */ |
---|
[7236] | 257 | #define SetConsoleCommand3(group, name, functionpointer) \ |
---|
| 258 | SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer)) |
---|
[7401] | 259 | /** |
---|
| 260 | @brief This macro is executed if you call SetConsoleCommand() with 4 arguments. |
---|
| 261 | @param group The group (string) of the console command |
---|
| 262 | @param name The name (string) of the console command |
---|
| 263 | @param functionpointer The function-pointer of the corresponding command-function |
---|
| 264 | @param object The object that will be assigned to the command. Used for member-functions. |
---|
| 265 | */ |
---|
[7236] | 266 | #define SetConsoleCommand4(group, name, functionpointer, object) \ |
---|
| 267 | SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object)) |
---|
[7179] | 268 | |
---|
[7401] | 269 | /// Internal macro |
---|
[7236] | 270 | #define SetConsoleCommandGeneric(group, name, functor) \ |
---|
| 271 | static orxonox::ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::createConsoleCommand(group, name, orxonox::createExecutor(functor))) |
---|
[7179] | 272 | |
---|
| 273 | |
---|
[7401] | 274 | /** |
---|
| 275 | @brief Declares a console command. The macro is overloaded for 2-3 parameters. |
---|
| 276 | |
---|
| 277 | This is an overloaded macro. Depending on the number of arguments a different |
---|
| 278 | overloaded implementation of the macro will be chosen. |
---|
| 279 | |
---|
| 280 | Console commands created with DeclareConsoleCommand() don't use the the given |
---|
| 281 | function-pointer to execute the command, it is only used to define the header |
---|
| 282 | of the future command-function. The command is inactive until you manually |
---|
| 283 | set a function with orxonox::ModifyConsoleCommand(). You can use a different |
---|
| 284 | function-pointer than in the final command, as long as it has the same header. |
---|
| 285 | */ |
---|
[7236] | 286 | #define DeclareConsoleCommand(...) \ |
---|
[7239] | 287 | BOOST_PP_EXPAND(BOOST_PP_CAT(DeclareConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)) |
---|
[7401] | 288 | /** |
---|
| 289 | @brief This macro is executed if you call DeclareConsoleCommand() with 2 arguments. |
---|
| 290 | @param name The name (string) of the console command |
---|
| 291 | @param functionpointer The function-pointer of an arbitrary function that has the same header as the final function |
---|
| 292 | */ |
---|
[7236] | 293 | #define DeclareConsoleCommand2(name, functionpointer) \ |
---|
| 294 | DeclareConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer)) |
---|
[7401] | 295 | /** |
---|
| 296 | @brief This macro is executed if you call DeclareConsoleCommand() with 3 arguments. |
---|
| 297 | @param group The group (string) of the console command |
---|
| 298 | @param name The name (string) of the console command |
---|
| 299 | @param functionpointer The function-pointer of an arbitrary function that has the same header as the final function |
---|
| 300 | */ |
---|
[7236] | 301 | #define DeclareConsoleCommand3(group, name, functionpointer) \ |
---|
| 302 | DeclareConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer)) |
---|
[7179] | 303 | |
---|
[7401] | 304 | /// Internal macro |
---|
[7236] | 305 | #define DeclareConsoleCommandGeneric(group, name, functor) \ |
---|
| 306 | static orxonox::ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::createConsoleCommand(group, name, orxonox::createExecutor(functor), false)) |
---|
[7179] | 307 | |
---|
| 308 | |
---|
| 309 | namespace orxonox |
---|
| 310 | { |
---|
[7401] | 311 | /** |
---|
| 312 | @brief A small collection of functions that can be used in DeclareConsoleCommand() if |
---|
| 313 | you don't want to use the real function-pointer. |
---|
| 314 | */ |
---|
[7218] | 315 | namespace prototype |
---|
| 316 | { |
---|
| 317 | inline void void__void(void) {} |
---|
| 318 | inline void void__string(const std::string&) {} |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | namespace AccessLevel |
---|
| 322 | { |
---|
[7401] | 323 | /** |
---|
| 324 | @brief Possible access levels: A command can only be executed if the program is in the state which is requested by the access level. |
---|
| 325 | */ |
---|
[7218] | 326 | enum Enum |
---|
| 327 | { |
---|
| 328 | All, |
---|
| 329 | Standalone, |
---|
| 330 | Master, |
---|
| 331 | Server, |
---|
| 332 | Client, |
---|
| 333 | Online, |
---|
| 334 | Offline, |
---|
| 335 | None |
---|
| 336 | }; |
---|
| 337 | } |
---|
| 338 | |
---|
[7401] | 339 | /** |
---|
| 340 | @brief The ConsoleCommand class stores all information about a console command which can be executed by CommandExecutor. |
---|
| 341 | |
---|
| 342 | Console commands can be entered by the user into the shell, called in scripts, or |
---|
| 343 | used for key-bindings. They are simple text strings that can be executed by |
---|
| 344 | CommandExecutor. CommandExecutor will search for a ConsoleCommand with the given |
---|
| 345 | group and name and will execute it's Executor (which again calls the Functor and |
---|
| 346 | this finally calls the command-function). |
---|
| 347 | |
---|
| 348 | @see See @ref ConsoleCommandExample "ConsoleCommand.h" for more information and some examples. |
---|
| 349 | */ |
---|
[7236] | 350 | class _CoreExport ConsoleCommand |
---|
[7179] | 351 | { |
---|
[7236] | 352 | friend struct ConsoleCommandManipulator; |
---|
[7179] | 353 | |
---|
[7401] | 354 | /** |
---|
| 355 | @brief Helper class that is used to put the current state of the ConsoleCommand on a stack. |
---|
| 356 | */ |
---|
[7214] | 357 | struct Command |
---|
| 358 | { |
---|
[7401] | 359 | ExecutorPtr executor_; ///< The executor |
---|
| 360 | FunctorPtr functor_; ///< The function that is used with the executor - has to be stored separatley because the executor is often used with different functors |
---|
| 361 | std::vector<void*> objectStack_; ///< The object stack |
---|
[7214] | 362 | }; |
---|
| 363 | |
---|
[7179] | 364 | public: |
---|
[7401] | 365 | /** |
---|
| 366 | @brief Helper class that is used to manipulate console commands. |
---|
| 367 | |
---|
| 368 | An instance of this class is returned if you call the ModifyConsoleCommand macro. |
---|
| 369 | This class provides an interface which wraps some functions of ConsoleCommand. It |
---|
| 370 | allows access to some private functions like setFunction() (that can't be called |
---|
| 371 | right after SetConsoleCommand()) but it also hides some functions that shouln't be |
---|
| 372 | called after the static declaration like addShortcut() or description(). |
---|
| 373 | |
---|
| 374 | @see See @ref ConsoleCommandExample "ConsoleCommand.h" for more information and examples. |
---|
| 375 | */ |
---|
[7236] | 376 | struct ConsoleCommandManipulator |
---|
[7179] | 377 | { |
---|
| 378 | public: |
---|
[7401] | 379 | /// Constructor: Creates a manipulator for a given ConsoleCommand. |
---|
[7236] | 380 | ConsoleCommandManipulator(const ConsoleCommand* command) : command_(const_cast<ConsoleCommand*>(command)) {} |
---|
[7179] | 381 | |
---|
[7401] | 382 | /// Changes the current function of the command. @param function The new function-pointer @param bForce If true, the new function-pointer is always assigned, even if the headers don't match |
---|
[7179] | 383 | template <class F> |
---|
[7236] | 384 | inline ConsoleCommandManipulator& setFunction(F function, bool bForce = false) |
---|
[7214] | 385 | { |
---|
| 386 | if (this->command_) |
---|
| 387 | { |
---|
[7401] | 388 | // check if the headers match. If they do, only change the function-pointer of the current Functor instead of creating a new Functor |
---|
[7215] | 389 | if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F)) |
---|
[7214] | 390 | { |
---|
[7215] | 391 | FunctorPointer<F>* functor = static_cast<FunctorPointer<F>*>(this->command_->getExecutor()->getFunctor().get()); |
---|
[7214] | 392 | functor->setFunction(function); |
---|
| 393 | return *this; |
---|
| 394 | } |
---|
| 395 | this->command_->setFunction(createFunctor(function), bForce); |
---|
| 396 | } |
---|
| 397 | return *this; |
---|
| 398 | } |
---|
[7401] | 399 | /// Changes the current function of the command. @param function The new function-pointer @param object The new object-pointer (for member-functions) @param bForce If true, the new function-pointer is always assigned, even if the headers don't match |
---|
[7179] | 400 | template <class F, class O> |
---|
[7236] | 401 | inline ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false) |
---|
[7214] | 402 | { |
---|
| 403 | if (this->command_) |
---|
| 404 | { |
---|
[7401] | 405 | // check if the headers match. If they do, only change the function-pointer of the current Functor instead of creating a new Functor |
---|
[7215] | 406 | if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F)) |
---|
[7214] | 407 | { |
---|
[7215] | 408 | FunctorPointer<F, O>* functor = static_cast<FunctorPointer<F, O>*>(this->command_->getExecutor()->getFunctor().get()); |
---|
[7214] | 409 | functor->setFunction(function); |
---|
| 410 | functor->setObject(object); |
---|
| 411 | return *this; |
---|
| 412 | } |
---|
| 413 | this->command_->setFunction(createFunctor(function, object), bForce); |
---|
| 414 | } |
---|
| 415 | return *this; |
---|
| 416 | } |
---|
[7401] | 417 | /// Changes the current Functor of the command. @param functor The new Functor @param bForce If true, the new Functor is always assigned, even if the headers don't match |
---|
[7236] | 418 | inline ConsoleCommandManipulator& setFunction(const FunctorPtr& functor, bool bForce = false) |
---|
[7214] | 419 | { if (this->command_) { this->command_->setFunction(functor, bForce); } return *this; } |
---|
[7401] | 420 | /// Changes the current Executor of the command. @param executor The new Executor @param bForce If true, the new Executor is always assigned, even if the headers don't match |
---|
[7236] | 421 | inline ConsoleCommandManipulator& setFunction(const ExecutorPtr& executor, bool bForce = false) |
---|
[7214] | 422 | { if (this->command_) { this->command_->setFunction(executor, bForce); } return *this; } |
---|
[7179] | 423 | |
---|
[7401] | 424 | /// Pushes a copy of the current Executor on the command-stack, that can be altered without changing the old Executor. @details This function is especially useful if you don't wan't to change the function, but only the default values of the executor. |
---|
[7270] | 425 | inline ConsoleCommandManipulator& pushFunction() |
---|
| 426 | { if (this->command_) { this->command_->pushFunction(); } return *this; } |
---|
[7401] | 427 | /// Pushes a new function on the command-stack. @param function The new function-pointer @param bForce If true, the new function-pointer is always assigned, even if the headers don't match |
---|
[7179] | 428 | template <class F> |
---|
[7236] | 429 | inline ConsoleCommandManipulator& pushFunction(F function, bool bForce = false) |
---|
[7214] | 430 | { if (this->command_) { this->command_->pushFunction(createFunctor(function), bForce); } return *this; } |
---|
[7401] | 431 | /// Pushes a new function on the command-stack. @param function The new function-pointer @param object The new object-pointer (for member-functions) @param bForce If true, the new function-pointer is always assigned, even if the headers don't match |
---|
[7179] | 432 | template <class F, class O> |
---|
[7236] | 433 | inline ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false) |
---|
[7214] | 434 | { if (this->command_) { this->command_->pushFunction(createFunctor(function, object), bForce); } return *this; } |
---|
[7401] | 435 | /// Pushes a new Functor on the command-stack. @param functor The new Functor @param bForce If true, the new Functor is always assigned, even if the headers don't match |
---|
[7236] | 436 | inline ConsoleCommandManipulator& pushFunction(const FunctorPtr& functor, bool bForce = false) |
---|
[7214] | 437 | { if (this->command_) { this->command_->pushFunction(functor, bForce); } return *this; } |
---|
[7401] | 438 | /// Pushes a new Executor on the command-stack. @param executor The new Executor @param bForce If true, the new Executor is always assigned, even if the headers don't match |
---|
[7236] | 439 | inline ConsoleCommandManipulator& pushFunction(const ExecutorPtr& executor, bool bForce = false) |
---|
[7214] | 440 | { if (this->command_) { this->command_->pushFunction(executor, bForce); } return *this; } |
---|
[7179] | 441 | |
---|
[7401] | 442 | /// Removes the current function from the stack and restores the old state. If there's no other function on the stack, the command is deactivated. |
---|
[7236] | 443 | inline ConsoleCommandManipulator& popFunction() |
---|
[7214] | 444 | { if (this->command_) { this->command_->popFunction(); } return *this; } |
---|
[7179] | 445 | |
---|
[7401] | 446 | /// Sets the current function-pointer to NULL, which also deactivates the command. |
---|
[7236] | 447 | inline ConsoleCommandManipulator& resetFunction() |
---|
[7218] | 448 | { if (this->command_) { this->command_->resetFunction(); } return *this; } |
---|
| 449 | |
---|
[7401] | 450 | /// Changes the current object (used for member-functions). |
---|
[7236] | 451 | inline ConsoleCommandManipulator& setObject(void* object) |
---|
[7179] | 452 | { if (this->command_) { this->command_->setObject(object); } return *this; } |
---|
[7401] | 453 | /// Pushes a new object on the object-stack. |
---|
[7236] | 454 | inline ConsoleCommandManipulator& pushObject(void* object) |
---|
[7185] | 455 | { if (this->command_) { this->command_->pushObject(object); } return *this; } |
---|
[7401] | 456 | /// Removes the current object from the object-stack and restores the old object (or NULL if there's no object left on the stack). |
---|
[7236] | 457 | inline ConsoleCommandManipulator& popObject() |
---|
[7185] | 458 | { if (this->command_) { this->command_->popObject(); } return *this; } |
---|
[7179] | 459 | |
---|
[7401] | 460 | /// Changes the activity of the command. |
---|
[7236] | 461 | inline ConsoleCommandManipulator& setActive(bool bActive) |
---|
[7179] | 462 | { if (this->command_) { this->command_->setActive(bActive); } return *this; } |
---|
[7401] | 463 | /// Activates the command. |
---|
[7236] | 464 | inline ConsoleCommandManipulator& activate() |
---|
[7214] | 465 | { return this->setActive(true); } |
---|
[7401] | 466 | /// Deactivates the command. |
---|
[7236] | 467 | inline ConsoleCommandManipulator& deactivate() |
---|
[7214] | 468 | { return this->setActive(false); } |
---|
[7179] | 469 | |
---|
[7401] | 470 | /// Changes the visibility of the command. |
---|
[7236] | 471 | inline ConsoleCommandManipulator& setHidden(bool bHidden) |
---|
[7215] | 472 | { if (this->command_) { this->command_->setHidden(bHidden); } return *this; } |
---|
[7401] | 473 | /// Hides the command (can still be executed, but is not visible in the list of available commands). |
---|
[7236] | 474 | inline ConsoleCommandManipulator& hide() |
---|
[7215] | 475 | { return this->setHidden(true); } |
---|
[7401] | 476 | /// Makes the command visible. |
---|
[7236] | 477 | inline ConsoleCommandManipulator& show() |
---|
[7215] | 478 | { return this->setHidden(false); } |
---|
[7214] | 479 | |
---|
[7401] | 480 | /// Changes the default values of the current executor (doesn't modify executors on deeper levels of the command-stack). |
---|
| 481 | inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1) |
---|
| 482 | { if (this->command_) { this->command_->defaultValues(arg1); } return *this; } |
---|
| 483 | /// Changes the default values of the current executor (doesn't modify executors on deeper levels of the command-stack). |
---|
| 484 | inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1, const MultiType& arg2) |
---|
| 485 | { if (this->command_) { this->command_->defaultValues(arg1, arg2); } return *this; } |
---|
| 486 | /// Changes the default values of the current executor (doesn't modify executors on deeper levels of the command-stack). |
---|
| 487 | inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3) |
---|
| 488 | { if (this->command_) { this->command_->defaultValues(arg1, arg2, arg3); } return *this; } |
---|
| 489 | /// Changes the default values of the current executor (doesn't modify executors on deeper levels of the command-stack). |
---|
| 490 | inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4) |
---|
| 491 | { if (this->command_) { this->command_->defaultValues(arg1, arg2, arg3, arg4); } return *this; } |
---|
| 492 | /// Changes the default values of the current executor (doesn't modify executors on deeper levels of the command-stack). |
---|
| 493 | inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5) |
---|
| 494 | { if (this->command_) { this->command_->defaultValues(arg1, arg2, arg3, arg4, arg5); } return *this; } |
---|
| 495 | /// Changes the default value of the argument with given index of the current executor (doesn't modify executors on deeper levels of the command-stack). |
---|
| 496 | inline ConsoleCommandManipulator& defaultValue(unsigned int index, const MultiType& arg) |
---|
| 497 | { if (this->command_) { this->command_->defaultValue(index, arg); } return *this; } |
---|
[7215] | 498 | |
---|
[7401] | 499 | /// Changes the access level of the command. |
---|
[7236] | 500 | inline ConsoleCommandManipulator& accessLevel(AccessLevel::Enum level) |
---|
[7215] | 501 | { if (this->command_) { this->command_->accessLevel(level); } return *this; } |
---|
| 502 | |
---|
[7401] | 503 | /// Changes the argument completer for the given parameter. |
---|
| 504 | inline ConsoleCommandManipulator& argumentCompleter(unsigned int index, ArgumentCompleter* completer) |
---|
| 505 | { if (this->command_) { this->command_->argumentCompleter(index, completer); } return *this; } |
---|
[7215] | 506 | |
---|
[7401] | 507 | /// Defines the command to be an input command. |
---|
[7236] | 508 | inline ConsoleCommandManipulator& setAsInputCommand() |
---|
[7215] | 509 | { if (this->command_) { this->command_->setAsInputCommand(); } return *this; } |
---|
[7401] | 510 | /// Changes the keybind mode of the command. |
---|
[7236] | 511 | inline ConsoleCommandManipulator& keybindMode(KeybindMode::Value mode) |
---|
[7861] | 512 | { if (this->command_) { this->command_->changeKeybindMode(mode); } return *this; } |
---|
[7401] | 513 | /// Sets the input configured param to the given index. |
---|
[7236] | 514 | inline ConsoleCommandManipulator& inputConfiguredParam(int index) |
---|
[7215] | 515 | { if (this->command_) { this->command_->inputConfiguredParam(index); } return *this; } |
---|
| 516 | |
---|
[7179] | 517 | private: |
---|
[7401] | 518 | ConsoleCommand* command_; ///< The command which is being manipulated by this object |
---|
[7179] | 519 | }; |
---|
| 520 | |
---|
| 521 | public: |
---|
[7236] | 522 | ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true); |
---|
| 523 | ~ConsoleCommand(); |
---|
[7179] | 524 | |
---|
[7236] | 525 | ConsoleCommand& addShortcut(); |
---|
| 526 | ConsoleCommand& addShortcut(const std::string& name); |
---|
| 527 | ConsoleCommand& addGroup(const std::string& group); |
---|
| 528 | ConsoleCommand& addGroup(const std::string& group, const std::string& name); |
---|
[7179] | 529 | |
---|
[7401] | 530 | /// Returns the name that was first used for this command. |
---|
[7218] | 531 | inline const std::string& getName() const |
---|
| 532 | { return this->baseName_; } |
---|
| 533 | |
---|
| 534 | const ExecutorPtr& getExecutor() const; |
---|
[7401] | 535 | /// Returns the functor that defines the required header for this command (but isn't necessarily executed). |
---|
[7267] | 536 | inline const FunctorPtr& getBaseFunctor() const |
---|
| 537 | { return this->baseFunctor_; } |
---|
[7218] | 538 | |
---|
[7401] | 539 | /// Changes the activity of the command. |
---|
[7236] | 540 | inline ConsoleCommand& setActive(bool bActive) |
---|
[7215] | 541 | { this->bActive_ = bActive; return *this; } |
---|
[7401] | 542 | /// Activates the command. |
---|
[7236] | 543 | inline ConsoleCommand& activate() |
---|
[7215] | 544 | { return this->setActive(true); } |
---|
[7401] | 545 | /// Deactivates the command. |
---|
[7236] | 546 | inline ConsoleCommand& deactivate() |
---|
[7215] | 547 | { return this->setActive(false); } |
---|
| 548 | |
---|
[7401] | 549 | /// Changes the visibility of the command. |
---|
[7236] | 550 | inline ConsoleCommand& setHidden(bool bHidden) |
---|
[7215] | 551 | { this->bHidden_ = bHidden; return *this; } |
---|
[7401] | 552 | /// Hides the command (can still be executed, but is not visible in the list of available commands). |
---|
[7236] | 553 | inline ConsoleCommand& hide() |
---|
[7215] | 554 | { return this->setHidden(true); } |
---|
[7401] | 555 | /// Makes the command visible. |
---|
[7236] | 556 | inline ConsoleCommand& show() |
---|
[7215] | 557 | { return this->setHidden(false); } |
---|
| 558 | |
---|
[7214] | 559 | bool isActive() const; |
---|
[7215] | 560 | bool hasAccess() const; |
---|
[7401] | 561 | /// Returns true if the command is currently hidden. |
---|
[7222] | 562 | inline bool isHidden() const |
---|
| 563 | { return this->bHidden_; } |
---|
[7179] | 564 | |
---|
[7236] | 565 | ConsoleCommand& description(const std::string& description); |
---|
[7215] | 566 | const std::string& getDescription() const; |
---|
| 567 | |
---|
[7401] | 568 | ConsoleCommand& descriptionParam(unsigned int index, const std::string& description); |
---|
| 569 | const std::string& getDescriptionParam(unsigned int index) const; |
---|
[7215] | 570 | |
---|
[7236] | 571 | ConsoleCommand& descriptionReturnvalue(const std::string& description); |
---|
[7401] | 572 | const std::string& getDescriptionReturnvalue(int index) const; |
---|
[7215] | 573 | |
---|
[7401] | 574 | ConsoleCommand& defaultValues(const MultiType& arg1); |
---|
| 575 | ConsoleCommand& defaultValues(const MultiType& arg1, const MultiType& arg2); |
---|
| 576 | ConsoleCommand& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3); |
---|
| 577 | ConsoleCommand& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4); |
---|
| 578 | ConsoleCommand& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5); |
---|
| 579 | ConsoleCommand& defaultValue(unsigned int index, const MultiType& arg); |
---|
[7215] | 580 | |
---|
[7401] | 581 | /// Changes the access level of the command. |
---|
[7236] | 582 | inline ConsoleCommand& accessLevel(AccessLevel::Enum level) |
---|
[7215] | 583 | { this->accessLevel_ = level; return *this; } |
---|
[7401] | 584 | /// Returns the access level of the command. |
---|
[7215] | 585 | inline AccessLevel::Enum getAccessLevel() const |
---|
| 586 | { return this->accessLevel_; } |
---|
| 587 | |
---|
[7401] | 588 | ConsoleCommand& argumentCompleter(unsigned int index, ArgumentCompleter* completer); |
---|
| 589 | ArgumentCompleter* getArgumentCompleter(unsigned int index) const; |
---|
[7215] | 590 | |
---|
[7401] | 591 | /// Defines the command to be an input command |
---|
[7236] | 592 | inline ConsoleCommand& setAsInputCommand() |
---|
[7215] | 593 | { |
---|
| 594 | this->keybindMode(KeybindMode::OnHold); |
---|
| 595 | this->defaultValue(0, Vector2(0.0f, 0.0f)); |
---|
| 596 | this->inputConfiguredParam(0); |
---|
| 597 | return *this; |
---|
| 598 | } |
---|
| 599 | |
---|
[7861] | 600 | /// Sets the keybind mode. Note: use changeKeybindMode if you intend to change the mode. |
---|
[7236] | 601 | inline ConsoleCommand& keybindMode(KeybindMode::Value mode) |
---|
[7215] | 602 | { this->keybindMode_ = mode; return *this; } |
---|
[7401] | 603 | /// Returns the keybind mode |
---|
[7215] | 604 | inline KeybindMode::Value getKeybindMode() const |
---|
| 605 | { return this->keybindMode_; } |
---|
| 606 | |
---|
[7861] | 607 | ConsoleCommand& changeKeybindMode(KeybindMode::Value mode); |
---|
| 608 | |
---|
[7401] | 609 | /// Changes the input configured param to the given index. |
---|
[7236] | 610 | inline ConsoleCommand& inputConfiguredParam(int index) |
---|
[7215] | 611 | { this->inputConfiguredParam_ = index; return *this; } |
---|
[7401] | 612 | /// Returns the input configured param. |
---|
[7215] | 613 | inline int getInputConfiguredParam_() const |
---|
| 614 | { return this->inputConfiguredParam_; } |
---|
| 615 | |
---|
[7401] | 616 | /// Returns a manipulator for this command. |
---|
[7236] | 617 | inline ConsoleCommandManipulator getManipulator() const |
---|
[7185] | 618 | { return this; } |
---|
| 619 | |
---|
[7179] | 620 | private: |
---|
[7214] | 621 | bool headersMatch(const FunctorPtr& functor); |
---|
| 622 | bool headersMatch(const ExecutorPtr& executor); |
---|
| 623 | |
---|
| 624 | bool setFunction(const ExecutorPtr& executor, bool bForce = false); |
---|
| 625 | bool setFunction(const FunctorPtr& functor, bool bForce = false); |
---|
| 626 | void pushFunction(const ExecutorPtr& executor, bool bForce = false); |
---|
| 627 | void pushFunction(const FunctorPtr& functor, bool bForce = false); |
---|
| 628 | void pushFunction(); |
---|
| 629 | void popFunction(); |
---|
[7218] | 630 | void resetFunction(); |
---|
[7179] | 631 | |
---|
[7214] | 632 | bool setObject(void* object); |
---|
[7185] | 633 | void pushObject(void* object); |
---|
| 634 | void popObject(); |
---|
| 635 | void* getObject() const; |
---|
[7179] | 636 | |
---|
[7401] | 637 | bool bActive_; ///< True if the command should be active (it can still be inactive, for example if the function is missing) |
---|
| 638 | bool bHidden_; ///< True if the command is hidden (it is still executable, but not visible in the list of available commands) |
---|
| 639 | AccessLevel::Enum accessLevel_; ///< The access level (the state of the game in which you can access the command) |
---|
| 640 | std::string baseName_; ///< The name that was first assigned to the command |
---|
| 641 | FunctorPtr baseFunctor_; ///< The functor that defines the header of the command-function |
---|
[7214] | 642 | |
---|
[7401] | 643 | ExecutorPtr executor_; ///< The Executor that is used to execute the command |
---|
| 644 | std::stack<Command> commandStack_; ///< A stack of commands, used to push and pop different functions |
---|
| 645 | std::vector<void*> objectStack_; ///< A stack of objects, used to push and pop different objects for a function |
---|
[7215] | 646 | |
---|
[7401] | 647 | ArgumentCompleter* argumentCompleter_[MAX_FUNCTOR_ARGUMENTS]; ///< ArgumentCompleter for each argument |
---|
[7215] | 648 | |
---|
[7401] | 649 | KeybindMode::Value keybindMode_; ///< The keybind mode |
---|
| 650 | int inputConfiguredParam_; ///< The input configured param |
---|
[7215] | 651 | |
---|
[7401] | 652 | LanguageEntryLabel description_; ///< The description of the command |
---|
| 653 | LanguageEntryLabel descriptionReturnvalue_; ///< A description of the return-value |
---|
| 654 | LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS]; ///< A description for each argument |
---|
[7215] | 655 | |
---|
| 656 | public: |
---|
[7401] | 657 | /// Returns the map with all groups and commands. |
---|
[7236] | 658 | static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommands() |
---|
| 659 | { return ConsoleCommand::getCommandMap(); } |
---|
[7401] | 660 | /// Returns the map with all groups and commands in lowercase. |
---|
[7236] | 661 | static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandsLC() |
---|
| 662 | { return ConsoleCommand::getCommandMapLC(); } |
---|
[7215] | 663 | |
---|
[7401] | 664 | /// Returns a command (shortcut) with given name. @param name The name of the command shortcut @param bPrintError If true, an error is printed if the command doesn't exist |
---|
[7282] | 665 | static inline const ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false) |
---|
[7236] | 666 | { return ConsoleCommand::getCommand("", name, bPrintError); } |
---|
[7401] | 667 | /// Returns a command (shortcut) with given name in lowercase. @param name The lowercase name of the command shortcut @param bPrintError If true, an error is printed if the command doesn't exist |
---|
[7282] | 668 | static inline const ConsoleCommand* getCommandLC(const std::string& name, bool bPrintError = false) |
---|
[7236] | 669 | { return ConsoleCommand::getCommandLC("", name, bPrintError); } |
---|
[7215] | 670 | |
---|
[7282] | 671 | static const ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false); |
---|
| 672 | static const ConsoleCommand* getCommandLC(const std::string& group, const std::string& name, bool bPrintError = false); |
---|
[7228] | 673 | |
---|
[7216] | 674 | static void destroyAll(); |
---|
| 675 | |
---|
[7215] | 676 | private: |
---|
[7236] | 677 | static std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandMap(); |
---|
| 678 | static std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandMapLC(); |
---|
[7228] | 679 | |
---|
[7236] | 680 | static void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command); |
---|
| 681 | static void unregisterCommand(ConsoleCommand* command); |
---|
[7179] | 682 | }; |
---|
| 683 | |
---|
[7401] | 684 | /** |
---|
| 685 | @brief Creates a new ConsoleCommand. |
---|
| 686 | @param name The name of the command |
---|
| 687 | @param executor The executor of the command |
---|
| 688 | @param bInitialized If true, the command is ready to be executed, otherwise it has to be activated first. |
---|
| 689 | */ |
---|
[7236] | 690 | inline ConsoleCommand* createConsoleCommand(const std::string& name, const ExecutorPtr& executor, bool bInitialized = true) |
---|
| 691 | { return new ConsoleCommand("", name, executor, bInitialized); } |
---|
[7401] | 692 | /** |
---|
| 693 | @brief Creates a new ConsoleCommand. |
---|
| 694 | @param group The group of the command |
---|
| 695 | @param name The name of the command |
---|
| 696 | @param executor The executor of the command |
---|
| 697 | @param bInitialized If true, the command is ready to be executed, otherwise it has to be activated first. |
---|
| 698 | */ |
---|
[7236] | 699 | inline ConsoleCommand* createConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true) |
---|
| 700 | { return new ConsoleCommand(group, name, executor, bInitialized); } |
---|
[7179] | 701 | |
---|
[7401] | 702 | |
---|
| 703 | /** |
---|
| 704 | @brief Returns a manipulator for a command with the given name. |
---|
| 705 | |
---|
| 706 | @note If the command doesn't exist, the manipulator contains a NULL pointer to the command, |
---|
| 707 | but it can still be used without checks, because all functions of ConsoleCommandManipulator |
---|
| 708 | check internally if the command exists. |
---|
| 709 | */ |
---|
[7236] | 710 | inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& name) |
---|
| 711 | { return ConsoleCommand::getCommand(name, true); } |
---|
[7401] | 712 | /** |
---|
| 713 | @brief Returns a manipulator for a command with the given group and name. |
---|
| 714 | |
---|
| 715 | @note If the command doesn't exist, the manipulator contains a NULL pointer to the command, |
---|
| 716 | but it can still be used without checks, because all functions of ConsoleCommandManipulator |
---|
| 717 | check internally if the command exists. |
---|
| 718 | */ |
---|
[7236] | 719 | inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& group, const std::string& name) |
---|
| 720 | { return ConsoleCommand::getCommand(group, name, true); } |
---|
[7179] | 721 | } |
---|
| 722 | |
---|
[1505] | 723 | #endif /* _ConsoleCommand_H__ */ |
---|