Skip to content

Commit

Permalink
Fix issue #1546 of UILayout(#1549) and small performance boost (#1559)
Browse files Browse the repository at this point in the history
* Fix issue #1546 of UILayout(#1549) and small performance boost

* add test

* Committing genbindings changes

---------

Co-authored-by: aismann <aismann@users.noreply.github.com>
  • Loading branch information
aismann and aismann authored Dec 31, 2023
1 parent 9414177 commit c389118
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 50 deletions.
11 changes: 8 additions & 3 deletions core/2d/DrawNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ inline bool isConvex(const Vec2* verts, int count)

// implementation of DrawNode

DrawNode::DrawNode(float lineWidth) : _lineWidth(lineWidth), _defaultLineWidth(lineWidth)
DrawNode::DrawNode(float lineWidth) : _lineWidth(lineWidth), _defaultLineWidth(lineWidth), _isConvex(false)
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
#if AX_ENABLE_CACHE_TEXTURE_DATA
Expand Down Expand Up @@ -400,7 +400,9 @@ void DrawNode::drawCircle(const Vec2& center,
}
if (_lineWidth > threshold)
{
_isConvex = true;
drawPolygon(vertices, segments, Color4B(1.0f, 0.0f, 0.0f, 1.0f), _lineWidth/4, color);
_isConvex = false;
}
else
{
Expand Down Expand Up @@ -635,7 +637,7 @@ void DrawNode::drawPolygon(const Vec2* verts,
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
V2F_C4B_T2F_Triangle* cursor = triangles;

if (count >= 3 && !isConvex(verts, count))
if (!_isConvex && count >= 3 && !isConvex(verts, count))
{
std::vector<p2t::Point> p2pointsStorage;
p2pointsStorage.reserve(count);
Expand Down Expand Up @@ -751,7 +753,9 @@ void DrawNode::drawSolidRect(const Vec2& origin, const Vec2& destination, const

void DrawNode::drawSolidPoly(const Vec2* poli, unsigned int numberOfPoints, const Color4B& color)
{
_isConvex = true; // Fix issue #1546 of UILayout(#1549)
drawPolygon(poli, numberOfPoints, color, 0.0, Color4B());
_isConvex = false;
}

void DrawNode::drawPie(const Vec2& center,
Expand Down Expand Up @@ -843,8 +847,9 @@ void DrawNode::drawSolidCircle(const Vec2& center,
vertices[i].x = j;
vertices[i].y = k;
}

_isConvex = true;
drawPolygon(vertices, segments, fillColor, borderWidth, borderColor);
_isConvex = false;
}

void DrawNode::drawSolidCircle(const Vec2& center,
Expand Down
4 changes: 4 additions & 0 deletions core/2d/DrawNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ class AX_DLL DrawNode : public Node
// Get CocosStudio guide lines width.
float getLineWidth();

void setIsConvex(bool isConvex) { _isConvex = isConvex; }; // Set backwards compatible with cocos2dx/axmol 2.0

/**
* When isolated is set, the position of the node is no longer affected by parent nodes.
* Which means it will be drawn just like a root node.
Expand Down Expand Up @@ -455,6 +457,8 @@ class AX_DLL DrawNode : public Node

ax::any_buffer _abuf;

bool _isConvex = true;

private:
AX_DISALLOW_COPY_AND_ASSIGN(DrawNode);
};
Expand Down
51 changes: 51 additions & 0 deletions extensions/scripting/lua-bindings/auto/axlua_base_auto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53302,6 +53302,56 @@ int lua_ax_base_DrawNode_getLineWidth(lua_State* tolua_S)

return 0;
}
int lua_ax_base_DrawNode_setIsConvex(lua_State* tolua_S)
{
int argc = 0;
ax::DrawNode* cobj = nullptr;
bool ok = true;

#if _AX_DEBUG >= 1
tolua_Error tolua_err;
#endif


#if _AX_DEBUG >= 1
if (!tolua_isusertype(tolua_S,1,"ax.DrawNode",0,&tolua_err)) goto tolua_lerror;
#endif

cobj = (ax::DrawNode*)tolua_tousertype(tolua_S,1,0);

#if _AX_DEBUG >= 1
if (!cobj)
{
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_ax_base_DrawNode_setIsConvex'", nullptr);
return 0;
}
#endif

argc = lua_gettop(tolua_S)-1;
if (argc == 1)
{
bool arg0;

ok &= luaval_to_boolean(tolua_S, 2,&arg0, "ax.DrawNode:setIsConvex");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_ax_base_DrawNode_setIsConvex'", nullptr);
return 0;
}
cobj->setIsConvex(arg0);
lua_settop(tolua_S, 1);
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "ax.DrawNode:setIsConvex",argc, 1);
return 0;

#if _AX_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_ax_base_DrawNode_setIsConvex'.",&tolua_err);
#endif

return 0;
}
int lua_ax_base_DrawNode_setIsolated(lua_State* tolua_S)
{
int argc = 0;
Expand Down Expand Up @@ -53530,6 +53580,7 @@ int lua_register_ax_base_DrawNode(lua_State* tolua_S)
tolua_function(tolua_S,"setBlendFunc",lua_ax_base_DrawNode_setBlendFunc);
tolua_function(tolua_S,"setLineWidth",lua_ax_base_DrawNode_setLineWidth);
tolua_function(tolua_S,"getLineWidth",lua_ax_base_DrawNode_getLineWidth);
tolua_function(tolua_S,"setIsConvex",lua_ax_base_DrawNode_setIsConvex);
tolua_function(tolua_S,"setIsolated",lua_ax_base_DrawNode_setIsolated);
tolua_function(tolua_S,"isIsolated",lua_ax_base_DrawNode_isIsolated);
tolua_function(tolua_S,"create", lua_ax_base_DrawNode_create);
Expand Down
1 change: 1 addition & 0 deletions extensions/scripting/lua-bindings/auto/axlua_base_auto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2431,6 +2431,7 @@ int register_all_ax_base(lua_State* tolua_S);






#endif // __ax_base_h__
102 changes: 58 additions & 44 deletions tests/cpp-tests/Source/DrawPrimitivesTest/DrawPrimitivesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using namespace std;
DrawPrimitivesTests::DrawPrimitivesTests()
{
ADD_TEST_CASE(DrawNodeTest);
ADD_TEST_CASE(Issue11942Test);
ADD_TEST_CASE(DrawNodeBackwardsAPITest);
ADD_TEST_CASE(BetterCircleRendering);
ADD_TEST_CASE(DrawNodeTestNewFeature1);
ADD_TEST_CASE(Issue829Test);
Expand Down Expand Up @@ -219,50 +219,64 @@ string DrawNodeTest::subtitle() const
//
// Issue11942Test
//
Issue11942Test::Issue11942Test()
DrawNodeBackwardsAPITest::DrawNodeBackwardsAPITest()
{
// DrawNode 0 ------------------------------------------
auto draw0 = DrawNode::create();
addChild(draw0, 10);

// draw a circle thickness 10
draw0->setLineWidth(10);
draw0->drawCircle(VisibleRect::center() - Vec2(140.0f, 40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1.0f));
draw0->setLineWidth(1); // thickness 10 will replaced with thickness 1 (also for all 'same' draw commands before!)
draw0->drawCircle(VisibleRect::center() - Vec2(140.0f, -40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1.0f));


// DrawNode 1 ------------------------------------------
auto draw1 = DrawNode::create();
addChild(draw1, 10);

// draw a second circle thickness 1
draw1->setLineWidth(1);
draw1->drawCircle(VisibleRect::center() + Vec2(140.0f, 40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1.0f));
draw1->setLineWidth(20); // thickness 1 will replaced with thickness 10 (also for all 'same' draw commands before!)
draw1->drawCircle(VisibleRect::center() + Vec2(140.0f, -40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1.0f));
float o = 80;
float w = 20;
float h = 50;

auto drawNode1 = DrawNode::create();
addChild(drawNode1);
drawNode1->setPosition(-40, 20);

int x = 0;
int y = 0;
Vec2 vertices[4];
drawNode1->setScale(0.5);
Color4F color;
for (int iy = 0; iy < 5; iy++)
{
x = 0;
for (int ix = 0; ix < 13; ix++)
{
vertices[0] = Vec2(x + o + w, y + o - h);
vertices[1] = Vec2(x + o + w * 2, y + o);
vertices[2] = Vec2(x + o + w * 2 + h, y + o + w);
vertices[3] = Vec2(x + o + w * 2, y + o + w * 2);

if (AXRANDOM_0_1() > 0.5f)
{
drawNode1->setIsConvex(true);
color = Color4F::YELLOW;
}
else
{
drawNode1->setIsConvex(false); // default value!
color = Color4F::ORANGE;
}
drawNode1->drawPolygon(vertices, 4, Color4F(0.7f, 0.7f, 0.7f, 0.5f), 1, color);
x += 70;
}
y += 80;
}
}

string Issue11942Test::title() const
string DrawNodeBackwardsAPITest::title() const
{
return "GitHub Issue #11942 (axmol #137)";
return "API backwards compatible test";
}

string Issue11942Test::subtitle() const
string DrawNodeBackwardsAPITest::subtitle() const
{
return "setLineWidth() change the WHOLE DrawNode object 'line with'";
return "YELLOW=cocos2dx/axmol <=2.0 ORANGE=axmol >2.0";
}

//
// drawCircle new feature (better rendering)
//
BetterCircleRendering::BetterCircleRendering()
{
//Add lines to see the correct "scale of the 'rings'" changing the window size
// Add lines to see the correct "scale of the 'rings'" changing the window size

auto draw = DrawNode::create();
draw->setLineWidth(1);
Expand Down Expand Up @@ -298,7 +312,8 @@ void BetterCircleRendering::changeThreshold(ax::Ref* pSender, ax::ui::Slider::Ev
{
ax::ui::Slider* sliderThreshold = dynamic_cast<ax::ui::Slider*>(pSender);
threshold = static_cast<float>(sliderThreshold->getPercent());
_thresholdLabel->setString("drawCircle(pos, radius, ..., segments, ..., color, " + Value(threshold).asString() + ")");
_thresholdLabel->setString("drawCircle(pos, radius, ..., segments, ..., color, " + Value(threshold).asString() +
")");
}
}

Expand All @@ -315,7 +330,7 @@ void BetterCircleRendering::initSliders()
slider->addEventListener(AX_CALLBACK_2(BetterCircleRendering::changeThreshold, this));

auto ttfConfig = TTFConfig("fonts/arial.ttf", 8);
_thresholdLabel = Label::createWithTTF(ttfConfig, "drawCircle(pos, radius, ..., segments, ..., color, 0)");
_thresholdLabel = Label::createWithTTF(ttfConfig, "drawCircle(pos, radius, ..., segments, ..., color, 0)");
addChild(_thresholdLabel, 20);
_thresholdLabel->setPosition(Vec2(vsize.width / 2, vsize.height / 6 + 15));

Expand All @@ -336,8 +351,8 @@ void BetterCircleRendering::initSliders()
addChild(sliderLineWidth, 20);
}

void BetterCircleRendering::update(float dt)
{
void BetterCircleRendering::update(float dt)
{
drawNode->clear();
drawNode->setLineWidth(lineWidth); // value from the slider

Expand All @@ -353,10 +368,10 @@ void BetterCircleRendering::initSliders()
}
drawNode->drawCircle(VisibleRect::center() - Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false, color,
threshold);
}
}

string BetterCircleRendering::title() const
{
{
return "Rendering drawCircle";
}

Expand All @@ -365,7 +380,6 @@ string BetterCircleRendering::subtitle() const
return "Green: smoother rendering; Red: faster but badly rendering";
}


DrawNodeTestNewFeature1::DrawNodeTestNewFeature1()
{
drawNode = DrawNode::create();
Expand Down Expand Up @@ -3937,8 +3951,7 @@ Issue1319Test::Issue1319Test()
{148.671875f, 216.566895f}, {148.681152f, 216.764160f}, {148.693359f, 216.961670f}, {148.701660f, 217.097168f},
{148.709473f, 217.235107f}, {148.720703f, 217.371826f}, {148.739746f, 217.504150f}, {148.770996f, 217.628906f},
{148.817383f, 217.741943f}, {148.883301f, 217.840576f}, {148.973145f, 217.920898f}, {149.090332f, 217.979736f},
{149.239258f, 218.013916f}
};
{149.239258f, 218.013916f}};

Vec2 vertices23[] = {
{289.365723f, 227.168457f}, {289.388672f, 227.053467f}, {289.411621f, 226.938965f}, {289.435059f, 226.824219f},
Expand Down Expand Up @@ -4143,8 +4156,7 @@ Issue1319Test::Issue1319Test()
{286.836914f, 230.352539f}, {287.020996f, 230.461182f}, {287.266113f, 230.535645f}, {287.574219f, 230.580078f},
{287.949219f, 230.598145f}, {288.163574f, 230.299805f}, {288.369141f, 229.996338f}, {288.563477f, 229.685059f},
{288.743164f, 229.365234f}, {288.905762f, 229.034912f}, {289.048828f, 228.692383f}, {289.169922f, 228.336670f},
{289.265137f, 227.965332f}, {289.332520f, 227.577148f}, {289.369629f, 227.170410f}
};
{289.265137f, 227.965332f}, {289.332520f, 227.577148f}, {289.369629f, 227.170410f}};

Vec2 vertices24[] = {
{45.750000f, 144.375000f}, {75.500000f, 136.875000f}, {75.500000f, 159.125000f}, {100.250000f, 161.375000f},
Expand Down Expand Up @@ -4877,7 +4889,8 @@ Issue1319Test::Issue1319Test()
2.0f, Color4F::WHITE);

drawNode[0]->setPosition(Vec2(-30, -20));
drawNode[0]->drawPolygon(vertices24, sizeof(vertices24) / sizeof(vertices24[0]), Color4F::RED, 0.3f, Color4F::GREEN);
drawNode[0]->drawPolygon(vertices24, sizeof(vertices24) / sizeof(vertices24[0]), Color4F::RED, 0.3f,
Color4F::GREEN);

drawNode[1]->drawPolygon(vertices1, sizeof(vertices1) / sizeof(vertices1[0]), Color4F::YELLOW, 0.3f, Color4F::RED);
drawNode[1]->drawPolygon(vertices2, sizeof(vertices2) / sizeof(vertices2[0]), Color4F::YELLOW, 0.3f, Color4F::RED);
Expand All @@ -4892,7 +4905,8 @@ Issue1319Test::Issue1319Test()
Color4F::RED);
drawNode[1]->drawPolygon(vertices11, sizeof(vertices11) / sizeof(vertices11[0]), Color4F::YELLOW, 0.3f,
Color4F::RED);
drawNode[1]->drawPolygon(vertices12, sizeof(vertices12) / sizeof(vertices12[0]), Color4F::WHITE, 0.3f, Color4F::RED);
drawNode[1]->drawPolygon(vertices12, sizeof(vertices12) / sizeof(vertices12[0]), Color4F::WHITE, 0.3f,
Color4F::RED);
drawNode[1]->drawPolygon(vertices13, sizeof(vertices13) / sizeof(vertices13[0]), Color4F::YELLOW, 0.3f,
Color4F::RED);
drawNode[1]->drawPolygon(vertices14, sizeof(vertices14) / sizeof(vertices14[0]), Color4F::YELLOW, 0.3f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class DrawNodeTest : public DrawPrimitivesBaseTest
virtual std::string subtitle() const override;
};

class Issue11942Test : public DrawPrimitivesBaseTest
class DrawNodeBackwardsAPITest : public DrawPrimitivesBaseTest
{
public:
CREATE_FUNC(Issue11942Test);
CREATE_FUNC(DrawNodeBackwardsAPITest);

Issue11942Test();
DrawNodeBackwardsAPITest();

virtual std::string title() const override;
virtual std::string subtitle() const override;
Expand Down

0 comments on commit c389118

Please sign in to comment.