MyGUI  3.2.0
MyGUI_ControllerManager.cpp
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #include "MyGUI_Precompiled.h"
00023 #include "MyGUI_Gui.h"
00024 #include "MyGUI_ControllerManager.h"
00025 #include "MyGUI_WidgetManager.h"
00026 #include "MyGUI_FactoryManager.h"
00027 
00028 #include "MyGUI_ControllerEdgeHide.h"
00029 #include "MyGUI_ControllerFadeAlpha.h"
00030 #include "MyGUI_ControllerPosition.h"
00031 
00032 namespace MyGUI
00033 {
00034 
00035     template <> ControllerManager* Singleton<ControllerManager>::msInstance = nullptr;
00036     template <> const char* Singleton<ControllerManager>::mClassTypeName("ControllerManager");
00037 
00038     ControllerManager::ControllerManager() :
00039         mIsInitialise(false)
00040     {
00041     }
00042 
00043     void ControllerManager::initialise()
00044     {
00045         MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
00046         MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
00047 
00048         WidgetManager::getInstance().registerUnlinker(this);
00049 
00050         const std::string factory_type = "Controller";
00051 
00052         FactoryManager::getInstance().registerFactory<ControllerEdgeHide>(factory_type);
00053         FactoryManager::getInstance().registerFactory<ControllerFadeAlpha>(factory_type);
00054         FactoryManager::getInstance().registerFactory<ControllerPosition>(factory_type);
00055 
00056         MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
00057         mIsInitialise = true;
00058     }
00059 
00060     void ControllerManager::shutdown()
00061     {
00062         MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
00063         MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
00064 
00065         const std::string factory_type = "Controller";
00066 
00067         FactoryManager::getInstance().unregisterFactory<ControllerEdgeHide>(factory_type);
00068         FactoryManager::getInstance().unregisterFactory<ControllerFadeAlpha>(factory_type);
00069         FactoryManager::getInstance().unregisterFactory<ControllerPosition>(factory_type);
00070 
00071         WidgetManager::getInstance().unregisterUnlinker(this);
00072         clear();
00073 
00074         MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
00075         mIsInitialise = false;
00076     }
00077 
00078     void ControllerManager::clear()
00079     {
00080         for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
00081         {
00082             delete (*iter).second;
00083         }
00084         mListItem.clear();
00085     }
00086 
00087     ControllerItem* ControllerManager::createItem(const std::string& _type)
00088     {
00089         IObject* object = FactoryManager::getInstance().createObject("Controller", _type);
00090         return object == nullptr ? nullptr : object->castType<ControllerItem>();
00091     }
00092 
00093     void ControllerManager::addItem(Widget* _widget, ControllerItem* _item)
00094     {
00095         // если виджет первый, то подписываемся на кадры
00096         if (mListItem.empty())
00097             Gui::getInstance().eventFrameStart += newDelegate(this, &ControllerManager::frameEntered);
00098 
00099         // подготавливаем
00100         _item->prepareItem(_widget);
00101 
00102         for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
00103         {
00104             // такой уже в списке есть
00105             if ((*iter).first == _widget)
00106             {
00107                 if ((*iter).second->getTypeName() == _item->getTypeName())
00108                 {
00109                     delete (*iter).second;
00110                     (*iter).second = _item;
00111                     return;
00112                 }
00113             }
00114         }
00115 
00116         // вставляем в самый конец
00117         mListItem.push_back(PairControllerItem(_widget, _item));
00118     }
00119 
00120     void ControllerManager::removeItem(Widget* _widget)
00121     {
00122         // не удаляем из списка, а обнуляем, в цикле он будет удален
00123         for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
00124         {
00125             if ((*iter).first == _widget) (*iter).first = nullptr;
00126         }
00127     }
00128 
00129     void ControllerManager::_unlinkWidget(Widget* _widget)
00130     {
00131         removeItem(_widget);
00132     }
00133 
00134     void ControllerManager::frameEntered(float _time)
00135     {
00136         for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); /*added in body*/)
00137         {
00138             if (nullptr == (*iter).first)
00139             {
00140                 delete (*iter).second;
00141                 // удаляем из списка, итератор не увеличиваем и на новый круг
00142                 iter = mListItem.erase(iter);
00143                 continue;
00144             }
00145 
00146             if ((*iter).second->addTime((*iter).first, _time))
00147             {
00148                 ++iter;
00149                 continue;
00150             }
00151 
00152             // на следующей итерации виджет вылетит из списка
00153             (*iter).first = nullptr;
00154         }
00155 
00156         if (mListItem.empty())
00157             Gui::getInstance().eventFrameStart -= newDelegate(this, &ControllerManager::frameEntered);
00158     }
00159 
00160 } // namespace MyGUI