Changeset - r25139:87bde218e763
[Not reviewed]
master
0 3 2
Niels Martin Hansen - 3 years ago 2021-04-04 08:04:06
nielsm@indvikleren.dk
Codechange: Move volume control slider logic to separate functions
5 files changed with 104 insertions and 34 deletions:
0 comments (0 inline, 0 general)
src/music_gui.cpp
Show inline comments
 
@@ -23,12 +23,13 @@
 
#include "core/geometry_func.hpp"
 
#include "string_func.h"
 
#include "settings_type.h"
 
#include "settings_gui.h"
 
#include "widgets/dropdown_func.h"
 
#include "widgets/dropdown_type.h"
 
#include "widgets/slider_func.h"
 

	
 
#include "widgets/music_widget.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 

	
 
@@ -640,14 +641,12 @@ static WindowDesc _music_track_selection
 
static void ShowMusicTrackSelection()
 
{
 
	AllocateWindowDescFront<MusicTrackSelectionWindow>(&_music_track_selection_desc, 0);
 
}
 

	
 
struct MusicWindow : public Window {
 
	static const int slider_width = 3;
 

	
 
	MusicWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
 
	{
 
		this->InitNested(number);
 
		this->LowerWidget(_settings_client.music.playlist + WID_M_ALL);
 
		this->SetWidgetLoweredState(WID_M_SHUFFLE, _settings_client.music.shuffle);
 

	
 
@@ -737,33 +736,19 @@ struct MusicWindow : public Window {
 
					SetDParamStr(0, entry.songname);
 
				}
 
				DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
 
				break;
 
			}
 

	
 
			case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: {
 
				/* 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);
 
				byte volume = (widget == WID_M_MUSIC_VOL) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
 
				if (_current_text_dir == TD_RTL) volume = 127 - volume;
 
				const int x = r.left + (volume * (r.right - r.left - sw) / 127);
 
				DrawFrameRect(x, r.top, x + sw, r.bottom, COLOUR_GREY, FR_NONE);
 
			case WID_M_MUSIC_VOL:
 
				DrawVolumeSliderWidget(r, _settings_client.music.music_vol);
 
				break;
 
			}
 

	
 
			case WID_M_EFFECT_VOL:
 
				DrawVolumeSliderWidget(r, _settings_client.music.effect_vol);
 
				break;
 
		}
 
	}
 

	
 
	/**
 
	 * Some data on this window has become invalid.
 
	 * @param data Information about the changed data.
 
@@ -798,24 +783,15 @@ struct MusicWindow : public Window {
 

	
 
			case WID_M_PLAY: // start playing
 
				_music.Play();
 
				break;
 

	
 
			case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: { // volume sliders
 
				int x = pt.x - this->GetWidget<NWidgetBase>(widget)->pos_x;
 

	
 
				byte *vol = (widget == WID_M_MUSIC_VOL) ? &_settings_client.music.music_vol : &_settings_client.music.effect_vol;
 

	
 
				byte new_vol = Clamp(x * 127 / (int)this->GetWidget<NWidgetBase>(widget)->current_x, 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 != *vol) {
 
					*vol = new_vol;
 
					if (widget == WID_M_MUSIC_VOL) MusicDriver::GetInstance()->SetVolume(new_vol);
 
				byte &vol = (widget == WID_M_MUSIC_VOL) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
 
				if (ClickVolumeSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, vol)) {
 
					if (widget == WID_M_MUSIC_VOL) MusicDriver::GetInstance()->SetVolume(vol);
 
					this->SetDirty();
 
				}
 

	
 
				if (click_count > 0) this->mouse_capture_widget = widget;
 
				break;
 
			}
src/settings_gui.cpp
Show inline comments
 
@@ -18,12 +18,13 @@
 
#include "settings_internal.h"
 
#include "strings_func.h"
 
#include "window_func.h"
 
#include "string_func.h"
 
#include "widgets/dropdown_type.h"
 
#include "widgets/dropdown_func.h"
 
#include "widgets/slider_func.h"
 
#include "highscore.h"
 
#include "base_media_base.h"
 
#include "company_base.h"
 
#include "company_func.h"
 
#include "viewport_func.h"
 
#include "core/geometry_func.hpp"
src/widgets/CMakeLists.txt
Show inline comments
 
@@ -41,12 +41,14 @@ add_files(
 
    osk_widget.h
 
    rail_widget.h
 
    road_widget.h
 
    screenshot_widget.h
 
    settings_widget.h
 
    sign_widget.h
 
    slider.cpp
 
    slider_func.h
 
    smallmap_widget.h
 
    station_widget.h
 
    statusbar_widget.h
 
    story_widget.h
 
    subsidy_widget.h
 
    terraform_widget.h
src/widgets/slider.cpp
Show inline comments
 
new file 100644
 
/*
 
 * 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;
 
}
src/widgets/slider_func.h
Show inline comments
 
new file 100644
 
/*
 
 * 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 */
0 comments (0 inline, 0 general)