1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-08 16:07:31 +08:00
osu-lazer/osu.Game/Graphics/Containers/OsuScrollContainer.cs

76 lines
2.4 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08: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 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-11-20 15:51:59 +08:00
using osuTK.Input;
2018-04-13 17:19:50 +08:00
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(MouseButtonEvent e) => RightMouseScrollbar && e.Button == MouseButton.Right;
2018-04-13 17:19:50 +08:00
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 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-04-13 17:19:50 +08:00
{
if (shouldPerformRightMouseScroll(e))
2018-04-13 17:19:50 +08:00
{
scrollToRelative(e.MousePosition[ScrollDim]);
2018-04-13 17:19:50 +08:00
return true;
}
2018-10-02 11:02:47 +08:00
return base.OnMouseDown(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnDrag(DragEvent e)
2018-04-13 17:19:50 +08:00
{
if (mouseScrollBarDragging)
{
scrollToRelative(e.MousePosition[ScrollDim]);
2018-04-13 17:19:50 +08:00
return true;
}
2018-10-02 11:02:47 +08:00
return base.OnDrag(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnDragStart(DragStartEvent e)
2018-04-13 17:19:50 +08:00
{
2018-10-02 11:02:47 +08:00
if (shouldPerformRightMouseScroll(e))
2018-04-13 17:19:50 +08:00
{
mouseScrollBarDragging = true;
return true;
}
2018-10-02 11:02:47 +08:00
return base.OnDragStart(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnDragEnd(DragEndEvent e)
2018-04-13 17:19:50 +08:00
{
if (mouseScrollBarDragging)
{
mouseScrollBarDragging = false;
return true;
}
2018-10-02 11:02:47 +08:00
return base.OnDragEnd(e);
2018-04-13 17:19:50 +08:00
}
}
}