MyGUI: MyGUI_TileRect.cpp Source File

MyGUI 3.2.2

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"
9 #include "MyGUI_RenderItem.h"
10 #include "MyGUI_SkinManager.h"
11 #include "MyGUI_LanguageManager.h"
12 #include "MyGUI_LayerNode.h"
13 #include "MyGUI_CommonStateInfo.h"
14 #include "MyGUI_RenderManager.h"
15 #include "MyGUI_TextureUtility.h"
16
17 namespace MyGUI
18 {
19
20 const size_t TILERECT_COUNT_VERTEX = 16 * VertexQuad::VertexCount;
21
22 TileRect::TileRect() :
23 mEmptyView(false),
24 mCurrentColour(0xFFFFFFFF),
25 mNode(nullptr),
26 mRenderItem(nullptr),
27 mCountVertex(TILERECT_COUNT_VERTEX),
28 mRealTileWidth(0),
29 mRealTileHeight(0),
30 mTextureHeightOne(0),
31 mTextureWidthOne(0),
32 mTileH(true),
33 mTileV(true)
34 {
35 mVertexFormat = RenderManager::getInstance().getVertexFormat();
36 }
37
38 TileRect::~TileRect()
39 {
40 }
41
42 void TileRect::setVisible(bool _visible)
43 {
44 if (mVisible == _visible)
45 return;
46 mVisible = _visible;
47
48 if (nullptr != mNode)
49 mNode->outOfDate(mRenderItem);
50 }
51
52 void TileRect::setAlpha(float _alpha)
53 {
54 uint32 alpha = ((uint8)(_alpha * 255) << 24);
55 mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
56
57 if (nullptr != mNode)
58 mNode->outOfDate(mRenderItem);
59 }
60
61 void TileRect::_correctView()
62 {
63 if (nullptr != mNode)
64 mNode->outOfDate(mRenderItem);
65 }
66
67 void TileRect::_setAlign(const IntSize& _oldsize)
68 {
69 // необходимо разобраться
70 bool need_update = true;
71
72 // первоначальное выравнивание
73 if (mAlign.isHStretch())
74 {
75 // растягиваем
76 mCoord.width = mCoord.width + (mCroppedParent->getWidth() - _oldsize.width);
77 need_update = true;
78 mIsMargin = true; // при изменении размеров все пересчитывать
79 }
80 else if (mAlign.isRight())
81 {
82 // двигаем по правому краю
83 mCoord.left = mCoord.left + (mCroppedParent->getWidth() - _oldsize.width);
84 need_update = true;
85 }
86 else if (mAlign.isHCenter())
87 {
88 // выравнивание по горизонтали без растяжения
89 mCoord.left = (mCroppedParent->getWidth() - mCoord.width) / 2;
90 need_update = true;
91 }
92
93 if (mAlign.isVStretch())
94 {
95 // растягиваем
96 mCoord.height = mCoord.height + (mCroppedParent->getHeight() - _oldsize.height);
97 need_update = true;
98 mIsMargin = true; // при изменении размеров все пересчитывать
99 }
100 else if (mAlign.isBottom())
101 {
102 // двигаем по нижнему краю
103 mCoord.top = mCoord.top + (mCroppedParent->getHeight() - _oldsize.height);
104 need_update = true;
105 }
106 else if (mAlign.isVCenter())
107 {
108 // выравнивание по вертикали без растяжения
109 mCoord.top = (mCroppedParent->getHeight() - mCoord.height) / 2;
110 need_update = true;
111 }
112
113 if (need_update)
114 {
115 mCurrentCoord = mCoord;
116 if (!mTileH) mTileSize.width = mCoord.width;
117 if (!mTileV) mTileSize.height = mCoord.height;
118 _updateView();
119 }
120 }
121
122 void TileRect::_updateView()
123 {
124 bool margin = _checkMargin();
125
126 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
127
128 mCurrentCoord.left = mCoord.left + mMargin.left;
129 mCurrentCoord.top = mCoord.top + mMargin.top;
130 mCurrentCoord.width = _getViewWidth();
131 mCurrentCoord.height = _getViewHeight();
132
133 // подсчитываем необходимое колличество тайлов
134 if (!mEmptyView)
135 {
136 size_t count = 0;
137 if (!mTileSize.empty())
138 {
139 size_t count_x = mCoord.width / mTileSize.width;
140 if ((mCoord.width % mTileSize.width) > 0)
141 count_x ++;
142
143 size_t count_y = mCoord.height / mTileSize.height;
144 if ((mCoord.height % mTileSize.height) > 0)
145 count_y ++;
146
147 count = count_y * count_x * VertexQuad::VertexCount;
148 }
149
150 // нужно больше вершин
151 if (count > mCountVertex)
152 {
153 mCountVertex = count + TILERECT_COUNT_VERTEX;
154 if (nullptr != mRenderItem)
155 mRenderItem->reallockDrawItem(this, mCountVertex);
156 }
157 }
158
159 // вьюпорт стал битым
160 if (margin)
161 {
162 // проверка на полный выход за границу
163 if (_checkOutside())
164 {
165 // запоминаем текущее состояние
166 mIsMargin = margin;
167
168 // обновить перед выходом
169 if (nullptr != mNode)
170 mNode->outOfDate(mRenderItem);
171 return;
172 }
173 }
174
175 // запоминаем текущее состояние
176 mIsMargin = margin;
177
178 if (nullptr != mNode)
179 mNode->outOfDate(mRenderItem);
180 }
181
182 void TileRect::_setUVSet(const FloatRect& _rect)
183 {
184 mCurrentTexture = _rect;
185 if (nullptr != mNode)
186 mNode->outOfDate(mRenderItem);
187 }
188
189 void TileRect::doRender()
190 {
191 if (!mVisible || mEmptyView || mTileSize.empty())
192 return;
193
194 VertexQuad* quad = reinterpret_cast<VertexQuad*>(mRenderItem->getCurrentVertexBuffer());
195
196 const RenderTargetInfo& info = mRenderItem->getRenderTarget()->getInfo();
197
198 // размер одного тайла
199 mRealTileWidth = info.pixScaleX * (float)(mTileSize.width) * 2;
200 mRealTileHeight = info.pixScaleY * (float)(mTileSize.height) * 2;
201
202 mTextureHeightOne = (mCurrentTexture.bottom - mCurrentTexture.top) / mRealTileHeight;
203 mTextureWidthOne = (mCurrentTexture.right - mCurrentTexture.left) / mRealTileWidth;
204
205 float vertex_z = mNode->getNodeDepth();
206
207 // абсолютный размер окна
208 float window_left = ((info.pixScaleX * (float)(mCoord.left + mCroppedParent->getAbsoluteLeft() - info.leftOffset) + info.hOffset) * 2) - 1;
209 float window_top = -(((info.pixScaleY * (float)(mCoord.top + mCroppedParent->getAbsoluteTop() - info.topOffset) + info.vOffset) * 2) - 1);
210
211 // размер вьюпорта
212 float real_left = ((info.pixScaleX * (float)(mCurrentCoord.left + mCroppedParent->getAbsoluteLeft() - info.leftOffset) + info.hOffset) * 2) - 1;
213 float real_right = real_left + (info.pixScaleX * (float)mCurrentCoord.width * 2);
214 float real_top = -(((info.pixScaleY * (float)(mCurrentCoord.top + mCroppedParent->getAbsoluteTop() - info.topOffset) + info.vOffset) * 2) - 1);
215 float real_bottom = real_top - (info.pixScaleY * (float)mCurrentCoord.height * 2);
216
217 size_t count = 0;
218
219 float left = window_left;
220 float right = window_left;
221 float top = window_top;
222 float bottom = window_top;
223
224 for (int y = 0; y < mCoord.height; y += mTileSize.height)
225 {
226 top = bottom;
227 bottom -= mRealTileHeight;
228 right = window_left;
229
230 float vertex_top = top;
231 float vertex_bottom = bottom;
232 bool texture_crop_height = false;
233
234 if (vertex_top > real_top)
235 {
236 // проверка на полный выход
237 if (vertex_bottom > real_top)
238 {
239 continue;
240 }
241 // обрезаем
242 vertex_top = real_top;
243 texture_crop_height = true;
244 }
245 if (vertex_bottom < real_bottom)
246 {
247 // вообще вниз ушли
248 if (vertex_top < real_bottom)
249 {
250 continue;
251 }
252 // обрезаем
253 vertex_bottom = real_bottom;
254 texture_crop_height = true;
255 }
256
257 for (int x = 0; x < mCoord.width; x += mTileSize.width)
258 {
259 left = right;
260 right += mRealTileWidth;
261
262 float vertex_left = left;
263 float vertex_right = right;
264 bool texture_crop_width = false;
265
266
267 if (vertex_left < real_left)
268 {
269 // проверка на полный выход
270 if (vertex_right < real_left)
271 {
272 continue;
273 }
274 // обрезаем
275 vertex_left = real_left;
276 texture_crop_width = true;
277 }
278
279 if (vertex_right > real_right)
280 {
281 // вообще строку до конца не нуна
282 if (vertex_left > real_right)
283 {
284 continue;
285 }
286 // обрезаем
287 vertex_right = real_right;
288 texture_crop_width = true;
289 }
290
291 // текущие текстурные координаты
292 float texture_left = mCurrentTexture.left;
293 float texture_right = mCurrentTexture.right;
294 float texture_top = mCurrentTexture.top;
295 float texture_bottom = mCurrentTexture.bottom;
296
297 // смещение текстуры по вертикили
298 if (texture_crop_height)
299 {
300 // прибавляем размер смещения в текстурных координатах
301 texture_top += (top - vertex_top) * mTextureHeightOne;
302 // отнимаем размер смещения в текстурных координатах
303 texture_bottom -= (vertex_bottom - bottom) * mTextureHeightOne;
304 }
305
306 // смещение текстуры по горизонтали
307 if (texture_crop_width)
308 {
309 // прибавляем размер смещения в текстурных координатах
310 texture_left += (vertex_left - left) * mTextureWidthOne;
311 // отнимаем размер смещения в текстурных координатах
312 texture_right -= (right - vertex_right) * mTextureWidthOne;
313 }
314
315 quad[count].set(
316 vertex_left,
317 vertex_top,
318 vertex_right,
319 vertex_bottom,
320 vertex_z,
321 texture_left,
322 texture_top,
323 texture_right,
324 texture_bottom,
325 mCurrentColour);
326
327 count ++;
328 }
329 }
330
331 mRenderItem->setLastVertexCount(VertexQuad::VertexCount * count);
332 }
333
334 void TileRect::createDrawItem(ITexture* _texture, ILayerNode* _node)
335 {
336 MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
337
338 mNode = _node;
339 mRenderItem = mNode->addToRenderItem(_texture, true, false);
340 mRenderItem->addDrawItem(this, mCountVertex);
341 }
342
343 void TileRect::destroyDrawItem()
344 {
345 MYGUI_ASSERT(mRenderItem, "mRenderItem must be not nullptr");
346
347 mNode = nullptr;
348 mRenderItem->removeDrawItem(this);
349 mRenderItem = nullptr;
350 }
351
352 void TileRect::setStateData(IStateInfo* _data)
353 {
354 TileRectStateInfo* data = _data->castType<TileRectStateInfo>();
355
356 mTileSize = data->getTileSize();
357 mTileH = data->getTileH();
358 mTileV = data->getTileV();
359 _setUVSet(data->getRect());
360 }
361
362 void TileRect::_setColour(const Colour& _value)
363 {
364 uint32 colour = texture_utility::toColourARGB(_value);
365 texture_utility::convertColour(colour, mVertexFormat);
366 mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
367
368 if (nullptr != mNode)
369 mNode->outOfDate(mRenderItem);
370 }
371
372 } // namespace MyGUI
MyGUI::TileRect::doRender
virtual void doRender()
Definition: MyGUI_TileRect.cpp:189
MyGUI::Singleton< RenderManager >::getInstance
static RenderManager & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI::types::TRect::bottom
T bottom
Definition: MyGUI_TRect.h:23
MyGUI::RenderItem::setLastVertexCount
void setLastVertexCount(size_t _count)
Definition: MyGUI_RenderItem.cpp:235
MyGUI::Align::isHCenter
bool isHCenter() const
Definition: MyGUI_Align.h:44
MyGUI::types::TSize::height
T height
Definition: MyGUI_TSize.h:21
MyGUI::RenderItem::reallockDrawItem
void reallockDrawItem(ISubWidget *_item, size_t _count)
Definition: MyGUI_RenderItem.cpp:143
MyGUI::VertexQuad::set
void set(float _l, float _t, float _r, float _b, float _z, float _u1, float _v1, float _u2, float _v2, uint32 _colour)
Definition: MyGUI_VertexData.h:46
MyGUI::types::TCoord::left
T left
Definition: MyGUI_TCoord.h:22
MyGUI::ICroppedRectangle::mIsMargin
bool mIsMargin
Definition: MyGUI_ICroppedRectangle.h:248
MyGUI::TileRect::mTileSize
IntSize mTileSize
Definition: MyGUI_TileRect.h:64
MyGUI::TileRect::mVertexFormat
VertexColourType mVertexFormat
Definition: MyGUI_TileRect.h:55
MyGUI::TileRect::mCurrentColour
uint32 mCurrentColour
Definition: MyGUI_TileRect.h:56
MyGUI::ICroppedRectangle::_getViewHeight
int _getViewHeight() const
Definition: MyGUI_ICroppedRectangle.h:153
MyGUI::types::TCoord::top
T top
Definition: MyGUI_TCoord.h:23
MyGUI::ICroppedRectangle::getAbsoluteTop
int getAbsoluteTop() const
Definition: MyGUI_ICroppedRectangle.h:88
MyGUI::types::TRect< float >
MyGUI::ILayerNode::addToRenderItem
virtual RenderItem * addToRenderItem(ITexture *_texture, bool _firstQueue, bool _separate)=0
MyGUI::TileRect::mRealTileWidth
float mRealTileWidth
Definition: MyGUI_TileRect.h:67
MyGUI::TileRect::_updateView
virtual void _updateView()
Definition: MyGUI_TileRect.cpp:122
MyGUI::TileRect::createDrawItem
virtual void createDrawItem(ITexture *_texture, ILayerNode *_node)
Definition: MyGUI_TileRect.cpp:334
MyGUI::ILayerNode::outOfDate
virtual void outOfDate(RenderItem *_item)=0
MyGUI::types::TRect::right
T right
Definition: MyGUI_TRect.h:22
MyGUI::TileRect::mTileV
bool mTileV
Definition: MyGUI_TileRect.h:74
MyGUI::ILayerNode
Definition: MyGUI_ILayerNode.h:27
MyGUI::ICroppedRectangle::getAbsoluteLeft
int getAbsoluteLeft() const
Definition: MyGUI_ICroppedRectangle.h:83
MyGUI::TileRectStateInfo::getTileH
bool getTileH() const
Definition: MyGUI_CommonStateInfo.h:76
MyGUI::ICroppedRectangle::mMargin
IntRect mMargin
Definition: MyGUI_ICroppedRectangle.h:244
MyGUI::Align::isVCenter
bool isVCenter() const
Definition: MyGUI_Align.h:49
MyGUI_CommonStateInfo.h
MyGUI::TileRect::_setColour
virtual void _setColour(const Colour &_value)
Definition: MyGUI_TileRect.cpp:362
MyGUI::types::TSize::width
T width
Definition: MyGUI_TSize.h:20
MyGUI::TileRect::TileRect
TileRect()
Definition: MyGUI_TileRect.cpp:22
MyGUI_LayerNode.h
MyGUI::types::TRect::left
T left
Definition: MyGUI_TRect.h:20
MyGUI::types::TSize::empty
bool empty() const
Definition: MyGUI_TSize.h:108
MyGUI::ISubWidget::mVisible
bool mVisible
Definition: MyGUI_ISubWidget.h:61
MyGUI::uint32
unsigned int uint32
Definition: MyGUI_Types.h:48
MyGUI::TileRect::mRealTileHeight
float mRealTileHeight
Definition: MyGUI_TileRect.h:68
MyGUI::ILayerNode::getNodeDepth
virtual float getNodeDepth()=0
MyGUI::TileRectStateInfo::getTileV
bool getTileV() const
Definition: MyGUI_CommonStateInfo.h:81
MyGUI::TileRect::mCountVertex
size_t mCountVertex
Definition: MyGUI_TileRect.h:65
MyGUI::IStateInfo
Definition: MyGUI_IStateInfo.h:16
MyGUI_Precompiled.h
MyGUI::ICroppedRectangle::_checkOutside
bool _checkOutside() const
Definition: MyGUI_ICroppedRectangle.h:235
MyGUI::TileRect::_correctView
virtual void _correctView()
Definition: MyGUI_TileRect.cpp:61
MyGUI::TileRect::mNode
ILayerNode * mNode
Definition: MyGUI_TileRect.h:61
MyGUI::TileRect::_setAlign
virtual void _setAlign(const IntSize &_oldsize)
Definition: MyGUI_TileRect.cpp:67
MyGUI::TileRect::destroyDrawItem
virtual void destroyDrawItem()
Definition: MyGUI_TileRect.cpp:343
MyGUI::TILERECT_COUNT_VERTEX
const size_t TILERECT_COUNT_VERTEX
Definition: MyGUI_TileRect.cpp:20
MyGUI::ICroppedRectangle::mCroppedParent
ICroppedRectangle * mCroppedParent
Definition: MyGUI_ICroppedRectangle.h:249
MyGUI::ICroppedRectangle::mCoord
IntCoord mCoord
Definition: MyGUI_ICroppedRectangle.h:245
MyGUI::TileRect::mTileH
bool mTileH
Definition: MyGUI_TileRect.h:73
MyGUI::TileRect::mEmptyView
bool mEmptyView
Definition: MyGUI_TileRect.h:53
MyGUI::IRenderTarget::getInfo
virtual const RenderTargetInfo & getInfo()=0
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:42
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:18
MyGUI::texture_utility::convertColour
__inline void convertColour(uint32 &_colour, VertexColourType _format)
Definition: MyGUI_TextureUtility.h:23
MyGUI::VertexQuad::VertexCount
Definition: MyGUI_VertexData.h:43
MyGUI::TileRect::mCurrentTexture
FloatRect mCurrentTexture
Definition: MyGUI_TileRect.h:58
MyGUI::Align::isVStretch
bool isVStretch() const
Definition: MyGUI_Align.h:84
MyGUI::RenderItem::getRenderTarget
IRenderTarget * getRenderTarget()
Definition: MyGUI_RenderItem.cpp:240
MyGUI::TileRectStateInfo::getTileSize
const IntSize & getTileSize() const
Definition: MyGUI_CommonStateInfo.h:71
MyGUI::VertexQuad
Definition: MyGUI_VertexData.h:33
MyGUI_LanguageManager.h
MyGUI::TileRect::_setUVSet
virtual void _setUVSet(const FloatRect &_rect)
Definition: MyGUI_TileRect.cpp:182
MyGUI::uint8
unsigned char uint8
Definition: MyGUI_Types.h:46
MyGUI_TextureUtility.h
MyGUI::types::TSize< int >
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI_RenderItem.h
MyGUI::ISubWidget::mAlign
Align mAlign
Definition: MyGUI_ISubWidget.h:60
MyGUI::Align::isRight
bool isRight() const
Definition: MyGUI_Align.h:64
MyGUI::ITexture
Definition: MyGUI_ITexture.h:26
MyGUI::Colour
Definition: MyGUI_Colour.h:16
MyGUI::types::TRect::top
T top
Definition: MyGUI_TRect.h:21
MyGUI::TileRect::setVisible
virtual void setVisible(bool _visible)
Definition: MyGUI_TileRect.cpp:42
MyGUI::RenderTargetInfo
Definition: MyGUI_RenderTargetInfo.h:15
MyGUI_RenderManager.h
MyGUI::Align::isBottom
bool isBottom() const
Definition: MyGUI_Align.h:79
MyGUI::types::TCoord::width
T width
Definition: MyGUI_TCoord.h:24
MyGUI_SkinManager.h
MyGUI::RenderItem::removeDrawItem
void removeDrawItem(ISubWidget *_item)
Definition: MyGUI_RenderItem.cpp:100
MyGUI::ICroppedRectangle::_checkMargin
bool _checkMargin()
Definition: MyGUI_ICroppedRectangle.h:185
MyGUI::ICroppedRectangle::_getViewWidth
int _getViewWidth() const
Definition: MyGUI_ICroppedRectangle.h:149
MyGUI::RenderManager::getVertexFormat
virtual VertexColourType getVertexFormat()=0
MyGUI::ICroppedRectangle::getHeight
int getHeight() const
Definition: MyGUI_ICroppedRectangle.h:119
MyGUI::RenderItem::addDrawItem
void addDrawItem(ISubWidget *_item, size_t _count)
Definition: MyGUI_RenderItem.cpp:125
MyGUI_TileRect.h
MyGUI::RenderItem::getCurrentVertexBuffer
Vertex * getCurrentVertexBuffer() const
Definition: MyGUI_RenderItem.cpp:230
MyGUI::TileRect::~TileRect
virtual ~TileRect()
Definition: MyGUI_TileRect.cpp:38
MyGUI::ICroppedRectangle::getWidth
int getWidth() const
Definition: MyGUI_ICroppedRectangle.h:114
MyGUI::TileRect::setAlpha
virtual void setAlpha(float _alpha)
Definition: MyGUI_TileRect.cpp:52
MyGUI::TileRectStateInfo::getRect
const FloatRect & getRect() const
Definition: MyGUI_CommonStateInfo.h:66
MyGUI::TileRectStateInfo
Definition: MyGUI_CommonStateInfo.h:52
MyGUI::TileRect::setStateData
virtual void setStateData(IStateInfo *_data)
Definition: MyGUI_TileRect.cpp:352
MyGUI::TileRect::mCurrentCoord
IntCoord mCurrentCoord
Definition: MyGUI_TileRect.h:59
MyGUI::types::TCoord::height
T height
Definition: MyGUI_TCoord.h:25
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::Align::isHStretch
bool isHStretch() const
Definition: MyGUI_Align.h:69
MyGUI::texture_utility::toColourARGB
uint32 toColourARGB(const Colour &_colour)
Definition: MyGUI_TextureUtility.cpp:69
MyGUI::TileRect::mTextureHeightOne
float mTextureHeightOne
Definition: MyGUI_TileRect.h:70
MyGUI::TileRect::mTextureWidthOne
float mTextureWidthOne
Definition: MyGUI_TileRect.h:71
MyGUI::TileRect::mRenderItem
RenderItem * mRenderItem
Definition: MyGUI_TileRect.h:62