MyGUI  3.2.0
MyGUI_ControllerManager.cpp
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_Gui.h"
25 #include "MyGUI_WidgetManager.h"
26 #include "MyGUI_FactoryManager.h"
27 
31 
32 namespace MyGUI
33 {
34 
35  template <> ControllerManager* Singleton<ControllerManager>::msInstance = nullptr;
36  template <> const char* Singleton<ControllerManager>::mClassTypeName("ControllerManager");
37 
39  mIsInitialise(false)
40  {
41  }
42 
44  {
45  MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
46  MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
47 
49 
50  const std::string factory_type = "Controller";
51 
55 
56  MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
57  mIsInitialise = true;
58  }
59 
61  {
62  MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
63  MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
64 
65  const std::string factory_type = "Controller";
66 
70 
72  clear();
73 
74  MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
75  mIsInitialise = false;
76  }
77 
78  void ControllerManager::clear()
79  {
80  for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
81  {
82  delete (*iter).second;
83  }
84  mListItem.clear();
85  }
86 
87  ControllerItem* ControllerManager::createItem(const std::string& _type)
88  {
89  IObject* object = FactoryManager::getInstance().createObject("Controller", _type);
90  return object == nullptr ? nullptr : object->castType<ControllerItem>();
91  }
92 
94  {
95  // если виджет первый, то подписываемся на кадры
96  if (mListItem.empty())
97  Gui::getInstance().eventFrameStart += newDelegate(this, &ControllerManager::frameEntered);
98 
99  // подготавливаем
100  _item->prepareItem(_widget);
101 
102  for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
103  {
104  // такой уже в списке есть
105  if ((*iter).first == _widget)
106  {
107  if ((*iter).second->getTypeName() == _item->getTypeName())
108  {
109  delete (*iter).second;
110  (*iter).second = _item;
111  return;
112  }
113  }
114  }
115 
116  // вставляем в самый конец
117  mListItem.push_back(PairControllerItem(_widget, _item));
118  }
119 
121  {
122  // не удаляем из списка, а обнуляем, в цикле он будет удален
123  for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
124  {
125  if ((*iter).first == _widget) (*iter).first = nullptr;
126  }
127  }
128 
129  void ControllerManager::_unlinkWidget(Widget* _widget)
130  {
131  removeItem(_widget);
132  }
133 
134  void ControllerManager::frameEntered(float _time)
135  {
136  for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); /*added in body*/)
137  {
138  if (nullptr == (*iter).first)
139  {
140  delete (*iter).second;
141  // удаляем из списка, итератор не увеличиваем и на новый круг
142  iter = mListItem.erase(iter);
143  continue;
144  }
145 
146  if ((*iter).second->addTime((*iter).first, _time))
147  {
148  ++iter;
149  continue;
150  }
151 
152  // на следующей итерации виджет вылетит из списка
153  (*iter).first = nullptr;
154  }
155 
156  if (mListItem.empty())
157  Gui::getInstance().eventFrameStart -= newDelegate(this, &ControllerManager::frameEntered);
158  }
159 
160 } // namespace MyGUI
void addItem(Widget *_widget, ControllerItem *_item)
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:166
virtual void prepareItem(Widget *_widget)=0
delegates::IDelegate0 * newDelegate(void(*_func)())
static WidgetManager & getInstance()
static const char * getClassTypeName()
#define MYGUI_LOG(level, text)
#define MYGUI_ASSERT(exp, dest)
virtual const std::string & getTypeName() const
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:33
void registerUnlinker(IUnlinkWidget *_unlink)
void unregisterFactory(const std::string &_category, const std::string &_type)
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
IObject * createObject(const std::string &_category, const std::string &_type)
void unregisterUnlinker(IUnlinkWidget *_unlink)
ControllerItem * createItem(const std::string &_type)