MyGUI 3.2.0

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

- File List
- Globals

MyGUI_LayerManager.cpp
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_LayerManager.h"
24 #include "MyGUI_LayerItem.h"
25 #include "MyGUI_WidgetManager.h"
26 #include "MyGUI_RenderManager.h"
27 #include "MyGUI_Widget.h"
28 #include "MyGUI_FactoryManager.h"
29
30 #include "MyGUI_SharedLayer.h"
31 #include "MyGUI_OverlappedLayer.h"
32
33 namespace MyGUI
34 {
35
36 const std::string XML_TYPE("Layer");
37
38 template <> LayerManager* Singleton<LayerManager>::msInstance = nullptr;
39 template <> const char* Singleton<LayerManager>::mClassTypeName("LayerManager");
40
41 LayerManager::LayerManager() :
42 mIsInitialise(false)
43 {
44 }
45
46 void LayerManager::initialise()
47 {
48 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
49 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
50
51 WidgetManager::getInstance().registerUnlinker(this);
52 ResourceManager::getInstance().registerLoadXmlDelegate(XML_TYPE) = newDelegate(this, &LayerManager::_load);
53
54 FactoryManager::getInstance().registerFactory<SharedLayer>(XML_TYPE);
55 FactoryManager::getInstance().registerFactory<OverlappedLayer>(XML_TYPE);
56
57 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
58 mIsInitialise = true;
59 }
60
61 void LayerManager::shutdown()
62 {
63 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
64 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
65
66 FactoryManager::getInstance().unregisterFactory<SharedLayer>(XML_TYPE);
67 FactoryManager::getInstance().unregisterFactory<OverlappedLayer>(XML_TYPE);
68
69 // удаляем все хранители слоев
70 clear();
71
72 WidgetManager::getInstance().unregisterUnlinker(this);
73 ResourceManager::getInstance().unregisterLoadXmlDelegate(XML_TYPE);
74
75 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
76 mIsInitialise = false;
77 }
78
79 void LayerManager::clear()
80 {
81 for (VectorLayer::iterator iter = mLayerNodes.begin(); iter != mLayerNodes.end(); ++iter)
82 {
83 destroy(*iter);
84 }
85 mLayerNodes.clear();
86 }
87
88 void LayerManager::_load(xml::ElementPtr _node, const std::string& _file, Version _version)
89 {
90 VectorLayer layers;
91 // берем детей и крутимся, основной цикл
92 xml::ElementEnumerator layer = _node->getElementEnumerator();
93 while (layer.next(XML_TYPE))
94 {
95
96 std::string name;
97
98 if ( !layer->findAttribute("name", name))
99 {
100 MYGUI_LOG(Warning, "Attribute 'name' not found (file : " << _file << ")");
101 continue;
102 }
103
104 for (VectorLayer::iterator iter = layers.begin(); iter != layers.end(); ++iter)
105 {
106 MYGUI_ASSERT((*iter)->getName() != name, "Layer '" << name << "' already exist (file : " << _file << ")");
107 }
108
109 std::string type = layer->findAttribute("type");
110 if (type.empty() && _version <= Version(1, 0))
111 {
112 bool overlapped = utility::parseBool(layer->findAttribute("overlapped"));
113 type = overlapped ? "OverlappedLayer" : "SharedLayer";
114 }
115
116 IObject* object = FactoryManager::getInstance().createObject(XML_TYPE, type);
117 MYGUI_ASSERT(object != nullptr, "factory '" << type << "' is not found");
118
119 ILayer* item = object->castType<ILayer>();
120 item->deserialization(layer.current(), _version);
121
122 layers.push_back(item);
123 }
124
125 // теперь мержим новые и старые слои
126 merge(layers);
127 }
128
129 void LayerManager::_unlinkWidget(Widget* _widget)
130 {
131 detachFromLayer(_widget);
132 }
133
134 // поправить на виджет и проверять на рутовость
135 void LayerManager::attachToLayerNode(const std::string& _name, Widget* _item)
136 {
137 MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
138 MYGUI_ASSERT(_item->isRootWidget(), "attached widget must be root");
139
140 // сначала отсоединяем
141 _item->detachFromLayer();
142
143 // а теперь аттачим
144 for (VectorLayer::iterator iter = mLayerNodes.begin(); iter != mLayerNodes.end(); ++iter)
145 {
146 if (_name == (*iter)->getName())
147 {
148 ILayerNode* node = (*iter)->createChildItemNode();
149 node->attachLayerItem(_item);
150
151 return;
152 }
153 }
154 MYGUI_LOG(Error, "Layer '" << _name << "' is not found");
155 //MYGUI_EXCEPT("Layer '" << _name << "' is not found");
156 }
157
158 void LayerManager::detachFromLayer(Widget* _item)
159 {
160 MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
161 _item->detachFromLayer();
162 }
163
164 void LayerManager::upLayerItem(Widget* _item)
165 {
166 MYGUI_ASSERT(nullptr != _item, "pointer must be valid");
167 _item->upLayerItem();
168 }
169
170 bool LayerManager::isExist(const std::string& _name) const
171 {
172 return getByName(_name, false) != nullptr;
173 }
174
175 void LayerManager::merge(VectorLayer& _layers)
176 {
177 for (VectorLayer::iterator iter = mLayerNodes.begin(); iter != mLayerNodes.end(); ++iter)
178 {
179 if ((*iter) == nullptr) continue;
180 bool find = false;
181 std::string name = (*iter)->getName();
182 for (VectorLayer::iterator iter2 = _layers.begin(); iter2 != _layers.end(); ++iter2)
183 {
184 if (name == (*iter2)->getName())
185 {
186 // заменяем новый слой, на уже существующий
187 delete (*iter2);
188 (*iter2) = (*iter);
189 (*iter) = nullptr;
190 find = true;
191 break;
192 }
193 }
194 if (!find)
195 {
196 destroy(*iter);
197 (*iter) = nullptr;
198 }
199 }
200
201 // теперь в основной
202 mLayerNodes = _layers;
203 }
204
205 void LayerManager::destroy(ILayer* _layer)
206 {
207 MYGUI_LOG(Info, "destroy layer '" << _layer->getName() << "'");
208 delete _layer;
209 }
210 Widget* LayerManager::getWidgetFromPoint(int _left, int _top)
211 {
212 VectorLayer::reverse_iterator iter = mLayerNodes.rbegin();
213 while (iter != mLayerNodes.rend())
214 {
215 ILayerItem* item = (*iter)->getLayerItemByPoint(_left, _top);
216 if (item != nullptr) return static_cast<Widget*>(item);
217 ++iter;
218 }
219 return nullptr;
220 }
221
222 void LayerManager::renderToTarget(IRenderTarget* _target, bool _update)
223 {
224 for (VectorLayer::iterator iter = mLayerNodes.begin(); iter != mLayerNodes.end(); ++iter)
225 {
226 (*iter)->renderToTarget(_target, _update);
227 }
228 }
229
230 ILayer* LayerManager::getByName(const std::string& _name, bool _throw) const
231 {
232 for (VectorLayer::const_iterator iter = mLayerNodes.begin(); iter != mLayerNodes.end(); ++iter)
233 {
234 if (_name == (*iter)->getName())
235 return (*iter);
236 }
237 MYGUI_ASSERT(!_throw, "Layer '" << _name << "' not found");
238 return nullptr;
239 }
240
241 LayerManager::EnumeratorLayer LayerManager::getEnumerator() const
242 {
243 return EnumeratorLayer(mLayerNodes);
244 }
245
246 void LayerManager::resizeView(const IntSize& _viewSize)
247 {
248 for (VectorLayer::const_iterator iter = mLayerNodes.begin(); iter != mLayerNodes.end(); ++iter)
249 (*iter)->resizeView(_viewSize);
250 }
251
252 } // namespace MyGUI
MyGUI::Widget
Definition: MyGUI_Widget.h:41
MyGUI::LayerItem::detachFromLayer
virtual void detachFromLayer()
Definition: MyGUI_LayerItem.cpp:120
MyGUI_FactoryManager.h
MyGUI::ILayerNode
Definition: MyGUI_ILayerNode.h:42
MyGUI::ILayerItem::getLayerItemByPoint
virtual ILayerItem * getLayerItemByPoint(int _left, int _top) const =0
MyGUI::ILayer::getName
const std::string & getName() const
Definition: MyGUI_ILayer.h:46
MyGUI::OverlappedLayer
Definition: MyGUI_OverlappedLayer.h:32
MyGUI::ResourceManager::unregisterLoadXmlDelegate
void unregisterLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:142
MyGUI::LayerManager::LayerManager
LayerManager()
Definition: MyGUI_LayerManager.cpp:41
MyGUI::LayerManager::initialise
void initialise()
Definition: MyGUI_LayerManager.cpp:46
MyGUI::Widget::isRootWidget
bool isRootWidget() const
Definition: MyGUI_Widget.cpp:1240
MyGUI_LayerManager.h
MyGUI_Precompiled.h
MyGUI::ResourceManager::registerLoadXmlDelegate
LoadXmlDelegate & registerLoadXmlDelegate(const std::string &_key)
Definition: MyGUI_ResourceManager.cpp:135
MyGUI::types::TSize< int >
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition: MyGUI_XmlDocument.h:114
MyGUI::newDelegate
delegates::IDelegate0 * newDelegate(void(*_func)())
Definition: MyGUI_Delegate.h:150
MyGUI::Singleton< WidgetManager >::getInstance
static WidgetManager & getInstance()
Definition: MyGUI_Singleton.h:53
MyGUI_WidgetManager.h
MyGUI::LayerManager::VectorLayer
std::vector< ILayer * > VectorLayer
Definition: MyGUI_LayerManager.h:43
MyGUI::SharedLayer
Definition: MyGUI_SharedLayer.h:33
MyGUI::Singleton< LayerManager >::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:64
MyGUI::LayerItem::upLayerItem
virtual void upLayerItem()
Definition: MyGUI_LayerItem.cpp:143
MyGUI::XML_TYPE
const std::string XML_TYPE("Font")
MyGUI::LayerManager::isExist
bool isExist(const std::string &_name) const
Definition: MyGUI_LayerManager.cpp:170
MyGUI_OverlappedLayer.h
MyGUI::ILayerNode::attachLayerItem
virtual void attachLayerItem(ILayerItem *_item)=0
MyGUI::Enumerator
Definition: MyGUI_Enumerator.h:63
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:38
MyGUI::LayerManager::resizeView
void resizeView(const IntSize &_viewSize)
Definition: MyGUI_LayerManager.cpp:246
MyGUI::LayerManager::shutdown
void shutdown()
Definition: MyGUI_LayerManager.cpp:61
MyGUI::ILayerNode::createChildItemNode
virtual ILayerNode * createChildItemNode()=0
MyGUI::LayerManager::EnumeratorLayer
Enumerator< VectorLayer > EnumeratorLayer
Definition: MyGUI_LayerManager.h:44
MyGUI_RenderManager.h
MyGUI::LayerManager::getEnumerator
EnumeratorLayer getEnumerator() const
Definition: MyGUI_LayerManager.cpp:241
MyGUI::ILayer
Definition: MyGUI_ILayer.h:36
MyGUI::LayerManager::detachFromLayer
void detachFromLayer(Widget *_item)
Definition: MyGUI_LayerManager.cpp:158
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:58
MyGUI::IRenderTarget
Definition: MyGUI_IRenderTarget.h:35
MyGUI::LayerManager::attachToLayerNode
void attachToLayerNode(const std::string &_name, Widget *_item)
Definition: MyGUI_LayerManager.cpp:135
MyGUI_LayerItem.h
MyGUI::WidgetManager::registerUnlinker
void registerUnlinker(IUnlinkWidget *_unlink)
Definition: MyGUI_WidgetManager.cpp:147
MyGUI_SharedLayer.h
MyGUI::ILayerItem
Definition: MyGUI_ILayerItem.h:33
MyGUI::FactoryManager::unregisterFactory
void unregisterFactory(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:61
MyGUI::utility::parseBool
bool parseBool(const std::string &_value)
Definition: MyGUI_StringUtility.h:206
MyGUI::FactoryManager::registerFactory
void registerFactory(const std::string &_category, const std::string &_type, Delegate::IDelegate *_delegate)
Definition: MyGUI_FactoryManager.cpp:55
MyGUI_Widget.h
MyGUI::LayerManager::getWidgetFromPoint
Widget * getWidgetFromPoint(int _left, int _top)
Definition: MyGUI_LayerManager.cpp:210
MyGUI::LayerManager::getByName
ILayer * getByName(const std::string &_name, bool _throw=true) const
Definition: MyGUI_LayerManager.cpp:230
MyGUI::Singleton::mClassTypeName
static const char * mClassTypeName
Definition: MyGUI_Singleton.h:73
MyGUI::FactoryManager::createObject
IObject * createObject(const std::string &_category, const std::string &_type)
Definition: MyGUI_FactoryManager.cpp:87
MyGUI::WidgetManager::unregisterUnlinker
void unregisterUnlinker(IUnlinkWidget *_unlink)
Definition: MyGUI_WidgetManager.cpp:153
MyGUI::LayerManager::renderToTarget
void renderToTarget(IRenderTarget *_target, bool _update)
Definition: MyGUI_LayerManager.cpp:222
MyGUI::LayerManager::upLayerItem
void upLayerItem(Widget *_item)
Definition: MyGUI_LayerManager.cpp:164