OpenTTD Source  14.0-beta3
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../palette_func.h"
12 #include "../window_gui.h"
13 #include "../window_func.h"
14 #include "../strings_func.h"
15 #include "../zoom_func.h"
16 #include "slider_func.h"
17 
18 #include "../safeguards.h"
19 
20 static const int SLIDER_WIDTH = 3;
21 
30 void DrawSliderWidget(Rect r, int min_value, int max_value, int value, const std::map<int, StringID> &labels)
31 {
32  /* Allow space for labels. We assume they are in the small font. */
33  if (!labels.empty()) r.bottom -= GetCharacterHeight(FS_SMALL) + WidgetDimensions::scaled.hsep_normal;
34 
35  max_value -= min_value;
36 
37  /* Draw a wedge indicating low to high value. */
38  const int ha = (r.bottom - r.top) / 5;
39  const int sw = ScaleGUITrad(SLIDER_WIDTH);
40  const int t = WidgetDimensions::scaled.bevel.top; /* Thickness of lines */
41  int wx1 = r.left + sw / 2;
42  int wx2 = r.right - sw / 2;
43  if (_current_text_dir == TD_RTL) std::swap(wx1, wx2);
44  const uint shadow = _colour_gradient[COLOUR_GREY][3];
45  const uint fill = _colour_gradient[COLOUR_GREY][6];
46  const uint light = _colour_gradient[COLOUR_GREY][7];
47  const std::vector<Point> wedge{ Point{wx1, r.bottom - ha}, Point{wx2, r.top + ha}, Point{wx2, r.bottom - ha} };
48  GfxFillPolygon(wedge, fill);
49  GfxDrawLine(wedge[0].x, wedge[0].y, wedge[2].x, wedge[2].y, light, t);
50  GfxDrawLine(wedge[1].x, wedge[1].y, wedge[2].x, wedge[2].y, _current_text_dir == TD_RTL ? shadow : light, t);
51  GfxDrawLine(wedge[0].x, wedge[0].y, wedge[1].x, wedge[1].y, shadow, t);
52 
53  int x;
54  for (auto label : labels) {
55  x = label.first - min_value;
56  if (_current_text_dir == TD_RTL) x = max_value - x;
57  x = r.left + (x * (r.right - r.left - sw) / max_value) + sw / 2;
58  GfxDrawLine(x, r.bottom - ha + 1, x, r.bottom + (label.second == STR_NULL ? 0 : WidgetDimensions::scaled.hsep_normal), shadow, t);
59  if (label.second != STR_NULL) {
60  Dimension d = GetStringBoundingBox(label.second, FS_SMALL);
61  x = Clamp(x - d.width / 2, r.left, r.right - d.width);
62  DrawString(x, x + d.width, r.bottom + 1 + WidgetDimensions::scaled.hsep_normal, label.second, TC_BLACK, SA_CENTER, false, FS_SMALL);
63  }
64  }
65 
66  /* Draw a slider handle indicating current value. */
67  value -= min_value;
68  if (_current_text_dir == TD_RTL) value = max_value - value;
69  x = r.left + (value * (r.right - r.left - sw) / max_value);
70  DrawFrameRect(x, r.top, x + sw, r.bottom, COLOUR_GREY, FR_NONE);
71 }
72 
80 bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &value)
81 {
82  max_value -= min_value;
83 
84  const int sw = ScaleGUITrad(SLIDER_WIDTH);
85  int new_value = Clamp((pt.x - r.left - sw / 2) * max_value / (r.right - r.left - sw), 0, max_value);
86  if (_current_text_dir == TD_RTL) new_value = max_value - new_value;
87  new_value += min_value;
88 
89  if (new_value != value) {
90  value = new_value;
91  return true;
92  }
93 
94  return false;
95 }
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
GfxFillPolygon
void GfxFillPolygon(const std::vector< Point > &shape, int colour, FillRectMode mode)
Fill a polygon with colour.
Definition: gfx.cpp:209
_colour_gradient
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: palette.cpp:26
ScaleGUITrad
int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:117
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:204
DrawSliderWidget
void DrawSliderWidget(Rect r, int min_value, int max_value, int value, const std::map< int, StringID > &labels)
Draw a slider widget with knob at given value.
Definition: slider.cpp:30
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
ClickSliderWidget
bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &value)
Handle click on a slider widget to change the value.
Definition: slider.cpp:80
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:281
SA_CENTER
@ SA_CENTER
Center both horizontally and vertically.
Definition: gfx_type.h:348
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:78
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:658
WidgetDimensions::bevel
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition: window_gui.h:40
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
WidgetDimensions::hsep_normal
int hsep_normal
Normal horizontal spacing.
Definition: window_gui.h:63
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition: strings_type.h:24
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:56
GetStringBoundingBox
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:852