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. */
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"
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 value Value to put the slider at
28 * @param labels List of positions and labels to draw along the slider.
30 void DrawSliderWidget(Rect r
, int min_value
, int max_value
, int value
, const std::map
<int, StringID
> &labels
)
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
;
35 max_value
-= min_value
;
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
);
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
);
66 /* Draw a slider handle indicating current 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
);
74 * Handle click on a slider widget to change the value
75 * @param r Rectangle of the widget
76 * @param pt Clicked point
77 * @param value[in,out] Value to modify
78 * @return True if the value setting was modified
80 bool ClickSliderWidget(Rect r
, Point pt
, int min_value
, int max_value
, int &value
)
82 max_value
-= min_value
;
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
;
89 if (new_value
!= value
) {