MyGUI: MyGUI_ControllerManager.cpp Source File

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"
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
18namespace MyGUI
19{
20
21 MYGUI_SINGLETON_DEFINITION(ControllerManager);
22
23 ControllerManager::ControllerManager() :
24 mCategoryName("Controller"),
25 mSingletonHolder(this)
26 {
27 }
28
29 void ControllerManager::initialise()
30 {
31 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
32 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
33
34 WidgetManager::getInstance().registerUnlinker(this);
35
36 FactoryManager::getInstance().registerFactory<ControllerEdgeHide>(mCategoryName);
37 FactoryManager::getInstance().registerFactory<ControllerFadeAlpha>(mCategoryName);
38 FactoryManager::getInstance().registerFactory<ControllerPosition>(mCategoryName);
39 FactoryManager::getInstance().registerFactory<ControllerRepeatClick>(mCategoryName);
40
41 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
42 mIsInitialise = true;
43 }
44
45 void ControllerManager::shutdown()
46 {
47 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
48 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
49
50 FactoryManager::getInstance().unregisterFactory<ControllerEdgeHide>(mCategoryName);
51 FactoryManager::getInstance().unregisterFactory<ControllerFadeAlpha>(mCategoryName);
52 FactoryManager::getInstance().unregisterFactory<ControllerPosition>(mCategoryName);
53 FactoryManager::getInstance().unregisterFactory<ControllerRepeatClick>(mCategoryName);
54
55 WidgetManager::getInstance().unregisterUnlinker(this);
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
71 ControllerItem* ControllerManager::createItem(std::string_view _type)
72 {
73 IObject* object = FactoryManager::getInstance().createObject(mCategoryName, _type);
74 return object == nullptr ? nullptr : object->castType<ControllerItem>();
75 }
76
77 void ControllerManager::addItem(Widget* _widget, ControllerItem* _item)
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
102 void ControllerManager::removeItem(Widget* _widget)
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
MyGUI_ControllerEdgeHide.h
MyGUI_ControllerFadeAlpha.h
MyGUI_ControllerManager.h
MyGUI_ControllerPosition.h
MyGUI_ControllerRepeatClick.h
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:31
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_FactoryManager.h
MyGUI_Gui.h
MyGUI_Precompiled.h
MYGUI_SINGLETON_DEFINITION
#define MYGUI_SINGLETON_DEFINITION(ClassName)
Definition MyGUI_Singleton.h:126
MyGUI_WidgetManager.h
MyGUI::ControllerEdgeHide
Definition MyGUI_ControllerEdgeHide.h:25
MyGUI::ControllerFadeAlpha
Definition MyGUI_ControllerFadeAlpha.h:19
MyGUI::ControllerItem
Definition MyGUI_ControllerItem.h:26
MyGUI::ControllerItem::getTypeName
virtual std::string_view getTypeName() const override
Definition MyGUI_ControllerItem.h:27
MyGUI::ControllerItem::prepareItem
virtual void prepareItem(Widget *_widget)=0
MyGUI::ControllerManager
Definition MyGUI_ControllerManager.h:21
MyGUI::ControllerManager::createItem
ControllerItem * createItem(std::string_view _type)
Definition MyGUI_ControllerManager.cpp:71
MyGUI::ControllerManager::shutdown
void shutdown()
Definition MyGUI_ControllerManager.cpp:45
MyGUI::ControllerManager::ControllerManager
ControllerManager()
Definition MyGUI_ControllerManager.cpp:23
MyGUI::ControllerManager::getClassTypeName
static std::string_view getClassTypeName()
MyGUI::ControllerManager::getCategoryName
const std::string & getCategoryName() const
Definition MyGUI_ControllerManager.cpp:142
MyGUI::ControllerManager::removeItem
void removeItem(Widget *_widget)
Definition MyGUI_ControllerManager.cpp:102
MyGUI::ControllerManager::addItem
void addItem(Widget *_widget, ControllerItem *_item)
Definition MyGUI_ControllerManager.cpp:77
MyGUI::ControllerManager::initialise
void initialise()
Definition MyGUI_ControllerManager.cpp:29
MyGUI::ControllerPosition
Definition MyGUI_ControllerPosition.h:21
MyGUI::ControllerRepeatClick
Definition MyGUI_ControllerRepeatClick.h:22
MyGUI::FactoryManager::registerFactory
void registerFactory(std::string_view _category, std::string_view _type, Delegate::IDelegate *_delegate)
Definition MyGUI_FactoryManager.cpp:39
MyGUI::FactoryManager::getInstance
static FactoryManager & getInstance()
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(std::string_view _category, std::string_view _type)
Definition MyGUI_FactoryManager.cpp:54
MyGUI::FactoryManager::createObject
IObject * createObject(std::string_view _category, std::string_view _type)
Definition MyGUI_FactoryManager.cpp:80
MyGUI::Gui::getInstance
static Gui & getInstance()
Definition MyGUI_Gui.cpp:34
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition MyGUI_Gui.h:215
MyGUI::IObject
Definition MyGUI_IObject.h:17
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition MyGUI_IObject.h:18
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:40
MyGUI::WidgetManager::unregisterUnlinker
void unregisterUnlinker(IUnlinkWidget *_unlink)
Definition MyGUI_WidgetManager.cpp:144
MyGUI::WidgetManager::getInstance
static WidgetManager & getInstance()
MyGUI::WidgetManager::registerUnlinker
void registerUnlinker(IUnlinkWidget *_unlink)
Definition MyGUI_WidgetManager.cpp:138
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100