MyGUI 3.2.2

MyGUI_LayerNode.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_LayerNode.h"
9 #include "MyGUI_ILayerItem.h"
10 #include "MyGUI_ITexture.h"
11 #include "MyGUI_ISubWidget.h"
12 #include "MyGUI_ISubWidgetText.h"
13
14 namespace MyGUI
15 {
16
17 LayerNode::LayerNode(ILayer* _layer, ILayerNode* _parent) :
18 mParent(_parent),
19 mLayer(_layer),
20 mOutOfDate(false),
21 mDepth(0.0f)
22 {
23 }
24
25 LayerNode::~LayerNode()
26 {
27 for (VectorRenderItem::iterator iter = mFirstRenderItems.begin(); iter != mFirstRenderItems.end(); ++iter)
28 delete (*iter);
29 mFirstRenderItems.clear();
30
31 for (VectorRenderItem::iterator iter = mSecondRenderItems.begin(); iter != mSecondRenderItems.end(); ++iter)
32 delete (*iter);
33 mSecondRenderItems.clear();
34
35 for (VectorILayerNode::iterator iter = mChildItems.begin(); iter != mChildItems.end(); ++iter)
36 delete (*iter);
37 mChildItems.clear();
38 }
39
40 ILayerNode* LayerNode::createChildItemNode()
41 {
42 LayerNode* layer = new LayerNode(mLayer, this);
43 mChildItems.push_back(layer);
44
45 mOutOfDate = true;
46
47 return layer;
48 }
49
50 void LayerNode::destroyChildItemNode(ILayerNode* _node)
51 {
52 for (VectorILayerNode::iterator iter = mChildItems.begin(); iter != mChildItems.end(); ++iter)
53 {
54 if ((*iter) == _node)
55 {
56 delete _node;
57 mChildItems.erase(iter);
58
59 mOutOfDate = true;
60
61 return;
62 }
63 }
64 MYGUI_EXCEPT("item node not found");
65 }
66
67 void LayerNode::upChildItemNode(ILayerNode* _item)
68 {
69 for (VectorILayerNode::iterator iter = mChildItems.begin(); iter != mChildItems.end(); ++iter)
70 {
71 if ((*iter) == _item)
72 {
73 mChildItems.erase(iter);
74 mChildItems.push_back(_item);
75
76 mOutOfDate = true;
77
78 return;
79 }
80 }
81 MYGUI_EXCEPT("item node not found");
82 }
83
84 void LayerNode::renderToTarget(IRenderTarget* _target, bool _update)
85 {
86 mDepth = _target->getInfo().maximumDepth;
87
88 // сначала отрисовываем свое
89 for (VectorRenderItem::iterator iter = mFirstRenderItems.begin(); iter != mFirstRenderItems.end(); ++iter)
90 (*iter)->renderToTarget(_target, _update);
91
92 for (VectorRenderItem::iterator iter = mSecondRenderItems.begin(); iter != mSecondRenderItems.end(); ++iter)
93 (*iter)->renderToTarget(_target, _update);
94
95 // теперь отрисовываем дочерние узлы
96 for (VectorILayerNode::iterator iter = mChildItems.begin(); iter != mChildItems.end(); ++iter)
97 (*iter)->renderToTarget(_target, _update);
98
99 mOutOfDate = false;
100 }
101
102 void LayerNode::resizeView(const IntSize& _viewSize)
103 {
104 IntSize oldSize = mLayer->getSize();
105
106 for (VectorLayerItem::const_iterator iter = mLayerItems.begin(); iter != mLayerItems.end(); ++iter)
107 (*iter)->resizeLayerItemView(oldSize, _viewSize);
108 }
109
110 ILayerItem* LayerNode::getLayerItemByPoint(int _left, int _top) const
111 {
112 // сначала пикаем детей
113 for (VectorILayerNode::const_iterator iter = mChildItems.begin(); iter != mChildItems.end(); ++iter)
114 {
115 ILayerItem* item = (*iter)->getLayerItemByPoint(_left, _top);
116 if (nullptr != item)
117 return item;
118 }
119
120 for (VectorLayerItem::const_iterator iter = mLayerItems.begin(); iter != mLayerItems.end(); ++iter)
121 {
122 ILayerItem* item = (*iter)->getLayerItemByPoint(_left, _top);
123 if (nullptr != item)
124 return item;
125 }
126
127 return nullptr;
128 }
129
130 RenderItem* LayerNode::addToRenderItem(ITexture* _texture, bool _firstQueue, bool _manualRender)
131 {
132 RenderItem* item = nullptr;
133 if (_firstQueue)
134 item = addToRenderItemFirstQueue(_texture, _manualRender);
135 else
136 item = addToRenderItemSecondQueue(_texture, _manualRender);
137
138 mOutOfDate = false;
139 return item;
140 }
141
142 RenderItem* LayerNode::addToRenderItemFirstQueue(ITexture* _texture, bool _manualRender)
143 {
144 if (mFirstRenderItems.empty() || _manualRender)
145 {
146 RenderItem* item = new RenderItem();
147 item->setTexture(_texture);
148 item->setManualRender(_manualRender);
149 mFirstRenderItems.push_back(item);
150
151 return item;
152 }
153
154 updateCompression();
155
156 // first queue keep order
157
158 // use either first empty buffer
159 // or last non-empty buffer if it have same texture
160 VectorRenderItem::reverse_iterator iter = mFirstRenderItems.rbegin();
161 bool itemFound = false;
162 while (iter != mFirstRenderItems.rend())
163 {
164 if ((*iter)->getNeedVertexCount() == 0)
165 {
166 iter++;
167 itemFound = true;
168 continue;
169 }
170 else if (!(*iter)->getManualRender() && (*iter)->getTexture() == _texture)
171 {
172 iter++;
173 itemFound = true;
174 break;
175 }
176
177 break;
178 }
179
180 if (itemFound)
181 {
182 iter--;
183 (*iter)->setTexture(_texture);
184
185 return (*iter);
186 }
187
188 // not found, create new
189 RenderItem* item = new RenderItem();
190 item->setTexture(_texture);
191 item->setManualRender(_manualRender);
192 mFirstRenderItems.push_back(item);
193
194 return item;
195 }
196
197 RenderItem* LayerNode::addToRenderItemSecondQueue(ITexture* _texture, bool _manualRender)
198 {
199 // order is not important in second queue
200 // use first buffer with same texture or empty buffer
201 for (VectorRenderItem::iterator iter = mSecondRenderItems.begin(); iter != mSecondRenderItems.end(); ++iter)
202 {
203 if ((*iter)->getTexture() == _texture)
204 {
205 return (*iter);
206 }
207 else if ((*iter)->getNeedVertexCount() == 0)
208 {
209 (*iter)->setTexture(_texture);
210
211 return (*iter);
212 }
213 }
214
215 // not found, create new
216 RenderItem* item = new RenderItem();
217 item->setTexture(_texture);
218 item->setManualRender(_manualRender);
219 mSecondRenderItems.push_back(item);
220
221 return item;
222 }
223
224 void LayerNode::attachLayerItem(ILayerItem* _item)
225 {
226 mLayerItems.push_back(_item);
227 _item->attachItemToNode(mLayer, this);
228
229 mOutOfDate = true;
230 }
231
232 void LayerNode::detachLayerItem(ILayerItem* _item)
233 {
234 for (VectorLayerItem::iterator iter = mLayerItems.begin(); iter != mLayerItems.end(); ++iter)
235 {
236 if ((*iter) == _item)
237 {
238 mLayerItems.erase(iter);
239
240 mOutOfDate = true;
241
242 return;
243 }
244 }
245 MYGUI_EXCEPT("layer item not found");
246 }
247
248 void LayerNode::outOfDate(RenderItem* _item)
249 {
250 mOutOfDate = true;
251 if (_item)
252 _item->outOfDate();
253 }
254
255 EnumeratorILayerNode LayerNode::getEnumerator() const
256 {
257 return EnumeratorILayerNode(mChildItems);
258 }
259
260 size_t LayerNode::getLayerNodeCount() const
261 {
262 return mChildItems.size();
263 }
264
265 ILayerNode* LayerNode::getLayerNodeAt(size_t _index) const
266 {
267 MYGUI_ASSERT_RANGE(_index, mChildItems.size(), "LayerNode::getLayerNodeAt");
268
269 return mChildItems[_index];
270 }
271
272 void LayerNode::updateCompression()
273 {
274 if (mFirstRenderItems.size() <= 1)
275 return;
276
277 bool need_compression = false;
278 for (VectorRenderItem::iterator iter = mFirstRenderItems.begin(); iter != mFirstRenderItems.end(); ++iter)
279 {
280 if ((*iter)->getNeedCompression())
281 {
282 need_compression = true;
283 break;
284 }
285 }
286
287 // pushing all empty buffers to the end of buffers list
288 if (need_compression)
289 {
290 VectorRenderItem nonEmptyItems;
291 VectorRenderItem emptyItems;
292
293 for (VectorRenderItem::iterator iter = mFirstRenderItems.begin(); iter != mFirstRenderItems.end(); ++iter)
294 {
295 (*iter)->setNeedCompression(false);
296
297 if ((*iter)->getNeedVertexCount() == 0 && !(*iter)->getManualRender())
298 emptyItems.push_back(*iter);
299 else
300 nonEmptyItems.push_back(*iter);
301 }
302 nonEmptyItems.insert(nonEmptyItems.end(), emptyItems.begin(), emptyItems.end());
303 std::swap(mFirstRenderItems, nonEmptyItems);
304 }
305
306 mOutOfDate = true;
307 }
308
309 ILayer* LayerNode::getLayer() const
310 {
311 return mLayer;
312 }
313
314 ILayerNode* LayerNode::getParent() const
315 {
316 return mParent;
317 }
318
319 bool LayerNode::isOutOfDate() const
320 {
321 for (VectorRenderItem::const_iterator item = mFirstRenderItems.begin(); item != mFirstRenderItems.end(); ++item)
322 {
323 if ((*item)->isOutOfDate())
324 return true;
325 }
326
327 for (VectorRenderItem::const_iterator item = mSecondRenderItems.begin(); item != mSecondRenderItems.end(); ++item)
328 {
329 if ((*item)->isOutOfDate())
330 return true;
331 }
332
333 for (VectorILayerNode::const_iterator item = mChildItems.begin(); item != mChildItems.end(); ++item)
334 {
335 if (static_cast<const LayerNode*>(*item)->isOutOfDate())
336 return true;
337 }
338
339 return mOutOfDate;
340 }
341
342 float LayerNode::getNodeDepth()
343 {
344 return mDepth;
345 }
346
347 } // namespace MyGUI
MyGUI::LayerNode::addToRenderItemSecondQueue
RenderItem * addToRenderItemSecondQueue(ITexture *_texture, bool _manualRender)
Definition: MyGUI_LayerNode.cpp:197
MyGUI::ILayerItem::attachItemToNode
virtual void attachItemToNode(ILayer *_layer, ILayerNode *_node)=0
MyGUI::ILayerNode
Definition: MyGUI_ILayerNode.h:27
MyGUI::LayerNode::renderToTarget
virtual void renderToTarget(IRenderTarget *_target, bool _update)
Definition: MyGUI_LayerNode.cpp:84
MyGUI::ILayerItem::getLayerItemByPoint
virtual ILayerItem * getLayerItemByPoint(int _left, int _top) const =0
MyGUI::RenderTargetInfo::maximumDepth
float maximumDepth
Definition: MyGUI_RenderTargetInfo.h:37
MyGUI_Precompiled.h
MyGUI::ILayer::getSize
virtual const IntSize & getSize() const =0
MyGUI_ILayerItem.h
MyGUI::LayerNode::updateCompression
void updateCompression()
Definition: MyGUI_LayerNode.cpp:272
MyGUI::types::TSize< int >
MyGUI::LayerNode::getEnumerator
virtual EnumeratorILayerNode getEnumerator() const
Definition: MyGUI_LayerNode.cpp:255
MyGUI::LayerNode::getLayerItemByPoint
virtual ILayerItem * getLayerItemByPoint(int _left, int _top) const
Definition: MyGUI_LayerNode.cpp:110
MyGUI::LayerNode
Definition: MyGUI_LayerNode.h:22
MyGUI::LayerNode::destroyChildItemNode
virtual void destroyChildItemNode(ILayerNode *_node)
Definition: MyGUI_LayerNode.cpp:50
MyGUI::LayerNode::mChildItems
VectorILayerNode mChildItems
Definition: MyGUI_LayerNode.h:90
MyGUI::RenderItem
Definition: MyGUI_RenderItem.h:22
MyGUI::LayerNode::mLayer
ILayer * mLayer
Definition: MyGUI_LayerNode.h:93
MyGUI::LayerNode::createChildItemNode
virtual ILayerNode * createChildItemNode()
Definition: MyGUI_LayerNode.cpp:40
MyGUI::LayerNode::mOutOfDate
bool mOutOfDate
Definition: MyGUI_LayerNode.h:94
MyGUI::LayerNode::upChildItemNode
virtual void upChildItemNode(ILayerNode *_node)
Definition: MyGUI_LayerNode.cpp:67
MyGUI::RenderItem::setTexture
void setTexture(ITexture *_value)
Definition: MyGUI_RenderItem.cpp:165
MyGUI::LayerNode::mSecondRenderItems
VectorRenderItem mSecondRenderItems
Definition: MyGUI_LayerNode.h:83
MyGUI::RenderItem::setManualRender
void setManualRender(bool _value)
Definition: MyGUI_RenderItem.cpp:195
MyGUI::LayerNode::~LayerNode
virtual ~LayerNode()
Definition: MyGUI_LayerNode.cpp:25
MyGUI::LayerNode::mFirstRenderItems
VectorRenderItem mFirstRenderItems
Definition: MyGUI_LayerNode.h:82
MyGUI::LayerNode::mParent
ILayerNode * mParent
Definition: MyGUI_LayerNode.h:92
MyGUI::Enumerator
Definition: MyGUI_Enumerator.h:48
MyGUI::LayerNode::addToRenderItemFirstQueue
RenderItem * addToRenderItemFirstQueue(ITexture *_texture, bool _manualRender)
Definition: MyGUI_LayerNode.cpp:142
MyGUI::RenderItem::outOfDate
void outOfDate()
Definition: MyGUI_RenderItem.cpp:205
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::LayerNode::getParent
virtual ILayerNode * getParent() const
Definition: MyGUI_LayerNode.cpp:314
MYGUI_EXCEPT
#define MYGUI_EXCEPT(dest)
Definition: MyGUI_Diagnostic.h:33
MyGUI::LayerNode::mDepth
float mDepth
Definition: MyGUI_LayerNode.h:95
MyGUI::ILayer
Definition: MyGUI_ILayer.h:21
MyGUI::LayerNode::attachLayerItem
virtual void attachLayerItem(ILayerItem *_item)
Definition: MyGUI_LayerNode.cpp:224
MyGUI_ITexture.h
MyGUI::LayerNode::getNodeDepth
virtual float getNodeDepth()
Definition: MyGUI_LayerNode.cpp:342
MyGUI::LayerNode::mLayerItems
VectorLayerItem mLayerItems
Definition: MyGUI_LayerNode.h:87
MyGUI::LayerNode::LayerNode
LayerNode(ILayer *_layer, ILayerNode *_parent=0)
Definition: MyGUI_LayerNode.cpp:17
MyGUI::ITexture
Definition: MyGUI_ITexture.h:26
MyGUI::LayerNode::isOutOfDate
bool isOutOfDate() const
Definition: MyGUI_LayerNode.cpp:319
MyGUI::IRenderTarget
Definition: MyGUI_IRenderTarget.h:20
MyGUI::IRenderTarget::getInfo
virtual const RenderTargetInfo & getInfo()=0
MyGUI::LayerNode::outOfDate
virtual void outOfDate(RenderItem *_item)
Definition: MyGUI_LayerNode.cpp:248
MyGUI::EnumeratorILayerNode
Enumerator< VectorILayerNode > EnumeratorILayerNode
Definition: MyGUI_ILayerNode.h:25
MyGUI::LayerNode::getLayerNodeAt
virtual ILayerNode * getLayerNodeAt(size_t _index) const
Definition: MyGUI_LayerNode.cpp:265
MyGUI::ILayerItem
Definition: MyGUI_ILayerItem.h:18
MyGUI_LayerNode.h
MyGUI::VectorRenderItem
std::vector< RenderItem * > VectorRenderItem
Definition: MyGUI_LayerNode.h:18
MYGUI_ASSERT_RANGE
#define MYGUI_ASSERT_RANGE(index, size, owner)
Definition: MyGUI_Diagnostic.h:54
MyGUI::LayerNode::resizeView
virtual void resizeView(const IntSize &_viewSize)
Definition: MyGUI_LayerNode.cpp:102
MyGUI::LayerNode::addToRenderItem
virtual RenderItem * addToRenderItem(ITexture *_texture, bool _firstQueue, bool _manualRender)
Definition: MyGUI_LayerNode.cpp:130
MyGUI_ISubWidget.h
MyGUI::LayerNode::detachLayerItem
virtual void detachLayerItem(ILayerItem *_item)
Definition: MyGUI_LayerNode.cpp:232
MyGUI::LayerNode::getLayerNodeCount
virtual size_t getLayerNodeCount() const
Definition: MyGUI_LayerNode.cpp:260
MyGUI::LayerNode::getLayer
virtual ILayer * getLayer() const
Definition: MyGUI_LayerNode.cpp:309
MyGUI_ISubWidgetText.h