mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-10 08:00:05 +00:00
Codechange: Add parameters to change range of slider widget.
This commit is contained in:
parent
d35f1d3d06
commit
1180c95372
@ -741,11 +741,11 @@ struct MusicWindow : public Window {
|
||||
}
|
||||
|
||||
case WID_M_MUSIC_VOL:
|
||||
DrawSliderWidget(r, _settings_client.music.music_vol);
|
||||
DrawSliderWidget(r, 0, INT8_MAX, _settings_client.music.music_vol);
|
||||
break;
|
||||
|
||||
case WID_M_EFFECT_VOL:
|
||||
DrawSliderWidget(r, _settings_client.music.effect_vol);
|
||||
DrawSliderWidget(r, 0, INT8_MAX, _settings_client.music.effect_vol);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -788,7 +788,7 @@ struct MusicWindow : public Window {
|
||||
|
||||
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;
|
||||
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) {
|
||||
MusicDriver::GetInstance()->SetVolume(vol);
|
||||
} else {
|
||||
|
@ -347,11 +347,11 @@ struct GameOptionsWindow : Window {
|
||||
break;
|
||||
|
||||
case WID_GO_BASE_SFX_VOLUME:
|
||||
DrawSliderWidget(r, _settings_client.music.effect_vol);
|
||||
DrawSliderWidget(r, 0, INT8_MAX, _settings_client.music.effect_vol);
|
||||
break;
|
||||
|
||||
case WID_GO_BASE_MUSIC_VOLUME:
|
||||
DrawSliderWidget(r, _settings_client.music.music_vol);
|
||||
DrawSliderWidget(r, 0, INT8_MAX, _settings_client.music.music_vol);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -478,7 +478,7 @@ struct GameOptionsWindow : Window {
|
||||
case WID_GO_BASE_SFX_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;
|
||||
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);
|
||||
this->SetWidgetDirty(widget);
|
||||
SetWindowClassesDirty(WC_MUSIC_WINDOW);
|
||||
|
@ -21,10 +21,14 @@ static const int SLIDER_WIDTH = 3;
|
||||
/**
|
||||
* Draw a slider widget with knob at given value
|
||||
* @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
|
||||
*/
|
||||
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. */
|
||||
const int ha = (r.bottom - r.top) / 5;
|
||||
int wx1 = r.left, wx2 = r.right;
|
||||
@ -40,8 +44,8 @@ void DrawSliderWidget(Rect r, byte value)
|
||||
|
||||
/* Draw a slider handle indicating current value. */
|
||||
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);
|
||||
if (_current_text_dir == TD_RTL) value = max_value - value;
|
||||
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);
|
||||
}
|
||||
|
||||
@ -52,11 +56,14 @@ void DrawSliderWidget(Rect r, byte value)
|
||||
* @param value[in,out] Value to modify
|
||||
* @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);
|
||||
byte new_value = Clamp((pt.x - r.left - sw / 2) * 127 / (r.right - r.left - sw), 0, 127);
|
||||
if (_current_text_dir == TD_RTL) new_value = 127 - new_value;
|
||||
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 = max_value - new_value;
|
||||
new_value += min_value;
|
||||
|
||||
if (new_value != value) {
|
||||
value = new_value;
|
||||
|
@ -14,8 +14,15 @@
|
||||
#include "../gfx_func.h"
|
||||
|
||||
|
||||
void DrawSliderWidget(Rect r, byte value);
|
||||
bool ClickSliderWidget(Rect r, Point pt, byte &value);
|
||||
void DrawSliderWidget(Rect r, int min_value, int max_value, int 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 */
|
||||
|
Loading…
Reference in New Issue
Block a user