OpenTTD Source: src/widgets/slider.cpp Source File

OpenTTD Source 12.0-beta1

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
26 void DrawVolumeSliderWidget(Rect r, byte value)
27 {
28 /* Draw a wedge indicating low to high volume level. */
29 const int ha = (r.bottom - r.top) / 5;
30 int wx1 = r.left, wx2 = r.right;
31 if (_current_text_dir == TD_RTL) std::swap(wx1, wx2);
32 const uint shadow = _colour_gradient[COLOUR_GREY][3];
33 const uint fill = _colour_gradient[COLOUR_GREY][6];
34 const uint light = _colour_gradient[COLOUR_GREY][7];
35 const std::vector<Point> wedge{ Point{wx1, r.bottom - ha}, Point{wx2, r.top + ha}, Point{wx2, r.bottom - ha} };
36 GfxFillPolygon(wedge, fill);
37 GfxDrawLine(wedge[0].x, wedge[0].y, wedge[2].x, wedge[2].y, light);
38 GfxDrawLine(wedge[1].x, wedge[1].y, wedge[2].x, wedge[2].y, _current_text_dir == TD_RTL ? shadow : light);
39 GfxDrawLine(wedge[0].x, wedge[0].y, wedge[1].x, wedge[1].y, shadow);
40
41 /* Draw a slider handle indicating current volume level. */
42 const int sw = ScaleGUITrad(SLIDER_WIDTH);
43 if (_current_text_dir == TD_RTL) value = 127 - value;
44 const int x = r.left + (value * (r.right - r.left - sw) / 127);
45 DrawFrameRect(x, r.top, x + sw, r.bottom, COLOUR_GREY, FR_NONE);
46 }
47
55 bool ClickVolumeSliderWidget(Rect r, Point pt, byte &value)
56 {
57 const int sw = ScaleGUITrad(SLIDER_WIDTH);
58 byte new_vol = Clamp((pt.x - r.left - sw / 2) * 127 / (r.right - r.left - sw), 0, 127);
59 if (_current_text_dir == TD_RTL) new_vol = 127 - new_vol;
60
61 if (new_vol != value) {
62 value = new_vol;
63 return true;
64 }
65
66 return false;
67 }
GfxFillPolygon
void GfxFillPolygon(const std::vector< Point > &shape, int colour, FillRectMode mode)
Fill a polygon with colour.
Definition: gfx.cpp:212
DrawVolumeSliderWidget
void DrawVolumeSliderWidget(Rect r, byte value)
Draw a volume slider widget with know at given value.
Definition: slider.cpp:26
_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:52
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
ClickVolumeSliderWidget
bool ClickVolumeSliderWidget(Rect r, Point pt, byte &value)
Handle click on a volume slider widget to change the value.
Definition: slider.cpp:55
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
ScaleGUITrad
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:76
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:209
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
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:48