#include "RUState.h" #include "RUDelegateFactory.h" NS_RU_BEGIN State* State::create(const std::string& stateName, const std::vector& fullStatePath, kStateType stateType, const std::vector& entryActInfos, const std::vector& idleActInfos, const std::vector& exitActInfos, const std::string & stateDelegateKey, void* userObj4StateDelegate) { State * ret = new (std::nothrow) State(); ret->_initData(stateName, fullStatePath, stateType, entryActInfos, idleActInfos, exitActInfos, stateDelegateKey, userObj4StateDelegate); return ret; } void State::_initData(const std::string& stateName, const std::vector& fullStatePath, kStateType stateType, const std::vector& entryActInfos, const std::vector& idleActInfos, const std::vector& exitActInfos, const std::string& stateDelegateKey, void* userObj4StateDelegate) { _stateName = stateName; _fullStatePath = fullStatePath; _stateType = stateType; _entryActInfos = entryActInfos; _idleActInfos = idleActInfos; _exitActInfos = exitActInfos; _stateDelegateKey = stateDelegateKey; _initStateDelegate(stateDelegateKey, userObj4StateDelegate); } void State::_initStateDelegate(const std::string& stateDelegateKey, void* userObj4StateDelegate) { _stateDelegate = DelegateFactory::getInstance()->createStateDelegate(stateDelegateKey); _stateDelegate->setUserObject(userObj4StateDelegate); } const std::string& State::getStateName() { return _stateName; } const std::vector& State::getFullStatePath() { return _fullStatePath; } std::vector State::getBindingStatePath4Blocks() { return _fullStatePath; } kStateType State::getStateType() { return _stateType; } void State::entry(const std::function& entryCb) { FSMLOG("%s进入状态: %s ", _indent.c_str(), _getFullStatePathStr4Debug().c_str()); auto execIdle = [=](void){ idle(entryCb); }; if (_stateDelegate) { _stateDelegate->entry(this, _entryActInfos, execIdle); } else { execIdle(); } } void State::idle(const std::function& idleCb) { FSMLOG("%s执行状态: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str()); if (_stateDelegate) { _stateDelegate->idle(this, _idleActInfos, idleCb); } else if (idleCb) { idleCb(); } } void State::exit(const std::function& exitCb) { FSMLOG("%s退出状态:%s", _indent.c_str(), _getFullStatePathStr4Debug().c_str()); if (_stateDelegate) { _stateDelegate->exit(this, _exitActInfos, exitCb); } else if (exitCb) { exitCb(); } } void State::beforeSwitch(std::vector actInfos, StateActInfo evt, std::function actsFinishCb) { if (_stateDelegate) { _stateDelegate->beforeSwitch(this, actInfos, evt, actsFinishCb); } else if (actsFinishCb) { actsFinishCb(); } } void State::afterSwitch(std::vector actInfos, StateActInfo evt, std::function actsFinishCb) { if (_stateDelegate) { _stateDelegate->afterSwitch(this, actInfos, evt, actsFinishCb); } else if (actsFinishCb) { actsFinishCb(); } } const std::vector& State::getIdleActInfos() { return _idleActInfos; } void State::setIndent4Dbg(const std::string& indent) { _indent = indent; } void State::debugInfo() { CCLOG("dump \"%s\":{",_stateName.c_str()); auto getActionsInfo = [=](std::vector actinfos){ std::string actions_str = "["; for(StateActInfo action : actinfos){ actions_str = actions_str + "{\"type\":\""+ action.actType + "\",\"params\":{"; for(auto paramIter : action.actParams){ actions_str = actions_str + "\""+ paramIter.first + "\":\"" + paramIter.second + "\","; } actions_str = actions_str + "}"; } actions_str = actions_str + "]"; return actions_str; }; std::string stateTypeStr = "普通状态"; if (_stateType == kStateType::META) { stateTypeStr = "分层状态"; } CCLOG(" \"stateType\":\"%s\",",stateTypeStr.c_str()); CCLOG(" \"entry\":%s,",getActionsInfo(_entryActInfos).c_str()); CCLOG(" \"idle\":%s,",getActionsInfo(_idleActInfos).c_str()); CCLOG(" \"exit\":%s,",getActionsInfo(_exitActInfos).c_str()); CCLOG(" \"stateDelegateKey\":\"%s\",",_stateDelegateKey.c_str()); CCLOG(" },"); } std::string State::_getFullStatePathStr4Debug() { std::string str; for (int i = 0; i < _fullStatePath.size(); i++) { if (i == _fullStatePath.size() - 1) { str = str + _fullStatePath[i]; }else{ str = str + _fullStatePath[i] + "#"; } } return str; } NS_RU_END