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