Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 25, 2010, 5:35:37 PM (14 years ago)
Author:
landauf
Message:

new console command interface now supports all functions of the old implementation

Location:
code/branches/consolecommands3/src/libraries/core/command
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/consolecommands3/src/libraries/core/command/ConsoleCommand.cc

    r7214 r7215  
    128128    {
    129129        this->bActive_ = true;
     130        this->bHidden_ = false;
     131        this->accessLevel_ = AccessLevel::All;
     132
    130133        this->baseName_ = name;
    131134        this->baseExecutor_ = executor;
     135
     136        this->argumentCompleter_[0] = 0;
     137        this->argumentCompleter_[1] = 0;
     138        this->argumentCompleter_[2] = 0;
     139        this->argumentCompleter_[3] = 0;
     140        this->argumentCompleter_[4] = 0;
     141
     142        this->keybindMode_ = KeybindMode::OnPress;
     143        this->inputConfiguredParam_ = -1;
    132144
    133145        if (bInitialized)
     
    246258        command.executor_ = this->getExecutor();
    247259        if (command.executor_)
    248             command.functor_ = this->getFunctor();
     260            command.functor_ = this->getExecutor()->getFunctor();
    249261
    250262        if (this->setFunction(executor, bForce))
     
    257269        command.executor_ = this->getExecutor();
    258270        if (command.executor_)
    259             command.functor_ = this->getFunctor();
     271            command.functor_ = this->getExecutor()->getFunctor();
    260272
    261273        if (this->setFunction(functor, bForce))
     
    288300    {
    289301        return this->executor_;
    290     }
    291 
    292     const FunctorPtr& _ConsoleCommand::getFunctor() const
    293     {
    294         return this->executor_->getFunctor();
    295302    }
    296303
     
    339346    }
    340347
     348    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1)
     349    {
     350        if (this->executor_)
     351            this->executor_->setDefaultValues(param1);
     352        else
     353            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
     354
     355        return *this;
     356    }
     357
     358    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2)
     359    {
     360        if (this->executor_)
     361            this->executor_->setDefaultValues(param1, param2);
     362        else
     363            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
     364
     365        return *this;
     366    }
     367
     368    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
     369    {
     370        if (this->executor_)
     371            this->executor_->setDefaultValues(param1, param2, param3);
     372        else
     373            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
     374
     375        return *this;
     376    }
     377
     378    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
     379    {
     380        if (this->executor_)
     381            this->executor_->setDefaultValues(param1, param2, param3, param4);
     382        else
     383            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
     384
     385        return *this;
     386    }
     387
     388    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
     389    {
     390        if (this->executor_)
     391            this->executor_->setDefaultValues(param1, param2, param3, param4, param5);
     392        else
     393            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
     394
     395        return *this;
     396    }
     397
     398    _ConsoleCommand& _ConsoleCommand::defaultValue(unsigned int index, const MultiType& param)
     399    {
     400        if (this->executor_)
     401            this->executor_->setDefaultValue(index, param);
     402        else
     403            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
     404
     405        return *this;
     406    }
     407
     408    _ConsoleCommand& _ConsoleCommand::argumentCompleter(unsigned int param, ArgumentCompleter* completer)
     409    {
     410        if (param < 5)
     411            this->argumentCompleter_[param] = completer;
     412        else
     413            COUT(2) << "Warning: Couldn't add autocompletion-function for param " << param << " in console command \"" << this->baseName_ << "\": index out of bound." << std::endl;
     414
     415        return *this;
     416    }
     417
     418    ArgumentCompleter* _ConsoleCommand::getArgumentCompleter(unsigned int param) const
     419    {
     420        if (param < 5)
     421            return this->argumentCompleter_[param];
     422        else
     423            return 0;
     424    }
     425
     426    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)
     427    {
     428        if (param < 5 && this->argumentCompleter_[param])
     429            this->argumentList_ = (*this->argumentCompleter_[param])(param1, param2, param3, param4, param5);
     430        else
     431            this->argumentList_.clear();
     432    }
     433
     434    _ConsoleCommand& _ConsoleCommand::description(const std::string& description)
     435    {
     436        this->description_ = std::string("ConsoleCommandDescription::" + this->baseName_ + "::function");
     437        AddLanguageEntry(this->description_, description);
     438        return *this;
     439    }
     440
     441    const std::string& _ConsoleCommand::getDescription() const
     442    {
     443        return GetLocalisation_noerror(this->description_);
     444    }
     445
     446    _ConsoleCommand& _ConsoleCommand::descriptionParam(unsigned int param, const std::string& description)
     447    {
     448        if (param < MAX_FUNCTOR_ARGUMENTS)
     449        {
     450            this->descriptionParam_[param] = std::string("ConsoleCommandDescription::" + this->baseName_ + "::param" + multi_cast<std::string>(param));
     451            AddLanguageEntry(this->descriptionParam_[param], description);
     452        }
     453        return *this;
     454    }
     455
     456    const std::string& _ConsoleCommand::getDescriptionParam(unsigned int param) const
     457    {
     458        if (param < MAX_FUNCTOR_ARGUMENTS)
     459            return GetLocalisation_noerror(this->descriptionParam_[param]);
     460
     461        return this->descriptionParam_[0];
     462    }
     463
     464    _ConsoleCommand& _ConsoleCommand::descriptionReturnvalue(const std::string& description)
     465    {
     466        this->descriptionReturnvalue_ = std::string("ConsoleCommandDescription::" + this->baseName_ + "::returnvalue");
     467        AddLanguageEntry(this->descriptionReturnvalue_, description);
     468        return *this;
     469    }
     470
     471    const std::string& _ConsoleCommand::getDescriptionReturnvalue(int param) const
     472    {
     473        return GetLocalisation_noerror(this->descriptionReturnvalue_);
     474    }
     475
    341476    /* static */ const _ConsoleCommand* _ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError)
    342477    {
  • code/branches/consolecommands3/src/libraries/core/command/ConsoleCommand.h

    r7214 r7215  
    192192
    193193
    194 #define _ModifyConsoleCommand(...) \
    195     orxonox::_ConsoleCommand::getCommand(__VA_ARGS__, true)->getManipulator()
    196 
    197 
    198194namespace orxonox
    199195{
     
    208204        };
    209205
     206        struct AccessLevel
     207        {
     208            enum Enum
     209            {
     210                All,
     211                Standalone,
     212                Master,
     213                Server,
     214                Client,
     215                Online,
     216                Offline,
     217                None
     218            };
     219        };
     220
    210221        public:
    211222            struct _ConsoleCommandManipulator
     
    219230                            if (this->command_)
    220231                            {
    221                                 if (this->command_->getExecutor() && this->command_->getFunctor() && this->command_->getFunctor()->getFullIdentifier() == typeid(F))
     232                                if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F))
    222233                                {
    223                                     FunctorPointer<F>* functor = static_cast<FunctorPointer<F>*>(this->command_->getFunctor().get());
     234                                    FunctorPointer<F>* functor = static_cast<FunctorPointer<F>*>(this->command_->getExecutor()->getFunctor().get());
    224235                                    functor->setFunction(function);
    225236                                    return *this;
     
    234245                            if (this->command_)
    235246                            {
    236                                 if (this->command_->getExecutor() && this->command_->getFunctor() && this->command_->getFunctor()->getFullIdentifier() == typeid(F))
     247                                if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F))
    237248                                {
    238                                     FunctorPointer<F, O>* functor = static_cast<FunctorPointer<F, O>*>(this->command_->getFunctor().get());
     249                                    FunctorPointer<F, O>* functor = static_cast<FunctorPointer<F, O>*>(this->command_->getExecutor()->getFunctor().get());
    239250                                    functor->setFunction(function);
    240251                                    functor->setObject(object);
     
    271282                        { if (this->command_) { this->command_->popObject(); } return *this; }
    272283
    273                     inline void* getObject() const
    274                         { if (this->command_) { return this->command_->getObject(); } else { return 0; } }
    275 
    276284                    inline _ConsoleCommandManipulator& setActive(bool bActive)
    277285                        { if (this->command_) { this->command_->setActive(bActive); } return *this; }
     
    281289                        { return this->setActive(false); }
    282290
    283                     inline bool isActive() const
    284                         { return this->command_ ? this->command_->isActive() : false; }
    285                     inline bool exists() const
    286                         { return (this->command_ != 0); }
     291                    inline _ConsoleCommandManipulator& setHidden(bool bHidden)
     292                        { if (this->command_) { this->command_->setHidden(bHidden); } return *this; }
     293                    inline _ConsoleCommandManipulator& hide()
     294                        { return this->setHidden(true); }
     295                    inline _ConsoleCommandManipulator& show()
     296                        { return this->setHidden(false); }
     297
     298                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1)
     299                        { if (this->command_) { this->command_->defaultValues(param1); } return *this; }
     300                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2)
     301                        { if (this->command_) { this->command_->defaultValues(param1, param2); } return *this; }
     302                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
     303                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3); } return *this; }
     304                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
     305                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3, param4); } return *this; }
     306                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
     307                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3, param4, param5); } return *this; }
     308                    inline _ConsoleCommandManipulator& defaultValue(unsigned int index, const MultiType& param)
     309                        { if (this->command_) { this->command_->defaultValue(index, param); } return *this; }
     310
     311                    inline _ConsoleCommandManipulator& accessLevel(_ConsoleCommand::AccessLevel::Enum level)
     312                        { if (this->command_) { this->command_->accessLevel(level); } return *this; }
     313
     314                    inline _ConsoleCommandManipulator& argumentCompleter(unsigned int param, ArgumentCompleter* completer)
     315                        { if (this->command_) { this->command_->argumentCompleter(param, completer); } return *this; }
     316
     317                    inline _ConsoleCommandManipulator& setAsInputCommand()
     318                        { if (this->command_) { this->command_->setAsInputCommand(); } return *this; }
     319                    inline _ConsoleCommandManipulator& keybindMode(KeybindMode::Value mode)
     320                        { if (this->command_) { this->command_->keybindMode(mode); } return *this; }
     321                    inline _ConsoleCommandManipulator& inputConfiguredParam(int index)
     322                        { if (this->command_) { this->command_->inputConfiguredParam(index); } return *this; }
    287323
    288324                private:
     
    299335            _ConsoleCommand& addGroup(const std::string& group, const std::string&  name);
    300336
    301             inline void setActive(bool bActive)
    302                 { this->bActive_ = bActive; }
    303             inline void activate()
    304                 { this->setActive(true); }
    305             inline void deactivate()
    306                 { this->setActive(false); }
     337            inline _ConsoleCommand setActive(bool bActive)
     338                { this->bActive_ = bActive; return *this; }
     339            inline _ConsoleCommand activate()
     340                { return this->setActive(true); }
     341            inline _ConsoleCommand deactivate()
     342                { return this->setActive(false); }
     343
     344            inline _ConsoleCommand& setHidden(bool bHidden)
     345                { this->bHidden_ = bHidden; return *this; }
     346            inline _ConsoleCommand& hide()
     347                { return this->setHidden(true); }
     348            inline _ConsoleCommand& show()
     349                { return this->setHidden(false); }
     350
    307351            bool isActive() const;
     352            bool hasAccess() const;
     353            bool isHidden() const;
     354
     355            _ConsoleCommand& description(const std::string& description);
     356            const std::string& getDescription() const;
     357
     358            _ConsoleCommand& descriptionParam(unsigned int param, const std::string& description);
     359            const std::string& getDescriptionParam(unsigned int param) const;
     360
     361            _ConsoleCommand& descriptionReturnvalue(const std::string& description);
     362            const std::string& getDescriptionReturnvalue(int param) const;
     363
     364            _ConsoleCommand& defaultValues(const MultiType& param1);
     365            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2);
     366            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3);
     367            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4);
     368            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5);
     369            _ConsoleCommand& defaultValue(unsigned int index, const MultiType& param);
     370
     371            inline _ConsoleCommand& accessLevel(AccessLevel::Enum level)
     372                { this->accessLevel_ = level; return *this; }
     373            inline AccessLevel::Enum getAccessLevel() const
     374                { return this->accessLevel_; }
     375
     376            _ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer);
     377            ArgumentCompleter* getArgumentCompleter(unsigned int param) const;
     378
     379            void createArgumentCompletionList(unsigned int param, const std::string& param1 = "", const std::string& param2 = "", const std::string& param3 = "", const std::string& param4 = "", const std::string& param5 = "");
     380            const ArgumentCompletionList& getArgumentCompletionList() const
     381                { return this->argumentList_; }
     382            ArgumentCompletionList::const_iterator getArgumentCompletionListBegin() const
     383                { return this->argumentList_.begin(); }
     384            ArgumentCompletionList::const_iterator getArgumentCompletionListEnd() const
     385                { return this->argumentList_.end(); }
     386
     387            inline _ConsoleCommand& setAsInputCommand()
     388            {
     389                this->keybindMode(KeybindMode::OnHold);
     390                this->defaultValue(0, Vector2(0.0f, 0.0f));
     391                this->inputConfiguredParam(0);
     392                return *this;
     393            }
     394
     395            inline _ConsoleCommand& keybindMode(KeybindMode::Value mode)
     396                { this->keybindMode_ = mode; return *this; }
     397            inline KeybindMode::Value getKeybindMode() const
     398                { return this->keybindMode_; }
     399
     400            inline _ConsoleCommand& inputConfiguredParam(int index)
     401                { this->inputConfiguredParam_ = index; return *this; }
     402            inline int getInputConfiguredParam_() const
     403                { return this->inputConfiguredParam_; }
    308404
    309405            inline _ConsoleCommandManipulator getManipulator() const
    310406                { return this; }
    311407
    312             static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands()
    313                 { return _ConsoleCommand::getCommandMap(); }
    314 
    315             static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
    316                 { return _ConsoleCommand::getCommand("", name, bPrintError); }
    317             static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
    318 
    319408        private:
    320             static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
    321             static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command);
    322             static void unregisterCommand(_ConsoleCommand* command);
    323 
    324409            bool headersMatch(const FunctorPtr& functor);
    325410            bool headersMatch(const ExecutorPtr& executor);
     
    340425
    341426            bool bActive_;
    342 //            const std::type_info& functionHeader_;
     427            bool bHidden_;
     428            AccessLevel::Enum accessLevel_;
    343429            std::string baseName_;
    344430            ExecutorPtr baseExecutor_;
     
    347433            std::stack<Command> commandStack_;
    348434            std::stack<void*> objectStack_;
     435
     436            ArgumentCompleter* argumentCompleter_[5];
     437            ArgumentCompletionList argumentList_;
     438
     439            KeybindMode::Value keybindMode_;
     440            int inputConfiguredParam_;
     441
     442            LanguageEntryLabel description_;
     443            LanguageEntryLabel descriptionReturnvalue_;
     444            LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS];
     445
     446        public:
     447            static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands()
     448                { return _ConsoleCommand::getCommandMap(); }
     449
     450            static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
     451                { return _ConsoleCommand::getCommand("", name, bPrintError); }
     452            static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
     453
     454        private:
     455            static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
     456            static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command);
     457            static void unregisterCommand(_ConsoleCommand* command);
    349458    };
    350459
    351460    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, const ExecutorPtr& executor, bool bInitialized = true)
    352     {
    353         return new _ConsoleCommand("", name, executor, bInitialized);
    354     }
    355 
     461        { return new _ConsoleCommand("", name, executor, bInitialized); }
    356462    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true)
    357     {
    358         return new _ConsoleCommand(group, name, executor, bInitialized);
    359     }
     463        { return new _ConsoleCommand(group, name, executor, bInitialized); }
     464
     465    inline _ConsoleCommand::_ConsoleCommandManipulator _ModifyConsoleCommand(const std::string& name)
     466        { return _ConsoleCommand::getCommand(name, true); }
     467    inline _ConsoleCommand::_ConsoleCommandManipulator _ModifyConsoleCommand(const std::string& group, const std::string& name)
     468        { return _ConsoleCommand::getCommand(group, name, true); }
    360469}
    361470
Note: See TracChangeset for help on using the changeset viewer.