Implementation
bool roundedRectangleRgba(
Pointer<SdlRenderer> renderer,
double x1,
double y1,
double x2,
double y2,
double rad,
int r,
int g,
int b,
int a, {
int blendMode = SDL_BLENDMODE_BLEND,
}) {
var result = true;
var x10 = x1;
var y10 = y1;
var x20 = x2;
var y20 = y2;
var rad0 = rad;
double tmp;
double w;
double h;
double xx1;
double xx2;
double yy1;
double yy2;
/*
* Check renderer
*/
if (renderer == nullptr) {
return false;
}
/*
* Check radius vor valid range
*/
if (rad0 < 0) {
return false;
}
/*
* Special case - no rounding
*/
if (rad0 <= 1) {
return rectangleRgba(
renderer,
x10,
y10,
x20,
y20,
r,
g,
b,
a,
blendMode: blendMode,
);
}
/*
* Test for special cases of straight lines or single point
*/
if (x10 == x20) {
if (y10 == y20) {
return pixelRgba(renderer, x10, y10, r, g, b, a, blendMode: blendMode);
} else {
return vlineRgba(
renderer,
x10,
y10,
y20,
r,
g,
b,
a,
blendMode: blendMode,
);
}
} else {
if (y10 == y20) {
return hlineRgba(
renderer,
x10,
x20,
y10,
r,
g,
b,
a,
blendMode: blendMode,
);
}
}
/*
* Swap x10, x20 if required
*/
if (x10 > x20) {
tmp = x10;
x10 = x20;
x20 = tmp;
}
/*
* Swap y10, y20 if required
*/
if (y10 > y20) {
tmp = y10;
y10 = y20;
y20 = tmp;
}
/*
* Calculate width&height
*/
w = x20 - x10;
h = y20 - y10;
/*
* Maybe adjust radius
*/
if ((rad0 * 2) > w) {
rad0 = w / 2;
}
if ((rad0 * 2) > h) {
rad0 = h / 2;
}
/*
* Draw corners
*/
xx1 = x10 + rad0;
xx2 = x20 - rad0;
yy1 = y10 + rad0;
yy2 = y20 - rad0;
if (result) {
result = arcRgba(
renderer,
xx1,
yy1,
rad0,
180,
270,
r,
g,
b,
a,
blendMode: blendMode,
);
}
if (result) {
result = arcRgba(
renderer,
xx2,
yy1,
rad0,
270,
360,
r,
g,
b,
a,
blendMode: blendMode,
);
}
if (result) {
result = arcRgba(
renderer,
xx1,
yy2,
rad0,
90,
180,
r,
g,
b,
a,
blendMode: blendMode,
);
}
if (result) {
result = arcRgba(
renderer,
xx2,
yy2,
rad0,
0,
90,
r,
g,
b,
a,
blendMode: blendMode,
);
}
/*
* Draw lines
*/
if (xx1 <= xx2) {
if (result) {
result = hlineRgba(
renderer,
xx1,
xx2,
y10,
r,
g,
b,
a,
blendMode: blendMode,
);
}
if (result) {
result = hlineRgba(
renderer,
xx1,
xx2,
y20,
r,
g,
b,
a,
blendMode: blendMode,
);
}
}
if (yy1 <= yy2) {
if (result) {
result = vlineRgba(
renderer,
x10,
yy1,
yy2,
r,
g,
b,
a,
blendMode: blendMode,
);
}
if (result) {
result = vlineRgba(
renderer,
x20,
yy1,
yy2,
r,
g,
b,
a,
blendMode: blendMode,
);
}
}
return result;
}