MyGUI 3.4.1
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"
9#include "MyGUI_LayerItem.h"
10#include "MyGUI_WidgetManager.h"
11#include "MyGUI_RenderManager.h"
12#include "MyGUI_Widget.h"
14
15#include "MyGUI_SharedLayer.h"
17
18namespace MyGUI
19{
20
22
24 mIsInitialise(false),
25 mCategoryName("Layer"),
26 mSingletonHolder(this)
27 {
28 }
29
31 {
32 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
33 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
34
36 ResourceManager::getInstance().registerLoadXmlDelegate(mCategoryName) = newDelegate(this, &LayerManager::_load);
37
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
52
53 // удаляем все хранители слоев
54 clear();
55
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
148 {
149 MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
150 _item->detachFromLayer();
151 }
152
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
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
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
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_ASSERT_RANGE(index, size, owner)
#define MYGUI_LOG(level, text)
void unregisterFactory(const std::string &_category, const std::string &_type)
static FactoryManager & getInstance()
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
IObject * createObject(const std::string &_category, const std::string &_type)
const std::string & getName() const
Definition: MyGUI_ILayer.h:29
void setName(const std::string &_name)
Definition: MyGUI_ILayer.h:34
virtual ILayerItem * getLayerItemByPoint(int _left, int _top) const =0
virtual void attachLayerItem(ILayerItem *_item)=0
virtual ILayerNode * createChildItemNode()=0
void detachFromLayer() override
void upLayerItem() override
void detachFromLayer(Widget *_item)
void upLayerItem(Widget *_item)
ILayer * getLayer(size_t _index) const
bool isExist(const std::string &_name) const
const std::string & getCategoryName() const
EnumeratorLayer getEnumerator() const
size_t getLayerCount() const
void renderToTarget(IRenderTarget *_target, bool _update)
Enumerator< VectorLayer > EnumeratorLayer
static const char * getClassTypeName()
void resizeView(const IntSize &_viewSize)
ILayer * getByName(const std::string &_name, bool _throw=true) const
Widget * getWidgetFromPoint(int _left, int _top) const
std::vector< ILayer * > VectorLayer
void attachToLayerNode(const std::string &_name, Widget *_item)
ILayer * createLayerAt(const std::string &_name, const std::string &_type, size_t _index)
void unregisterLoadXmlDelegate(const std::string &_key)
static ResourceManager & getInstance()
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
widget description should be here.
Definition: MyGUI_Widget.h:37
bool isRootWidget() const
void unregisterUnlinker(IUnlinkWidget *_unlink)
static WidgetManager & getInstance()
void registerUnlinker(IUnlinkWidget *_unlink)
bool parseBool(const std::string &_value)
Element * ElementPtr
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
MYGUI_SINGLETON_DEFINITION(ClipboardManager)