diff --git a/src/nanovg.c b/src/nanovg.c index c388696..ae17dd4 100644 --- a/src/nanovg.c +++ b/src/nanovg.c @@ -2119,22 +2119,30 @@ void nvgRect(NVGcontext* ctx, float x, float y, float w, float h) void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r) { - if (r < 0.1f) { - nvgRect(ctx, x,y,w,h); + nvgRoundedRectEx(ctx, x, y, w, h, r, r, r, r); +} + +void nvgRoundedRectEx(NVGcontext* ctx, float x, float y, float w, float h, float r1, float r2, float r3, float r4) +{ + if(r1 < 0.1f && r2 < 0.1f && r3 < 0.1f && r4 < 0.1f) { + nvgRect(ctx, x, y, w, h); return; } else { - float rx = nvg__minf(r, nvg__absf(w)*0.5f) * nvg__signf(w), ry = nvg__minf(r, nvg__absf(h)*0.5f) * nvg__signf(h); + float r1x = nvg__minf(r1, nvg__absf(w)*0.5f) * nvg__signf(w), r1y = nvg__minf(r1, nvg__absf(h)*0.5f) * nvg__signf(h); + float r2x = nvg__minf(r2, nvg__absf(w)*0.5f) * nvg__signf(w), r2y = nvg__minf(r2, nvg__absf(h)*0.5f) * nvg__signf(h); + float r3x = nvg__minf(r3, nvg__absf(w)*0.5f) * nvg__signf(w), r3y = nvg__minf(r3, nvg__absf(h)*0.5f) * nvg__signf(h); + float r4x = nvg__minf(r4, nvg__absf(w)*0.5f) * nvg__signf(w), r4y = nvg__minf(r4, nvg__absf(h)*0.5f) * nvg__signf(h); float vals[] = { - NVG_MOVETO, x, y+ry, - NVG_LINETO, x, y+h-ry, - NVG_BEZIERTO, x, y+h-ry*(1-NVG_KAPPA90), x+rx*(1-NVG_KAPPA90), y+h, x+rx, y+h, - NVG_LINETO, x+w-rx, y+h, - NVG_BEZIERTO, x+w-rx*(1-NVG_KAPPA90), y+h, x+w, y+h-ry*(1-NVG_KAPPA90), x+w, y+h-ry, - NVG_LINETO, x+w, y+ry, - NVG_BEZIERTO, x+w, y+ry*(1-NVG_KAPPA90), x+w-rx*(1-NVG_KAPPA90), y, x+w-rx, y, - NVG_LINETO, x+rx, y, - NVG_BEZIERTO, x+rx*(1-NVG_KAPPA90), y, x, y+ry*(1-NVG_KAPPA90), x, y+ry, + NVG_MOVETO, x, y + r4y, + NVG_LINETO, x, y + h - r1y, + NVG_BEZIERTO, x, y + h - r1y*(1 - NVG_KAPPA90), x + r1x*(1 - NVG_KAPPA90), y + h, x + r1x, y + h, + NVG_LINETO, x + w - r2x, y + h, + NVG_BEZIERTO, x + w - r2x*(1 - NVG_KAPPA90), y + h, x + w, y + h - r2y*(1 - NVG_KAPPA90), x + w, y + h - r2y, + NVG_LINETO, x + w, y + r3y, + NVG_BEZIERTO, x + w, y + r3y*(1 - NVG_KAPPA90), x + w - r3x*(1 - NVG_KAPPA90), y, x + w - r3x, y, + NVG_LINETO, x + r4x, y, + NVG_BEZIERTO, x + r4x*(1 - NVG_KAPPA90), y, x, y + r4y*(1 - NVG_KAPPA90), x, y + r4y, NVG_CLOSE }; nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals)); diff --git a/src/nanovg.h b/src/nanovg.h index e8a59fa..63b4940 100644 --- a/src/nanovg.h +++ b/src/nanovg.h @@ -489,6 +489,10 @@ void nvgRect(NVGcontext* ctx, float x, float y, float w, float h); // Creates new rounded rectangle shaped sub-path. void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r); +// Creates new rounded rectangle shaped sub-path with more radius control for each corner. +// r1 is bottom left, r2 is bottom right, r3, is top right, r4 is top left. +void nvgRoundedRectEx(NVGcontext* ctx, float x, float y, float w, float h, float r1, float r2, float r3, float r4); + // Creates new ellipse shaped sub-path. void nvgEllipse(NVGcontext* ctx, float cx, float cy, float rx, float ry);