2019-01-24 08:43:03 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-04-28 11:03:54 +00:00
|
|
|
|
using System;
|
2019-06-14 06:55:32 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
2017-07-13 21:43:33 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-06-14 06:55:32 +00:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2018-10-02 03:02:47 +00:00
|
|
|
|
using osu.Framework.Input.Events;
|
2021-10-13 20:05:46 +00:00
|
|
|
|
using osu.Game.Overlays;
|
2019-06-14 06:55:32 +00:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK.Input;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-07-12 09:57:44 +00:00
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
2020-11-26 09:25:56 +00:00
|
|
|
|
public partial class OsuScrollContainer : OsuScrollContainer<Drawable>
|
|
|
|
|
{
|
|
|
|
|
public OsuScrollContainer()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OsuScrollContainer(Direction direction)
|
|
|
|
|
: base(direction)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 19:57:34 +00:00
|
|
|
|
public partial class OsuScrollContainer<T> : ScrollContainer<T> where T : Drawable
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2023-07-01 15:04:53 +00:00
|
|
|
|
public const float SCROLL_BAR_WIDTH = 10;
|
2020-02-19 08:37:01 +00:00
|
|
|
|
public const float SCROLL_BAR_PADDING = 3;
|
|
|
|
|
|
2017-07-12 09:57:44 +00:00
|
|
|
|
/// <summary>
|
2017-07-14 09:08:47 +00:00
|
|
|
|
/// Allows controlling the scroll bar from any position in the container using the right mouse button.
|
2017-07-12 09:57:44 +00:00
|
|
|
|
/// Uses the value of <see cref="DistanceDecayOnRightMouseScrollbar"/> to smoothly scroll to the dragged location.
|
|
|
|
|
/// </summary>
|
2020-11-01 17:47:40 +00:00
|
|
|
|
public bool RightMouseScrollbar;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-07-12 09:57:44 +00:00
|
|
|
|
/// <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;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-09-19 11:52:57 +00:00
|
|
|
|
private bool shouldPerformRightMouseScroll(MouseButtonEvent e) => RightMouseScrollbar && e.Button == MouseButton.Right;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-07-29 22:03:59 +00:00
|
|
|
|
private bool rightMouseDragging;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-07-29 22:03:59 +00:00
|
|
|
|
protected override bool IsDragging => base.IsDragging || rightMouseDragging;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-06-14 06:55:32 +00:00
|
|
|
|
public OsuScrollContainer(Direction scrollDirection = Direction.Vertical)
|
|
|
|
|
: base(scrollDirection)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 11:03:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Scrolls a <see cref="Drawable"/> into view.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="d">The <see cref="Drawable"/> to scroll into view.</param>
|
|
|
|
|
/// <param name="animated">Whether to animate the movement.</param>
|
|
|
|
|
/// <param name="extraScroll">An added amount to scroll beyond the requirement to bring the target into view.</param>
|
|
|
|
|
public void ScrollIntoView(Drawable d, bool animated = true, float extraScroll = 0)
|
|
|
|
|
{
|
|
|
|
|
float childPos0 = GetChildPosInContent(d);
|
|
|
|
|
float childPos1 = GetChildPosInContent(d, d.DrawSize);
|
|
|
|
|
|
|
|
|
|
float minPos = Math.Min(childPos0, childPos1);
|
|
|
|
|
float maxPos = Math.Max(childPos0, childPos1);
|
|
|
|
|
|
|
|
|
|
if (minPos < Current || (minPos > Current && d.DrawSize[ScrollDim] > DisplayableContent))
|
|
|
|
|
ScrollTo(minPos - extraScroll, animated);
|
|
|
|
|
else if (maxPos > Current + DisplayableContent)
|
|
|
|
|
ScrollTo(maxPos - DisplayableContent + extraScroll, animated);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2018-09-19 11:52:57 +00:00
|
|
|
|
if (shouldPerformRightMouseScroll(e))
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2023-12-18 10:17:59 +00:00
|
|
|
|
ScrollFromMouseEvent(e);
|
2017-07-12 09:57:44 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnMouseDown(e);
|
2017-07-12 09:57:44 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
|
protected override void OnDrag(DragEvent e)
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2019-07-29 22:03:59 +00:00
|
|
|
|
if (rightMouseDragging)
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2023-12-18 10:17:59 +00:00
|
|
|
|
ScrollFromMouseEvent(e);
|
2020-01-20 09:17:21 +00:00
|
|
|
|
return;
|
2017-07-12 09:57:44 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
|
base.OnDrag(e);
|
2017-07-12 09:57:44 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnDragStart(DragStartEvent e)
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2018-10-02 03:02:47 +00:00
|
|
|
|
if (shouldPerformRightMouseScroll(e))
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2019-07-29 22:03:59 +00:00
|
|
|
|
rightMouseDragging = true;
|
2017-07-12 09:57:44 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnDragStart(e);
|
2017-07-12 09:57:44 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
|
protected override void OnDragEnd(DragEndEvent e)
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2019-07-29 22:03:59 +00:00
|
|
|
|
if (rightMouseDragging)
|
2017-07-12 09:57:44 +00:00
|
|
|
|
{
|
2019-07-29 22:03:59 +00:00
|
|
|
|
rightMouseDragging = false;
|
2020-01-20 09:17:21 +00:00
|
|
|
|
return;
|
2017-07-12 09:57:44 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
|
base.OnDragEnd(e);
|
2017-07-12 09:57:44 +00:00
|
|
|
|
}
|
2019-06-14 06:55:32 +00:00
|
|
|
|
|
2019-12-14 06:27:14 +00:00
|
|
|
|
protected override bool OnScroll(ScrollEvent e)
|
|
|
|
|
{
|
2019-12-17 03:24:59 +00:00
|
|
|
|
// allow for controlling volume when alt is held.
|
|
|
|
|
// mostly for compatibility with osu-stable.
|
2019-12-14 06:27:14 +00:00
|
|
|
|
if (e.AltPressed) return false;
|
|
|
|
|
|
|
|
|
|
return base.OnScroll(e);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-18 10:17:59 +00:00
|
|
|
|
protected virtual void ScrollFromMouseEvent(MouseEvent e) =>
|
2024-02-28 14:59:22 +00:00
|
|
|
|
ScrollTo(Clamp(ToLocalSpace(e.ScreenSpaceMousePosition)[ScrollDim] / DrawSize[ScrollDim] * Content.DrawSize[ScrollDim]), true, DistanceDecayOnRightMouseScrollbar);
|
2023-12-18 10:17:59 +00:00
|
|
|
|
|
2019-06-14 06:55:32 +00:00
|
|
|
|
protected override ScrollbarContainer CreateScrollbar(Direction direction) => new OsuScrollbar(direction);
|
|
|
|
|
|
2021-10-13 19:57:34 +00:00
|
|
|
|
protected partial class OsuScrollbar : ScrollbarContainer
|
2019-06-14 06:55:32 +00:00
|
|
|
|
{
|
|
|
|
|
private Color4 hoverColour;
|
|
|
|
|
private Color4 defaultColour;
|
2021-10-13 19:57:34 +00:00
|
|
|
|
private Color4 highlightColour;
|
2019-06-14 06:55:32 +00:00
|
|
|
|
|
|
|
|
|
private readonly Box box;
|
|
|
|
|
|
2023-07-01 15:04:53 +00:00
|
|
|
|
protected override float MinimumDimSize => SCROLL_BAR_WIDTH * 3;
|
|
|
|
|
|
2019-06-14 06:55:32 +00:00
|
|
|
|
public OsuScrollbar(Direction scrollDir)
|
|
|
|
|
: base(scrollDir)
|
|
|
|
|
{
|
2019-08-21 04:29:50 +00:00
|
|
|
|
Blending = BlendingParameters.Additive;
|
2019-06-14 06:55:32 +00:00
|
|
|
|
|
|
|
|
|
CornerRadius = 5;
|
|
|
|
|
|
2020-09-24 16:05:12 +00:00
|
|
|
|
// needs to be set initially for the ResizeTo to respect minimum size
|
2023-07-01 15:04:53 +00:00
|
|
|
|
Size = new Vector2(SCROLL_BAR_WIDTH);
|
2020-09-24 16:05:12 +00:00
|
|
|
|
|
2019-06-14 06:55:32 +00:00
|
|
|
|
const float margin = 3;
|
|
|
|
|
|
|
|
|
|
Margin = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Left = scrollDir == Direction.Vertical ? margin : 0,
|
|
|
|
|
Right = scrollDir == Direction.Vertical ? margin : 0,
|
|
|
|
|
Top = scrollDir == Direction.Horizontal ? margin : 0,
|
|
|
|
|
Bottom = scrollDir == Direction.Horizontal ? margin : 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Masking = true;
|
|
|
|
|
Child = box = new Box { RelativeSizeAxes = Axes.Both };
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 20:05:46 +00:00
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
|
private void load(OverlayColourProvider? colourProvider, OsuColour colours)
|
2019-06-14 06:55:32 +00:00
|
|
|
|
{
|
|
|
|
|
Colour = defaultColour = colours.Gray8;
|
|
|
|
|
hoverColour = colours.GrayF;
|
2021-10-13 20:05:46 +00:00
|
|
|
|
highlightColour = colourProvider?.Highlight1 ?? colours.Green;
|
2019-06-14 06:55:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void ResizeTo(float val, int duration = 0, Easing easing = Easing.None)
|
|
|
|
|
{
|
2023-07-01 15:04:53 +00:00
|
|
|
|
this.ResizeTo(new Vector2(SCROLL_BAR_WIDTH)
|
2019-06-14 06:55:32 +00:00
|
|
|
|
{
|
|
|
|
|
[(int)ScrollDirection] = val
|
2023-07-01 15:04:53 +00:00
|
|
|
|
}, duration, easing);
|
2019-06-14 06:55:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
|
{
|
2021-10-13 19:57:34 +00:00
|
|
|
|
this.FadeColour(hoverColour, 100);
|
2019-06-14 06:55:32 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
|
{
|
2021-10-13 19:57:34 +00:00
|
|
|
|
this.FadeColour(defaultColour, 100);
|
2019-06-14 06:55:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
|
|
|
|
{
|
|
|
|
|
if (!base.OnMouseDown(e)) return false;
|
|
|
|
|
|
2021-10-13 19:57:34 +00:00
|
|
|
|
// note that we are changing the colour of the box here as to not interfere with the hover effect.
|
|
|
|
|
box.FadeColour(highlightColour, 100);
|
2019-06-14 06:55:32 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
2019-06-14 06:55:32 +00:00
|
|
|
|
{
|
2020-01-20 09:17:21 +00:00
|
|
|
|
if (e.Button != MouseButton.Left) return;
|
2019-06-14 06:55:32 +00:00
|
|
|
|
|
2021-10-13 19:57:34 +00:00
|
|
|
|
box.FadeColour(Color4.White, 100);
|
2019-06-14 06:55:32 +00:00
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
|
base.OnMouseUp(e);
|
2019-06-14 06:55:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-12 09:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|