MyGUI: MyGUI_TileRect.cpp Source File

MyGUI 3.4.2

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

- File List
- Globals

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