MyGUI 3.5.3
MyGUI_RotatingSkin.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
9#ifndef MYGUI_DONT_USE_OBSOLETE
10
11 #include "MyGUI_RotatingSkin.h"
12 #include "MyGUI_CoordConverter.h"
13 #include "MyGUI_RenderItem.h"
15 #include "MyGUI_RenderManager.h"
17
18namespace MyGUI
19{
20
21 RotatingSkin::RotatingSkin()
22 {
23 mVertexFormat = RenderManager::getInstance().getVertexFormat();
24 }
25
26 void RotatingSkin::setAngle(float _angle)
27 {
28 mAngle = _angle;
29 _correctView();
30 }
31
32 void RotatingSkin::setCenter(const IntPoint& _center)
33 {
34 mCenterPos = _center;
35 _correctView();
36 }
37
38 IntPoint RotatingSkin::getCenter(bool _local) const
39 {
40 return mCenterPos + (_local ? IntPoint() : mCroppedParent->getAbsolutePosition());
41 }
42
43 void RotatingSkin::setVisible(bool _visible)
44 {
45 if (mVisible == _visible)
46 return;
47
48 mVisible = _visible;
49 _correctView();
50 }
51
52 void RotatingSkin::setAlpha(float _alpha)
53 {
54 uint32 alpha = ((uint8)(_alpha * 255) << 24);
55 if ((mCurrentColour & 0xFF000000) == alpha)
56 return;
57 mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
58
59 if (nullptr != mNode)
60 mNode->outOfDate(mRenderItem);
61 }
62
63 void RotatingSkin::_correctView()
64 {
65 mGeometryOutdated = true;
66
67 if (nullptr != mNode)
68 mNode->outOfDate(mRenderItem);
69 }
70
71 void RotatingSkin::_setAlign(const IntSize& _oldsize)
72 {
73 auto [coord, resize, move] = CoordConverter::applyAlign(mAlign, mCoord, mCroppedParent->getSize(), _oldsize);
74 mCoord = coord;
75 if (resize)
76 mIsMargin = true;
77 mCurrentCoord = mCoord;
78 _updateView();
79 }
80
81 void RotatingSkin::_updateView()
82 {
83 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
84
85 _correctView();
86 }
87
88 void RotatingSkin::createDrawItem(ITexture* _texture, ILayerNode* _node)
89 {
90 MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
91
92 mNode = _node;
93 mRenderItem = mNode->addToRenderItem(_texture, true);
94 mRenderItem->addDrawItem(this, (GEOMETRY_VERTICIES_TOTAL_COUNT - 2) * 3);
95 }
96
97 void RotatingSkin::destroyDrawItem()
98 {
99 MYGUI_ASSERT(mRenderItem, "mRenderItem must be not nullptr");
100
101 mNode = nullptr;
102 mRenderItem->removeDrawItem(this);
103 mRenderItem = nullptr;
104 }
105
106 void RotatingSkin::doRender()
107 {
108 if (!mVisible || mEmptyView)
109 return;
110
111 Vertex* verticies = mRenderItem->getCurrentVertexBuffer();
112
113 float vertex_z = mNode->getNodeDepth();
114
115 if (mGeometryOutdated || mRenderItem->getCurrentUpdate())
116 {
117 _rebuildGeometry();
118 mGeometryOutdated = false;
119 }
120
121 for (size_t i = 1; i + 1 < mVertexCount; ++i)
122 {
123 verticies[3 * i - 3].set(
124 mResultVerticiesPos[0].left,
125 mResultVerticiesPos[0].top,
126 vertex_z,
127 mResultVerticiesUV[0].left,
128 mResultVerticiesUV[0].top,
129 mCurrentColour);
130 verticies[3 * i - 2].set(
131 mResultVerticiesPos[i].left,
132 mResultVerticiesPos[i].top,
133 vertex_z,
134 mResultVerticiesUV[i].left,
135 mResultVerticiesUV[i].top,
136 mCurrentColour);
137 verticies[3 * i - 1].set(
138 mResultVerticiesPos[i + 1].left,
139 mResultVerticiesPos[i + 1].top,
140 vertex_z,
141 mResultVerticiesUV[i + 1].left,
142 mResultVerticiesUV[i + 1].top,
143 mCurrentColour);
144 }
145
146 mRenderItem->setLastVertexCount(mVertexCount < 3 ? 0 : (mVertexCount - 2) * 3);
147 }
148
149 void RotatingSkin::_setColour(const Colour& _value)
150 {
151 uint32 colour = texture_utility::toNativeColour(_value, mVertexFormat);
152 mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
153
154 if (nullptr != mNode)
155 mNode->outOfDate(mRenderItem);
156 }
157
158 void RotatingSkin::setStateData(IStateInfo* _data)
159 {
160 RotatingSkinStateInfo* data = _data->castType<RotatingSkinStateInfo>();
161
162 setAngle(data->getAngle());
163 setCenter(data->getCenter());
164
165 _setUVSet(data->getRect());
166 }
167
168 void RotatingSkin::_setUVSet(const FloatRect& _rect)
169 {
170 mCurrentTexture = _rect;
171
172 _correctView();
173 }
174
175 void RotatingSkin::_rebuildGeometry()
176 {
177 // Rotate the four corners around the widget-local center.
178 const float left = (float)mCurrentCoord.left;
179 const float top = (float)mCurrentCoord.top;
180 const float right = (float)mCurrentCoord.right();
181 const float bottom = (float)mCurrentCoord.bottom();
182 FloatPoint corners[RECT_VERTICIES_COUNT] = {{left, top}, {right, top}, {right, bottom}, {left, bottom}};
183 const float cos = std::cos(mAngle);
184 const float sin = std::sin(mAngle);
185 for (auto& point : corners)
186 {
187 const float x = point.left - mCenterPos.left;
188 const float y = point.top - mCenterPos.top;
189 point = {mCenterPos.left + x * cos - y * sin, mCenterPos.top + x * sin + y * cos};
190 }
191
192 mVertexCount = RECT_VERTICIES_COUNT;
193 std::copy(corners, corners + RECT_VERTICIES_COUNT, mResultVerticiesPos);
194 if (ICroppedRectangle* parent = mCroppedParent->getCroppedParent())
195 {
197 corners,
198 RECT_VERTICIES_COUNT,
199 IntCoord(
200 parent->_getMarginLeft() - mCroppedParent->getLeft(),
201 parent->_getMarginTop() - mCroppedParent->getTop(),
202 parent->_getViewWidth(),
203 parent->_getViewHeight()));
204 mVertexCount = points.size();
205 std::copy(points.begin(), points.end(), mResultVerticiesPos);
206 }
207
208 const FloatPoint baseUV(mCurrentTexture.left, mCurrentTexture.top);
209 const FloatPoint u(mCurrentTexture.right - mCurrentTexture.left, 0);
210 const FloatPoint v(0, mCurrentTexture.bottom - mCurrentTexture.top);
211 for (size_t i = 0; i < mVertexCount; ++i)
212 {
213 const FloatPoint position =
214 geometry_utility::getPositionInsideRect(mResultVerticiesPos[i], corners[0], corners[1], corners[3]);
215 mResultVerticiesUV[i] = geometry_utility::getUVFromPositionInsideRect(position, v, u, baseUV);
216 }
217
219 mResultVerticiesPos,
220 mVertexCount,
221 mRenderItem->getRenderTarget()->getInfo(),
222 mCroppedParent->getAbsolutePosition());
223 }
224
225 float RotatingSkin::getAngle() const
226 {
227 return mAngle;
228 }
229
230} // namespace MyGUI
231
232#endif // MYGUI_DONT_USE_OBSOLETE
#define MYGUI_ASSERT(exp, dest)
FloatPoint getUVFromPositionInsideRect(const FloatPoint &_point, const FloatPoint &_v0, const FloatPoint &_v1, const FloatPoint &_baseUV)
VectorFloatPoint cropPolygon(const FloatPoint *_baseVerticiesPos, size_t _size, const IntCoord &_cropRectangle)
FloatPoint getPositionInsideRect(const FloatPoint &_point, const FloatPoint &_corner0, const FloatPoint &_corner1, const FloatPoint &_corner2)
void toRenderTargetInPlace(FloatPoint *_points, size_t _count, 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::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
types::TRect< float > FloatRect
Definition MyGUI_Types.h:33
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:27
std::vector< FloatPoint > VectorFloatPoint
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29