1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-09 09:37:42 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/ComposeScreen.cs

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

160 lines
5.1 KiB
C#
Raw Normal View History

// 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 17:19:50 +08:00
using System.Diagnostics;
2021-03-29 17:30:23 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2017-10-02 09:09:21 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2021-03-29 17:30:23 +08:00
using osu.Framework.Platform;
using osu.Game.Beatmaps;
2021-03-29 17:30:23 +08:00
using osu.Game.Extensions;
using osu.Game.IO.Serialization;
using osu.Game.Rulesets;
2019-12-05 19:12:25 +08:00
using osu.Game.Rulesets.Edit;
using osu.Game.Screens.Edit.Compose.Components.Timeline;
2018-04-13 17:19:50 +08:00
2018-11-06 17:28:22 +08:00
namespace osu.Game.Screens.Edit.Compose
2017-10-02 09:09:21 +08:00
{
public class ComposeScreen : EditorScreenWithTimeline
2017-10-02 09:09:21 +08:00
{
2021-03-29 17:30:23 +08:00
[Resolved]
private GameHost host { get; set; }
[Resolved]
private EditorClock clock { get; set; }
2019-12-05 19:12:25 +08:00
private Bindable<string> clipboard { get; set; }
2021-03-29 17:30:23 +08:00
private HitObjectComposer composer;
public ComposeScreen()
: base(EditorScreenMode.Compose)
{
}
private Ruleset ruleset;
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
2017-10-02 09:09:21 +08:00
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2022-01-12 21:34:07 +08:00
ruleset = parent.Get<IBindable<WorkingBeatmap>>().Value.BeatmapInfo.Ruleset.CreateInstance();
2019-12-05 19:12:25 +08:00
composer = ruleset?.CreateHitObjectComposer();
2019-04-01 11:16:05 +08:00
// make the composer available to the timeline and other components in this screen.
if (composer != null)
dependencies.CacheAs(composer);
return dependencies;
}
protected override Drawable CreateMainContent()
{
2019-12-06 11:51:43 +08:00
if (ruleset == null || composer == null)
return new ScreenWhiteBox.UnderConstructionMessage(ruleset == null ? "This beatmap" : $"{ruleset.Description}'s composer");
2018-04-13 17:19:50 +08:00
return wrapSkinnableContent(composer);
}
protected override Drawable CreateTimelineContent()
{
if (ruleset == null || composer == null)
return base.CreateTimelineContent();
return wrapSkinnableContent(new TimelineBlueprintContainer(composer));
}
private Drawable wrapSkinnableContent(Drawable content)
{
Debug.Assert(ruleset != null);
return new EditorSkinProvidingContainer(EditorBeatmap).WithChild(content);
2017-10-02 09:09:21 +08:00
}
[BackgroundDependencyLoader]
private void load(EditorClipboard clipboard)
{
this.clipboard = clipboard.Content.GetBoundCopy();
}
protected override void LoadComplete()
{
base.LoadComplete();
// May be null in the case of a ruleset that doesn't have editor support, see CreateMainContent().
if (composer == null)
return;
EditorBeatmap.SelectedHitObjects.BindCollectionChanged((_, __) => updateClipboardActionAvailability());
clipboard.BindValueChanged(_ => updateClipboardActionAvailability());
composer.OnLoadComplete += _ => updateClipboardActionAvailability();
updateClipboardActionAvailability();
}
#region Clipboard operations
protected override void PerformCut()
{
base.PerformCut();
Copy();
EditorBeatmap.RemoveRange(EditorBeatmap.SelectedHitObjects.ToArray());
}
protected override void PerformCopy()
{
base.PerformCopy();
clipboard.Value = new ClipboardContent(EditorBeatmap).Serialize();
host.GetClipboard()?.SetText(formatSelectionAsString());
}
protected override void PerformPaste()
{
base.PerformPaste();
var objects = clipboard.Value.Deserialize<ClipboardContent>().HitObjects;
Debug.Assert(objects.Any());
double timeOffset = clock.CurrentTime - objects.Min(o => o.StartTime);
foreach (var h in objects)
h.StartTime += timeOffset;
EditorBeatmap.BeginChange();
EditorBeatmap.SelectedHitObjects.Clear();
EditorBeatmap.AddRange(objects);
EditorBeatmap.SelectedHitObjects.AddRange(objects);
EditorBeatmap.EndChange();
}
private void updateClipboardActionAvailability()
{
CanCut.Value = CanCopy.Value = EditorBeatmap.SelectedHitObjects.Any();
CanPaste.Value = composer.IsLoaded && !string.IsNullOrEmpty(clipboard.Value);
}
private string formatSelectionAsString()
{
if (composer == null)
return string.Empty;
double displayTime = EditorBeatmap.SelectedHitObjects.OrderBy(h => h.StartTime).FirstOrDefault()?.StartTime ?? clock.CurrentTime;
string selectionAsString = composer.ConvertSelectionToString();
return !string.IsNullOrEmpty(selectionAsString)
? $"{displayTime.ToEditorFormattedString()} ({selectionAsString}) - "
: $"{displayTime.ToEditorFormattedString()} - ";
}
#endregion
2017-10-02 09:09:21 +08:00
}
}