MyGUI: MyGUI_ControllerManager.cpp Source File

MyGUI 3.2.2

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"
9 #include "MyGUI_ControllerManager.h"
10 #include "MyGUI_WidgetManager.h"
11 #include "MyGUI_FactoryManager.h"
12
13 #include "MyGUI_ControllerEdgeHide.h"
14 #include "MyGUI_ControllerFadeAlpha.h"
15 #include "MyGUI_ControllerPosition.h"
16 #include "MyGUI_ControllerRepeatClick.h"
17
18 namespace MyGUI
19 {
20
21 template <> ControllerManager* Singleton<ControllerManager>::msInstance = nullptr;
22 template <> const char* Singleton<ControllerManager>::mClassTypeName = "ControllerManager";
23
24 ControllerManager::ControllerManager() :
25 mIsInitialise(false),
26 mCategoryName("Controller")
27 {
28 }
29
30 void ControllerManager::initialise()
31 {
32 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
33 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
34
35 WidgetManager::getInstance().registerUnlinker(this);
36
37 FactoryManager::getInstance().registerFactory<ControllerEdgeHide>(mCategoryName);
38 FactoryManager::getInstance().registerFactory<ControllerFadeAlpha>(mCategoryName);
39 FactoryManager::getInstance().registerFactory<ControllerPosition>(mCategoryName);
40 FactoryManager::getInstance().registerFactory<ControllerRepeatClick>(mCategoryName);
41
42 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
43 mIsInitialise = true;
44 }
45
46 void ControllerManager::shutdown()
47 {
48 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
49 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
50
51 FactoryManager::getInstance().unregisterFactory<ControllerEdgeHide>(mCategoryName);
52 FactoryManager::getInstance().unregisterFactory<ControllerFadeAlpha>(mCategoryName);
53 FactoryManager::getInstance().unregisterFactory<ControllerPosition>(mCategoryName);
54 FactoryManager::getInstance().unregisterFactory<ControllerRepeatClick>(mCategoryName);
55
56 WidgetManager::getInstance().unregisterUnlinker(this);
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
78 void ControllerManager::addItem(Widget* _widget, ControllerItem* _item)
79 {
80 // если виджет первый, то подписываемся на кадры
81 if (mListItem.empty())
82 Gui::getInstance().eventFrameStart += newDelegate(this, &ControllerManager::frameEntered);
83
84 // подготавливаем
85 _item->prepareItem(_widget);
86
87 for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); ++iter)
88 {
89 // такой уже в списке есть
90 if ((*iter).first == _widget)
91 {
92 if ((*iter).second->getTypeName() == _item->getTypeName())
93 {
94 delete (*iter).second;
95 (*iter).second = _item;
96 return;
97 }
98 }
99 }
100
101 // вставляем в самый конец
102 mListItem.push_back(PairControllerItem(_widget, _item));
103 }
104
105 void ControllerManager::removeItem(Widget* _widget)
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:38
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::ControllerItem::getTypeName
virtual const std::string & getTypeName() const
Definition: MyGUI_ControllerItem.h:28
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:49
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:29
MyGUI::ControllerItem
Definition: MyGUI_ControllerItem.h:25
MyGUI_ControllerRepeatClick.h
MyGUI::ControllerPosition
Definition: MyGUI_ControllerPosition.h:20
MyGUI_ControllerEdgeHide.h
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:42
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
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI::IObject
Definition: MyGUI_IObject.h:16
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:24
MyGUI::ControllerFadeAlpha
Definition: MyGUI_ControllerFadeAlpha.h:18
MyGUI::ControllerManager::getCategoryName
const std::string & getCategoryName() const
Definition: MyGUI_ControllerManager.cpp:145
MyGUI
Definition: MyGUI_ActionController.h:14
newDelegate
MYGUI_TEMPLATE MYGUI_TEMPLATE_PARAMS delegates::IDelegateMYGUI_SUFFIX MYGUI_TEMPLATE_ARGS * newDelegate(void(*_func)(MYGUI_PARAMS))
Definition: MyGUI_DelegateImplement.h:117
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:21
MyGUI_FactoryManager.h
MyGUI::ControllerManager::shutdown
void shutdown()
Definition: MyGUI_ControllerManager.cpp:46