OpenTTD Source  1.11.2
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 
25 void DrawVolumeSliderWidget(Rect r, byte value)
26 {
27  static const int slider_width = 3;
28 
29  /* Draw a wedge indicating low to high volume level. */
30  const int ha = (r.bottom - r.top) / 5;
31  int wx1 = r.left, wx2 = r.right;
32  if (_current_text_dir == TD_RTL) std::swap(wx1, wx2);
33  const uint shadow = _colour_gradient[COLOUR_GREY][3];
34  const uint fill = _colour_gradient[COLOUR_GREY][6];
35  const uint light = _colour_gradient[COLOUR_GREY][7];
36  const std::vector<Point> wedge{ Point{wx1, r.bottom - ha}, Point{wx2, r.top + ha}, Point{wx2, r.bottom - ha} };
37  GfxFillPolygon(wedge, fill);
38  GfxDrawLine(wedge[0].x, wedge[0].y, wedge[2].x, wedge[2].y, light);
39  GfxDrawLine(wedge[1].x, wedge[1].y, wedge[2].x, wedge[2].y, _current_text_dir == TD_RTL ? shadow : light);
40  GfxDrawLine(wedge[0].x, wedge[0].y, wedge[1].x, wedge[1].y, shadow);
41 
42  /* Draw a slider handle indicating current volume level. */
43  const int sw = ScaleGUITrad(slider_width);
44  if (_current_text_dir == TD_RTL) value = 127 - value;
45  const int x = r.left + (value * (r.right - r.left - sw) / 127);
46  DrawFrameRect(x, r.top, x + sw, r.bottom, COLOUR_GREY, FR_NONE);
47 }
48 
56 bool ClickVolumeSliderWidget(Rect r, Point pt, byte &value)
57 {
58  byte new_vol = Clamp((pt.x - r.left) * 127 / (r.right - r.left), 0, 127);
59  if (_current_text_dir == TD_RTL) new_vol = 127 - new_vol;
60 
61  /* Clamp to make sure min and max are properly settable */
62  if (new_vol > 124) new_vol = 127;
63  if (new_vol < 3) new_vol = 0;
64  if (new_vol != value) {
65  value = new_vol;
66  return true;
67  }
68 
69  return false;
70 }
GfxFillPolygon
void GfxFillPolygon(const std::vector< Point > &shape, int colour, FillRectMode mode)
Fill a polygon with colour.
Definition: gfx.cpp:210
DrawVolumeSliderWidget
void DrawVolumeSliderWidget(Rect r, byte value)
Draw a volume slider widget with know at given value.
Definition: slider.cpp:25
_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:56
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:175
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