Skip to content

Commit 739f641

Browse files
authored
DrawNode: API improvement: supporting 'closed' Splines; Update UserDefault.cpp/h (fix typo) (#2476)
* DrawNode: API improvement: supporting 'closed' Splines * Color4B (back to Axmol 2.x) * Update DrawNodeTest.cpp * Update DrawNodeTest.cpp * Update DrawNodeTest.cpp * Update DrawNodeTest.cpp * Update UserDefault.h (fix typo) * Update UserDefault.cpp (fix typo)
1 parent 2469061 commit 739f641

File tree

6 files changed

+134
-65
lines changed

6 files changed

+134
-65
lines changed

core/2d/DrawNode.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,28 +468,40 @@ void DrawNode::drawCubicBezier(const Vec2& origin,
468468
_drawPoly(_vertices.data(), segments + 1, false, color, thickness, true);
469469
}
470470

471-
void DrawNode::drawCardinalSpline(PointArray* config,
471+
void DrawNode::drawCardinalSpline(const PointArray* configIn,
472472
float tension,
473473
unsigned int segments,
474474
const Color4B& color,
475-
float thickness)
475+
float thickness,
476+
bool closed)
476477
{
477478
if (thickness <= 0.0f)
478479
{
479480
AXLOGW("{}: thickness <= 0", __FUNCTION__);
480481
return;
481482
}
482483

483-
axstd::pod_vector<Vec2> _vertices{static_cast<size_t>(segments)};
484+
// Don't change the original PointArray
485+
PointArray* config = configIn->clone();
486+
487+
if (closed && config->count() > 2)
488+
{
489+
config->addControlPoint(config->getControlPointAtIndex(0));
490+
config->addControlPoint(config->getControlPointAtIndex(1));
491+
config->insertControlPoint(config->getControlPointAtIndex(config->count() - 3), 0);
492+
config->insertControlPoint(config->getControlPointAtIndex(config->count() - 4), 0);
493+
}
484494

485495
ssize_t p;
486496
float lt;
487497
float deltaT = 1.0f / config->count();
488498

499+
axstd::pod_vector<Vec2> _vertices{static_cast<size_t>(segments)};
500+
489501
for (unsigned int i = 0; i < segments; i++)
490502
{
491503
float dt = (float)i / segments;
492-
p = static_cast<ssize_t>(dt / deltaT);
504+
p = static_cast<ssize_t>(dt / deltaT);
493505

494506
// Check last control point reached
495507
if (p >= (config->count() - 1))
@@ -510,17 +522,25 @@ void DrawNode::drawCardinalSpline(PointArray* config,
510522
_vertices[i] = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
511523
}
512524

525+
// Dont draw the first and the last point
526+
if (closed && config->count() > 2)
527+
{
528+
auto seg = segments / (config->count() - 2);
529+
for (int i = seg; i < segments - seg; i++)
530+
_vertices[i - seg] = _vertices[i];
531+
segments -= (seg + seg);
532+
}
513533
_drawPoly(_vertices.data(), segments, false, color, thickness, true);
514534
}
515535

516-
void DrawNode::drawCatmullRom(PointArray* points, unsigned int segments, const Color4B& color, float thickness)
536+
void DrawNode::drawCatmullRom(const PointArray* pointsIn, unsigned int segments, const Color4B& color, float thickness, bool closed)
517537
{
518538
if (thickness <= 0.0f)
519539
{
520540
AXLOGW("{}: thickness <= 0", __FUNCTION__);
521541
return;
522542
}
523-
drawCardinalSpline(points, 0.5f, segments, color, thickness);
543+
drawCardinalSpline(pointsIn, 0.5f, segments, color, thickness, closed);
524544
}
525545

526546
void DrawNode::drawDot(const Vec2& pos, float radius, const Color4B& color)

core/2d/DrawNode.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,19 +279,24 @@ class AX_DLL DrawNode : public Node
279279
* @param segments The number of segments.
280280
* @param color Set the Spline color.
281281
*/
282-
void drawCardinalSpline(PointArray* config,
282+
void drawCardinalSpline(const PointArray* configIn,
283283
float tension,
284284
unsigned int segments,
285285
const Color4B& color,
286-
float thickness = 1.0f);
286+
float thickness = 1.0f,
287+
bool closed = false);
287288

288289
/** Draws a Catmull Rom path.
289290
*
290291
* @param points A point array of control point.
291292
* @param segments The number of segments.
292293
* @param color The Catmull Rom color.
293294
*/
294-
void drawCatmullRom(PointArray* points, unsigned int segments, const Color4B& color, float thickness = 1.0f);
295+
void drawCatmullRom(const PointArray* pointsIn,
296+
unsigned int segments,
297+
const Color4B& color,
298+
float thickness = 1.0f,
299+
bool closed = false);
295300

296301
/** draw a dot at a position, with a given radius and color.
297302
*

core/base/UserDefault.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ namespace ax
6464

6565
UserDefault* UserDefault::_userDefault = nullptr;
6666
#if !USER_DEFAULT_PLAIN_MODE
67-
std::string UserDefault::_userDefalutFileName = "UserDefault.bin";
67+
std::string UserDefault::_userDefaultFileName = "UserDefault.bin";
6868
#else
69-
std::string UserDefault::_userDefalutFileName = "UserDefault.xml";
69+
std::string UserDefault::_userDefaultFileName = "UserDefault.xml";
7070
#endif
7171

7272
static void ud_setkey(std::string& lhs, const cxx17::string_view& rhs)
@@ -375,7 +375,7 @@ void UserDefault::lazyInit()
375375
return;
376376

377377
#if !USER_DEFAULT_PLAIN_MODE
378-
_filePath = FileUtils::getInstance()->getNativeWritableAbsolutePath() + _userDefalutFileName;
378+
_filePath = FileUtils::getInstance()->getNativeWritableAbsolutePath() + _userDefaultFileName;
379379

380380
// construct file mapping
381381
if (!_fileStream.open(_filePath, IFileStream::Mode::OVERLAPPED))
@@ -438,7 +438,7 @@ void UserDefault::lazyInit()
438438
#else
439439
pugi::xml_document doc;
440440

441-
_filePath = FileUtils::getInstance()->getWritablePath() + _userDefalutFileName;
441+
_filePath = FileUtils::getInstance()->getWritablePath() + _userDefaultFileName;
442442

443443
if (FileUtils::getInstance()->isFileExist(_filePath))
444444
{
@@ -529,9 +529,9 @@ void UserDefault::deleteValueForKey(const char* key)
529529
void UserDefault::setFileName(std::string_view nameFile)
530530
{
531531
if (USER_DEFAULT_PLAIN_MODE)
532-
_userDefalutFileName.assign(nameFile).append("UserDefault.xml"sv);
532+
_userDefaultFileName.assign(nameFile).append("UserDefault.xml"sv);
533533
else
534-
_userDefalutFileName.assign(nameFile).append("UserDefault.bin"sv);
534+
_userDefaultFileName.assign(nameFile).append("UserDefault.bin"sv);
535535
}
536536

537537
}

core/base/UserDefault.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class AX_DLL UserDefault
284284
hlookup::string_map<std::string> _values;
285285

286286
static UserDefault* _userDefault;
287-
static std::string _userDefalutFileName;
287+
static std::string _userDefaultFileName;
288288

289289
std::string _filePath;
290290
FileStream _fileStream; // the file handle for data persistence

tests/cpp-tests/Source/DrawNodeTest/DrawNodeTest.cpp

Lines changed: 81 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ DrawNodeTests::DrawNodeTests()
13331333
{
13341334
ADD_TEST_CASE(DrawNodeMethodsTest);
13351335
ADD_TEST_CASE(DrawNodeSpLinesTest);
1336+
ADD_TEST_CASE(DrawNodeSpLinesOpenClosedTest);
13361337
ADD_TEST_CASE(DrawNodeAxmolTest2);
13371338

13381339
#if defined(AX_PLATFORM_PC)
@@ -3462,9 +3463,74 @@ string DrawNodeIssueTester::subtitle() const
34623463
}
34633464

34643465
DrawNodeSpLinesTest::DrawNodeSpLinesTest()
3466+
{
3467+
drawNode = DrawNode::create();
3468+
drawNode->properties.setTransform(true);
3469+
addChild(drawNode, 30);
3470+
3471+
screen = Director::getInstance()->getVisibleSize();
3472+
origin = Director::getInstance()->getVisibleOrigin();
3473+
center = Vec2(screen.width / 2, screen.height / 2);
3474+
sixth = Vec2(screen.width / 6, screen.height / 6);
3475+
sixth.y;
3476+
3477+
defY = (int)(center.y + sixth.y);
3478+
defY2 = (int)(center.y - sixth.y);
3479+
dev = sixth.y;
3480+
3481+
pts = PointArray::create(n);
3482+
pts2 = PointArray::create(n);
3483+
pts->retain();
3484+
pts2->retain();
3485+
for (int i = 0; i < n; ++i)
3486+
{
3487+
pts->insertControlPoint(Vec2(0, 0), i);
3488+
pts2->insertControlPoint(Vec2(0, 0), i);
3489+
}
3490+
3491+
DrawNodeBaseTest::generateDataPoints();
3492+
scheduleUpdate();
3493+
}
3494+
3495+
std::string DrawNodeSpLinesTest::title() const
3496+
{
3497+
return "Testing SpLines";
3498+
}
3499+
3500+
std::string DrawNodeSpLinesTest::subtitle() const
3501+
{
3502+
return "";
3503+
}
3504+
3505+
void DrawNodeSpLinesTest::update(float dt)
3506+
{
3507+
drawNode->clear();
3508+
3509+
// Issue #2302
3510+
auto array = PointArray::create(20);
3511+
for (int i = 0; i < 10; i++)
3512+
{
3513+
array->addControlPoint(Vec2((i % 2) ? 20 : screen.width - 20, 50 + i * 20));
3514+
drawNode->drawPoint(array->getControlPointAtIndex(i), 10, Color4F::BLUE);
3515+
}
3516+
drawNode->drawCardinalSpline(array, 0.1, 20, Color4F::ORANGE);
3517+
3518+
drawNode->drawCardinalSpline(pts, 0.5f, 360, Color4F::RED, 5.0f);
3519+
drawNode->drawCardinalSpline(pts2, 0.5f, 360, Color4F::GREEN, 2.0f);
3520+
3521+
int i1 = RandomHelper::random_int(0, n - 1);
3522+
int i2 = RandomHelper::random_int(0, n - 1);
3523+
drawNode->drawDot(pts->getControlPointAtIndex(i1), 7, Color4F(0, 1, 0, 0.3));
3524+
drawNode->drawDot(pts->getControlPointAtIndex(i1), 4, Color4F::GREEN);
3525+
3526+
drawNode->drawDot(pts2->getControlPointAtIndex(i2), 7, Color4F(0, 1, 0, 0.3));
3527+
drawNode->drawDot(pts2->getControlPointAtIndex(i2), 4, Color4F::GREEN);
3528+
}
3529+
3530+
DrawNodeSpLinesOpenClosedTest::DrawNodeSpLinesOpenClosedTest()
34653531
{
34663532
auto listener = EventListenerTouchAllAtOnce::create();
3467-
listener->onTouchesEnded = AX_CALLBACK_2(DrawNodeSpLinesTest::onTouchesEnded, this);
3533+
listener->onTouchesEnded = AX_CALLBACK_2(DrawNodeSpLinesOpenClosedTest::onTouchesEnded, this);
34683534
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
34693535

34703536
drawNodeCP = DrawNode::create();
@@ -3496,18 +3562,15 @@ DrawNodeSpLinesTest::DrawNodeSpLinesTest()
34963562
}
34973563

34983564
DrawNodeBaseTest::generateDataPoints();
3499-
3500-
addNewControlPoint(VisibleRect::center());
3501-
35023565
scheduleUpdate();
35033566
}
35043567

3505-
void DrawNodeSpLinesTest::addNewControlPoint(Vec2 p)
3568+
void DrawNodeSpLinesOpenClosedTest::addNewControlPoint(Vec2 p)
35063569
{
35073570
points.emplace_back(Vec2(p.x, p.y));
35083571
}
35093572

3510-
void DrawNodeSpLinesTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
3573+
void DrawNodeSpLinesOpenClosedTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
35113574
{
35123575
for (auto& touch : touches)
35133576
{
@@ -3516,66 +3579,39 @@ void DrawNodeSpLinesTest::onTouchesEnded(const std::vector<Touch*>& touches, Eve
35163579
}
35173580
}
35183581

3519-
std::string DrawNodeSpLinesTest::title() const
3582+
std::string DrawNodeSpLinesOpenClosedTest::title() const
35203583
{
3521-
return "Testing SpLines";
3584+
return "Testing open/closed SpLines";
35223585
}
35233586

3524-
std::string DrawNodeSpLinesTest::subtitle() const
3587+
std::string DrawNodeSpLinesOpenClosedTest::subtitle() const
35253588
{
3526-
return "Tap screen to add more (control) points";
3589+
return "Tap screen to add (more) control points";
35273590
}
35283591

3529-
void DrawNodeSpLinesTest::update(float dt)
3592+
void DrawNodeSpLinesOpenClosedTest::update(float dt)
35303593
{
35313594
if (points.size() == 0)
35323595
return;
3533-
3534-
array = PointArray::create(points.size());
3596+
if (points.size() > 2)
3597+
array = PointArray::create(points.size() + 4);
3598+
else
3599+
array = PointArray::create(points.size());
35353600

35363601
drawNodeCP->clear();
3537-
3538-
drawNode->clear();
35393602
drawNode->clear();
35403603

35413604
int boxSize = 3;
35423605
for (auto&& p : points)
35433606
{
3607+
35443608
drawNodeCP->drawRect(Vec2(p.x - boxSize, p.y - boxSize), Vec2(p.x + boxSize, p.y + boxSize), Color4F::BLUE);
35453609
array->addControlPoint(Vec2(p.x, p.y));
35463610
}
35473611

3548-
drawNode->drawCardinalSpline(array, 0.2f, points.size() * 8, Color4F::GREEN, 20.0f);
3549-
drawNode->drawCardinalSpline(array, 0.2f, points.size() * 8, Color4F::BLUE);
3550-
drawNode->drawCardinalSpline(array, 0.2f, points.size() * 16, Color4F(1.0f, 1.0f, 0.5f, 1.0f), 10.0f);
3551-
3552-
// Issue #2302
3553-
auto array3 = PointArray::create(20);
3554-
for (int i = 0; i < 10; i++)
3555-
{
3556-
array3->addControlPoint(Vec2((i % 2) ? 20 : screen.width - 20, 50 + i * 20));
3557-
drawNode->drawPoint(array3->getControlPointAtIndex(i), 10, Color4F::BLUE);
3558-
}
3559-
drawNode->drawCardinalSpline(array3, 0.1, 20, Color4F::ORANGE);
3560-
3561-
// drawNode->drawCatmullRom(array, points.size() * 8, Color4F::YELLOW,5);
3562-
// if (points.size()>3)
3563-
//{
3564-
// int step = points.size()/4;
3565-
// drawNode->drawCubicBezier(points.at(0), points.at(step),points.at(step*2),points.at(points.size()-1),
3566-
// points.size(), Color4F::BLUE);
3567-
//}
3568-
3569-
drawNode->drawCardinalSpline(pts, 0.5f, 360, Color4F::RED, 5.0f);
3570-
drawNode->drawCardinalSpline(pts2, 0.5f, 360, Color4F::GREEN, 2.0f);
3612+
drawNode->drawCardinalSpline(array, 0.0f, points.size() * 20, Color4F::GREEN, 1.0f, true);
3613+
drawNode->drawCardinalSpline(array, 0.0f, points.size() * 20, Color4F::RED, 1.0f, false);
35713614

3572-
int i1 = RandomHelper::random_int(0, n - 1);
3573-
int i2 = RandomHelper::random_int(0, n - 1);
3574-
drawNode->drawDot(pts->getControlPointAtIndex(i1), 7, Color4F(0, 1, 0, 0.3));
3575-
drawNode->drawDot(pts->getControlPointAtIndex(i1), 4, Color4F::GREEN);
3576-
3577-
drawNode->drawDot(pts2->getControlPointAtIndex(i2), 7, Color4F(0, 1, 0, 0.3));
3578-
drawNode->drawDot(pts2->getControlPointAtIndex(i2), 4, Color4F::GREEN);
35793615
}
35803616

35813617
#if defined(AX_PLATFORM_PC)

tests/cpp-tests/Source/DrawNodeTest/DrawNodeTest.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,25 @@ class DrawNodeThicknessStressTest : public DrawNodeBaseTest
405405
float threshold = 0;
406406
};
407407

408+
class DrawNodeSpLinesTest : public DrawNodeBaseTest
409+
{
410+
public:
411+
CREATE_FUNC(DrawNodeSpLinesTest);
408412

413+
DrawNodeSpLinesTest();
409414

415+
virtual std::string title() const override;
416+
virtual std::string subtitle() const override;
410417

418+
void update(float dt);
419+
};
411420

412-
413-
class DrawNodeSpLinesTest : public DrawNodeBaseTest
421+
class DrawNodeSpLinesOpenClosedTest : public DrawNodeBaseTest
414422
{
415423
public:
416-
CREATE_FUNC(DrawNodeSpLinesTest);
424+
CREATE_FUNC(DrawNodeSpLinesOpenClosedTest);
417425

418-
DrawNodeSpLinesTest();
426+
DrawNodeSpLinesOpenClosedTest();
419427

420428
virtual std::string title() const override;
421429
virtual std::string subtitle() const override;

0 commit comments

Comments
 (0)