Skip to content

Instantly share code, notes, and snippets.

@v1993
Last active May 2, 2018 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v1993/93e0f07c7f88ebd0f8b8c985110c158a to your computer and use it in GitHub Desktop.
Save v1993/93e0f07c7f88ebd0f8b8c985110c158a to your computer and use it in GitHub Desktop.
RoundedBox node for Cocos2d-x (tested on 3.16)
#include "RoundedBox.hpp"
#define _USE_MATH_DEFINES
#include <cmath>
USING_NS_CC;
void RoundedBox::drawSolidRoundedRect(const Vec2 &origin, const Vec2 &destination, float radius, unsigned int segments, const Color4F &color){
const float coef = 2.0f * (float)M_PI/(float)(segments - 8);
Vec2 *vertices = new (std::nothrow) Vec2[segments+1];
if( ! vertices )
return;
// Draw right edge
vertices[0].x = destination.x;
vertices[0].y = origin.y + radius;
vertices[1].x = destination.x;
vertices[1].y = destination.y - radius;
unsigned int quadrant = 1;
unsigned int radsI = 0;
for(unsigned int i = 2; i <= segments; i++) {
float rads = radsI*coef;
GLfloat j = radius * cosf(rads);
GLfloat k = radius * sinf(rads);
if (rads < M_PI_2 || rads > M_PI + M_PI_2) {
if (quadrant == 3) {
// Draw bottom edge
vertices[i].x = origin.x + radius;
vertices[i].y = origin.y;
i++;
vertices[i].x = destination.x - radius;
vertices[i].y = origin.y;
quadrant++;
continue;
}
j += destination.x - radius;
} else {
if (quadrant == 1) {
// Draw top edge
vertices[i].x = destination.x - radius;
vertices[i].y = destination.y;
i++;
vertices[i].x = origin.x + radius;
vertices[i].y = destination.y;
quadrant++;
continue;
}
j += origin.x + radius;
}
if (rads < M_PI) {
k += destination.y - radius;
} else {
if (quadrant == 2) {
// Draw left edge
vertices[i].x = origin.x;
vertices[i].y = destination.y - radius;
i++;
vertices[i].x = origin.x;
vertices[i].y = origin.y + radius;
quadrant++;
continue;
}
k += origin.y + radius;
}
vertices[i].x = j;
vertices[i].y = k;
radsI++;
}
drawSolidPoly(vertices, segments, color);
CC_SAFE_DELETE_ARRAY(vertices);
}
RoundedBox::RoundedBox(Vec2 size, float radius):
_size(size),
_radius(radius),
_color(Color3B::WHITE)
{};
RoundedBox::~RoundedBox() {};
RoundedBox* RoundedBox::create(Vec2 size, float radius) {
RoundedBox* ret = new (std::nothrow) RoundedBox(size, radius);
if (ret && ret->init()) {
ret->autorelease();
} else {
CC_SAFE_DELETE(ret);
}
return ret;
}
bool RoundedBox::init() {
if (!DrawNode::init()) {
return false;
}
myDraw();
setContentSize(Size(_size.x, _size.y));
return true;
}
void RoundedBox::myDraw() {
clear();
drawSolidRoundedRect(Vec2::ZERO, _size, _radius, M_PI*_radius, Color4F(_color));
}
void RoundedBox::setColor(const Color3B& color) {
_color = color;
myDraw();
}
const Color3B& RoundedBox::getColor() const {
return _color;
}
#pragma once
#include "cocos2d.h"
class RoundedBox: public cocos2d::DrawNode {
public:
static RoundedBox* create(cocos2d::Vec2 size, float radius);
virtual void setColor(const cocos2d::Color3B& color);
virtual const cocos2d::Color3B& getColor() const;
CC_CONSTRUCTOR_ACCESS:
RoundedBox(cocos2d::Vec2 size, float radius);
virtual ~RoundedBox();
virtual bool init();
protected:
void drawSolidRoundedRect(const cocos2d::Vec2 &origin, const cocos2d::Vec2 &destination, float radius, unsigned int segments, const cocos2d::Color4F &color);
void myDraw();
private:
cocos2d::Color3B _color;
cocos2d::Vec2 _size;
float _radius;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment