Skip to content

Commit

Permalink
Fix: volume slider behavior in music gui
Browse files Browse the repository at this point in the history
In the music gui, the volume sliders don't respond to mouse drags
that extend outside of their small area.

This changes the event code to give them more typical dragging behavior
that's easier to work with.
  • Loading branch information
nikolas committed Feb 10, 2019
1 parent 6e21190 commit 89b2dbd
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/music_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "string_func.h"
#include "settings_type.h"
#include "settings_gui.h"
#include "tilehighlight_func.h"
#include "widgets/dropdown_func.h"
#include "widgets/dropdown_type.h"

Expand Down Expand Up @@ -650,6 +651,7 @@ static void ShowMusicTrackSelection()

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

MusicWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
{
Expand Down Expand Up @@ -795,22 +797,14 @@ struct MusicWindow : public Window {
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 = x * 127 / this->GetWidget<NWidgetBase>(widget)->current_x;
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);
this->SetDirty();
}
this->dragged_vol_widget = widget;

this->UpdateVolumeWidget(pt);

_left_button_clicked = false;

/* Activate dragging */
SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
break;
}

Expand All @@ -834,6 +828,35 @@ struct MusicWindow : public Window {
break;
}
}

virtual void OnMouseDrag(Point pt, int widget)
{
this->UpdateVolumeWidget(pt);
}

void UpdateVolumeWidget(Point pt)
{
uint widget_width = this->GetWidget<NWidgetBase>(this->dragged_vol_widget)->current_x;

int x = pt.x - this->GetWidget<NWidgetBase>(this->dragged_vol_widget)->pos_x;

/* Only update the volume along the width of this widget */
if (x < 0) x = 0;
if (x > (int)widget_width) x = (int)widget_width;

byte *vol = (this->dragged_vol_widget == WID_M_MUSIC_VOL) ? &_settings_client.music.music_vol : &_settings_client.music.effect_vol;

byte new_vol = x * 127 / widget_width;
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 (this->dragged_vol_widget == WID_M_MUSIC_VOL) MusicDriver::GetInstance()->SetVolume(new_vol);
this->SetDirty();
}
}
};

static const NWidgetPart _nested_music_window_widgets[] = {
Expand Down

0 comments on commit 89b2dbd

Please sign in to comment.