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/>.
8 /** @file slider.cpp Implementation of the horizontal slider widget. */
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"
18 #include "safeguards.h"
20 static const int SLIDER_WIDTH
= 3;
23 * Draw a slider widget with knob at given value
24 * @param r Rectangle to draw the widget in
25 * @param min_value Minimum value of slider
26 * @param max_value Maximum value of slider
27 * @param nmarks Number of marks to display (when mark_func is provided.)
28 * @param value Value to put the slider at
29 * @param mark_func Callback function to get the StringID to draw on a mark.
31 void DrawSliderWidget(Rect r
, int min_value
, int max_value
, int nmarks
, int value
, SliderMarkFunc
*mark_func
)
33 /* Allow space for labels. We assume they are in the small font. */
34 if (mark_func
!= nullptr) r
.bottom
-= GetCharacterHeight(FS_SMALL
) + WidgetDimensions::scaled
.hsep_normal
;
36 max_value
-= min_value
;
38 /* Draw a wedge indicating low to high value. */
39 const int ha
= (r
.bottom
- r
.top
) / 5;
40 const int sw
= ScaleGUITrad(SLIDER_WIDTH
);
41 const int t
= WidgetDimensions::scaled
.bevel
.top
; /* Thickness of lines */
42 int wx1
= r
.left
+ sw
/ 2;
43 int wx2
= r
.right
- sw
/ 2;
44 if (_current_text_dir
== TD_RTL
) std::swap(wx1
, wx2
);
45 const uint shadow
= GetColourGradient(COLOUR_GREY
, SHADE_DARK
);
46 const uint fill
= GetColourGradient(COLOUR_GREY
, SHADE_LIGHTER
);
47 const uint light
= GetColourGradient(COLOUR_GREY
, SHADE_LIGHTEST
);
48 const std::vector
<Point
> wedge
{ Point
{wx1
, r
.bottom
- ha
}, Point
{wx2
, r
.top
+ ha
}, Point
{wx2
, r
.bottom
- ha
} };
49 GfxFillPolygon(wedge
, fill
);
50 GfxDrawLine(wedge
[0].x
, wedge
[0].y
, wedge
[2].x
, wedge
[2].y
, light
, t
);
51 GfxDrawLine(wedge
[1].x
, wedge
[1].y
, wedge
[2].x
, wedge
[2].y
, _current_text_dir
== TD_RTL
? shadow
: light
, t
);
52 GfxDrawLine(wedge
[0].x
, wedge
[0].y
, wedge
[1].x
, wedge
[1].y
, shadow
, t
);
55 if (mark_func
!= nullptr) {
56 for (int mark
= 0; mark
< nmarks
; ++mark
) {
57 const int mark_value
= (max_value
* mark
) / (nmarks
- 1);
59 const StringID str
= mark_func(nmarks
, mark
, mark_value
+ min_value
);
60 if (str
== INVALID_STRING_ID
) continue;
63 if (_current_text_dir
== TD_RTL
) x
= max_value
- mark_value
;
64 x
= r
.left
+ (x
* (r
.right
- r
.left
- sw
) / max_value
) + sw
/ 2;
65 GfxDrawLine(x
, r
.bottom
- ha
+ 1, x
, r
.bottom
+ (str
== STR_NULL
? 0 : WidgetDimensions::scaled
.hsep_normal
), shadow
, t
);
66 if (str
== STR_NULL
) continue;
68 Dimension d
= GetStringBoundingBox(str
, FS_SMALL
);
69 x
= Clamp(x
- d
.width
/ 2, r
.left
, r
.right
- d
.width
);
70 DrawString(x
, x
+ d
.width
, r
.bottom
+ 1 + WidgetDimensions::scaled
.hsep_normal
, str
, TC_BLACK
, SA_CENTER
, false, FS_SMALL
);
74 /* Draw a slider handle indicating current value. */
76 if (_current_text_dir
== TD_RTL
) value
= max_value
- value
;
77 x
= r
.left
+ (value
* (r
.right
- r
.left
- sw
) / max_value
);
78 DrawFrameRect(x
, r
.top
, x
+ sw
, r
.bottom
, COLOUR_GREY
, FR_NONE
);
82 * Handle click on a slider widget to change the value
83 * @param r Rectangle of the widget
84 * @param pt Clicked point
85 * @param min_value Minimum value of slider
86 * @param max_value Maximum value of slider
87 * @param nmarks Number of marks displayed. Value will be rounded to nearest mark.
88 * @param value[in,out] Value to modify
89 * @return True if the value setting was modified
91 bool ClickSliderWidget(Rect r
, Point pt
, int min_value
, int max_value
, int nmarks
, int &value
)
93 max_value
-= min_value
;
95 const int sw
= ScaleGUITrad(SLIDER_WIDTH
);
96 int new_value
= Clamp((pt
.x
- r
.left
- sw
/ 2) * max_value
/ (r
.right
- r
.left
- sw
), 0, max_value
);
97 if (_current_text_dir
== TD_RTL
) new_value
= max_value
- new_value
;
98 new_value
+= min_value
;
101 const int step
= max_value
/ (nmarks
- 1);
102 new_value
= ((new_value
+ step
/ 2) / step
) * step
;
105 if (new_value
!= value
) {