diff options
author | Niels Martin Hansen <nielsm@indvikleren.dk> | 2021-04-04 10:04:06 +0200 |
---|---|---|
committer | Niels Martin Hansen <nielsm@indvikleren.dk> | 2021-04-09 12:18:52 +0200 |
commit | 88c92f7b7c51295f5a9cc8d1b837fbca03be37fb (patch) | |
tree | 41fa06dfa9eafcf58c919a546a3a5884a74058fe /src/widgets | |
parent | 64c9af09914472c6a9eeb495cd790f7c325b194c (diff) | |
download | openttd-88c92f7b7c51295f5a9cc8d1b837fbca03be37fb.tar.xz |
Codechange: Move volume control slider logic to separate functions
Diffstat (limited to 'src/widgets')
-rw-r--r-- | src/widgets/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/widgets/slider.cpp | 70 | ||||
-rw-r--r-- | src/widgets/slider_func.h | 21 |
3 files changed, 93 insertions, 0 deletions
diff --git a/src/widgets/CMakeLists.txt b/src/widgets/CMakeLists.txt index 18ecd529e..5586870a3 100644 --- a/src/widgets/CMakeLists.txt +++ b/src/widgets/CMakeLists.txt @@ -44,6 +44,8 @@ add_files( screenshot_widget.h settings_widget.h sign_widget.h + slider.cpp + slider_func.h smallmap_widget.h station_widget.h statusbar_widget.h diff --git a/src/widgets/slider.cpp b/src/widgets/slider.cpp new file mode 100644 index 000000000..6d6d73288 --- /dev/null +++ b/src/widgets/slider.cpp @@ -0,0 +1,70 @@ +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** @file slider.cpp Implementation of the horizontal slider widget. */ + +#include "../stdafx.h" +#include "../window_gui.h" +#include "../window_func.h" +#include "../strings_func.h" +#include "../zoom_func.h" +#include "slider_func.h" + +#include "../safeguards.h" + + +/** + * Draw a volume slider widget with know at given value + * @param r Rectangle to draw the widget in + * @param value Value to put the slider at + */ +void DrawVolumeSliderWidget(Rect r, byte value) +{ + static const int slider_width = 3; + + /* Draw a wedge indicating low to high volume level. */ + const int ha = (r.bottom - r.top) / 5; + int wx1 = r.left, wx2 = r.right; + if (_current_text_dir == TD_RTL) std::swap(wx1, wx2); + const uint shadow = _colour_gradient[COLOUR_GREY][3]; + const uint fill = _colour_gradient[COLOUR_GREY][6]; + const uint light = _colour_gradient[COLOUR_GREY][7]; + const std::vector<Point> wedge{ Point{wx1, r.bottom - ha}, Point{wx2, r.top + ha}, Point{wx2, r.bottom - ha} }; + GfxFillPolygon(wedge, fill); + GfxDrawLine(wedge[0].x, wedge[0].y, wedge[2].x, wedge[2].y, light); + GfxDrawLine(wedge[1].x, wedge[1].y, wedge[2].x, wedge[2].y, _current_text_dir == TD_RTL ? shadow : light); + GfxDrawLine(wedge[0].x, wedge[0].y, wedge[1].x, wedge[1].y, shadow); + + /* Draw a slider handle indicating current volume level. */ + const int sw = ScaleGUITrad(slider_width); + if (_current_text_dir == TD_RTL) value = 127 - value; + const int x = r.left + (value * (r.right - r.left - sw) / 127); + DrawFrameRect(x, r.top, x + sw, r.bottom, COLOUR_GREY, FR_NONE); +} + +/** + * Handle click on a volume slider widget to change the value + * @param r Rectangle of the widget + * @param pt Clicked point + * @param value[in,out] Volume value to modify + * @return True if the volume setting was modified + */ +bool ClickVolumeSliderWidget(Rect r, Point pt, byte &value) +{ + byte new_vol = Clamp((pt.x - r.left) * 127 / (r.right - r.left), 0, 127); + if (_current_text_dir == TD_RTL) new_vol = 127 - new_vol; + + /* Clamp to make sure min and max are properly settable */ + if (new_vol > 124) new_vol = 127; + if (new_vol < 3) new_vol = 0; + if (new_vol != value) { + value = new_vol; + return true; + } + + return false; +} diff --git a/src/widgets/slider_func.h b/src/widgets/slider_func.h new file mode 100644 index 000000000..1aa1fa10c --- /dev/null +++ b/src/widgets/slider_func.h @@ -0,0 +1,21 @@ +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** @file slider_type.h Types related to the horizontal slider widget. */ + +#ifndef WIDGETS_SLIDER_TYPE_H +#define WIDGETS_SLIDER_TYPE_H + +#include "../window_type.h" +#include "../gfx_func.h" + + +void DrawVolumeSliderWidget(Rect r, byte value); +bool ClickVolumeSliderWidget(Rect r, Point pt, byte &value); + + +#endif /* WIDGETS_SLIDER_TYPE_H */ |