OpenTTD Source  13.2.1
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 "../window_gui.h"
12 #include "../window_func.h"
13 #include "../strings_func.h"
14 #include "../zoom_func.h"
15 #include "slider_func.h"
16 
17 #include "../safeguards.h"
18 
19 static const int SLIDER_WIDTH = 3;
20 
29 void DrawSliderWidget(Rect r, int min_value, int max_value, int value, const std::map<int, StringID> &labels)
30 {
31  /* Allow space for labels. We assume they are in the small font. */
32  if (labels.size() > 0) r.bottom -= FONT_HEIGHT_SMALL + WidgetDimensions::scaled.hsep_normal;
33 
34  max_value -= min_value;
35 
36  /* Draw a wedge indicating low to high value. */
37  const int ha = (r.bottom - r.top) / 5;
38  const int sw = ScaleGUITrad(SLIDER_WIDTH);
39  const int t = WidgetDimensions::scaled.bevel.top; /* Thickness of lines */
40  int wx1 = r.left + sw / 2;
41  int wx2 = r.right - sw / 2;
42  if (_current_text_dir == TD_RTL) std::swap(wx1, wx2);
43  const uint shadow = _colour_gradient[COLOUR_GREY][3];
44  const uint fill = _colour_gradient[COLOUR_GREY][6];
45  const uint light = _colour_gradient[COLOUR_GREY][7];
46  const std::vector<Point> wedge{ Point{wx1, r.bottom - ha}, Point{wx2, r.top + ha}, Point{wx2, r.bottom - ha} };
47  GfxFillPolygon(wedge, fill);
48  GfxDrawLine(wedge[0].x, wedge[0].y, wedge[2].x, wedge[2].y, light, t);
49  GfxDrawLine(wedge[1].x, wedge[1].y, wedge[2].x, wedge[2].y, _current_text_dir == TD_RTL ? shadow : light, t);
50  GfxDrawLine(wedge[0].x, wedge[0].y, wedge[1].x, wedge[1].y, shadow, t);
51 
52  int x;
53  for (auto label : labels) {
54  x = label.first - min_value;
55  if (_current_text_dir == TD_RTL) x = max_value - x;
56  x = r.left + (x * (r.right - r.left - sw) / max_value) + sw / 2;
57  GfxDrawLine(x, r.bottom - ha + 1, x, r.bottom + (label.second == STR_NULL ? 0 : WidgetDimensions::scaled.hsep_normal), shadow, t);
58  if (label.second != STR_NULL) {
59  Dimension d = GetStringBoundingBox(label.second, FS_SMALL);
60  x = Clamp(x - d.width / 2, r.left, r.right - d.width);
61  DrawString(x, x + d.width, r.bottom + 1 + WidgetDimensions::scaled.hsep_normal, label.second, TC_BLACK, SA_CENTER, false, FS_SMALL);
62  }
63  }
64 
65  /* Draw a slider handle indicating current value. */
66  value -= min_value;
67  if (_current_text_dir == TD_RTL) value = max_value - value;
68  x = r.left + (value * (r.right - r.left - sw) / max_value);
69  DrawFrameRect(x, r.top, x + sw, r.bottom, COLOUR_GREY, FR_NONE);
70 }
71 
79 bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &value)
80 {
81  max_value -= min_value;
82 
83  const int sw = ScaleGUITrad(SLIDER_WIDTH);
84  int new_value = Clamp((pt.x - r.left - sw / 2) * max_value / (r.right - r.left - sw), 0, max_value);
85  if (_current_text_dir == TD_RTL) new_value = max_value - new_value;
86  new_value += min_value;
87 
88  if (new_value != value) {
89  value = new_value;
90  return true;
91  }
92 
93  return false;
94 }
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
GfxFillPolygon
void GfxFillPolygon(const std::vector< Point > &shape, int colour, FillRectMode mode)
Fill a polygon with colour.
Definition: gfx.cpp:212
DrawString
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:644
_colour_gradient
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:55
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:890
WidgetDimensions::hsep_normal
int hsep_normal
Normal horizontal spacing.
Definition: window_gui.h:63
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:29
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
FONT_HEIGHT_SMALL
#define FONT_HEIGHT_SMALL
Height of characters in the small (FS_SMALL) font.
Definition: gfx_func.h:203
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
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:79
WidgetDimensions::bevel
RectPadding bevel
Widths of bevel border.
Definition: window_gui.h:45
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:414
SA_CENTER
@ SA_CENTER
Center both horizontally and vertically.
Definition: gfx_type.h:344
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69
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:49
ScaleGUITrad
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition: widget.cpp:168