Refactor drawing small circles: add fl_draw_circle()

This method can be used to draw small circles as part of the GUI.
It is independent of the current scheme.

Very small circles are approximated by drawing several rectangles.
This commit is contained in:
Albrecht Schlosser 2023-01-03 19:40:37 +01:00
parent b5b88d5f0d
commit 4d1a508c7e
3 changed files with 66 additions and 29 deletions

View File

@ -1,7 +1,7 @@
//
// Portable drawing function header file for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2022 by Bill Spitzak and others.
// Copyright 1998-2023 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@ -972,6 +972,9 @@ void fl_draw_check(Fl_Rect bb, Fl_Color col);
// Draw one or more "arrows" (triangles)
FL_EXPORT void fl_draw_arrow(Fl_Rect bb, Fl_Arrow_Type t, Fl_Orientation o, Fl_Color color);
// Draw a potentially small, filled circle
FL_EXPORT void fl_draw_circle(int x0, int y0, int d, Fl_Color color);
// images:
/**

View File

@ -1,7 +1,7 @@
//
// Menu code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2022 by Bill Spitzak and others.
// Copyright 1998-2023 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@ -245,34 +245,12 @@ void Fl_Menu_Item::draw(int x, int y, int w, int h, const Fl_Menu_* m,
tW --;
fl_pie(x + td + 1, y + d + td - 1, tW + 3, tW + 3, 0.0, 360.0);
fl_color(fl_color_average(FL_WHITE, FL_SELECTION_COLOR, 0.2f));
} else fl_color(labelcolor_);
switch (tW) {
// Larger circles draw fine...
default :
fl_pie(x + td + 2, y + d + td, tW, tW, 0.0, 360.0);
break;
// Small circles don't draw well on many systems...
case 6 :
fl_rectf(x + td + 4, y + d + td, tW - 4, tW);
fl_rectf(x + td + 3, y + d + td + 1, tW - 2, tW - 2);
fl_rectf(x + td + 2, y + d + td + 2, tW, tW - 4);
break;
case 5 :
case 4 :
case 3 :
fl_rectf(x + td + 3, y + d + td, tW - 2, tW);
fl_rectf(x + td + 2, y + d + td + 1, tW, tW - 2);
break;
case 2 :
case 1 :
fl_rectf(x + td + 2, y + d + td, tW, tW);
break;
} else {
fl_color(labelcolor_);
}
fl_draw_circle(x + td + 2, y + d + td, tW, fl_color());
if (Fl::is_scheme("gtk+")) {
fl_color(fl_color_average(FL_WHITE, FL_SELECTION_COLOR, 0.5));
fl_arc(x + td + 2, y + d + td, tW + 1, tW + 1, 60.0, 180.0);

View File

@ -1,7 +1,7 @@
//
// Label drawing code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2022 by Bill Spitzak and others.
// Copyright 1998-2023 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@ -587,3 +587,59 @@ void fl_draw_check(Fl_Rect bb, Fl_Color col) {
fl_color(saved_color);
} // fl_draw_check()
/**
Draw a potentially small, filled circle as a GUI element.
This method draws a filled circle, using fl_pie() if the given diameter
\p d is larger than 6 pixels (aka FLTK units).
If \p d is 6 or smaller it approximates a filled circle by drawing several
filled rectangles, depending on the size because fl_pie() might not draw
well on many systems for smaller sizes.
\param[in] x0,y0 coordinates of top left of the bounding box
\param[in] d diameter == width and height of the bounding box
\param[in] color drawing color
*/
void fl_draw_circle(int x0, int y0, int d, Fl_Color color) {
#define DEBUG_DRAW_CIRCLE (0) // bit 1 = draw bounding box (green)
Fl_Color saved_color = fl_color();
#if (DEBUG_DRAW_CIRCLE & 1)
fl_rectf(x0, y0, d, d, FL_GREEN);
#endif
// draw the circle
switch (d) {
// Larger circles draw fine...
default:
fl_pie(x0, y0, d, d, 0.0, 360.0);
break;
// Small circles don't draw well on many systems...
case 6:
fl_rectf(x0 + 2, y0, d - 4, d);
fl_rectf(x0 + 1, y0 + 1, d - 2, d - 2);
fl_rectf(x0, y0 + 2, d, d - 4);
break;
case 5:
case 4:
case 3:
fl_rectf(x0 + 1, y0, d - 2, d);
fl_rectf(x0, y0 + 1, d, d - 2);
break;
case 2:
case 1:
fl_rectf(x0, y0, d, d);
break;
}
fl_color(saved_color);
} // fl_draw_circle()