OpenTTD Source: src/slider.cpp Source File

OpenTTD Source 15.0-RC2

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
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 = 3;
21
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
94 bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int nmarks, int &value)
95{
96 max_value -= min_value;
97
98 const int sw = ScaleGUITrad(SLIDER_WIDTH);
99 int new_value = Clamp((pt.x - r.left - sw / 2) * max_value / (r.right - r.left - sw), 0, max_value);
100 if (_current_text_dir == TD_RTL) new_value = max_value - new_value;
101 new_value += min_value;
102
103 if (nmarks > 0) {
104 const int step = max_value / (nmarks - 1);
105 new_value = ((new_value + step / 2) / step) * step;
106 }
107
108 if (new_value != value) {
109 value = new_value;
110 return true;
111 }
112
113 return false;
114}
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
WidgetDimensions::hsep_normal
int hsep_normal
Normal horizontal spacing.
Definition window_gui.h:61
WidgetDimensions::bevel
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition window_gui.h:38
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:87
GetStringBoundingBox
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:894
DrawString
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:665
GfxFillPolygon
void GfxFillPolygon(std::span< const Point > shape, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Fill a polygon with colour.
Definition gfx.cpp:212
gfx_func.h
Functions related to the gfx engine.
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:250
SA_CENTER
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:398
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
GetColourGradient
PixelColour GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:388
palette_func.h
Functions related to palettes.
safeguards.h
A number of safeguards to prevent using unsafe methods.
DrawSliderWidget
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)
Draw a slider widget with knob at given value.
Definition slider.cpp:34
ClickSliderWidget
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:94
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
strings_func.h
Functions related to OTTD's strings.
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition strings_type.h:27
Coord2D< int >
Coord2D::x
T x
X coordinate.
Definition geometry_type.hpp:34
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
PixelColour
Colour for pixel/line drawing.
Definition gfx_type.h:405
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:289
ScaleGUITrad
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
window_gui.h
Functions, definitions and such used only by the GUI.
zoom_func.h
Functions related to zooming.