MyGUI: MyGUI_LayerManager.cpp Source File

MyGUI 3.4.2

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

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