MyGUI 3.5.3
MyGUI_TileRect.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_TileRect.h"
10#include "MyGUI_RenderItem.h"
11#include "MyGUI_SkinManager.h"
12#include "MyGUI_LayerNode.h"
14#include "MyGUI_RenderManager.h"
17
18namespace MyGUI
19{
20
22
23 TileRect::TileRect() :
24 mCountVertex(TILERECT_COUNT_VERTEX)
25 {
26 mVertexFormat = RenderManager::getInstance().getVertexFormat();
27 }
28
29 void TileRect::setVisible(bool _visible)
30 {
31 if (mVisible == _visible)
32 return;
33 mVisible = _visible;
34
35 if (nullptr != mNode)
36 mNode->outOfDate(mRenderItem);
37 }
38
39 void TileRect::setAlpha(float _alpha)
40 {
41 uint32 alpha = ((uint8)(_alpha * 255) << 24);
42 if ((mCurrentColour & 0xFF000000) == alpha)
43 return;
44 mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
45
46 if (nullptr != mNode)
47 mNode->outOfDate(mRenderItem);
48 }
49
50 void TileRect::_correctView()
51 {
52 if (nullptr != mNode)
53 mNode->outOfDate(mRenderItem);
54 }
55
56 void TileRect::_setAlign(const IntSize& _oldsize)
57 {
58 auto [coord, resize, move] = CoordConverter::applyAlign(mAlign, mCoord, mCroppedParent->getSize(), _oldsize);
59 mCoord = coord;
60 if (resize)
61 mIsMargin = true;
62 mCurrentCoord = mCoord;
63 if (!mTileH)
64 mTileSize.width = mCoord.width;
65 if (!mTileV)
66 mTileSize.height = mCoord.height;
67 _updateView();
68 }
69
70 void TileRect::_updateView()
71 {
72 bool margin = _checkMargin();
73
74 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
75
76 mCurrentCoord.left = mCoord.left + mMargin.left;
77 mCurrentCoord.top = mCoord.top + mMargin.top;
78 mCurrentCoord.width = _getViewWidth();
79 mCurrentCoord.height = _getViewHeight();
80
81 // calculate required number of tiles
82 if (!mEmptyView)
83 {
84 size_t count = 0;
85 if (mTileSize.width > 0 && mTileSize.height > 0)
86 {
87 size_t count_x = mCoord.width / mTileSize.width;
88 if ((mCoord.width % mTileSize.width) > 0)
89 count_x++;
90
91 size_t count_y = mCoord.height / mTileSize.height;
92 if ((mCoord.height % mTileSize.height) > 0)
93 count_y++;
94
95 count = count_y * count_x * VertexQuad::VertexCount;
96 }
97
98 // need more vertices
99 if (count > mCountVertex)
100 {
101 mCountVertex = count + TILERECT_COUNT_VERTEX;
102 if (nullptr != mRenderItem)
103 mRenderItem->reallockDrawItem(this, mCountVertex);
104 }
105 }
106
107 // viewport became invalid
108 if (margin)
109 {
110 if (_checkOutsideUnrotated())
111 {
112 // remember current state
113 mIsMargin = margin;
114
115 // update before exit
116 if (nullptr != mNode)
117 mNode->outOfDate(mRenderItem);
118 return;
119 }
120 }
121
122 // remember current state
123 mIsMargin = margin;
124
125 if (nullptr != mNode)
126 mNode->outOfDate(mRenderItem);
127 }
128
129 void TileRect::_setUVSet(const FloatRect& _rect)
130 {
131 if (mCurrentTexture == _rect)
132 return;
133 mCurrentTexture = _rect;
134 if (nullptr != mNode)
135 mNode->outOfDate(mRenderItem);
136 }
137
138 void TileRect::doRender()
139 {
140 if (!mVisible || mEmptyView || mTileSize.width <= 0 || mTileSize.height <= 0)
141 return;
142
143 VertexQuad* quad = reinterpret_cast<VertexQuad*>(mRenderItem->getCurrentVertexBuffer());
144
145 const RenderTargetInfo& info = mRenderItem->getRenderTarget()->getInfo();
146
147 // size of one tile
148 mRealTileWidth = info.pixScaleX * (float)(mTileSize.width) * 2;
149 mRealTileHeight = info.pixScaleY * (float)(mTileSize.height) * 2;
150
151 mTextureHeightOne = (mCurrentTexture.bottom - mCurrentTexture.top) / mRealTileHeight;
152 mTextureWidthOne = (mCurrentTexture.right - mCurrentTexture.left) / mRealTileWidth;
153
154 float vertex_z = mNode->getNodeDepth();
155
156 // Original tile origin and clipped viewport corners.
157 const IntPoint origin = mCroppedParent->getAbsolutePosition();
158 const FloatRect window = geometry_utility::toRenderTarget(mCoord, info, origin);
159 const FloatRect viewport = geometry_utility::toRenderTarget(mCurrentCoord, info, origin);
160
161 float window_left = window.left;
162 float window_top = window.top;
163 float real_left = viewport.left;
164 float real_top = viewport.top;
165 float real_right = viewport.right;
166 float real_bottom = viewport.bottom;
167
168 size_t count = 0;
169
170 float bottom = window_top;
171
172 for (int y = 0; y < mCoord.height; y += mTileSize.height)
173 {
174 float top = bottom;
175 bottom -= mRealTileHeight;
176 float right = window_left;
177
178 float vertex_top = top;
179 float vertex_bottom = bottom;
180 bool texture_crop_height = false;
181
182 if (vertex_top > real_top)
183 {
184 // check for complete out-of-bounds
185 if (vertex_bottom > real_top)
186 {
187 continue;
188 }
189 vertex_top = real_top;
190 texture_crop_height = true;
191 }
192 if (vertex_bottom < real_bottom)
193 {
194 // went completely out at bottom
195 if (vertex_top < real_bottom)
196 {
197 continue;
198 }
199 vertex_bottom = real_bottom;
200 texture_crop_height = true;
201 }
202
203 for (int x = 0; x < mCoord.width; x += mTileSize.width)
204 {
205 float left = right;
206 right += mRealTileWidth;
207
208 float vertex_left = left;
209 float vertex_right = right;
210 bool texture_crop_width = false;
211
212
213 if (vertex_left < real_left)
214 {
215 // check for complete out-of-bounds
216 if (vertex_right < real_left)
217 {
218 continue;
219 }
220 vertex_left = real_left;
221 texture_crop_width = true;
222 }
223
224 if (vertex_right > real_right)
225 {
226 // don't need the full row
227 if (vertex_left > real_right)
228 {
229 continue;
230 }
231 vertex_right = real_right;
232 texture_crop_width = true;
233 }
234
235 // current texture coordinates
236 float texture_left = mCurrentTexture.left;
237 float texture_right = mCurrentTexture.right;
238 float texture_top = mCurrentTexture.top;
239 float texture_bottom = mCurrentTexture.bottom;
240
241 // texture offset vertically
242 if (texture_crop_height)
243 {
244 // add offset size in texture coordinates
245 texture_top += (top - vertex_top) * mTextureHeightOne;
246 // subtract offset size in texture coordinates
247 texture_bottom -= (vertex_bottom - bottom) * mTextureHeightOne;
248 }
249
250 // texture offset horizontally
251 if (texture_crop_width)
252 {
253 // add offset size in texture coordinates
254 texture_left += (vertex_left - left) * mTextureWidthOne;
255 // subtract offset size in texture coordinates
256 texture_right -= (right - vertex_right) * mTextureWidthOne;
257 }
258
259 quad[count].set(
260 vertex_left,
261 vertex_top,
262 vertex_right,
263 vertex_bottom,
264 vertex_z,
265 texture_left,
266 texture_top,
267 texture_right,
268 texture_bottom,
269 mCurrentColour);
270
271 count++;
272 }
273 }
274
275 mRenderItem->setLastVertexCount(VertexQuad::VertexCount * count);
276 }
277
278 void TileRect::createDrawItem(ITexture* _texture, ILayerNode* _node)
279 {
280 MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
281
282 mNode = _node;
283 mRenderItem = mNode->addToRenderItem(_texture, true);
284 mRenderItem->addDrawItem(this, mCountVertex);
285 }
286
287 void TileRect::destroyDrawItem()
288 {
289 MYGUI_ASSERT(mRenderItem, "mRenderItem must be not nullptr");
290
291 mNode = nullptr;
292 mRenderItem->removeDrawItem(this);
293 mRenderItem = nullptr;
294 }
295
296 void TileRect::setStateData(IStateInfo* _data)
297 {
298 TileRectStateInfo* data = _data->castType<TileRectStateInfo>();
299
300 mTileSize = data->getTileSize();
301 mTileH = data->getTileH();
302 mTileV = data->getTileV();
303 _setUVSet(data->getRect());
304 }
305
306 void TileRect::_setColour(const Colour& _value)
307 {
308 uint32 colour = texture_utility::toNativeColour(_value, mVertexFormat);
309 mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
310
311 if (nullptr != mNode)
312 mNode->outOfDate(mRenderItem);
313 }
314
315} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
uint32_t uint32
Definition MyGUI_Types.h:47
uint8_t uint8
Definition MyGUI_Types.h:45
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
types::TRect< float > FloatRect
Definition MyGUI_Types.h:33
const size_t TILERECT_COUNT_VERTEX