Skip to content

Commit 58ba339

Browse files
authored
Refactor DrawNode (#3076)
- Flat properties members into DrawNode - removed all "hardcoded clamps" - add const to all properties getters - add missing DrawNode::properties.getScale() - removed pragma push_macro("TRANSPARENT") section - cpp-tests DrawNode: switch from ax::ui to ax::extension::ImGUI - class ax::Label adapt properties: underline/strikethrough
1 parent 5eb9fe6 commit 58ba339

File tree

8 files changed

+920
-1050
lines changed

8 files changed

+920
-1050
lines changed

axmol/2d/DrawNode.cpp

Lines changed: 59 additions & 273 deletions
Large diffs are not rendered by default.

axmol/2d/DrawNode.h

Lines changed: 75 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,9 @@ class AX_DLL DrawNode : public Node, public BlendProtocol
489489
*/
490490

491491
void drawColoredTriangle(const Vec2* vertices3, const Color* color3);
492-
void drawTriangle(const Vec2* vertices3, const Color& color);
493492

494-
void drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color& color);
493+
void drawTriangle(const Vec2* vertices3, const Color& color, float thickness = 1.0f);
494+
void drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color& color, float thickness = 1.0f);
495495

496496
void drawSolidTriangle(const Vec2* vertices3,
497497
const Color& fillColor,
@@ -582,15 +582,9 @@ class AX_DLL DrawNode : public Node, public BlendProtocol
582582
// Internal function _drawDot
583583
void _drawDot(const Vec2& pos, float radius, const Color& color);
584584

585-
// Internal function _drawTriangle
585+
// Internal function _drawColoredTriangle
586586
// Note: modifies supplied vertex array
587-
void _drawTriangle(Vec2* vertices3,
588-
const Color& borderColor,
589-
const Color& fillColor,
590-
bool solid = true,
591-
float thickness = 0.0f);
592-
593-
void _drawColoredTriangle(Vec2* vertices3, const Color* color3);
587+
void _drawColoredTriangle(const Vec2* vertices3, const Color* color3);
594588

595589
// Internal function _drawAStar
596590
void _drawAStar(const Vec2& center,
@@ -663,96 +657,91 @@ class AX_DLL DrawNode : public Node, public BlendProtocol
663657
*/
664658
tlx::pod_vector<Vec2> _transform(const Vec2* vertices, unsigned int& count, bool closedPolygon = false);
665659

666-
void applyTransform(const Vec2* from, Vec2* to, unsigned int count);
667-
668-
private:
669-
AX_DISALLOW_COPY_AND_ASSIGN(DrawNode);
660+
void applyLocalTransform(const Vec2* from, Vec2* to, unsigned int count) const;
670661

662+
/// Advanced settings
671663
public:
672-
class AX_DLL Properties
664+
float getThicknessScale() const { return _thicknessScale; }
665+
void setThicknessScale(float s)
673666
{
674-
public:
675-
float factor; /// thickness scale factor
676-
677-
// transforming stuff
678-
Vec2 scale;
679-
Vec2 center;
680-
float rotation;
681-
Vec2 position;
682-
683-
// Drawing flags
684-
bool transform = false;
685-
bool drawOrder = false;
686-
687-
/** Set the DrawNode drawOrder
688-
*
689-
* @param drawOrder. true/false = On/Off
690-
* Its for performance there
691-
* false = cocos2dx behaviour => faster but works only on 1.0f thickness
692-
693-
*/
694-
void setDrawOrder(bool dO) { drawOrder = dO; };
695-
696-
/** Get the DrawNode drawOrder
697-
*
698-
*/
699-
bool getDrawOrder(void) { return drawOrder; };
700-
701-
/** Set the DrawNode transform
702-
*
703-
* @param transform. true/false = On/Off
704-
*
705-
*/
706-
void setTransform(bool t) { transform = t; };
707-
708-
/** Get the DrawNode transform
709-
*
710-
*/
711-
bool getTransform(void) { return transform; };
712-
713-
/** Set the DrawNode scale for each drawing primitive after this.
714-
715-
*/
716-
void setScale(Vec2 s) { scale = s; };
717-
718-
/** Set the DrawNode rotation for each drawing primitive after this.
667+
_thicknessScale = s;
668+
_linesDirty = _trianglesDirty = true;
669+
}
719670

720-
*/
721-
void setRotation(float r) { rotation = r; };
722-
723-
/** Get the DrawNode rotation for each drawing primitive after this.
724-
725-
*/
726-
float getRotation() { return rotation; };
727-
728-
/** Set the DrawNode center of rotation for each drawing primitive after this.
671+
bool isLocalTransformEnabled() const { return _localTransformEnabled; }
672+
void setLocalTransformEnabled(bool e)
673+
{
674+
_localTransformEnabled = e;
675+
_linesDirty = _trianglesDirty = true;
676+
}
729677

730-
*/
731-
void setCenter(Vec2 c) { center = c; };
678+
void setLocalScale(const Vec2& s)
679+
{
680+
_localScale = s;
681+
_trianglesDirty = _linesDirty = true;
682+
}
683+
const Vec2& getLocalScale() const { return _localScale; }
732684

733-
/** Get the DrawNode center of rotation for each drawing primitive after this.
685+
void setLocalPivot(const Vec2& c)
686+
{
687+
_localPivot = c;
688+
_trianglesDirty = _linesDirty = true;
689+
}
690+
const Vec2& getLocalPivot() const { return _localPivot; }
734691

735-
*/
736-
Vec2 getCenter() { return center; };
692+
void setLocalRotation(float a)
693+
{
694+
_localRotation = a;
695+
_localRotationRad = AX_DEGREES_TO_RADIANS(_localRotation);
696+
_trianglesDirty = _linesDirty = true;
697+
}
698+
float getLocalRotation() const { return _localRotation; }
737699

738-
/** Set the DrawNode position for each drawing primitive after this.
700+
void setLocalPosition(const Vec2& p)
701+
{
702+
_localPosition = p;
703+
_trianglesDirty = _linesDirty = true;
704+
}
705+
const Vec2& getLocalPosition() const { return _localPosition; }
739706

740-
*/
741-
void setPosition(Vec2 p) { position = p; };
707+
void setPreserveDrawOrder(bool v)
708+
{
709+
_preserveDrawOrder = v;
710+
_trianglesDirty = _linesDirty = true;
711+
}
712+
bool isPreserveDrawOrder() const { return _preserveDrawOrder; }
742713

743-
/** Get the DrawNode position for drawing primitive.
714+
// convenience: reset to defaults
715+
void resetAdvancedSettings()
716+
{
717+
_thicknessScale = 1.0f;
718+
_localScale = Vec2(1.0f, 1.0f);
719+
_localPivot = Vec2::ZERO;
720+
_localRotation = 0.0f;
721+
_localRotationRad = 0.0f;
722+
_localPosition = Vec2::ZERO;
723+
_preserveDrawOrder = false;
724+
_localTransformEnabled = false;
725+
_trianglesDirty = _linesDirty = _pointsDirty = true;
726+
}
744727

745-
*/
746-
Vec2 getPosition() { return position; };
728+
protected:
729+
// thickness scale (was factor)
730+
float _thicknessScale{1.0f};
747731

748-
/** Set all default DrawNode properties.
732+
// optional local transform applied to primitives when enabled
733+
Vec2 _localScale{1.0f, 1.0f};
734+
Vec2 _localPivot{0.0f, 0.0f};
735+
float _localRotation{0.0f}; // local rotation in degrees
736+
float _localRotationRad{0.0f}; // local rotation in radians (cached for efficiency)
737+
Vec2 _localPosition{0.0f, 0.0f};
749738

750-
*/
751-
void setDefaultValues();
752-
float getFactor() { return factor; };
753-
void setFactor(float fac) { factor = fac; };
739+
// flags
740+
bool _localTransformEnabled{false};
741+
bool _preserveDrawOrder{false};
754742

755-
} properties;
743+
private:
744+
AX_DISALLOW_COPY_AND_ASSIGN(DrawNode);
756745
};
757746

758747
/** @} */

axmol/2d/Label.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,8 +1523,7 @@ void Label::enableUnderline()
15231523
_lineDrawNode = DrawNode::create();
15241524
_lineDrawNode->setGlobalZOrder(getGlobalZOrder());
15251525
_lineDrawNode->setOpacity(_displayedColor.a);
1526-
_lineDrawNode->properties.setFactor(_lineDrawNode->properties.getFactor() *
1527-
2.0f); // 2.0f: Makes the line smaller
1526+
_lineDrawNode->setThicknessScale(_lineDrawNode->getThicknessScale() * 0.5f); // 0.5f: Makes the line smaller
15281527
addChild(_lineDrawNode, 100000);
15291528
}
15301529
}
@@ -1542,8 +1541,7 @@ void Label::enableStrikethrough()
15421541
_lineDrawNode = DrawNode::create();
15431542
_lineDrawNode->setGlobalZOrder(getGlobalZOrder());
15441543
_lineDrawNode->setOpacity(_displayedColor.a);
1545-
_lineDrawNode->properties.setFactor(_lineDrawNode->properties.getFactor() *
1546-
2.0f); // 2.0f: Makes the line smaller
1544+
_lineDrawNode->setThicknessScale(_lineDrawNode->getThicknessScale() * 0.5f); // 0.5f: Makes the line smaller
15471545
addChild(_lineDrawNode, 100000);
15481546
}
15491547
}

0 commit comments

Comments
 (0)