Skip to content

Commit 076ccd4

Browse files
committed
Improve Label underline, strikethrough and color (#2364)
1 parent 8882888 commit 076ccd4

File tree

10 files changed

+359
-103
lines changed

10 files changed

+359
-103
lines changed

.github/FUNDING.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# These are supported funding model platforms
22

3-
github: axmolengine # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
3+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
44
patreon: # Replace with a single Patreon username
55
open_collective: # Replace with a single Open Collective username
66
ko_fi: # Replace with a single Ko-fi username

core/2d/DrawNode.cpp

+42-17
Original file line numberDiff line numberDiff line change
@@ -909,18 +909,17 @@ void DrawNode::_drawPolygon(const Vec2* verts,
909909
}
910910
if (outline)
911911
{
912+
float width = thickness / properties.factor;
912913
if (thickness != 1.0f || properties.drawOrder)
913914
{
914-
thickness *= properties.factor;
915-
916915
for (unsigned int i = 1; i < (count); i++)
917916
{
918917
Vec2 a = _vertices[i - 1];
919918
Vec2 b = _vertices[i];
920919
Vec2 n = ((b - a).getPerp()).getNormalized();
921920
Vec2 t = n.getPerp();
922-
Vec2 nw = n * thickness;
923-
Vec2 tw = t * thickness;
921+
Vec2 nw = n * width;
922+
Vec2 tw = t * width;
924923
Vec2 v0 = b - (nw + tw);
925924
Vec2 v1 = b + (nw - tw);
926925
Vec2 v2 = b - nw;
@@ -1001,10 +1000,10 @@ void DrawNode::_drawPolygon(const Vec2* verts,
10011000
Vec2 offset0 = extrude[i].offset;
10021001
Vec2 offset1 = extrude[j].offset;
10031002

1004-
Vec2 inner0 = v0 - offset0 * thickness;
1005-
Vec2 inner1 = v1 - offset1 * thickness;
1006-
Vec2 outer0 = v0 + offset0 * thickness;
1007-
Vec2 outer1 = v1 + offset1 * thickness;
1003+
Vec2 inner0 = v0 - offset0 * width;
1004+
Vec2 inner1 = v1 - offset1 * width;
1005+
Vec2 outer0 = v0 + offset0 * width;
1006+
Vec2 outer1 = v1 + offset1 * width;
10081007

10091008
triangles[ii++] = {{inner0, borderColor, -n0}, {inner1, borderColor, -n0}, {outer1, borderColor, n0}};
10101009

@@ -1055,25 +1054,26 @@ void DrawNode::_drawSegment(const Vec2& from,
10551054
DrawNode::EndType etStart,
10561055
DrawNode::EndType etEnd)
10571056
{
1058-
Vec2 vertices[2] = {from, to};
1059-
applyTransform(vertices, vertices, 2);
1057+
if (thickness < 1.0f)
1058+
thickness = 1.0f;
10601059

10611060
if (thickness == 1.0f && !properties.drawOrder)
10621061
{
1063-
auto line = expandBufferAndGetPointer(_lines, 2);
1064-
_linesDirty = true;
1065-
1066-
line[0] = {vertices[0], color, Vec2::ZERO};
1067-
line[1] = {vertices[1], color, Vec2::ZERO};
1062+
_drawLine(from, to, color); // fastest way to draw a line
10681063
}
10691064
else
10701065
{
1066+
Vec2 vertices[2] = {from, to};
1067+
applyTransform(vertices, vertices, 2);
1068+
1069+
float width = thickness / (2 * properties.factor);
1070+
10711071
Vec2 a = vertices[0];
10721072
Vec2 b = vertices[1];
10731073
Vec2 n = ((b - a).getPerp()).getNormalized();
10741074
Vec2 t = n.getPerp();
1075-
Vec2 nw = n * thickness;
1076-
Vec2 tw = t * thickness;
1075+
Vec2 nw = n * width;
1076+
Vec2 tw = t * width;
10771077
Vec2 v0 = b - (nw + tw);
10781078
Vec2 v1 = b + (nw - tw);
10791079
Vec2 v2 = b - nw;
@@ -1177,6 +1177,19 @@ void DrawNode::_drawSegment(const Vec2& from,
11771177
}
11781178
}
11791179
}
1180+
// Internal function _drawLine => thickness is always 1 (fastes way to draw a line)
1181+
void DrawNode::_drawLine(const Vec2& from, const Vec2& to, const Color4B& color)
1182+
{
1183+
Vec2 vertices[2] = {from, to};
1184+
applyTransform(vertices, vertices, 2);
1185+
1186+
1187+
auto line = expandBufferAndGetPointer(_lines, 2);
1188+
_linesDirty = true;
1189+
1190+
line[0] = {vertices[0], color, Vec2::ZERO};
1191+
line[1] = {vertices[1], color, Vec2::ZERO};
1192+
}
11801193

11811194
void DrawNode::_drawDot(const Vec2& pos, float radius, const Color4B& color)
11821195
{
@@ -1572,6 +1585,18 @@ void DrawNode::applyTransform(const Vec2* from, Vec2* to, unsigned int count)
15721585
}
15731586
}
15741587

1588+
void DrawNode::Properties::setDefaultValues()
1589+
{
1590+
auto fac = Director::getInstance()->getContentScaleFactor();
1591+
factor = fac;
1592+
1593+
scale = Vec2(1.0f, 1.0f);
1594+
center = Vec2(0.0f, 0.0f);
1595+
rotation = 0.0f;
1596+
position = Vec2(0.0f, 0.0f);
1597+
drawOrder = false;
1598+
};
1599+
15751600
#if defined(_WIN32)
15761601
# pragma pop_macro("TRANSPARENT")
15771602
#endif

core/2d/DrawNode.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,9 @@ class AX_DLL DrawNode : public Node
638638
float thickness = 1.0f,
639639
bool isconvex = true);
640640

641+
// Internal function _drawLine
642+
void _drawLine(const Vec2& from, const Vec2& to, const Color4B& color);
643+
641644
// Internal function _drawSegment
642645
void _drawSegment(const Vec2& origin,
643646
const Vec2& destination,
@@ -686,10 +689,10 @@ class AX_DLL DrawNode : public Node
686689
AX_DISALLOW_COPY_AND_ASSIGN(DrawNode);
687690

688691
public:
689-
class Properties
692+
class AX_DLL Properties
690693
{
691694
public:
692-
float factor = 0.5f; /// thickness factor
695+
float factor; /// thickness scale factor
693696

694697
// transforming stuff
695698
Vec2 scale;
@@ -778,14 +781,10 @@ class AX_DLL DrawNode : public Node
778781
779782
* @js NA
780783
*/
781-
void setDefaultValues()
782-
{
783-
scale = Vec2(1.0f, 1.0f);
784-
center = Vec2(0.0f, 0.0f);
785-
rotation = 0.0f;
786-
position = Vec2(0.0f, 0.0f);
787-
drawOrder = false;
788-
};
784+
void setDefaultValues();
785+
float getFactor() { return factor; };
786+
void setFactor(float fac) { factor = fac; };
787+
789788
} properties;
790789
};
791790

0 commit comments

Comments
 (0)