MyGUI 3.5.3
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"
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
21 SubSkin::SubSkin()
22 {
23 mVertexFormat = RenderManager::getInstance().getVertexFormat();
24 }
25
26 void SubSkin::setVisible(bool _visible)
27 {
28 if (mVisible == _visible)
29 return;
30 mVisible = _visible;
31
32 if (nullptr != mNode)
33 mNode->outOfDate(mRenderItem);
34 }
35
36 void SubSkin::setAlpha(float _alpha)
37 {
38 uint32 alpha = ((uint8)(_alpha * 255) << 24);
39 if ((mCurrentColour & 0xFF000000) == alpha)
40 return;
41 mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
42
43 if (nullptr != mNode)
44 mNode->outOfDate(mRenderItem);
45 }
46
47 void SubSkin::_correctView()
48 {
49 if (nullptr != mNode)
50 mNode->outOfDate(mRenderItem);
51 }
52
53 void SubSkin::_setAlign(const IntSize& _oldsize)
54 {
55 auto [coord, resize, move] = CoordConverter::applyAlign(mAlign, mCoord, mCroppedParent->getSize(), _oldsize);
56 mCoord = coord;
57 if (resize)
58 mIsMargin = true;
59 mCurrentCoord = mCoord;
60 _updateView();
61 }
62
63 void SubSkin::_updateView()
64 {
65 bool margin = _checkMargin();
66
67 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
68
69 mCurrentCoord.left = mCoord.left + mMargin.left;
70 mCurrentCoord.top = mCoord.top + mMargin.top;
71
72 // viewport became broken
73 if (margin)
74 {
75 if (_checkOutsideUnrotated())
76 {
77 mIsMargin = margin;
78
79 if (nullptr != mNode)
80 mNode->outOfDate(mRenderItem);
81 return;
82 }
83 }
84
85 // we are clipped or were clipped
86 if (mIsMargin || margin)
87 {
88 mCurrentCoord.width = _getViewWidth();
89 mCurrentCoord.height = _getViewHeight();
90
91 if ((mCurrentCoord.width > 0) && (mCurrentCoord.height > 0))
92 {
93 _updateClippedUV();
94 }
95 }
96
97 if (mIsMargin && !margin)
98 {
99 // we are not clipped but were, set base coordinates
100 mCurrentTexture = mRectTexture;
101 }
102
103 mIsMargin = margin;
104
105 if (nullptr != mNode)
106 mNode->outOfDate(mRenderItem);
107 }
108
109 void SubSkin::createDrawItem(ITexture* _texture, ILayerNode* _node)
110 {
111 MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
112
113 mNode = _node;
114 mRenderItem = mNode->addToRenderItem(_texture, true);
115 mRenderItem->addDrawItem(this, VertexQuad::VertexCount);
116 }
117
118 void SubSkin::destroyDrawItem()
119 {
120 MYGUI_ASSERT(mRenderItem, "mRenderItem must be not nullptr");
121
122 mNode = nullptr;
123 mRenderItem->removeDrawItem(this);
124 mRenderItem = nullptr;
125 }
126
127 void SubSkin::_setUVSet(const FloatRect& _rect)
128 {
129 if (mRectTexture == _rect)
130 return;
131 mRectTexture = _rect;
132
133 // if clipped, calculate with clipping
134 if (mIsMargin)
135 {
136 _updateClippedUV();
137 }
138 // we are not clipped, base coordinates
139 else
140 {
141 mCurrentTexture = mRectTexture;
142 }
143
144 if (nullptr != mNode)
145 mNode->outOfDate(mRenderItem);
146 }
147
148 void SubSkin::_updateClippedUV()
149 {
150 float UV_lft = mMargin.left / (float)mCoord.width;
151 float UV_top = mMargin.top / (float)mCoord.height;
152 float UV_rgt = (mCoord.width - mMargin.right) / (float)mCoord.width;
153 float UV_btm = (mCoord.height - mMargin.bottom) / (float)mCoord.height;
154
155 float UV_sizeX = mRectTexture.right - mRectTexture.left;
156 float UV_sizeY = mRectTexture.bottom - mRectTexture.top;
157
158 float UV_lft_total = mRectTexture.left + UV_lft * UV_sizeX;
159 float UV_top_total = mRectTexture.top + UV_top * UV_sizeY;
160 float UV_rgt_total = mRectTexture.right - (1 - UV_rgt) * UV_sizeX;
161 float UV_btm_total = mRectTexture.bottom - (1 - UV_btm) * UV_sizeY;
162
163 mCurrentTexture.set(UV_lft_total, UV_top_total, UV_rgt_total, UV_btm_total);
164 }
165
166 void SubSkin::doRender()
167 {
168 if (!mVisible || mEmptyView)
169 return;
170
171 VertexQuad* quad = reinterpret_cast<VertexQuad*>(mRenderItem->getCurrentVertexBuffer());
172
173 const RenderTargetInfo& info = mRenderItem->getRenderTarget()->getInfo();
174
175 float vertex_z = mNode->getNodeDepth();
176
177 const FloatRect rect =
178 geometry_utility::toRenderTarget(mCurrentCoord, info, mCroppedParent->getAbsolutePosition());
179
180 quad->set(
181 rect.left,
182 rect.top,
183 rect.right,
184 rect.bottom,
185 vertex_z,
186 mCurrentTexture.left,
187 mCurrentTexture.top,
188 mCurrentTexture.right,
189 mCurrentTexture.bottom,
190 mCurrentColour);
191
192 mRenderItem->setLastVertexCount(VertexQuad::VertexCount);
193 }
194
195 void SubSkin::_setColour(const Colour& _value)
196 {
197 uint32 colour = texture_utility::toNativeColour(_value, mVertexFormat);
198 mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
199
200 if (nullptr != mNode)
201 mNode->outOfDate(mRenderItem);
202 }
203
204 void SubSkin::setStateData(IStateInfo* _data)
205 {
206 _setUVSet(_data->castType<SubSkinStateInfo>()->getRect());
207 }
208
209} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
FloatPoint toRenderTarget(FloatPoint _point, const RenderTargetInfo &_info, IntPoint _origin={})
uint32 toNativeColour(const Colour &_colour, VertexColourType _format)
uint32_t uint32
Definition MyGUI_Types.h:47
uint8_t uint8
Definition MyGUI_Types.h:45
types::TRect< float > FloatRect
Definition MyGUI_Types.h:33
types::TSize< int > IntSize
Definition MyGUI_Types.h:29