OpenTTD Source 20261112-master-gbcbec5e63f
slider.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "gfx_func.h"
12#include "palette_func.h"
13#include "slider_func.h"
14#include "strings_func.h"
15#include "window_gui.h"
16#include "zoom_func.h"
17
18#include "safeguards.h"
19
20static const int SLIDER_WIDTH = 5;
21
36void 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, int soft_min, int soft_max)
37{
38 /* Allow space for labels. We assume they are in the small font. */
39 Rect tr = r;
40 if (mark_func != nullptr) {
41 tr.top = tr.bottom - GetCharacterHeight(FontSize::Small) + 1;
42 r.bottom = tr.top - WidgetDimensions::scaled.hsep_normal - 1;
43 }
44
45 const int ha = r.Height() / 5;
46 const int sw = ScaleGUITrad(SLIDER_WIDTH);
47 const int t = WidgetDimensions::scaled.bevel.top; /* Thickness of lines */
48
49 /* Position the wedge so that slider can overlap the ends. */
50 r.left += sw / 2;
51 r.right -= sw / 2;
52
53 const PixelColour shadow = GetColourGradient(wedge_colour, Shade::Dark);
54 const PixelColour fill = GetColourGradient(wedge_colour, Shade::Lighter);
55 const PixelColour light = GetColourGradient(wedge_colour, Shade::Lightest);
56
57 /* Adjust the wedge direction for LTR/RTL languages. */
58 int wx1 = r.left;
59 int wx2 = r.right;
60 if (_current_text_dir == TD_RTL) std::swap(wx1, wx2);
61
62 /* Draw a wedge indicating low to high value. */
63 const std::array<Point, 3> wedge{ Point{wx1, r.bottom - ha}, Point{wx2, r.top + ha}, Point{wx2, r.bottom - ha} };
64 GfxFillPolygon(wedge, fill);
65 GfxDrawLine(wedge[0].x, wedge[0].y, wedge[2].x, wedge[2].y, light, t);
66 GfxDrawLine(wedge[1].x, wedge[1].y, wedge[2].x, wedge[2].y, _current_text_dir == TD_RTL ? shadow : light, t);
67 GfxDrawLine(wedge[0].x, wedge[0].y, wedge[1].x, wedge[1].y, shadow, t);
68
69 int range = max_value - min_value;
70 int width = r.right - r.left;
71
72 int x;
73 if (mark_func != nullptr) {
74 for (int mark = 0; mark < nmarks; ++mark) {
75 int mark_value = range * mark / (nmarks - 1);
76
77 auto str = mark_func(nmarks, mark, mark_value + min_value);
78 if (!str.has_value()) continue;
79
80 if (_current_text_dir == TD_RTL) mark_value = range - mark_value;
81 x = r.left + (mark_value * width / range);
82
83 GfxDrawLine(x, r.bottom - ha + 1, x, r.bottom + (str->empty() ? 0 : WidgetDimensions::scaled.hsep_normal), shadow, t);
84 if (str->empty()) continue;
85
87 x = Clamp(x - d.width / 2, r.left, r.right - d.width);
88 DrawString(tr.WithX(x, x + d.width), *str, text_colour, {AlignmentH::Centre, AlignmentV::Middle}, false, FontSize::Small);
89
90 if (soft_min > mark_value + min_value || soft_max < mark_value + min_value) {
92 }
93 }
94 }
95
96 if (soft_min > min_value) {
97 x = (soft_min - min_value) * width / range - t;
99 }
100
101 if (soft_max < max_value) {
102 x = (soft_max - min_value) * width / range + t;
104 }
105
106 /* Draw a slider handle indicating current value. */
107 value -= min_value;
108 if (_current_text_dir == TD_RTL) value = range - value;
109 x = r.left + (value * width / range) - sw / 2;
110 DrawFrameRect(x, r.top, x + sw - 1, r.bottom, handle_colour, {});
111}
112
123bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int nmarks, int &value)
124{
125 max_value -= min_value;
126
127 const int sw = ScaleGUITrad(SLIDER_WIDTH);
128 int new_value = Clamp((pt.x - r.left - sw / 2) * max_value / (r.right - r.left - sw), 0, max_value);
129 if (_current_text_dir == TD_RTL) new_value = max_value - new_value;
130 new_value += min_value;
131
132 if (nmarks > 0) {
133 const int step = max_value / (nmarks - 1);
134 new_value = ((new_value + step / 2) / step) * step;
135 }
136
137 if (new_value != value) {
138 value = new_value;
139 return true;
140 }
141
142 return false;
143}
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:88
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:894
void GfxFillPolygon(std::span< const Point > shape, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Fill a polygon with colour.
Definition gfx.cpp:208
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:111
int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:663
Functions related to the gfx engine.
@ Small
Index of the small font in the font tables.
Definition gfx_type.h:250
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:315
@ Checker
Draw only every second pixel, used for greying-out.
Definition gfx_type.h:393
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
PixelColour GetColourGradient(Colours colour, Shade shade)
Get colour gradient palette index.
Definition palette.cpp:393
Functions related to palettes.
@ Lightest
Lightest colour shade.
@ Lighter
Lighter colour shade.
@ Light
Light colour shade.
@ Dark
Dark colour shade.
A number of safeguards to prevent using unsafe methods.
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, int soft_min, int soft_max)
Draw a slider widget with knob at given value.
Definition slider.cpp:36
static const int SLIDER_WIDTH
Width of the slider handle.
Definition slider.cpp:20
bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int nmarks, int &value)
Handle click on a slider widget to change the value.
Definition slider.cpp:123
Functions related to the horizontal slider widget.
Definition of base types and functions in a cross-platform compatible way.
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:55
Functions related to OTTD's strings.
@ TD_RTL
Text is written right-to-left by default.
T x
X coordinate.
Dimensions (a width and height) of a rectangle in 2D.
value_type width
The width of the rectangle.
Colour for pixel/line drawing.
Definition gfx_type.h:307
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
int Height() const
Get height of Rect.
Rect WithX(int new_left, int new_right) const
Create a new Rect, replacing the left and right coordinates.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:308
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
Functions, definitions and such used only by the GUI.
Functions related to zooming.