From a9e662a2b6d593681acb0e54058cec9635f013ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Tue, 18 Jun 2024 14:55:59 +0200 Subject: [PATCH] Add break display to editor timeline --- .../Components/Timeline/TimelineBreak.cs | 87 +++++++++++++++++ .../Timeline/TimelineBreakDisplay.cs | 94 +++++++++++++++++++ .../Screens/Edit/Compose/ComposeScreen.cs | 10 +- 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineBreak.cs create mode 100644 osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineBreakDisplay.cs diff --git a/osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineBreak.cs b/osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineBreak.cs new file mode 100644 index 0000000000..dc54661644 --- /dev/null +++ b/osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineBreak.cs @@ -0,0 +1,87 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Shapes; +using osu.Game.Beatmaps.Timing; +using osu.Game.Graphics; +using osu.Game.Graphics.Sprites; + +namespace osu.Game.Screens.Edit.Compose.Components.Timeline +{ + public partial class TimelineBreak : CompositeDrawable + { + public BreakPeriod Break { get; } + + public TimelineBreak(BreakPeriod b) + { + Break = b; + } + + [BackgroundDependencyLoader] + private void load(OsuColour colours) + { + RelativePositionAxes = Axes.X; + RelativeSizeAxes = Axes.Both; + Origin = Anchor.TopLeft; + X = (float)Break.StartTime; + Width = (float)Break.Duration; + CornerRadius = 10; + Masking = true; + + InternalChildren = new Drawable[] + { + new Box + { + RelativeSizeAxes = Axes.Both, + Colour = colours.GreyCarmineLight, + Alpha = 0.4f, + }, + new Circle + { + Anchor = Anchor.CentreLeft, + Origin = Anchor.CentreLeft, + RelativeSizeAxes = Axes.Y, + Width = 10, + CornerRadius = 5, + Colour = colours.GreyCarmineLighter, + }, + new OsuSpriteText + { + Anchor = Anchor.TopLeft, + Origin = Anchor.TopLeft, + Text = "Break", + Margin = new MarginPadding + { + Left = 16, + Top = 3, + }, + Colour = colours.GreyCarmineLighter, + }, + new Circle + { + Anchor = Anchor.CentreRight, + Origin = Anchor.CentreRight, + RelativeSizeAxes = Axes.Y, + Width = 10, + CornerRadius = 5, + Colour = colours.GreyCarmineLighter, + }, + new OsuSpriteText + { + Anchor = Anchor.TopRight, + Origin = Anchor.TopRight, + Text = "Break", + Margin = new MarginPadding + { + Right = 16, + Top = 3, + }, + Colour = colours.GreyCarmineLighter, + }, + }; + } + } +} diff --git a/osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineBreakDisplay.cs b/osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineBreakDisplay.cs new file mode 100644 index 0000000000..587db23e9a --- /dev/null +++ b/osu.Game/Screens/Edit/Compose/Components/Timeline/TimelineBreakDisplay.cs @@ -0,0 +1,94 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Allocation; +using osu.Framework.Bindables; +using osu.Framework.Caching; +using osu.Game.Beatmaps.Timing; +using osu.Game.Screens.Edit.Components.Timelines.Summary.Parts; + +namespace osu.Game.Screens.Edit.Compose.Components.Timeline +{ + public partial class TimelineBreakDisplay : TimelinePart + { + [Resolved] + private Timeline timeline { get; set; } = null!; + + /// + /// The visible time/position range of the timeline. + /// + private (float min, float max) visibleRange = (float.MinValue, float.MaxValue); + + private readonly Cached breakCache = new Cached(); + + private readonly BindableList breaks = new BindableList(); + + protected override void LoadBeatmap(EditorBeatmap beatmap) + { + base.LoadBeatmap(beatmap); + + // TODO: this will have to be mutable soon enough + breaks.AddRange(beatmap.Breaks); + } + + protected override void Update() + { + base.Update(); + + if (DrawWidth <= 0) return; + + (float, float) newRange = ( + (ToLocalSpace(timeline.ScreenSpaceDrawQuad.TopLeft).X) / DrawWidth * Content.RelativeChildSize.X, + (ToLocalSpace(timeline.ScreenSpaceDrawQuad.TopRight).X) / DrawWidth * Content.RelativeChildSize.X); + + if (visibleRange != newRange) + { + visibleRange = newRange; + breakCache.Invalidate(); + } + + if (!breakCache.IsValid) + { + recreateBreaks(); + breakCache.Validate(); + } + } + + private void recreateBreaks() + { + // Remove groups outside the visible range + foreach (TimelineBreak drawableBreak in this) + { + if (!shouldBeVisible(drawableBreak.Break)) + drawableBreak.Expire(); + } + + // Add remaining ones + for (int i = 0; i < breaks.Count; i++) + { + var breakPeriod = breaks[i]; + + if (!shouldBeVisible(breakPeriod)) + continue; + + bool alreadyVisible = false; + + foreach (var b in this) + { + if (ReferenceEquals(b.Break, breakPeriod)) + { + alreadyVisible = true; + break; + } + } + + if (alreadyVisible) + continue; + + Add(new TimelineBreak(breakPeriod)); + } + } + + private bool shouldBeVisible(BreakPeriod breakPeriod) => breakPeriod.EndTime >= visibleRange.min && breakPeriod.StartTime <= visibleRange.max; + } +} diff --git a/osu.Game/Screens/Edit/Compose/ComposeScreen.cs b/osu.Game/Screens/Edit/Compose/ComposeScreen.cs index 0a58b34da6..ed4ef896f5 100644 --- a/osu.Game/Screens/Edit/Compose/ComposeScreen.cs +++ b/osu.Game/Screens/Edit/Compose/ComposeScreen.cs @@ -69,7 +69,15 @@ namespace osu.Game.Screens.Edit.Compose if (ruleset == null || composer == null) return base.CreateTimelineContent(); - return wrapSkinnableContent(new TimelineBlueprintContainer(composer)); + return wrapSkinnableContent(new Container + { + RelativeSizeAxes = Axes.Both, + Children = new Drawable[] + { + new TimelineBreakDisplay { RelativeSizeAxes = Axes.Both, }, + new TimelineBlueprintContainer(composer) + } + }); } private Drawable wrapSkinnableContent(Drawable content)