osu/osu.Game/Graphics/Containers/OsuScrollContainer.cs

77 lines
2.5 KiB
C#
Raw Normal View History

2018-04-13 09:19:50 +00:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Graphics.Containers;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2018-07-21 02:38:28 +00:00
using osu.Framework.Input.States;
2018-04-13 09:19:50 +00:00
using OpenTK.Input;
namespace osu.Game.Graphics.Containers
{
public class OsuScrollContainer : ScrollContainer
{
/// <summary>
/// Allows controlling the scroll bar from any position in the container using the right mouse button.
/// Uses the value of <see cref="DistanceDecayOnRightMouseScrollbar"/> to smoothly scroll to the dragged location.
/// </summary>
public bool RightMouseScrollbar = false;
/// <summary>
/// Controls the rate with which the target position is approached when performing a relative drag. Default is 0.02.
/// </summary>
public double DistanceDecayOnRightMouseScrollbar = 0.02;
private bool shouldPerformRightMouseScroll(InputState state) => RightMouseScrollbar && state.Mouse.IsPressed(MouseButton.Right);
private void scrollToRelative(float value) => ScrollTo(Clamp((value - Scrollbar.DrawSize[ScrollDim] / 2) / Scrollbar.Size[ScrollDim]), true, DistanceDecayOnRightMouseScrollbar);
private bool mouseScrollBarDragging;
protected override bool IsDragging => base.IsDragging || mouseScrollBarDragging;
2018-10-02 03:02:47 +00:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-04-13 09:19:50 +00:00
{
if (shouldPerformRightMouseScroll(state))
{
scrollToRelative(state.Mouse.Position[ScrollDim]);
return true;
}
2018-10-02 03:02:47 +00:00
return base.OnMouseDown(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override bool OnDrag(DragEvent e)
2018-04-13 09:19:50 +00:00
{
if (mouseScrollBarDragging)
{
2018-10-02 03:02:47 +00:00
scrollToRelative(e.Mouse.Position[ScrollDim]);
2018-04-13 09:19:50 +00:00
return true;
}
2018-10-02 03:02:47 +00:00
return base.OnDrag(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override bool OnDragStart(DragStartEvent e)
2018-04-13 09:19:50 +00:00
{
2018-10-02 03:02:47 +00:00
if (shouldPerformRightMouseScroll(e))
2018-04-13 09:19:50 +00:00
{
mouseScrollBarDragging = true;
return true;
}
2018-10-02 03:02:47 +00:00
return base.OnDragStart(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override bool OnDragEnd(DragEndEvent e)
2018-04-13 09:19:50 +00:00
{
if (mouseScrollBarDragging)
{
mouseScrollBarDragging = false;
return true;
}
2018-10-02 03:02:47 +00:00
return base.OnDragEnd(e);
2018-04-13 09:19:50 +00:00
}
}
}