Codechange: Let ClickSliderWidget handle rounding to nearest mark.

This commit is contained in:
Peter Nelson 2024-05-02 22:31:47 +01:00 committed by Peter Nelson
parent 9d2efd4c96
commit 9a7c30a109
4 changed files with 15 additions and 8 deletions

View File

@ -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 {

View File

@ -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);
}
@ -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 {

View File

@ -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;
@ -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;

View File

@ -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;
}