MyGUI 3.2.2

MyGUI_SubSkin.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_SubSkin.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 SubSkin::SubSkin() :
21 ISubWidgetRect(),
22 mEmptyView(false),
23 mCurrentColour(0xFFFFFFFF),
24 mNode(nullptr),
25 mRenderItem(nullptr),
26 mSeparate(false)
27 {
28 mVertexFormat = RenderManager::getInstance().getVertexFormat();
29 }
30
31 SubSkin::~SubSkin()
32 {
33 }
34
35 void SubSkin::setVisible(bool _visible)
36 {
37 if (mVisible == _visible)
38 return;
39 mVisible = _visible;
40
41 if (nullptr != mNode)
42 mNode->outOfDate(mRenderItem);
43 }
44
45 void SubSkin::setAlpha(float _alpha)
46 {
47 uint32 alpha = ((uint8)(_alpha * 255) << 24);
48 mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
49
50 if (nullptr != mNode)
51 mNode->outOfDate(mRenderItem);
52 }
53
54 void SubSkin::_correctView()
55 {
56 if (nullptr != mNode)
57 mNode->outOfDate(mRenderItem);
58 }
59
60 void SubSkin::_setAlign(const IntSize& _oldsize)
61 {
62 // необходимо разобраться
63 bool need_update = true;//_update;
64
65 // первоначальное выравнивание
66 if (mAlign.isHStretch())
67 {
68 // растягиваем
69 mCoord.width = mCoord.width + (mCroppedParent->getWidth() - _oldsize.width);
70 need_update = true;
71 mIsMargin = true; // при изменении размеров все пересчитывать
72 }
73 else if (mAlign.isRight())
74 {
75 // двигаем по правому краю
76 mCoord.left = mCoord.left + (mCroppedParent->getWidth() - _oldsize.width);
77 need_update = true;
78 }
79 else if (mAlign.isHCenter())
80 {
81 // выравнивание по горизонтали без растяжения
82 mCoord.left = (mCroppedParent->getWidth() - mCoord.width) / 2;
83 need_update = true;
84 }
85
86 if (mAlign.isVStretch())
87 {
88 // растягиваем
89 mCoord.height = mCoord.height + (mCroppedParent->getHeight() - _oldsize.height);
90 need_update = true;
91 mIsMargin = true; // при изменении размеров все пересчитывать
92 }
93 else if (mAlign.isBottom())
94 {
95 // двигаем по нижнему краю
96 mCoord.top = mCoord.top + (mCroppedParent->getHeight() - _oldsize.height);
97 need_update = true;
98 }
99 else if (mAlign.isVCenter())
100 {
101 // выравнивание по вертикали без растяжения
102 mCoord.top = (mCroppedParent->getHeight() - mCoord.height) / 2;
103 need_update = true;
104 }
105
106 if (need_update)
107 {
108 mCurrentCoord = mCoord;
109 _updateView();
110 }
111 }
112
113 void SubSkin::_updateView()
114 {
115 //mAbsolutePosition = mCroppedParent->getAbsolutePosition() + mCoord.point();
116 bool margin = _checkMargin();
117
118 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
119
120 mCurrentCoord.left = mCoord.left + mMargin.left;
121 mCurrentCoord.top = mCoord.top + mMargin.top;
122
123 // вьюпорт стал битым
124 if (margin)
125 {
126 // проверка на полный выход за границу
127 if (_checkOutside())
128 {
129 // запоминаем текущее состояние
130 mIsMargin = margin;
131
132 // обновить перед выходом
133 if (nullptr != mNode)
134 mNode->outOfDate(mRenderItem);
135 return;
136 }
137 }
138
139 // мы обрезаны или были обрезаны
140 if (mIsMargin || margin)
141 {
142 mCurrentCoord.width = _getViewWidth();
143 mCurrentCoord.height = _getViewHeight();
144
145 if ((mCurrentCoord.width > 0) && (mCurrentCoord.height > 0))
146 {
147 // теперь смещаем текстуру
148 float UV_lft = mMargin.left / (float)mCoord.width;
149 float UV_top = mMargin.top / (float)mCoord.height;
150 float UV_rgt = (mCoord.width - mMargin.right) / (float)mCoord.width;
151 float UV_btm = (mCoord.height - mMargin.bottom) / (float)mCoord.height;
152
153 float UV_sizeX = mRectTexture.right - mRectTexture.left;
154 float UV_sizeY = mRectTexture.bottom - mRectTexture.top;
155
156 float UV_lft_total = mRectTexture.left + UV_lft * UV_sizeX;
157 float UV_top_total = mRectTexture.top + UV_top * UV_sizeY;
158 float UV_rgt_total = mRectTexture.right - (1 - UV_rgt) * UV_sizeX;
159 float UV_btm_total = mRectTexture.bottom - (1 - UV_btm) * UV_sizeY;
160
161 mCurrentTexture.set(UV_lft_total, UV_top_total, UV_rgt_total, UV_btm_total);
162 }
163 }
164
165 if (mIsMargin && !margin)
166 {
167 // мы не обрезаны, но были, ставим базовые координаты
168 mCurrentTexture = mRectTexture;
169 }
170
171 // запоминаем текущее состояние
172 mIsMargin = margin;
173
174 if (nullptr != mNode)
175 mNode->outOfDate(mRenderItem);
176 }
177
178 void SubSkin::createDrawItem(ITexture* _texture, ILayerNode* _node)
179 {
180 MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
181
182 mNode = _node;
183 mRenderItem = mNode->addToRenderItem(_texture, true, mSeparate);
184 mRenderItem->addDrawItem(this, VertexQuad::VertexCount);
185 }
186
187 void SubSkin::destroyDrawItem()
188 {
189 MYGUI_ASSERT(mRenderItem, "mRenderItem must be not nullptr");
190
191 mNode = nullptr;
192 mRenderItem->removeDrawItem(this);
193 mRenderItem = nullptr;
194 }
195
196 void SubSkin::_setUVSet(const FloatRect& _rect)
197 {
198 if (mRectTexture == _rect)
199 return;
200 mRectTexture = _rect;
201
202 // если обрезаны, то просчитываем с учето обрезки
203 if (mIsMargin)
204 {
205 float UV_lft = mMargin.left / (float)mCoord.width;
206 float UV_top = mMargin.top / (float)mCoord.height;
207 float UV_rgt = (mCoord.width - mMargin.right) / (float)mCoord.width;
208 float UV_btm = (mCoord.height - mMargin.bottom) / (float)mCoord.height;
209
210 float UV_sizeX = mRectTexture.right - mRectTexture.left;
211 float UV_sizeY = mRectTexture.bottom - mRectTexture.top;
212
213 float UV_lft_total = mRectTexture.left + UV_lft * UV_sizeX;
214 float UV_top_total = mRectTexture.top + UV_top * UV_sizeY;
215 float UV_rgt_total = mRectTexture.right - (1 - UV_rgt) * UV_sizeX;
216 float UV_btm_total = mRectTexture.bottom - (1 - UV_btm) * UV_sizeY;
217
218 mCurrentTexture.set(UV_lft_total, UV_top_total, UV_rgt_total, UV_btm_total);
219 }
220 // мы не обрезаны, базовые координаты
221 else
222 {
223 mCurrentTexture = mRectTexture;
224 }
225
226 if (nullptr != mNode)
227 mNode->outOfDate(mRenderItem);
228 }
229
230 void SubSkin::doRender()
231 {
232 if (!mVisible || mEmptyView)
233 return;
234
235 VertexQuad* quad = reinterpret_cast<VertexQuad*>(mRenderItem->getCurrentVertexBuffer());
236
237 const RenderTargetInfo& info = mRenderItem->getRenderTarget()->getInfo();
238
239 float vertex_z = mNode->getNodeDepth();
240
241 float vertex_left = ((info.pixScaleX * (float)(mCurrentCoord.left + mCroppedParent->getAbsoluteLeft() - info.leftOffset) + info.hOffset) * 2) - 1;
242 float vertex_right = vertex_left + (info.pixScaleX * (float)mCurrentCoord.width * 2);
243 float vertex_top = -(((info.pixScaleY * (float)(mCurrentCoord.top + mCroppedParent->getAbsoluteTop() - info.topOffset) + info.vOffset) * 2) - 1);
244 float vertex_bottom = vertex_top - (info.pixScaleY * (float)mCurrentCoord.height * 2);
245
246 quad->set(
247 vertex_left,
248 vertex_top,
249 vertex_right,
250 vertex_bottom,
251 vertex_z,
252 mCurrentTexture.left,
253 mCurrentTexture.top,
254 mCurrentTexture.right,
255 mCurrentTexture.bottom,
256 mCurrentColour);
257
258 mRenderItem->setLastVertexCount(VertexQuad::VertexCount);
259 }
260
261 void SubSkin::_setColour(const Colour& _value)
262 {
263 uint32 colour = texture_utility::toColourARGB(_value);
264 texture_utility::convertColour(colour, mVertexFormat);
265 mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
266
267 if (nullptr != mNode)
268 mNode->outOfDate(mRenderItem);
269 }
270
271 void SubSkin::setStateData(IStateInfo* _data)
272 {
273 _setUVSet(_data->castType<SubSkinStateInfo>()->getRect());
274 }
275
276 } // namespace MyGUI
MyGUI_TextureUtility.h
MyGUI::SubSkin::mEmptyView
bool mEmptyView
Definition: MyGUI_SubSkin.h:55
MyGUI::uint32
unsigned int uint32
Definition: MyGUI_Types.h:48
MyGUI::ILayerNode::outOfDate
virtual void outOfDate(RenderItem *_item)=0
MyGUI::RenderTargetInfo::pixScaleY
float pixScaleY
Definition: MyGUI_RenderTargetInfo.h:39
MyGUI::ILayerNode
Definition: MyGUI_ILayerNode.h:27
MyGUI::ISubWidgetRect
Definition: MyGUI_ISubWidgetRect.h:17
MyGUI::RenderItem::removeDrawItem
void removeDrawItem(ISubWidget *_item)
Definition: MyGUI_RenderItem.cpp:100
MyGUI::Align::isRight
bool isRight() const
Definition: MyGUI_Align.h:64
MyGUI::VertexQuad
Definition: MyGUI_VertexData.h:33
MyGUI::SubSkin::~SubSkin
virtual ~SubSkin()
Definition: MyGUI_SubSkin.cpp:31
MyGUI::types::TRect::top
T top
Definition: MyGUI_TRect.h:21
MyGUI::RenderItem::addDrawItem
void addDrawItem(ISubWidget *_item, size_t _count)
Definition: MyGUI_RenderItem.cpp:125
MyGUI_Precompiled.h
MyGUI::RenderTargetInfo::leftOffset
int leftOffset
Definition: MyGUI_RenderTargetInfo.h:44
MyGUI::ICroppedRectangle::mIsMargin
bool mIsMargin
Definition: MyGUI_ICroppedRectangle.h:248
MyGUI::RenderItem::getRenderTarget
IRenderTarget * getRenderTarget()
Definition: MyGUI_RenderItem.cpp:240
MyGUI::SubSkin::createDrawItem
virtual void createDrawItem(ITexture *_texture, ILayerNode *_node)
Definition: MyGUI_SubSkin.cpp:178
MyGUI::texture_utility::convertColour
__inline void convertColour(uint32 &_colour, VertexColourType _format)
Definition: MyGUI_TextureUtility.h:23
MyGUI::IStateInfo
Definition: MyGUI_IStateInfo.h:16
MyGUI::SubSkinStateInfo::getRect
const FloatRect & getRect() const
Definition: MyGUI_CommonStateInfo.h:27
MyGUI::types::TSize< int >
MyGUI::SubSkin::mNode
ILayerNode * mNode
Definition: MyGUI_SubSkin.h:63
MyGUI::SubSkin::setStateData
virtual void setStateData(IStateInfo *_data)
Definition: MyGUI_SubSkin.cpp:271
MyGUI::Singleton< RenderManager >::getInstance
static RenderManager & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI::SubSkin::mRenderItem
RenderItem * mRenderItem
Definition: MyGUI_SubSkin.h:64
MyGUI::ILayerNode::getNodeDepth
virtual float getNodeDepth()=0
MyGUI::RenderTargetInfo::topOffset
int topOffset
Definition: MyGUI_RenderTargetInfo.h:45
MyGUI::SubSkin::mRectTexture
FloatRect mRectTexture
Definition: MyGUI_SubSkin.h:54
MyGUI::ICroppedRectangle::mCroppedParent
ICroppedRectangle * mCroppedParent
Definition: MyGUI_ICroppedRectangle.h:249
MyGUI::RenderManager::getVertexFormat
virtual VertexColourType getVertexFormat()=0
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::ICroppedRectangle::getAbsoluteTop
int getAbsoluteTop() const
Definition: MyGUI_ICroppedRectangle.h:88
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI::types::TCoord::width
T width
Definition: MyGUI_TCoord.h:24
MyGUI_LanguageManager.h
MyGUI::SubSkin::setAlpha
virtual void setAlpha(float _alpha)
Definition: MyGUI_SubSkin.cpp:45
MyGUI::Align::isHCenter
bool isHCenter() const
Definition: MyGUI_Align.h:44
MyGUI::ICroppedRectangle::_getViewWidth
int _getViewWidth() const
Definition: MyGUI_ICroppedRectangle.h:149
MyGUI::ILayerNode::addToRenderItem
virtual RenderItem * addToRenderItem(ITexture *_texture, bool _firstQueue, bool _separate)=0
MyGUI::ICroppedRectangle::mCoord
IntCoord mCoord
Definition: MyGUI_ICroppedRectangle.h:245
MyGUI::Align::isVStretch
bool isVStretch() const
Definition: MyGUI_Align.h:84
MyGUI::types::TCoord::top
T top
Definition: MyGUI_TCoord.h:23
MyGUI::SubSkin::_setColour
virtual void _setColour(const Colour &_value)
Definition: MyGUI_SubSkin.cpp:261
MyGUI::SubSkin::_setUVSet
virtual void _setUVSet(const FloatRect &_rect)
Definition: MyGUI_SubSkin.cpp:196
MyGUI::types::TRect::left
T left
Definition: MyGUI_TRect.h:20
MyGUI::ICroppedRectangle::getWidth
int getWidth() const
Definition: MyGUI_ICroppedRectangle.h:114
MyGUI_RenderManager.h
MyGUI::ICroppedRectangle::mMargin
IntRect mMargin
Definition: MyGUI_ICroppedRectangle.h:244
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::VertexQuad::VertexCount
Definition: MyGUI_VertexData.h:43
MyGUI::SubSkin::mCurrentTexture
FloatRect mCurrentTexture
Definition: MyGUI_SubSkin.h:60
MyGUI::types::TRect< float >
MyGUI::RenderItem::getCurrentVertexBuffer
Vertex * getCurrentVertexBuffer() const
Definition: MyGUI_RenderItem.cpp:230
MyGUI::types::TSize::width
T width
Definition: MyGUI_TSize.h:20
MyGUI::ICroppedRectangle::getAbsoluteLeft
int getAbsoluteLeft() const
Definition: MyGUI_ICroppedRectangle.h:83
MyGUI::ISubWidget::mVisible
bool mVisible
Definition: MyGUI_ISubWidget.h:61
MyGUI::RenderTargetInfo::pixScaleX
float pixScaleX
Definition: MyGUI_RenderTargetInfo.h:38
MyGUI::SubSkin::mSeparate
bool mSeparate
Definition: MyGUI_SubSkin.h:66
MyGUI::Align::isHStretch
bool isHStretch() const
Definition: MyGUI_Align.h:69
MyGUI::SubSkin::_setAlign
virtual void _setAlign(const IntSize &_oldsize)
Definition: MyGUI_SubSkin.cpp:60
MyGUI_CommonStateInfo.h
MyGUI_RenderItem.h
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:42
MyGUI::ITexture
Definition: MyGUI_ITexture.h:26
MyGUI::IRenderTarget::getInfo
virtual const RenderTargetInfo & getInfo()=0
MyGUI::texture_utility::toColourARGB
uint32 toColourARGB(const Colour &_colour)
Definition: MyGUI_TextureUtility.cpp:69
MyGUI::RenderTargetInfo::hOffset
float hOffset
Definition: MyGUI_RenderTargetInfo.h:40
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:18
MyGUI::types::TRect::set
void set(T const &_left, T const &_top, T const &_right, T const &_bottom)
Definition: MyGUI_TRect.h:121
MyGUI::SubSkinStateInfo
Definition: MyGUI_CommonStateInfo.h:19
MyGUI::SubSkin::mVertexFormat
VertexColourType mVertexFormat
Definition: MyGUI_SubSkin.h:57
MyGUI_LayerNode.h
MyGUI::ISubWidget::mAlign
Align mAlign
Definition: MyGUI_ISubWidget.h:60
MyGUI::SubSkin::setVisible
virtual void setVisible(bool _visible)
Definition: MyGUI_SubSkin.cpp:35
MyGUI::SubSkin::doRender
virtual void doRender()
Definition: MyGUI_SubSkin.cpp:230
MyGUI::ICroppedRectangle::_checkOutside
bool _checkOutside() const
Definition: MyGUI_ICroppedRectangle.h:235
MyGUI::ICroppedRectangle::_getViewHeight
int _getViewHeight() const
Definition: MyGUI_ICroppedRectangle.h:153
MyGUI::RenderTargetInfo
Definition: MyGUI_RenderTargetInfo.h:15
MyGUI::SubSkin::destroyDrawItem
virtual void destroyDrawItem()
Definition: MyGUI_SubSkin.cpp:187
MyGUI::Align::isBottom
bool isBottom() const
Definition: MyGUI_Align.h:79
MyGUI::Colour
Definition: MyGUI_Colour.h:16
MyGUI::Align::isVCenter
bool isVCenter() const
Definition: MyGUI_Align.h:49
MyGUI::RenderTargetInfo::vOffset
float vOffset
Definition: MyGUI_RenderTargetInfo.h:41
MyGUI::types::TSize::height
T height
Definition: MyGUI_TSize.h:21
MyGUI::types::TCoord::left
T left
Definition: MyGUI_TCoord.h:22
MyGUI::types::TRect::right
T right
Definition: MyGUI_TRect.h:22
MyGUI::SubSkin::SubSkin
SubSkin()
Definition: MyGUI_SubSkin.cpp:20
MyGUI::uint8
unsigned char uint8
Definition: MyGUI_Types.h:46
MyGUI::types::TRect::bottom
T bottom
Definition: MyGUI_TRect.h:23
MyGUI::ICroppedRectangle::_checkMargin
bool _checkMargin()
Definition: MyGUI_ICroppedRectangle.h:185
MyGUI_SubSkin.h
MyGUI::RenderItem::setLastVertexCount
void setLastVertexCount(size_t _count)
Definition: MyGUI_RenderItem.cpp:235
MyGUI_SkinManager.h
MyGUI::SubSkin::mCurrentCoord
IntCoord mCurrentCoord
Definition: MyGUI_SubSkin.h:61
MyGUI::types::TCoord::height
T height
Definition: MyGUI_TCoord.h:25
MyGUI::SubSkin::mCurrentColour
uint32 mCurrentColour
Definition: MyGUI_SubSkin.h:58
MyGUI::ICroppedRectangle::getHeight
int getHeight() const
Definition: MyGUI_ICroppedRectangle.h:119
MyGUI::SubSkin::_correctView
virtual void _correctView()
Definition: MyGUI_SubSkin.cpp:54
MyGUI::SubSkin::_updateView
virtual void _updateView()
Definition: MyGUI_SubSkin.cpp:113