1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 10:17:36 +08:00
osu-lazer/osu.Game/Screens/Edit/Timing/ControlPointTable.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

341 lines
12 KiB
C#
Raw Normal View History

2019-10-18 16:59:54 +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.
using System;
2019-10-18 16:59:54 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Pooling;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
2019-10-18 16:59:54 +08:00
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Extensions;
2019-10-18 16:59:54 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2019-10-18 16:59:54 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays;
using osu.Game.Screens.Edit.Timing.RowAttributes;
2019-10-18 16:59:54 +08:00
using osuTK;
namespace osu.Game.Screens.Edit.Timing
{
public partial class ControlPointTable : CompositeDrawable
2019-10-18 16:59:54 +08:00
{
public BindableList<ControlPointGroup> Groups { get; } = new BindableList<ControlPointGroup>();
2019-10-18 16:59:54 +08:00
private const float timing_column_width = 300;
private const float row_height = 25;
private const float row_horizontal_padding = 20;
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colours)
2019-10-18 16:59:54 +08:00
{
RelativeSizeAxes = Axes.Both;
2019-10-18 16:59:54 +08:00
InternalChildren = new Drawable[]
{
new Box
{
Colour = colours.Background4,
RelativeSizeAxes = Axes.Both,
},
new Box
2019-10-18 16:59:54 +08:00
{
Colour = colours.Background3,
RelativeSizeAxes = Axes.Y,
Width = timing_column_width + 10,
},
new Container
{
RelativeSizeAxes = Axes.X,
Height = row_height,
Padding = new MarginPadding { Horizontal = row_horizontal_padding },
Children = new Drawable[]
{
new TableHeaderText("Time")
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
new TableHeaderText("Attributes")
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Margin = new MarginPadding { Left = ControlPointTable.timing_column_width }
},
}
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = row_height },
Child = new ControlPointRowList
{
RelativeSizeAxes = Axes.Both,
RowData = { BindTarget = Groups, },
},
},
};
}
2019-10-18 16:59:54 +08:00
private partial class ControlPointRowList : VirtualisedListContainer<ControlPointGroup, DrawableControlGroup>
{
[Resolved]
private Bindable<ControlPointGroup?> selectedGroup { get; set; } = null!;
public ControlPointRowList()
: base(row_height, 50)
{
}
protected override ScrollContainer<Drawable> CreateScrollContainer() => new OsuScrollContainer();
protected override void LoadComplete()
{
base.LoadComplete();
selectedGroup.BindValueChanged(val =>
{
// can't use `.ScrollIntoView()` here because of the list virtualisation not giving
// child items valid coordinates from the start, so ballpark something similar
// using estimated row height.
var row = Items.FlowingChildren.SingleOrDefault(item => item.Row.Equals(val.NewValue));
2024-07-16 17:20:33 +08:00
if (row == null)
return;
2024-07-16 17:20:33 +08:00
float minPos = row.Y;
float maxPos = minPos + row_height;
if (minPos < Scroll.Current)
Scroll.ScrollTo(minPos);
else if (maxPos > Scroll.Current + Scroll.DisplayableContent)
Scroll.ScrollTo(maxPos - Scroll.DisplayableContent);
});
2019-10-18 16:59:54 +08:00
}
}
public partial class DrawableControlGroup : PoolableDrawable, IHasCurrentValue<ControlPointGroup>
{
public Bindable<ControlPointGroup> Current
{
get => current.Current;
set => current.Current = value;
}
private readonly BindableWithCurrent<ControlPointGroup> current = new BindableWithCurrent<ControlPointGroup>();
private Box background = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
[Resolved]
private Bindable<ControlPointGroup?> selectedGroup { get; set; } = null!;
[Resolved]
private EditorClock editorClock { get; set; } = null!;
[BackgroundDependencyLoader]
private void load()
2019-10-18 16:59:54 +08:00
{
RelativeSizeAxes = Axes.Both;
2019-10-18 16:59:54 +08:00
InternalChildren = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background1,
Alpha = 0,
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Horizontal = row_horizontal_padding, },
Children = new Drawable[]
{
new ControlGroupTiming { Group = { BindTarget = current }, },
new ControlGroupAttributes(point => point is not TimingControlPoint)
{
Group = { BindTarget = current },
Margin = new MarginPadding { Left = timing_column_width }
}
}
}
};
}
2019-10-18 16:59:54 +08:00
protected override void LoadComplete()
2019-10-18 16:59:54 +08:00
{
base.LoadComplete();
selectedGroup.BindValueChanged(_ => updateState(), true);
FinishTransforms(true);
}
protected override void PrepareForUse()
{
base.PrepareForUse();
updateState();
}
protected override bool OnHover(HoverEvent e)
{
updateState();
return true;
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
updateState();
}
protected override bool OnClick(ClickEvent e)
{
// schedule to give time for any modified focused text box to lose focus and commit changes (e.g. BPM / time signature textboxes) before switching to new point.
var currentGroup = Current.Value;
Schedule(() =>
{
selectedGroup.Value = currentGroup;
editorClock.SeekSmoothlyTo(currentGroup.Time);
});
return true;
}
private void updateState()
{
bool isSelected = selectedGroup.Value?.Equals(current.Value) == true;
if (IsHovered || isSelected)
background.FadeIn(100, Easing.OutQuint);
else
background.FadeOut(100, Easing.OutQuint);
background.Colour = isSelected ? colourProvider.Colour3 : colourProvider.Background1;
}
}
private partial class ControlGroupTiming : FillFlowContainer
{
public Bindable<ControlPointGroup> Group { get; } = new Bindable<ControlPointGroup>();
private OsuSpriteText timeText = null!;
[BackgroundDependencyLoader]
private void load()
{
Name = @"ControlGroupTiming";
RelativeSizeAxes = Axes.Y;
Width = timing_column_width;
Spacing = new Vector2(5);
Children = new Drawable[]
{
timeText = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold),
Width = 70,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
new ControlGroupAttributes(c => c is TimingControlPoint)
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Group = { BindTarget = Group },
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Group.BindValueChanged(_ => timeText.Text = Group.Value?.Time.ToEditorFormattedString() ?? default(LocalisableString), true);
}
}
2019-10-18 16:59:54 +08:00
private partial class ControlGroupAttributes : CompositeDrawable
2019-10-18 16:59:54 +08:00
{
public Bindable<ControlPointGroup> Group { get; } = new Bindable<ControlPointGroup>();
private BindableList<ControlPoint> controlPoints { get; } = new BindableList<ControlPoint>();
private readonly Func<ControlPoint, bool> matchFunction;
2019-10-18 16:59:54 +08:00
private FillFlowContainer fill = null!;
2019-10-18 16:59:54 +08:00
public ControlGroupAttributes(Func<ControlPoint, bool> matchFunction)
{
this.matchFunction = matchFunction;
}
[BackgroundDependencyLoader]
private void load()
{
AutoSizeAxes = Axes.X;
RelativeSizeAxes = Axes.Y;
Name = @"ControlGroupAttributes";
InternalChild = fill = new FillFlowContainer
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(2)
};
}
2019-10-18 16:59:54 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
Group.BindValueChanged(_ =>
{
controlPoints.UnbindBindings();
controlPoints.Clear();
if (Group.Value != null)
((IBindableList<ControlPoint>)controlPoints).BindTo(Group.Value.ControlPoints);
}, true);
controlPoints.BindCollectionChanged((_, _) => createChildren(), true);
}
private void createChildren()
{
fill.ChildrenEnumerable = controlPoints
.Where(matchFunction)
.Select(createAttribute)
// arbitrary ordering to make timing points first.
// probably want to explicitly define order in the future.
.OrderByDescending(c => c.GetType().Name);
2019-10-18 16:59:54 +08:00
}
2022-11-30 13:20:54 +08:00
private Drawable createAttribute(ControlPoint controlPoint)
{
switch (controlPoint)
{
case TimingControlPoint timing:
return new TimingRowAttribute(timing);
case DifficultyControlPoint difficulty:
return new DifficultyRowAttribute(difficulty);
case EffectControlPoint effect:
return new EffectRowAttribute(effect);
case SampleControlPoint sample:
return new SampleRowAttribute(sample);
}
2022-11-30 13:20:54 +08:00
throw new ArgumentOutOfRangeException(nameof(controlPoint), $"Control point type {controlPoint.GetType()} is not supported");
}
2019-10-18 16:59:54 +08:00
}
}
}