18 PolygonalSkin::PolygonalSkin()
20 mVertexFormat = RenderManager::getInstance().getVertexFormat();
23 inline float len(
float x,
float y)
25 return std::sqrt(x * x + y * y);
28 void PolygonalSkin::setPoints(
const std::vector<FloatPoint>& _points)
31 mLinePoints.reserve(_points.size());
33 for (
const auto& point : _points)
35 if (!mLinePoints.empty())
37 const auto& previous = mLinePoints.back();
38 if (previous == point)
40 mLineLength +=
len(point.left - previous.left, point.top - previous.top);
42 mLinePoints.push_back(point);
45 const size_t segments = mLinePoints.empty() ? 0 : mLinePoints.size() - 1;
47 size_t count = segments * 12;
48#ifndef MYGUI_NO_POLYGONAL_SKIN_CROPPING
53 if (count > mVertexCount)
56 if (
nullptr != mRenderItem)
57 mRenderItem->reallockDrawItem(
this, mVertexCount);
62 void PolygonalSkin::setWidth(
float _width)
64 if (mLineWidth == _width)
70 void PolygonalSkin::setStroke(
size_t _value)
72 if (mLineStroke == _value)
78 void PolygonalSkin::setVisible(
bool _visible)
80 if (mVisible == _visible)
87 void PolygonalSkin::setAlpha(
float _alpha)
90 if ((mCurrentColour & 0xFF000000) == alpha)
92 mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
95 mNode->outOfDate(mRenderItem);
98 void PolygonalSkin::_correctView()
100 mGeometryOutdated =
true;
102 if (
nullptr != mNode)
103 mNode->outOfDate(mRenderItem);
106 void PolygonalSkin::_setAlign(
const IntSize& _oldsize)
108 auto [coord, resize, move] = CoordConverter::applyAlign(mAlign, mCoord, mCroppedParent->getSize(), _oldsize);
112 mCurrentCoord = mCoord;
116 void PolygonalSkin::_updateView()
118 bool margin = _checkMargin();
120 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
122 mGeometryOutdated =
true;
124 mCurrentCoord.left = mCoord.left + mMargin.left;
125 mCurrentCoord.top = mCoord.top + mMargin.top;
130 if (_checkOutsideUnrotated())
134 if (
nullptr != mNode)
135 mNode->outOfDate(mRenderItem);
141 if (mIsMargin || margin)
143 mCurrentCoord.width = _getViewWidth();
144 mCurrentCoord.height = _getViewHeight();
149 if (
nullptr != mNode)
150 mNode->outOfDate(mRenderItem);
153 void PolygonalSkin::createDrawItem(ITexture* _texture, ILayerNode* _node)
155 MYGUI_ASSERT(!mRenderItem,
"mRenderItem must be nullptr");
158 mRenderItem = mNode->addToRenderItem(_texture,
true);
159 mRenderItem->addDrawItem(
this, mVertexCount);
162 void PolygonalSkin::destroyDrawItem()
164 MYGUI_ASSERT(mRenderItem,
"mRenderItem must be not nullptr");
167 mRenderItem->removeDrawItem(
this);
168 mRenderItem =
nullptr;
171 void PolygonalSkin::doRender()
173 if (!mVisible || mEmptyView)
176 Vertex* verticies = mRenderItem->getCurrentVertexBuffer();
178 float vertex_z = mNode->getNodeDepth();
180 if (mGeometryOutdated || mRenderItem->getCurrentUpdate())
185 size_t size = mResultVerticiesPos.size();
187 for (
size_t i = 0; i < size; ++i)
190 mResultVerticiesPos[i].left,
191 mResultVerticiesPos[i].top,
193 mResultVerticiesUV[i].left,
194 mResultVerticiesUV[i].top,
198 mRenderItem->setLastVertexCount(size);
201 void PolygonalSkin::_setColour(
const Colour& _value)
204 mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
206 if (
nullptr != mNode)
207 mNode->outOfDate(mRenderItem);
210 void PolygonalSkin::setStateData(IStateInfo* _data)
212 _setUVSet(_data->castType<SubSkinStateInfo>()->getRect());
215 void PolygonalSkin::_setUVSet(
const FloatRect& _rect)
217 mCurrentTexture = _rect;
222 void PolygonalSkin::_rebuildGeometry()
224 if (!mRenderItem || !mRenderItem->getRenderTarget())
227 mGeometryOutdated =
false;
228 mResultVerticiesPos.clear();
229 mResultVerticiesUV.clear();
230 if (mLinePoints.size() < 2)
237 FloatPoint(mCurrentTexture.left, mCurrentTexture.top),
238 FloatPoint(mCurrentTexture.right, mCurrentTexture.top),
239 FloatPoint(mCurrentTexture.right, mCurrentTexture.bottom),
240 FloatPoint(mCurrentTexture.left, mCurrentTexture.bottom)};
243 FloatPoint vectorU = baseVerticiesUV[1] - baseVerticiesUV[0];
247 FloatPoint normal = _getPerpendicular(mLinePoints[0], mLinePoints[1]);
249 FloatPoint points[2] = {mLinePoints[0] + normal, mLinePoints[0] - normal};
250 FloatPoint pointsUV[2] = {baseVerticiesUV[0], baseVerticiesUV[3]};
256 float currentLength = 0.0f;
257 for (
size_t i = 1; i < mLinePoints.size(); ++i)
259 if (mLineStroke != 0)
262 if (stroke == mLineStroke)
270 len(mLinePoints[i - 1].left - mLinePoints[i].left, mLinePoints[i - 1].top - mLinePoints[i].top);
273 if (i != mLinePoints.size() - 1)
274 normal = _getMiddleLine(mLinePoints[i - 1], mLinePoints[i + 1], mLinePoints[i]);
276 normal = _getPerpendicular(mLinePoints[i - 1], mLinePoints[i]);
283 normal = _getPerpendicular(mLinePoints[i - 1], mLinePoints[i]);
285 else if (
len(normal.left, normal.top) > mLineWidth * 1.5f)
288 normal = _getPerpendicular(mLinePoints[i - 1], mLinePoints[i]);
292 FloatPoint lineDir = mLinePoints[i] - mLinePoints[i - 1];
293 if (lineDir.left * normal.top - lineDir.top * normal.left < 0)
295 normal.left = -normal.left;
296 normal.top = -normal.top;
299 FloatPoint UVoffset(currentLength / mLineLength * vectorU.left, currentLength / mLineLength * vectorU.top);
303 mResultVerticiesPos.push_back(points[0]);
304 mResultVerticiesPos.push_back(points[1]);
305 mResultVerticiesPos.push_back(mLinePoints[i] + normal);
306 mResultVerticiesUV.push_back(pointsUV[0]);
307 mResultVerticiesUV.push_back(pointsUV[1]);
308 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
310 mResultVerticiesPos.push_back(points[1]);
311 mResultVerticiesPos.push_back(mLinePoints[i] - normal);
312 mResultVerticiesPos.push_back(mLinePoints[i] + normal);
313 mResultVerticiesUV.push_back(pointsUV[1]);
314 mResultVerticiesUV.push_back(baseVerticiesUV[3] + UVoffset);
315 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
318 points[edge ? 1 : 0] = mLinePoints[i] + normal;
319 points[edge ? 0 : 1] = mLinePoints[i] - normal;
320 pointsUV[0] = baseVerticiesUV[0] + UVoffset;
321 pointsUV[1] = baseVerticiesUV[3] + UVoffset;
325 normal = _getMiddleLine(mLinePoints[i - 1], mLinePoints[i + 1], mLinePoints[i]);
327 float length =
len(normal.left, normal.top);
328 float sharpness = length / mLineWidth;
329 normal.left *= 2 * mLineWidth / length / (sharpness - 0.5f);
330 normal.top *= 2 * mLineWidth / length / (sharpness - 0.5f);
333 lineDir = mLinePoints[i] - mLinePoints[i - 1];
334 if (lineDir.left * normal.top - lineDir.top * normal.left < 0)
336 normal.left = -normal.left;
337 normal.top = -normal.top;
339 FloatPoint lineDir1 = mLinePoints[i] - mLinePoints[i - 1];
340 FloatPoint lineDir2 = mLinePoints[i + 1] - mLinePoints[i];
341 if (lineDir1.left * lineDir2.top - lineDir1.top * lineDir2.left > 0)
343 normal.left = -normal.left;
344 normal.top = -normal.top;
348 FloatPoint normal2 = _getPerpendicular(mLinePoints[i], mLinePoints[i + 1]);
349 lineDir = mLinePoints[i - 1] - mLinePoints[i];
350 if (lineDir.left * normal2.top - lineDir.top * normal2.left < 0)
352 normal2.left = -normal2.left;
353 normal2.top = -normal2.top;
357 (baseVerticiesUV[0].left + baseVerticiesUV[3].left) / 2,
358 (baseVerticiesUV[0].top + baseVerticiesUV[3].top) / 2);
362 mResultVerticiesPos.push_back(points[0]);
363 mResultVerticiesPos.push_back(mLinePoints[i] + normal);
364 mResultVerticiesPos.push_back(mLinePoints[i]);
365 mResultVerticiesUV.push_back(pointsUV[0]);
366 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
367 mResultVerticiesUV.push_back(UVcenter + UVoffset);
369 mResultVerticiesPos.push_back(mLinePoints[i] + normal);
370 mResultVerticiesPos.push_back(mLinePoints[i] + normal2);
371 mResultVerticiesPos.push_back(mLinePoints[i]);
372 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
373 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
374 mResultVerticiesUV.push_back(UVcenter + UVoffset);
377 points[0] = mLinePoints[i] + normal2;
378 points[1] = mLinePoints[i] - normal2;
379 pointsUV[0] = baseVerticiesUV[0] + UVoffset;
380 pointsUV[1] = baseVerticiesUV[3] + UVoffset;
385#ifndef MYGUI_NO_POLYGONAL_SKIN_CROPPING
387 IntCoord cropRectangle(mCurrentCoord.left, mCurrentCoord.top, mCurrentCoord.width, mCurrentCoord.height);
391 newResultVerticiesPos.reserve(mResultVerticiesPos.size());
392 newResultVerticiesUV.reserve(mResultVerticiesPos.size());
393 for (
size_t i = 0; i < mResultVerticiesPos.size(); i += 3)
396 const FloatPoint v0 = mResultVerticiesUV[i + 2] - mResultVerticiesUV[i];
397 const FloatPoint v1 = mResultVerticiesUV[i + 1] - mResultVerticiesUV[i];
398 for (
size_t j = 1; j + 1 < croppedTriangle.size(); ++j)
400 for (
size_t index : {size_t(0), j, j + 1})
402 const FloatPoint& vertex = croppedTriangle[index];
403 newResultVerticiesPos.push_back(vertex);
406 mResultVerticiesPos[i],
407 mResultVerticiesPos[i + 1],
408 mResultVerticiesPos[i + 2]);
409 newResultVerticiesUV.push_back(
414 std::swap(mResultVerticiesPos, newResultVerticiesPos);
415 std::swap(mResultVerticiesUV, newResultVerticiesUV);
421 mRenderItem->getRenderTarget()->getInfo(),
422 mCroppedParent->getAbsolutePosition());
428 FloatPoint result(_point1.top - _point2.top, -(_point1.left - _point2.left));
430 float length =
len(result.top, result.left);
431 result.left /= length;
432 result.top /= length;
433 result.left *= mLineWidth / 2;
434 result.top *= mLineWidth / 2;
446 float length =
len(line1.top, line1.left);
447 line1.left /= length;
449 length =
len(line2.top, line2.left);
450 line2.left /= length;
454 length =
len(result.top, result.left);
457 return _getPerpendicular(_point1, _point2);
459 result.left /= length;
460 result.top /= length;
462 float cos = result.left * line1.left + result.top * line1.top;
463 float angle = std::acos(std::clamp(cos, -1.0f, 1.0f));
466 if (std::fabs(angle) < 1e-6f)
469 float width = mLineWidth / 2 / std::sin(angle);
470 result.left *= width;
473 MYGUI_SUPPRESS_CLANG(
"-Wnrvo")
475 MYGUI_UNSUPPRESS_CLANG()
#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)
types::TRect< float > FloatRect
types::TPoint< float > FloatPoint
std::vector< FloatPoint > VectorFloatPoint
types::TCoord< int > IntCoord
types::TSize< int > IntSize
float len(float x, float y)