2020-02-14 06:36:16 +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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-02-14 06:36:16 +00:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Input.Events;
|
2021-07-30 12:32:08 +00:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2020-02-14 06:36:16 +00:00
|
|
|
using osuTK;
|
|
|
|
using osuTK.Graphics;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
{
|
|
|
|
public abstract class OsuRearrangeableListItem<TModel> : RearrangeableListItem<TModel>
|
|
|
|
{
|
|
|
|
public const float FADE_DURATION = 100;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether any item is currently being dragged. Used to hide other items' drag handles.
|
|
|
|
/// </summary>
|
2020-09-08 07:36:36 +00:00
|
|
|
public readonly BindableBool DragActive = new BindableBool();
|
2020-02-14 06:36:16 +00:00
|
|
|
|
|
|
|
private Color4 handleColour = Color4.White;
|
|
|
|
|
2020-02-14 07:09:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The colour of the drag handle.
|
|
|
|
/// </summary>
|
2020-02-14 06:36:16 +00:00
|
|
|
protected Color4 HandleColour
|
|
|
|
{
|
|
|
|
get => handleColour;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (handleColour == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
handleColour = value;
|
|
|
|
|
|
|
|
if (handle != null)
|
|
|
|
handle.Colour = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 07:09:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Whether the drag handle should be shown.
|
|
|
|
/// </summary>
|
2020-09-10 18:11:45 +00:00
|
|
|
protected readonly Bindable<bool> ShowDragHandle = new Bindable<bool>(true);
|
2020-02-14 07:09:15 +00:00
|
|
|
|
2020-09-08 07:36:36 +00:00
|
|
|
private Container handleContainer;
|
2020-02-14 06:36:16 +00:00
|
|
|
private PlaylistItemHandle handle;
|
|
|
|
|
|
|
|
protected OsuRearrangeableListItem(TModel item)
|
|
|
|
: base(item)
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2021-07-30 12:32:08 +00:00
|
|
|
InternalChildren = new Drawable[]
|
2020-02-14 06:36:16 +00:00
|
|
|
{
|
2021-07-30 12:32:08 +00:00
|
|
|
new GridContainer
|
2020-02-14 06:36:16 +00:00
|
|
|
{
|
2021-07-30 12:32:08 +00:00
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Content = new[]
|
2020-02-14 06:36:16 +00:00
|
|
|
{
|
2021-07-30 12:32:08 +00:00
|
|
|
new[]
|
2020-02-14 06:36:16 +00:00
|
|
|
{
|
2021-07-30 12:32:08 +00:00
|
|
|
handleContainer = new Container
|
2020-02-14 06:36:16 +00:00
|
|
|
{
|
2021-07-30 12:32:08 +00:00
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Padding = new MarginPadding { Horizontal = 5 },
|
|
|
|
Child = handle = new PlaylistItemHandle
|
|
|
|
{
|
|
|
|
Size = new Vector2(12),
|
|
|
|
Colour = HandleColour,
|
|
|
|
AlwaysPresent = true,
|
|
|
|
Alpha = 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CreateContent()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ColumnDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
|
|
|
|
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) }
|
2020-02-14 06:36:16 +00:00
|
|
|
},
|
2021-07-30 12:32:08 +00:00
|
|
|
new HoverClickSounds()
|
2020-02-14 06:36:16 +00:00
|
|
|
};
|
2020-09-08 07:36:36 +00:00
|
|
|
}
|
2020-02-14 07:09:15 +00:00
|
|
|
|
2020-09-08 07:36:36 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
ShowDragHandle.BindValueChanged(show => handleContainer.Alpha = show.NewValue ? 1 : 0, true);
|
2020-02-14 06:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnDragStart(DragStartEvent e)
|
|
|
|
{
|
|
|
|
if (!base.OnDragStart(e))
|
|
|
|
return false;
|
|
|
|
|
2020-09-08 07:36:36 +00:00
|
|
|
DragActive.Value = true;
|
2020-02-14 06:36:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnDragEnd(DragEndEvent e)
|
|
|
|
{
|
2020-09-08 07:36:36 +00:00
|
|
|
DragActive.Value = false;
|
2020-02-14 06:36:16 +00:00
|
|
|
base.OnDragEnd(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool IsDraggableAt(Vector2 screenSpacePos) => handle.HandlingDrag;
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
2020-09-08 07:36:36 +00:00
|
|
|
handle.UpdateHoverState(IsDragged || !DragActive.Value);
|
2020-02-14 06:36:16 +00:00
|
|
|
return base.OnHover(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e) => handle.UpdateHoverState(false);
|
|
|
|
|
|
|
|
protected abstract Drawable CreateContent();
|
|
|
|
|
2020-02-14 07:09:15 +00:00
|
|
|
public class PlaylistItemHandle : SpriteIcon
|
2020-02-14 06:36:16 +00:00
|
|
|
{
|
|
|
|
public bool HandlingDrag { get; private set; }
|
|
|
|
private bool isHovering;
|
|
|
|
|
|
|
|
public PlaylistItemHandle()
|
|
|
|
{
|
|
|
|
Icon = FontAwesome.Solid.Bars;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
|
|
|
{
|
|
|
|
base.OnMouseDown(e);
|
|
|
|
|
|
|
|
HandlingDrag = true;
|
|
|
|
UpdateHoverState(isHovering);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
|
|
|
{
|
|
|
|
base.OnMouseUp(e);
|
|
|
|
|
|
|
|
HandlingDrag = false;
|
|
|
|
UpdateHoverState(isHovering);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateHoverState(bool hovering)
|
|
|
|
{
|
|
|
|
isHovering = hovering;
|
|
|
|
|
|
|
|
if (isHovering || HandlingDrag)
|
|
|
|
this.FadeIn(FADE_DURATION);
|
|
|
|
else
|
|
|
|
this.FadeOut(FADE_DURATION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|