MyGUI 3.4.3
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"
12
17
18namespace MyGUI
19{
20
22
24 mCategoryName("Controller"),
25 mSingletonHolder(this)
26 {
27 }
28
30 {
31 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
32 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
33
35
40
41 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
42 mIsInitialise = true;
43 }
44
46 {
47 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
48 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
49
54
56 clear();
57
58 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
59 mIsInitialise = false;
60 }
61
62 void ControllerManager::clear()
63 {
64 for (auto& iter : mListItem)
65 {
66 delete iter.second;
67 }
68 mListItem.clear();
69 }
70
72 {
73 IObject* object = FactoryManager::getInstance().createObject(mCategoryName, _type);
74 return object == nullptr ? nullptr : object->castType<ControllerItem>();
75 }
76
78 {
79 _item->prepareItem(_widget);
80
81 for (auto& iter : mListItem)
82 {
83 // already in the list
84 if (iter.first == _widget)
85 {
86 if (iter.second->getTypeName() == _item->getTypeName())
87 {
88 delete iter.second;
89 iter.second = _item;
90 return;
91 }
92 }
93 }
94
95 // subscribe to events only for first item
96 if (mListItem.empty())
97 Gui::getInstance().eventFrameStart += newDelegate(this, &ControllerManager::frameEntered);
98
99 mListItem.emplace_back(_widget, _item);
100 }
101
103 {
104 // replace with nullptr instead of removing, will be deleted in frameEntered
105 for (auto& iter : mListItem)
106 {
107 if (iter.first == _widget)
108 iter.first = nullptr;
109 }
110 }
111
112 void ControllerManager::_unlinkWidget(Widget* _widget)
113 {
114 removeItem(_widget);
115 }
116
117 void ControllerManager::frameEntered(float _time)
118 {
119 for (ListControllerItem::iterator iter = mListItem.begin(); iter != mListItem.end(); /*added in body*/)
120 {
121 if (nullptr == (*iter).first)
122 {
123 delete (*iter).second;
124 iter = mListItem.erase(iter);
125 continue;
126 }
127
128 if ((*iter).second->addTime((*iter).first, _time))
129 {
130 ++iter;
131 continue;
132 }
133
134 // will be removed in next iteration
135 (*iter).first = nullptr;
136 }
137
138 if (mListItem.empty())
139 Gui::getInstance().eventFrameStart -= newDelegate(this, &ControllerManager::frameEntered);
140 }
141
142 const std::string& ControllerManager::getCategoryName() const
143 {
144 return mCategoryName;
145 }
146
147} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_LOG(level, text)
#define MYGUI_SINGLETON_DEFINITION(ClassName)
virtual std::string_view getTypeName() const override
virtual void prepareItem(Widget *_widget)=0
ControllerItem * createItem(std::string_view _type)
static std::string_view getClassTypeName()
const std::string & getCategoryName() const
void addItem(Widget *_widget, ControllerItem *_item)
void registerFactory(std::string_view _category, std::string_view _type, Delegate::IDelegate *_delegate)
static FactoryManager & getInstance()
void unregisterFactory(std::string_view _category, std::string_view _type)
IObject * createObject(std::string_view _category, std::string_view _type)
static Gui & getInstance()
Definition MyGUI_Gui.cpp:34
EventHandle_FrameEventDelegate eventFrameStart
Definition MyGUI_Gui.h:215
Type * castType(bool _throw=true)
widget description should be here.
void unregisterUnlinker(IUnlinkWidget *_unlink)
static WidgetManager & getInstance()
void registerUnlinker(IUnlinkWidget *_unlink)
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))