MyGUI  3.4.0
MyGUI_ControllerManager.cpp
Go to the documentation of this file.
1 /*
2  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3  * Distributed under the MIT License
4  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5  */
6 
7 #include "MyGUI_Precompiled.h"
8 #include "MyGUI_Gui.h"
10 #include "MyGUI_WidgetManager.h"
11 #include "MyGUI_FactoryManager.h"
12 
17 
18 namespace MyGUI
19 {
20 
21  template <> ControllerManager* Singleton<ControllerManager>::msInstance = nullptr;
22  template <> const char* Singleton<ControllerManager>::mClassTypeName = "ControllerManager";
23 
25  mIsInitialise(false),
26  mCategoryName("Controller")
27  {
28  }
29 
31  {
32  MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
33  MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
34 
36 
41 
42  MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
43  mIsInitialise = true;
44  }
45 
47  {
48  MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
49  MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
50 
55 
57  clear();
58 
59  MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
60  mIsInitialise = false;
61  }
62 
63  void ControllerManager::clear()
64  {
65  for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
66  {
67  delete (*iter).second;
68  }
69  mListItem.clear();
70  }
71 
72  ControllerItem* ControllerManager::createItem(const std::string& _type)
73  {
74  IObject* object = FactoryManager::getInstance().createObject(mCategoryName, _type);
75  return object == nullptr ? nullptr : object->castType<ControllerItem>();
76  }
77 
79  {
80  // подготавливаем
81  _item->prepareItem(_widget);
82 
83  for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
84  {
85  // такой уже в списке есть
86  if ((*iter).first == _widget)
87  {
88  if ((*iter).second->getTypeName() == _item->getTypeName())
89  {
90  delete (*iter).second;
91  (*iter).second = _item;
92  return;
93  }
94  }
95  }
96 
97  // если виджет первый, то подписываемся на кадры
98  if (mListItem.empty())
99  Gui::getInstance().eventFrameStart += newDelegate(this, &ControllerManager::frameEntered);
100 
101  // вставляем в самый конец
102  mListItem.push_back(PairControllerItem(_widget, _item));
103  }
104 
106  {
107  // не удаляем из списка, а обнуляем, в цикле он будет удален
108  for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
109  {
110  if ((*iter).first == _widget) (*iter).first = nullptr;
111  }
112  }
113 
114  void ControllerManager::_unlinkWidget(Widget* _widget)
115  {
116  removeItem(_widget);
117  }
118 
119  void ControllerManager::frameEntered(float _time)
120  {
121  for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); /*added in body*/)
122  {
123  if (nullptr == (*iter).first)
124  {
125  delete (*iter).second;
126  // удаляем из списка, итератор не увеличиваем и на новый круг
127  iter = mListItem.erase(iter);
128  continue;
129  }
130 
131  if ((*iter).second->addTime((*iter).first, _time))
132  {
133  ++iter;
134  continue;
135  }
136 
137  // на следующей итерации виджет вылетит из списка
138  (*iter).first = nullptr;
139  }
140 
141  if (mListItem.empty())
142  Gui::getInstance().eventFrameStart -= newDelegate(this, &ControllerManager::frameEntered);
143  }
144 
145  const std::string& ControllerManager::getCategoryName() const
146  {
147  return mCategoryName;
148  }
149 
150 } // namespace MyGUI
MyGUI::Singleton< WidgetManager >::getInstance
static WidgetManager & getInstance()
Definition: MyGUI_Singleton.h:44
MyGUI::ControllerManager::initialise
void initialise()
Definition: MyGUI_ControllerManager.cpp:30
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:150
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:46
MyGUI_Gui.h
MyGUI_ControllerManager.h
MyGUI::WidgetManager::registerUnlinker
void registerUnlinker(IUnlinkWidget *_unlink)
Definition: MyGUI_WidgetManager.cpp:133
MyGUI::ControllerManager::ControllerManager
ControllerManager()
Definition: MyGUI_ControllerManager.cpp:24
MyGUI::Singleton< ControllerManager >::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:55
MyGUI::FactoryManager::registerFactory
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
Definition: MyGUI_FactoryManager.cpp:40
MyGUI::Widget
Widget properties. Skin childs. Widget widget description should be here.
Definition: MyGUI_Widget.h:37
MyGUI::ControllerItem
Definition: MyGUI_ControllerItem.h:27
MyGUI_ControllerRepeatClick.h
MyGUI::ControllerPosition
Definition: MyGUI_ControllerPosition.h:22
MyGUI_ControllerEdgeHide.h
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition: MyGUI_Delegate.h:99
MyGUI_Precompiled.h
MyGUI_ControllerPosition.h
MyGUI::WidgetManager::unregisterUnlinker
void unregisterUnlinker(IUnlinkWidget *_unlink)
Definition: MyGUI_WidgetManager.cpp:139
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:34
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:18
MyGUI_WidgetManager.h
MyGUI_ControllerFadeAlpha.h
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:22
MyGUI::FactoryManager::createObject
IObject * createObject(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:72
MyGUI::IObject
Definition: MyGUI_IObject.h:17
MyGUI::ControllerManager::createItem
ControllerItem * createItem(const std::string &_type)
Definition: MyGUI_ControllerManager.cpp:72
MyGUI::ControllerManager::addItem
void addItem(Widget *_widget, ControllerItem *_item)
Definition: MyGUI_ControllerManager.cpp:78
MyGUI::ControllerEdgeHide
Definition: MyGUI_ControllerEdgeHide.h:26
MyGUI::ControllerFadeAlpha
Definition: MyGUI_ControllerFadeAlpha.h:20
MyGUI::ControllerManager::getCategoryName
const std::string & getCategoryName() const
Definition: MyGUI_ControllerManager.cpp:145
MyGUI::ControllerItem::getTypeName
virtual const std::string & getTypeName() const override
Definition: MyGUI_ControllerItem.h:28
MyGUI
Definition: MyGUI_ActionController.h:15
MyGUI::ControllerManager::removeItem
void removeItem(Widget *_widget)
Definition: MyGUI_ControllerManager.cpp:105
MyGUI::ControllerItem::prepareItem
virtual void prepareItem(Widget *_widget)=0
MyGUI::ControllerRepeatClick
Definition: MyGUI_ControllerRepeatClick.h:23
MyGUI_FactoryManager.h
MyGUI::ControllerManager::shutdown
void shutdown()
Definition: MyGUI_ControllerManager.cpp:46