Skip to content

Commit

Permalink
guiScrollBar: move directly to clicked pos if clicked into tray
Browse files Browse the repository at this point in the history
  • Loading branch information
Desour authored and sfan5 committed Jul 28, 2019
1 parent 8efa1de commit 705630e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
59 changes: 21 additions & 38 deletions src/gui/guiScrollBar.cpp
Expand Up @@ -21,8 +21,7 @@ guiScrollBar::guiScrollBar(IGUIEnvironment *environment, IGUIElement *parent, s3
is_horizontal(horizontal), is_auto_scaling(auto_scale),
dragged_by_slider(false), tray_clicked(false), scroll_pos(0),
draw_center(0), thumb_size(0), min_pos(0), max_pos(100), small_step(10),
large_step(50), desired_pos(0), last_change(0), drag_offset(0),
page_size(100), border_size(0)
large_step(50), last_change(0), drag_offset(0), page_size(100), border_size(0)
{
refreshControls();
setNotClipped(false);
Expand Down Expand Up @@ -116,12 +115,25 @@ bool guiScrollBar::OnEvent(const SEvent &event)
if (is_inside) {
is_dragging = true;
dragged_by_slider = slider_rect.isPointInside(p);
core::vector2di corner =
slider_rect.UpperLeftCorner;
drag_offset = is_horizontal ? p.X - corner.X
: p.Y - corner.Y;
core::vector2di corner = slider_rect.UpperLeftCorner;
drag_offset = is_horizontal ? p.X - corner.X : p.Y - corner.Y;
tray_clicked = !dragged_by_slider;
desired_pos = getPosFromMousePos(p);
if (tray_clicked) {
const s32 new_pos = getPosFromMousePos(p);
const s32 old_pos = scroll_pos;
setPos(new_pos);
// drag in the middle
drag_offset = thumb_size / 2;
// report the scroll event
if (scroll_pos != old_pos && Parent) {
SEvent e;
e.EventType = EET_GUI_EVENT;
e.GUIEvent.Caller = this;
e.GUIEvent.Element = nullptr;
e.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
Parent->OnEvent(e);
}
}
Environment->setFocus(this);
return true;
}
Expand Down Expand Up @@ -158,10 +170,7 @@ bool guiScrollBar::OnEvent(const SEvent &event)
const s32 new_pos = getPosFromMousePos(p);
const s32 old_pos = scroll_pos;

if (dragged_by_slider)
setPos(new_pos);
else
desired_pos = new_pos;
setPos(new_pos);

if (scroll_pos != old_pos && Parent) {
SEvent e;
Expand All @@ -184,32 +193,6 @@ bool guiScrollBar::OnEvent(const SEvent &event)
return IGUIElement::OnEvent(event);
}

void guiScrollBar::OnPostRender(u32 timeMs)
{
if (is_dragging && !dragged_by_slider && tray_clicked &&
timeMs > last_change + 200) {
last_change = timeMs;
const s32 old_pos = scroll_pos;

if (desired_pos >= scroll_pos + large_step)
setPos(scroll_pos + large_step);
else if (desired_pos <= scroll_pos - large_step)
setPos(scroll_pos - large_step);
else if (desired_pos >= scroll_pos - large_step &&
desired_pos <= scroll_pos + large_step)
setPos(desired_pos);

if (scroll_pos != old_pos && Parent) {
SEvent e;
e.EventType = EET_GUI_EVENT;
e.GUIEvent.Caller = this;
e.GUIEvent.Element = nullptr;
e.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
Parent->OnEvent(e);
}
}
}

void guiScrollBar::draw()
{
if (!IsVisible)
Expand Down Expand Up @@ -439,4 +422,4 @@ void guiScrollBar::refreshControls()
bool visible = (border_size != 0);
up_button->setVisible(visible);
down_button->setVisible(visible);
}
}
4 changes: 1 addition & 3 deletions src/gui/guiScrollBar.h
Expand Up @@ -26,7 +26,6 @@ class guiScrollBar : public IGUIElement
virtual void draw();
virtual void updateAbsolutePosition();
virtual bool OnEvent(const SEvent &event);
virtual void OnPostRender(u32 timeMs);

s32 getMax() const { return max_pos; }
s32 getMin() const { return min_pos; }
Expand Down Expand Up @@ -60,12 +59,11 @@ class guiScrollBar : public IGUIElement
s32 max_pos;
s32 small_step;
s32 large_step;
s32 desired_pos;
u32 last_change;
s32 drag_offset;
s32 page_size;
s32 border_size;

core::rect<s32> slider_rect;
video::SColor current_icon_color;
};
};

0 comments on commit 705630e

Please sign in to comment.