2021-04-13 22:05:58 +08: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-11-27 10:45:56 +08:00
|
|
|
using System;
|
2023-12-26 19:42:04 +08:00
|
|
|
using System.Diagnostics;
|
2021-04-13 22:26:19 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-07-28 00:22:47 +08:00
|
|
|
using osu.Framework.Extensions.LocalisationExtensions;
|
2021-04-13 22:05:58 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-04-13 22:26:19 +08:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Input.Events;
|
2021-07-28 00:22:47 +08:00
|
|
|
using osu.Framework.Localisation;
|
2021-04-13 22:05:58 +08:00
|
|
|
using osu.Game.Graphics;
|
2021-04-13 22:26:19 +08:00
|
|
|
using osu.Game.Graphics.Containers;
|
2021-04-13 22:05:58 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2021-04-19 13:11:26 +08:00
|
|
|
using osu.Game.Overlays;
|
2021-04-13 22:26:19 +08:00
|
|
|
using osuTK.Graphics;
|
2021-04-13 22:05:58 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit
|
|
|
|
{
|
|
|
|
public abstract partial class EditorTable : TableContainer
|
|
|
|
{
|
2022-11-27 10:47:54 +08:00
|
|
|
public event Action<Drawable>? OnRowSelected;
|
|
|
|
|
2021-04-13 22:05:58 +08:00
|
|
|
private const float horizontal_inset = 20;
|
|
|
|
|
|
|
|
protected const float ROW_HEIGHT = 25;
|
|
|
|
|
2021-04-19 14:06:07 +08:00
|
|
|
public const int TEXT_SIZE = 14;
|
2021-04-13 22:05:58 +08:00
|
|
|
|
2021-04-13 22:26:19 +08:00
|
|
|
protected readonly FillFlowContainer<RowBackground> BackgroundFlow;
|
2021-04-13 22:05:58 +08:00
|
|
|
|
2024-04-21 22:40:35 +08:00
|
|
|
// We can avoid potentially thousands of objects being added to the input sub-tree since item selection is being handled by the BackgroundFlow
|
|
|
|
// and no items in the underlying table are clickable.
|
|
|
|
protected override bool ShouldBeConsideredForInput(Drawable child) => base.ShouldBeConsideredForInput(child) && child == BackgroundFlow;
|
|
|
|
|
2021-04-13 22:05:58 +08:00
|
|
|
protected EditorTable()
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
|
|
|
Padding = new MarginPadding { Horizontal = horizontal_inset };
|
|
|
|
RowSize = new Dimension(GridSizeMode.Absolute, ROW_HEIGHT);
|
|
|
|
|
2021-04-13 22:26:19 +08:00
|
|
|
AddInternal(BackgroundFlow = new FillFlowContainer<RowBackground>
|
2021-04-13 22:05:58 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Depth = 1f,
|
|
|
|
Padding = new MarginPadding { Horizontal = -horizontal_inset },
|
|
|
|
Margin = new MarginPadding { Top = ROW_HEIGHT }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-26 19:42:04 +08:00
|
|
|
protected int GetIndexForObject(object? item)
|
2022-11-27 10:47:54 +08:00
|
|
|
{
|
2023-12-26 19:42:04 +08:00
|
|
|
for (int i = 0; i < BackgroundFlow.Count; i++)
|
|
|
|
{
|
|
|
|
if (BackgroundFlow[i].Item == item)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-26 20:20:18 +08:00
|
|
|
protected virtual bool SetSelectedRow(object? item)
|
2023-12-26 19:42:04 +08:00
|
|
|
{
|
|
|
|
bool foundSelection = false;
|
|
|
|
|
2022-11-27 10:47:54 +08:00
|
|
|
foreach (var b in BackgroundFlow)
|
|
|
|
{
|
|
|
|
b.Selected = ReferenceEquals(b.Item, item);
|
|
|
|
|
|
|
|
if (b.Selected)
|
2023-12-26 19:42:04 +08:00
|
|
|
{
|
|
|
|
Debug.Assert(!foundSelection);
|
2022-11-27 10:47:54 +08:00
|
|
|
OnRowSelected?.Invoke(b);
|
2023-12-26 19:42:04 +08:00
|
|
|
foundSelection = true;
|
|
|
|
}
|
2022-11-27 10:47:54 +08:00
|
|
|
}
|
2023-12-26 19:42:04 +08:00
|
|
|
|
|
|
|
return foundSelection;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected object? GetObjectAtIndex(int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index > BackgroundFlow.Count - 1)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return BackgroundFlow[index].Item;
|
2022-11-27 10:47:54 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 11:23:08 +08:00
|
|
|
protected override Drawable CreateHeader(int index, TableColumn? column) => new HeaderText(column?.Header ?? default);
|
2021-04-13 22:05:58 +08:00
|
|
|
|
|
|
|
private partial class HeaderText : OsuSpriteText
|
|
|
|
{
|
2021-07-28 00:22:47 +08:00
|
|
|
public HeaderText(LocalisableString text)
|
2021-04-13 22:05:58 +08:00
|
|
|
{
|
|
|
|
Text = text.ToUpper();
|
|
|
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold);
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 22:26:19 +08:00
|
|
|
|
|
|
|
public partial class RowBackground : OsuClickableContainer
|
|
|
|
{
|
|
|
|
public readonly object Item;
|
|
|
|
|
|
|
|
private const int fade_duration = 100;
|
|
|
|
|
|
|
|
private readonly Box hoveredBackground;
|
|
|
|
|
|
|
|
public RowBackground(object item)
|
|
|
|
{
|
|
|
|
Item = item;
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
Height = 25;
|
|
|
|
|
|
|
|
AlwaysPresent = true;
|
|
|
|
|
|
|
|
CornerRadius = 3;
|
|
|
|
Masking = true;
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
hoveredBackground = new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Alpha = 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private Color4 colourHover;
|
|
|
|
private Color4 colourSelected;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2021-04-19 13:11:26 +08:00
|
|
|
private void load(OverlayColourProvider colours)
|
2021-04-13 22:26:19 +08:00
|
|
|
{
|
2022-06-20 15:38:11 +08:00
|
|
|
colourHover = colours.Background1;
|
2021-04-19 17:25:30 +08:00
|
|
|
colourSelected = colours.Colour3;
|
2021-04-13 22:26:19 +08:00
|
|
|
}
|
|
|
|
|
2022-05-31 21:02:30 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-06-20 15:38:11 +08:00
|
|
|
updateState();
|
2022-05-31 21:02:30 +08:00
|
|
|
FinishTransforms(true);
|
|
|
|
}
|
|
|
|
|
2021-04-13 22:26:19 +08:00
|
|
|
private bool selected;
|
|
|
|
|
|
|
|
public bool Selected
|
|
|
|
{
|
|
|
|
get => selected;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == selected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
selected = value;
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
|
|
|
updateState();
|
|
|
|
return base.OnHover(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
{
|
|
|
|
updateState();
|
|
|
|
base.OnHoverLost(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateState()
|
|
|
|
{
|
|
|
|
hoveredBackground.FadeColour(selected ? colourSelected : colourHover, 450, Easing.OutQuint);
|
|
|
|
|
|
|
|
if (selected || IsHovered)
|
|
|
|
hoveredBackground.FadeIn(fade_duration, Easing.OutQuint);
|
|
|
|
else
|
|
|
|
hoveredBackground.FadeOut(fade_duration, Easing.OutQuint);
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 22:05:58 +08:00
|
|
|
}
|
|
|
|
}
|