Skip to content

Commit

Permalink
Codechange: Let ClickSliderWidget handle rounding to nearest mark.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterN committed May 3, 2024
1 parent 9d2efd4 commit 9a7c30a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/music_gui.cpp
Expand Up @@ -826,7 +826,7 @@ struct MusicWindow : public Window {

case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: { // volume sliders
uint8_t &vol = (widget == WID_M_MUSIC_VOL) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, 0, INT8_MAX, vol)) {
if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, 0, INT8_MAX, 0, vol)) {
if (widget == WID_M_MUSIC_VOL) {
MusicDriver::GetInstance()->SetVolume(vol);
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/settings_gui.cpp
Expand Up @@ -786,8 +786,7 @@ struct GameOptionsWindow : Window {
#endif /* HAS_TRUETYPE_FONT */

case WID_GO_GUI_SCALE:
if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, MIN_INTERFACE_SCALE, MAX_INTERFACE_SCALE, this->gui_scale)) {
if (!_ctrl_pressed) this->gui_scale = ((this->gui_scale + 12) / 25) * 25;
if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, MIN_INTERFACE_SCALE, MAX_INTERFACE_SCALE, _ctrl_pressed ? 0 : SCALE_NMARKS, this->gui_scale)) {
this->SetWidgetDirty(widget);
}

Expand Down Expand Up @@ -822,7 +821,7 @@ struct GameOptionsWindow : Window {
case WID_GO_BASE_SFX_VOLUME:
case WID_GO_BASE_MUSIC_VOLUME: {
uint8_t &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, 0, INT8_MAX, vol)) {
if (ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, 0, INT8_MAX, 0, vol)) {
if (widget == WID_GO_BASE_MUSIC_VOLUME) {
MusicDriver::GetInstance()->SetVolume(vol);
} else {
Expand Down
10 changes: 9 additions & 1 deletion src/slider.cpp
Expand Up @@ -82,10 +82,13 @@ void DrawSliderWidget(Rect r, int min_value, int max_value, int nmarks, int valu
* Handle click on a slider widget to change the value
* @param r Rectangle of the widget
* @param pt Clicked point
* @param min_value Minimum value of slider
* @param max_value Maximum value of slider
* @param nmarks Number of marks displayed. Value will be rounded to nearest mark.
* @param value[in,out] Value to modify
* @return True if the value setting was modified
*/
bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &value)
bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int nmarks, int &value)
{
max_value -= min_value;

Expand All @@ -94,6 +97,11 @@ bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &valu
if (_current_text_dir == TD_RTL) new_value = max_value - new_value;
new_value += min_value;

if (nmarks > 0) {
const int step = max_value / (nmarks - 1);
new_value = ((new_value + step / 2) / step) * step;
}

if (new_value != value) {
value = new_value;
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/slider_func.h
Expand Up @@ -15,12 +15,12 @@

using SliderMarkFunc = StringID(int nmarks, int mark, int value);
void DrawSliderWidget(Rect r, int min_value, int max_value, int nmarks, int value, SliderMarkFunc *mark_func);
bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &value);
bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int nmarks, int &value);

inline bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, uint8_t &value)
inline bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int nmarks, uint8_t &value)
{
int tmp_value = value;
if (!ClickSliderWidget(r, pt, min_value, max_value, tmp_value)) return false;
if (!ClickSliderWidget(r, pt, min_value, max_value, nmarks, tmp_value)) return false;
value = tmp_value;
return true;
}
Expand Down

0 comments on commit 9a7c30a

Please sign in to comment.