MyGUI: MyGUI_LayerManager.cpp Source File

MyGUI 3.4.3

MyGUI_LayerManager.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_LayerManager.h"
9#include "MyGUI_LayerItem.h"
10#include "MyGUI_WidgetManager.h"
11#include "MyGUI_RenderManager.h"
12#include "MyGUI_Widget.h"
13#include "MyGUI_FactoryManager.h"
14
15#include "MyGUI_SharedLayer.h"
16#include "MyGUI_OverlappedLayer.h"
17
18namespace MyGUI
19{
20
21 MYGUI_SINGLETON_DEFINITION(LayerManager);
22
23 LayerManager::LayerManager() :
24 mCategoryName("Layer"),
25 mSingletonHolder(this)
26 {
27 }
28
29 void LayerManager::initialise()
30 {
31 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
32 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
33
34 WidgetManager::getInstance().registerUnlinker(this);
35 ResourceManager::getInstance().registerLoadXmlDelegate(mCategoryName) = newDelegate(this, &LayerManager::_load);
36
37 FactoryManager::getInstance().registerFactory<SharedLayer>(mCategoryName);
38 FactoryManager::getInstance().registerFactory<OverlappedLayer>(mCategoryName);
39
40 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
41 mIsInitialise = true;
42 }
43
44 void LayerManager::shutdown()
45 {
46 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
47 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
48
49 FactoryManager::getInstance().unregisterFactory<SharedLayer>(mCategoryName);
50 FactoryManager::getInstance().unregisterFactory<OverlappedLayer>(mCategoryName);
51
52 // удаляем все хранители слоев
53 clear();
54
55 WidgetManager::getInstance().unregisterUnlinker(this);
56 ResourceManager::getInstance().unregisterLoadXmlDelegate(mCategoryName);
57
58 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
59 mIsInitialise = false;
60 }
61
62 void LayerManager::clear()
63 {
64 for (auto& layerNode : mLayerNodes)
65 {
66 destroy(layerNode);
67 }
68 mLayerNodes.clear();
69 }
70
71 void LayerManager::_load(xml::ElementPtr _node, std::string_view _file, Version _version)
72 {
73 VectorLayer layers;
74 // берем детей и крутимся, основной цикл
75 xml::ElementEnumerator layer = _node->getElementEnumerator();
76 while (layer.next(mCategoryName))
77 {
78 std::string name;
79
80 if (!layer->findAttribute("name", name))
81 {
82 MYGUI_LOG(Warning, "Attribute 'name' not found (file : " << _file << ")");
83 continue;
84 }
85
86 for (const auto& item : layers)
87 {
88 MYGUI_ASSERT(item->getName() != name, "Layer '" << name << "' already exist (file : " << _file << ")");
89 }
90
91 std::string_view type = layer->findAttribute("type");
92 if (type.empty() && _version <= Version(1, 0))
93 {
94 bool overlapped = utility::parseBool(layer->findAttribute("overlapped"));
95 type = overlapped ? "OverlappedLayer" : "SharedLayer";
96 }
97
98 ILayer* item = _createLayerObject(type);
99 item->deserialization(layer.current(), _version);
100
101 layers.push_back(item);
102 }
103
104 // теперь мержим новые и старые слои
105 merge(layers);
106 }
107
108 ILayer* LayerManager::_createLayerObject(std::string_view _type)
109 {
110 IObject* object = FactoryManager::getInstance().createObject(mCategoryName, _type);
111 MYGUI_ASSERT(object != nullptr, "factory '" << _type << "' is not found");
112
113 return object->castType<ILayer>();
114 }
115
116 void LayerManager::_unlinkWidget(Widget* _widget)
117 {
118 detachFromLayer(_widget);
119 }
120
121 // поправить на виджет и проверять на рутовость
122 void LayerManager::attachToLayerNode(std::string_view _name, Widget* _item)
123 {
124 MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
125 MYGUI_ASSERT(_item->isRootWidget(), "attached widget must be root");
126
127 // сначала отсоединяем
128 _item->detachFromLayer();
129
130 // а теперь аттачим
131 for (auto& layerNode : mLayerNodes)
132 {
133 if (_name == layerNode->getName())
134 {
135 ILayerNode* node = layerNode->createChildItemNode();
136 node->attachLayerItem(_item);
137
138 return;
139 }
140 }
141 MYGUI_LOG(Error, "Layer '" << _name << "' is not found");
142 //MYGUI_EXCEPT("Layer '" << _name << "' is not found");
143 }
144
145 void LayerManager::detachFromLayer(Widget* _item)
146 {
147 MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
148 _item->detachFromLayer();
149 }
150
151 void LayerManager::upLayerItem(Widget* _item)
152 {
153 MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
154 _item->upLayerItem();
155 }
156
157 bool LayerManager::isExist(std::string_view _name) const
158 {
159 return getByName(_name, false) != nullptr;
160 }
161
162 void LayerManager::merge(VectorLayer& _layers)
163 {
164 for (auto& layerNode : mLayerNodes)
165 {
166 if (layerNode == nullptr)
167 continue;
168 bool find = false;
169 const std::string& name = layerNode->getName();
170 for (auto& layer : _layers)
171 {
172 if (name == layer->getName())
173 {
174 // заменяем новый слой, на уже существующий
175 delete layer;
176 layer = layerNode;
177 layerNode = nullptr;
178 find = true;
179 break;
180 }
181 }
182 if (!find)
183 {
184 destroy(layerNode);
185 layerNode = nullptr;
186 }
187 }
188
189 // теперь в основной
190 mLayerNodes = _layers;
191 }
192
193 ILayer* LayerManager::createLayerAt(std::string_view _name, std::string_view _type, size_t _index)
194 {
195 MYGUI_ASSERT_RANGE(_index, mLayerNodes.size() + 1, "LayerManager::getLayer");
196
197 ILayer* item = _createLayerObject(_type);
198 item->setName(_name);
199
200 mLayerNodes.insert(mLayerNodes.begin() + _index, item);
201
202 return item;
203 }
204
205 void LayerManager::destroy(ILayer* _layer)
206 {
207 MYGUI_LOG(Info, "destroy layer '" << _layer->getName() << "'");
208 delete _layer;
209 }
210
211 Widget* LayerManager::getWidgetFromPoint(int _left, int _top) const
212 {
213 VectorLayer::const_reverse_iterator iter = mLayerNodes.rbegin();
214 while (iter != mLayerNodes.rend())
215 {
216 ILayerItem* item = (*iter)->getLayerItemByPoint(_left, _top);
217 if (item != nullptr)
218 return static_cast<Widget*>(item);
219 ++iter;
220 }
221 return nullptr;
222 }
223
224 void LayerManager::renderToTarget(IRenderTarget* _target, bool _update)
225 {
226 for (auto& layerNode : mLayerNodes)
227 {
228 layerNode->renderToTarget(_target, _update);
229 }
230 }
231
232 ILayer* LayerManager::getByName(std::string_view _name, bool _throw) const
233 {
234 for (const auto& layerNode : mLayerNodes)
235 {
236 if (_name == layerNode->getName())
237 return layerNode;
238 }
239 MYGUI_ASSERT(!_throw, "Layer '" << _name << "' not found");
240 return nullptr;
241 }
242
243 LayerManager::EnumeratorLayer LayerManager::getEnumerator() const
244 {
245 return EnumeratorLayer(mLayerNodes);
246 }
247
248 void LayerManager::resizeView(const IntSize& _viewSize)
249 {
250 for (const auto& node : mLayerNodes)
251 node->resizeView(_viewSize);
252 }
253
254 size_t LayerManager::getLayerCount() const
255 {
256 return mLayerNodes.size();
257 }
258
259 ILayer* LayerManager::getLayer(size_t _index) const
260 {
261 MYGUI_ASSERT_RANGE(_index, mLayerNodes.size(), "LayerManager::getLayer");
262 return mLayerNodes[_index];
263 }
264
265 const std::string& LayerManager::getCategoryName() const
266 {
267 return mCategoryName;
268 }
269
270} // namespace MyGUI
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:31
MYGUI_ASSERT_RANGE
#define MYGUI_ASSERT_RANGE(index, size, owner)
Definition MyGUI_Diagnostic.h:44
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MyGUI_FactoryManager.h
MyGUI_LayerItem.h
MyGUI_LayerManager.h
MyGUI_OverlappedLayer.h
MyGUI_Precompiled.h
MyGUI_RenderManager.h
MyGUI_SharedLayer.h
MYGUI_SINGLETON_DEFINITION
#define MYGUI_SINGLETON_DEFINITION(ClassName)
Definition MyGUI_Singleton.h:126
MyGUI_Widget.h
MyGUI_WidgetManager.h
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::ILayer
Definition MyGUI_ILayer.h:22
MyGUI::ILayer::getName
const std::string & getName() const
Definition MyGUI_ILayer.h:28
MyGUI::ILayer::setName
void setName(std::string_view _name)
Definition MyGUI_ILayer.h:33
MyGUI::ILayerItem
Definition MyGUI_ILayerItem.h:20
MyGUI::ILayerItem::getLayerItemByPoint
virtual ILayerItem * getLayerItemByPoint(int _left, int _top) const =0
MyGUI::ILayerNode
Definition MyGUI_ILayerNode.h:29
MyGUI::ILayerNode::attachLayerItem
virtual void attachLayerItem(ILayerItem *_item)=0
MyGUI::ILayerNode::createChildItemNode
virtual ILayerNode * createChildItemNode()=0
MyGUI::IRenderTarget
Definition MyGUI_IRenderTarget.h:21
MyGUI::LayerItem::detachFromLayer
void detachFromLayer() override
Definition MyGUI_LayerItem.cpp:96
MyGUI::LayerItem::upLayerItem
void upLayerItem() override
Definition MyGUI_LayerItem.cpp:120
MyGUI::LayerManager
Definition MyGUI_LayerManager.h:23
MyGUI::LayerManager::LayerManager
LayerManager()
Definition MyGUI_LayerManager.cpp:23
MyGUI::LayerManager::detachFromLayer
void detachFromLayer(Widget *_item)
Definition MyGUI_LayerManager.cpp:145
MyGUI::LayerManager::upLayerItem
void upLayerItem(Widget *_item)
Definition MyGUI_LayerManager.cpp:151
MyGUI::LayerManager::getLayer
ILayer * getLayer(size_t _index) const
Definition MyGUI_LayerManager.cpp:259
MyGUI::LayerManager::getCategoryName
const std::string & getCategoryName() const
Definition MyGUI_LayerManager.cpp:265
MyGUI::LayerManager::getClassTypeName
static std::string_view getClassTypeName()
MyGUI::LayerManager::getEnumerator
EnumeratorLayer getEnumerator() const
Definition MyGUI_LayerManager.cpp:243
MyGUI::LayerManager::getLayerCount
size_t getLayerCount() const
Definition MyGUI_LayerManager.cpp:254
MyGUI::LayerManager::createLayerAt
ILayer * createLayerAt(std::string_view _name, std::string_view _type, size_t _index)
Definition MyGUI_LayerManager.cpp:193
MyGUI::LayerManager::renderToTarget
void renderToTarget(IRenderTarget *_target, bool _update)
Definition MyGUI_LayerManager.cpp:224
MyGUI::LayerManager::shutdown
void shutdown()
Definition MyGUI_LayerManager.cpp:44
MyGUI::LayerManager::VectorLayer
std::vector< ILayer * > VectorLayer
Definition MyGUI_LayerManager.h:27
MyGUI::LayerManager::getByName
ILayer * getByName(std::string_view _name, bool _throw=true) const
Definition MyGUI_LayerManager.cpp:232
MyGUI::LayerManager::resizeView
void resizeView(const IntSize &_viewSize)
Definition MyGUI_LayerManager.cpp:248
MyGUI::LayerManager::attachToLayerNode
void attachToLayerNode(std::string_view _name, Widget *_item)
Definition MyGUI_LayerManager.cpp:122
MyGUI::LayerManager::getWidgetFromPoint
Widget * getWidgetFromPoint(int _left, int _top) const
Definition MyGUI_LayerManager.cpp:211
MyGUI::LayerManager::initialise
void initialise()
Definition MyGUI_LayerManager.cpp:29
MyGUI::LayerManager::EnumeratorLayer
Enumerator< VectorLayer > EnumeratorLayer
Definition MyGUI_LayerManager.h:28
MyGUI::LayerManager::isExist
bool isExist(std::string_view _name) const
Definition MyGUI_LayerManager.cpp:157
MyGUI::OverlappedLayer
Definition MyGUI_OverlappedLayer.h:18
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:127
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(std::string_view _key)
Definition MyGUI_ResourceManager.cpp:120
MyGUI::ResourceManager::getInstance
static ResourceManager & getInstance()
MyGUI::SharedLayer
Definition MyGUI_SharedLayer.h:19
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:40
MyGUI::Widget::isRootWidget
bool isRootWidget() const
Definition MyGUI_Widget.cpp:1347
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::utility::parseBool
bool parseBool(std::string_view _value)
Definition MyGUI_StringUtility.h:127
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::IntSize
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100