guiScrollBar: move directly to clicked pos if clicked into tray

This commit is contained in:
DS-Minetest 2019-07-25 15:16:49 +02:00 committed by sfan5
parent 8efa1de835
commit 705630e7d0
2 changed files with 22 additions and 41 deletions

@ -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);
@ -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;
}
@ -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;
if (scroll_pos != old_pos && Parent) {
SEvent e;
@ -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)

@ -26,7 +26,6 @@ public:
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; }
@ -60,7 +59,6 @@ private:
s32 max_pos;
s32 small_step;
s32 large_step;
s32 desired_pos;
u32 last_change;
s32 drag_offset;
s32 page_size;