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