diff options
| author | Matthias Melcher <github@matthiasm.com> | 2022-11-24 12:47:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-24 12:47:49 +0100 |
| commit | b16309f13e732e566c1beb4a02a2165ebb3ab4ab (patch) | |
| tree | 36b2b7de094dac8627f15586a17c49251160fe67 /src/Fl_Graphics_Driver.cxx | |
| parent | 12dccaf711991c574d9654923b271d3d1f905dfe (diff) | |
Refactor code to make rounded rectangles accessible (#553)
This adds fl_rounded_rect and fl_rounded_rectf so the
user can draw rounded rectangles. This uses existing and
optimised code that is rearranged.
Diffstat (limited to 'src/Fl_Graphics_Driver.cxx')
| -rw-r--r-- | src/Fl_Graphics_Driver.cxx | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Fl_Graphics_Driver.cxx b/src/Fl_Graphics_Driver.cxx index 95f79ee22..9d120961e 100644 --- a/src/Fl_Graphics_Driver.cxx +++ b/src/Fl_Graphics_Driver.cxx @@ -455,6 +455,50 @@ void Fl_Graphics_Driver::rect(int x, int y, int w, int h) {} /** see fl_rectf() */ void Fl_Graphics_Driver::rectf(int x, int y, int w, int h) {} +void Fl_Graphics_Driver::_rbox(int fill, int x, int y, int w, int h, int r) { + static double lut[] = { 0.0, 0.07612, 0.29289, 0.61732, 1.0}; + if (r == 5) r = 4; // use only even sizes for small corners (STR #2943) + if (r == 7) r = 8; // note: 8 is better than 6 (really) + double xd = x, yd = y, rd = (x+w-1), bd = (y+h-1); + double rr = r; + if (fill) begin_polygon(); else begin_loop(); + // top left + transformed_vertex(xd+lut[0]*rr, yd+lut[4]*rr); + transformed_vertex(xd+lut[1]*rr, yd+lut[3]*rr); + transformed_vertex(xd+lut[2]*rr, yd+lut[2]*rr); + transformed_vertex(xd+lut[3]*rr, yd+lut[1]*rr); + transformed_vertex(xd+lut[4]*rr, yd+lut[0]*rr); + // top right + transformed_vertex(rd-lut[4]*rr, yd+lut[0]*rr); + transformed_vertex(rd-lut[3]*rr, yd+lut[1]*rr); + transformed_vertex(rd-lut[2]*rr, yd+lut[2]*rr); + transformed_vertex(rd-lut[1]*rr, yd+lut[3]*rr); + transformed_vertex(rd-lut[0]*rr, yd+lut[4]*rr); + // bottom right + transformed_vertex(rd-lut[0]*rr, bd-lut[4]*rr); + transformed_vertex(rd-lut[1]*rr, bd-lut[3]*rr); + transformed_vertex(rd-lut[2]*rr, bd-lut[2]*rr); + transformed_vertex(rd-lut[3]*rr, bd-lut[1]*rr); + transformed_vertex(rd-lut[4]*rr, bd-lut[0]*rr); + // bottom left + transformed_vertex(xd+lut[4]*rr, bd-lut[0]*rr); + transformed_vertex(xd+lut[3]*rr, bd-lut[1]*rr); + transformed_vertex(xd+lut[2]*rr, bd-lut[2]*rr); + transformed_vertex(xd+lut[1]*rr, bd-lut[3]*rr); + transformed_vertex(xd+lut[0]*rr, bd-lut[4]*rr); + if (fill) fl_end_polygon(); else fl_end_loop(); +} + +/** see fl_rrect() */ +void Fl_Graphics_Driver::rounded_rect(int x, int y, int w, int h, int r) { + _rbox(0, x, y, w, h, r); +} + +/** see fl_rrectf() */ +void Fl_Graphics_Driver::rounded_rectf(int x, int y, int w, int h, int r) { + _rbox(1, x, y, w, h, r); +} + void Fl_Graphics_Driver::colored_rectf(int x, int y, int w, int h, uchar r, uchar g, uchar b) { color(r, g, b); rectf(x, y, w, h); |
