Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 20, 2010, 2:21:10 PM (14 years ago)
Author:
landauf
Message:

merged current state of the new cc system to the updated branch

File:
1 edited

Legend:

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

    r5781 r6452  
    2828
    2929#include "ConsoleCommand.h"
     30#include <cassert>
    3031
    3132namespace orxonox
     
    7172    }
    7273}
     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->getHeaderIndentifier())
     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 TracChangeset for help on using the changeset viewer.