Codechange: Add parameters to change range of slider widget.

This commit is contained in:
Peter Nelson 2022-10-23 15:35:35 +01:00 committed by PeterN
parent d35f1d3d06
commit 1180c95372
4 changed files with 29 additions and 15 deletions

View File

@ -741,11 +741,11 @@ struct MusicWindow : public Window {
} }
case WID_M_MUSIC_VOL: case WID_M_MUSIC_VOL:
DrawSliderWidget(r, _settings_client.music.music_vol); DrawSliderWidget(r, 0, INT8_MAX, _settings_client.music.music_vol);
break; break;
case WID_M_EFFECT_VOL: case WID_M_EFFECT_VOL:
DrawSliderWidget(r, _settings_client.music.effect_vol); DrawSliderWidget(r, 0, INT8_MAX, _settings_client.music.effect_vol);
break; break;
} }
} }
@ -788,7 +788,7 @@ struct MusicWindow : public Window {
case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: { // volume sliders case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: { // volume sliders
byte &vol = (widget == WID_M_MUSIC_VOL) ? _settings_client.music.music_vol : _settings_client.music.effect_vol; byte &vol = (widget == WID_M_MUSIC_VOL) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, vol)) { if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, 0, INT8_MAX, vol)) {
if (widget == WID_M_MUSIC_VOL) { if (widget == WID_M_MUSIC_VOL) {
MusicDriver::GetInstance()->SetVolume(vol); MusicDriver::GetInstance()->SetVolume(vol);
} else { } else {

View File

@ -347,11 +347,11 @@ struct GameOptionsWindow : Window {
break; break;
case WID_GO_BASE_SFX_VOLUME: case WID_GO_BASE_SFX_VOLUME:
DrawSliderWidget(r, _settings_client.music.effect_vol); DrawSliderWidget(r, 0, INT8_MAX, _settings_client.music.effect_vol);
break; break;
case WID_GO_BASE_MUSIC_VOLUME: case WID_GO_BASE_MUSIC_VOLUME:
DrawSliderWidget(r, _settings_client.music.music_vol); DrawSliderWidget(r, 0, INT8_MAX, _settings_client.music.music_vol);
break; break;
} }
} }
@ -478,7 +478,7 @@ struct GameOptionsWindow : Window {
case WID_GO_BASE_SFX_VOLUME: case WID_GO_BASE_SFX_VOLUME:
case WID_GO_BASE_MUSIC_VOLUME: { case WID_GO_BASE_MUSIC_VOLUME: {
byte &vol = (widget == WID_GO_BASE_MUSIC_VOLUME) ? _settings_client.music.music_vol : _settings_client.music.effect_vol; byte &vol = (widget == WID_GO_BASE_MUSIC_VOLUME) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, vol)) { if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, 0, INT8_MAX, vol)) {
if (widget == WID_GO_BASE_MUSIC_VOLUME) MusicDriver::GetInstance()->SetVolume(vol); if (widget == WID_GO_BASE_MUSIC_VOLUME) MusicDriver::GetInstance()->SetVolume(vol);
this->SetWidgetDirty(widget); this->SetWidgetDirty(widget);
SetWindowClassesDirty(WC_MUSIC_WINDOW); SetWindowClassesDirty(WC_MUSIC_WINDOW);

View File

@ -21,10 +21,14 @@ static const int SLIDER_WIDTH = 3;
/** /**
* Draw a slider widget with knob at given value * Draw a slider widget with knob at given value
* @param r Rectangle to draw the widget in * @param r Rectangle to draw the widget in
* @param min_value Minimum value of slider
* @param max_value Maximum value of slider
* @param value Value to put the slider at * @param value Value to put the slider at
*/ */
void DrawSliderWidget(Rect r, byte value) void DrawSliderWidget(Rect r, int min_value, int max_value, int value)
{ {
max_value -= min_value;
/* Draw a wedge indicating low to high value. */ /* Draw a wedge indicating low to high value. */
const int ha = (r.bottom - r.top) / 5; const int ha = (r.bottom - r.top) / 5;
int wx1 = r.left, wx2 = r.right; int wx1 = r.left, wx2 = r.right;
@ -40,8 +44,8 @@ void DrawSliderWidget(Rect r, byte value)
/* Draw a slider handle indicating current value. */ /* Draw a slider handle indicating current value. */
const int sw = ScaleGUITrad(SLIDER_WIDTH); const int sw = ScaleGUITrad(SLIDER_WIDTH);
if (_current_text_dir == TD_RTL) value = 127 - value; if (_current_text_dir == TD_RTL) value = max_value - value;
const int x = r.left + (value * (r.right - r.left - sw) / 127); const int x = r.left + ((value - min_value) * (r.right - r.left - sw) / max_value);
DrawFrameRect(x, r.top, x + sw, r.bottom, COLOUR_GREY, FR_NONE); DrawFrameRect(x, r.top, x + sw, r.bottom, COLOUR_GREY, FR_NONE);
} }
@ -52,11 +56,14 @@ void DrawSliderWidget(Rect r, byte value)
* @param value[in,out] Value to modify * @param value[in,out] Value to modify
* @return True if the value setting was modified * @return True if the value setting was modified
*/ */
bool ClickSliderWidget(Rect r, Point pt, byte &value) bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &value)
{ {
max_value -= min_value;
const int sw = ScaleGUITrad(SLIDER_WIDTH); const int sw = ScaleGUITrad(SLIDER_WIDTH);
byte new_value = Clamp((pt.x - r.left - sw / 2) * 127 / (r.right - r.left - sw), 0, 127); int new_value = Clamp((pt.x - r.left - sw / 2) * max_value / (r.right - r.left - sw), 0, max_value);
if (_current_text_dir == TD_RTL) new_value = 127 - new_value; if (_current_text_dir == TD_RTL) new_value = max_value - new_value;
new_value += min_value;
if (new_value != value) { if (new_value != value) {
value = new_value; value = new_value;

View File

@ -14,8 +14,15 @@
#include "../gfx_func.h" #include "../gfx_func.h"
void DrawSliderWidget(Rect r, byte value); void DrawSliderWidget(Rect r, int min_value, int max_value, int value);
bool ClickSliderWidget(Rect r, Point pt, byte &value); bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &value);
inline bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, byte &value)
{
int tmp_value = value;
if (!ClickSliderWidget(r, pt, min_value, max_value, tmp_value)) return false;
value = tmp_value;
return true;
}
#endif /* WIDGETS_SLIDER_TYPE_H */ #endif /* WIDGETS_SLIDER_TYPE_H */