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