drawCircle tester improved (#1054)

* drawCircle tester improved

* add more info to the tester

* codacy fix
This commit is contained in:
aismann 2023-02-11 01:05:27 +01:00 committed by GitHub
parent 2712f0ee2f
commit 294ce2fffc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 26 deletions

View File

@ -260,42 +260,91 @@ string Issue11942Test::subtitle() const
// //
BetterCircleRendering::BetterCircleRendering() BetterCircleRendering::BetterCircleRendering()
{ {
//// DrawNode 0 ------------------------------------------ //Add lines to see the correct "scale of the 'rings'" changing the window size
//auto draw0 = DrawNode::create(); auto draw0 = DrawNode::create();
//addChild(draw0, 10); draw0->setLineWidth(1);
addChild(draw0, 10);
//// draw a circle thickness 10 for (float y = 0; y < VisibleRect::top().y; y += 10)
//draw0->setLineWidth(10); {
//draw0->drawCircle(VisibleRect::center() - Vec2(140.0f, 40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false, draw0->drawLine({VisibleRect::left().x, y}, {VisibleRect::right().x, y}, Color4B::GRAY);
// Color4F::GREEN, 2); }
//draw0->setLineWidth(30); // thickness 10 will replaced with thickness 1 (also for all 'same' draw commands before!) initSliders();
//draw0->drawCircle(VisibleRect::center() - Vec2(140.0f, -40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
// Color4F::GREEN, 2);
//// DrawNode 1 ------------------------------------------
drawNode = DrawNode::create(); drawNode = DrawNode::create();
addChild(drawNode, 10); addChild(drawNode, 10);
thick = 0;
scheduleUpdate(); scheduleUpdate();
}
void BetterCircleRendering::changeThickness(ax::Ref* pSender, ax::ui::Slider::EventType type)
{
if (type == ax::ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
{
ax::ui::Slider* sliderThickness = dynamic_cast<ax::ui::Slider*>(pSender);
thick = sliderThickness->getPercent();
_thickNessLabel->setString("setLineWidth(" + Value(thick).asString() + ")");
}
}
void BetterCircleRendering::changeLineWidth(ax::Ref* pSender, ax::ui::Slider::EventType type)
{
if (type == ax::ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
{
ax::ui::Slider* sliderLineWidth = dynamic_cast<ax::ui::Slider*>(pSender);
lineWidth = sliderLineWidth->getPercent();
_lineWidthLabel->setString("drawCircle(pos, radius, ..., segemnts, ..., color, " + Value(lineWidth).asString() + ")");
}
}
void BetterCircleRendering::initSliders()
{
auto vsize = Director::getInstance()->getVisibleSize();
ax::ui::Slider* slider = ax::ui::Slider::create();
slider->setPercent(0);
slider->loadBarTexture("cocosui/sliderTrack.png");
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
slider->setPosition(Vec2(vsize.width / 2, vsize.height / 6));
slider->addEventListener(AX_CALLBACK_2(BetterCircleRendering::changeThickness, this));
auto ttfConfig = TTFConfig("fonts/arial.ttf", 8);
_thickNessLabel = Label::createWithTTF(ttfConfig, "setLineWidth(0)");
addChild(_thickNessLabel, 20);
_thickNessLabel->setPosition(Vec2(vsize.width / 2, vsize.height / 6 + 15));
addChild(slider, 20);
ax::ui::Slider* sliderLineWidth = ax::ui::Slider::create();
sliderLineWidth->setPercent(0);
sliderLineWidth->loadBarTexture("cocosui/sliderTrack.png");
sliderLineWidth->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
sliderLineWidth->loadProgressBarTexture("cocosui/sliderProgress.png");
sliderLineWidth->setPosition(Vec2(vsize.width / 2, vsize.height / 6 + 35));
sliderLineWidth->addEventListener(AX_CALLBACK_2(BetterCircleRendering::changeLineWidth, this));
_lineWidthLabel = Label::createWithTTF(ttfConfig, "drawCircle(pos, radius, ..., segments, ..., color, 0)");
addChild(_lineWidthLabel, 20);
_lineWidthLabel->setPosition(Vec2(vsize.width / 2, vsize.height / 6 + 50));
addChild(sliderLineWidth, 20);
} }
void BetterCircleRendering::update(float dt) void BetterCircleRendering::update(float dt)
{ {
static float thick = 0;
thick += 0.5;
if (thick > 200)
thick = 0;
drawNode->clear(); drawNode->clear();
drawNode->setLineWidth(thick); drawNode->setLineWidth(thick); // value from the slider
drawNode->drawCircle(VisibleRect::center() + Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false, // Old behavior => faster but badly rendering if line width > 5 (= rings)
Color4F::RED); drawNode->drawCircle(VisibleRect::center() + Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false, Color4F::RED);
drawNode->drawCircle(VisibleRect::center() - Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false, // New behavior => slower but good rendering if line width > 5
Color4F::GREEN, 2); auto color = Color4F::GREEN;
if (thick <= lineWidth)
{
color = Color4F::RED; // using the faster rendering internal method of drawCircle (old behavior)
}
drawNode->drawCircle(VisibleRect::center() - Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false, color,
lineWidth);
} }
string BetterCircleRendering::title() const string BetterCircleRendering::title() const
@ -305,7 +354,7 @@ string BetterCircleRendering::title() const
string BetterCircleRendering::subtitle() const string BetterCircleRendering::subtitle() const
{ {
return "Green be the optimized rendering circle"; return "Green: smoother rendering; Red: faster but badly rendering";
} }
Issue829Test::Issue829Test() Issue829Test::Issue829Test()

View File

@ -24,6 +24,7 @@
#pragma once #pragma once
#include "axmol.h" #include "axmol.h"
#include "ui/CocosGUI.h"
#include "../BaseTest.h" #include "../BaseTest.h"
#include <string> #include <string>
@ -58,7 +59,6 @@ public:
virtual std::string subtitle() const override; virtual std::string subtitle() const override;
}; };
class BetterCircleRendering : public DrawPrimitivesBaseTest class BetterCircleRendering : public DrawPrimitivesBaseTest
{ {
public: public:
@ -70,8 +70,16 @@ public:
virtual std::string subtitle() const override; virtual std::string subtitle() const override;
void update(float dt); void update(float dt);
void initSliders();
void changeThickness(Ref* pSender, ax::ui::Slider::EventType type);
void changeLineWidth(Ref* pSender, ax::ui::Slider::EventType type);
private: private:
ax::DrawNode* drawNode; ax::DrawNode* drawNode;
float thick = 0;
ax::Label* _thickNessLabel;
float lineWidth = 0;
ax::Label* _lineWidthLabel;
}; };
class Issue829Test : public DrawPrimitivesBaseTest class Issue829Test : public DrawPrimitivesBaseTest