* Draw a slider widget with knob at given value * @param r Rectangle to draw the widget in * @param wedge_colour Colour to draw wedge. * @param handle_colour Colour to draw handle. * @param text_colour Colour of text. * @param min_value Minimum value of slider * @param max_value Maximum value of slider * @param nmarks Number of marks to display (when mark_func is provided.) * @param value
| 32 | * @param mark_func Callback function to get the StringID to draw on a mark. |
| 33 | */ |
| 34 | void DrawSliderWidget(Rect r, Colours wedge_colour, Colours handle_colour, TextColour text_colour, int min_value, int max_value, int nmarks, int value, SliderMarkFunc *mark_func) |
| 35 | { |
| 36 | /* Allow space for labels. We assume they are in the small font. */ |
| 37 | if (mark_func != nullptr) r.bottom -= GetCharacterHeight(FS_SMALL) + WidgetDimensions::scaled.hsep_normal; |
| 38 | |
| 39 | max_value -= min_value; |
| 40 | |
| 41 | /* Draw a wedge indicating low to high value. */ |
| 42 | const int ha = (r.bottom - r.top) / 5; |
| 43 | const int sw = ScaleGUITrad(SLIDER_WIDTH); |
| 44 | const int t = WidgetDimensions::scaled.bevel.top; /* Thickness of lines */ |
| 45 | int wx1 = r.left + sw / 2; |
| 46 | int wx2 = r.right - sw / 2; |
| 47 | if (_current_text_dir == TD_RTL) std::swap(wx1, wx2); |
| 48 | const PixelColour shadow = GetColourGradient(wedge_colour, SHADE_DARK); |
| 49 | const PixelColour fill = GetColourGradient(wedge_colour, SHADE_LIGHTER); |
| 50 | const PixelColour light = GetColourGradient(wedge_colour, SHADE_LIGHTEST); |
| 51 | const std::array<Point, 3> wedge{ Point{wx1, r.bottom - ha}, Point{wx2, r.top + ha}, Point{wx2, r.bottom - ha} }; |
| 52 | GfxFillPolygon(wedge, fill); |
| 53 | GfxDrawLine(wedge[0].x, wedge[0].y, wedge[2].x, wedge[2].y, light, t); |
| 54 | GfxDrawLine(wedge[1].x, wedge[1].y, wedge[2].x, wedge[2].y, _current_text_dir == TD_RTL ? shadow : light, t); |
| 55 | GfxDrawLine(wedge[0].x, wedge[0].y, wedge[1].x, wedge[1].y, shadow, t); |
| 56 | |
| 57 | int x; |
| 58 | if (mark_func != nullptr) { |
| 59 | for (int mark = 0; mark < nmarks; ++mark) { |
| 60 | const int mark_value = (max_value * mark) / (nmarks - 1); |
| 61 | |
| 62 | auto str = mark_func(nmarks, mark, mark_value + min_value); |
| 63 | if (!str.has_value()) continue; |
| 64 | |
| 65 | x = mark_value; |
| 66 | if (_current_text_dir == TD_RTL) x = max_value - mark_value; |
| 67 | x = r.left + (x * (r.right - r.left - sw) / max_value) + sw / 2; |
| 68 | GfxDrawLine(x, r.bottom - ha + 1, x, r.bottom + (str->empty() ? 0 : WidgetDimensions::scaled.hsep_normal), shadow, t); |
| 69 | if (str->empty()) continue; |
| 70 | |
| 71 | Dimension d = GetStringBoundingBox(*str, FS_SMALL); |
| 72 | x = Clamp(x - d.width / 2, r.left, r.right - d.width); |
| 73 | DrawString(x, x + d.width, r.bottom + 1 + WidgetDimensions::scaled.hsep_normal, *str, text_colour, SA_CENTER, false, FS_SMALL); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /* Draw a slider handle indicating current value. */ |
| 78 | value -= min_value; |
| 79 | if (_current_text_dir == TD_RTL) value = max_value - value; |
| 80 | x = r.left + (value * (r.right - r.left - sw) / max_value); |
| 81 | DrawFrameRect(x, r.top, x + sw, r.bottom, handle_colour, {}); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Handle click on a slider widget to change the value |
no test coverage detected