1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 11:32:55 +08:00
osu-lazer/osu.Game/Screens/Edit/Editor.cs

678 lines
24 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
2019-06-30 18:31:31 +08:00
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Screens;
using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.UserInterface;
2019-06-30 18:31:31 +08:00
using osu.Game.Input.Bindings;
using osu.Game.IO.Serialization;
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Rulesets.Edit;
using osu.Game.Screens.Edit.Components;
using osu.Game.Screens.Edit.Components.Menus;
using osu.Game.Screens.Edit.Components.Timelines.Summary;
2019-10-09 15:04:58 +08:00
using osu.Game.Screens.Edit.Compose;
using osu.Game.Screens.Edit.Design;
using osu.Game.Screens.Edit.Setup;
using osu.Game.Screens.Edit.Timing;
2019-12-12 12:04:32 +08:00
using osu.Game.Screens.Play;
using osu.Game.Users;
using osuTK.Graphics;
using osuTK.Input;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Edit
{
[Cached(typeof(IBeatSnapProvider))]
[Cached(typeof(ISamplePlaybackDisabler))]
[Cached]
public class Editor : ScreenWithBeatmapBackground, IKeyBindingHandler<GlobalAction>, IKeyBindingHandler<PlatformAction>, IBeatSnapProvider, ISamplePlaybackDisabler
2018-04-13 17:19:50 +08:00
{
2019-11-06 17:11:56 +08:00
public override float BackgroundParallaxAmount => 0.1f;
2019-06-25 15:55:49 +08:00
public override bool AllowBackButton => false;
public override bool HideOverlaysOnEnter => true;
2019-02-01 14:42:15 +08:00
public override bool DisallowExternalBeatmapRulesetChanges => true;
2018-04-13 17:19:50 +08:00
public override bool AllowRateAdjustments => false;
2020-09-09 18:57:28 +08:00
protected bool HasUnsavedChanges => lastSavedHash != changeHandler.CurrentStateHash;
2020-09-09 18:42:03 +08:00
2020-01-10 18:57:34 +08:00
[Resolved]
private BeatmapManager beatmapManager { get; set; }
[Resolved(canBeNull: true)]
private DialogOverlay dialogOverlay { get; set; }
public IBindable<bool> SamplePlaybackDisabled => samplePlaybackDisabled;
private readonly Bindable<bool> samplePlaybackDisabled = new Bindable<bool>();
private bool exitConfirmed;
private string lastSavedHash;
2018-04-13 17:19:50 +08:00
private Box bottomBackground;
private Container<EditorScreen> screenContainer;
2018-04-13 17:19:50 +08:00
private EditorScreen currentScreen;
private readonly BindableBeatDivisor beatDivisor = new BindableBeatDivisor();
private EditorClock clock;
private IBeatmap playableBeatmap;
private EditorBeatmap editorBeatmap;
private EditorChangeHandler changeHandler;
private EditorMenuBar menuBar;
2018-04-13 17:19:50 +08:00
private DependencyContainer dependencies;
private bool isNewBeatmap;
protected override UserActivity InitialActivity => new UserActivity.Editing(Beatmap.Value.BeatmapInfo);
2018-07-11 16:07:14 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
=> dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2018-04-13 17:19:50 +08:00
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private MusicController music { get; set; }
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours, GameHost host, OsuConfigManager config)
2018-04-13 17:19:50 +08:00
{
if (Beatmap.Value is DummyWorkingBeatmap)
{
isNewBeatmap = true;
Beatmap.Value = beatmapManager.CreateNew(Ruleset.Value, api.LocalUser.Value);
}
beatDivisor.Value = Beatmap.Value.BeatmapInfo.BeatDivisor;
beatDivisor.BindValueChanged(divisor => Beatmap.Value.BeatmapInfo.BeatDivisor = divisor.NewValue);
2019-11-08 16:12:47 +08:00
// Todo: should probably be done at a DrawableRuleset level to share logic with Player.
clock = new EditorClock(Beatmap.Value, beatDivisor) { IsCoupled = false };
UpdateClockSource();
2018-04-13 17:19:50 +08:00
dependencies.CacheAs(clock);
AddInternal(clock);
clock.SeekingOrStopped.BindValueChanged(_ => updateSampleDisabledState());
// todo: remove caching of this and consume via editorBeatmap?
2018-04-13 17:19:50 +08:00
dependencies.Cache(beatDivisor);
try
{
playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Beatmap.Value.BeatmapInfo.Ruleset);
}
catch (Exception e)
{
Logger.Error(e, "Could not load beatmap successfully!");
// couldn't load, hard abort!
this.Exit();
return;
}
2020-08-31 03:12:45 +08:00
AddInternal(editorBeatmap = new EditorBeatmap(playableBeatmap, Beatmap.Value.Skin));
dependencies.CacheAs(editorBeatmap);
changeHandler = new EditorChangeHandler(editorBeatmap);
dependencies.CacheAs<IEditorChangeHandler>(changeHandler);
updateLastSavedHash();
OsuMenuItem undoMenuItem;
OsuMenuItem redoMenuItem;
2018-04-13 17:19:50 +08:00
EditorMenuItem cutMenuItem;
EditorMenuItem copyMenuItem;
EditorMenuItem pasteMenuItem;
2020-02-25 19:52:33 +08:00
var fileMenuItems = new List<MenuItem>
{
2020-09-09 18:57:28 +08:00
new EditorMenuItem("Save", MenuItemType.Standard, Save)
2020-02-25 19:52:33 +08:00
};
2020-02-25 17:59:16 +08:00
if (RuntimeInfo.IsDesktop)
fileMenuItems.Add(new EditorMenuItem("Export package", MenuItemType.Standard, exportBeatmap));
fileMenuItems.Add(new EditorMenuItemSpacer());
fileMenuItems.Add(new EditorMenuItem("Exit", MenuItemType.Standard, this.Exit));
AddInternal(new OsuContextMenuContainer
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Children = new[]
2018-04-13 17:19:50 +08:00
{
new Container
2018-04-13 17:19:50 +08:00
{
Name = "Screen container",
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = 40, Bottom = 60 },
Child = screenContainer = new Container<EditorScreen>
{
RelativeSizeAxes = Axes.Both,
Masking = true
}
},
new Container
2018-04-13 17:19:50 +08:00
{
Name = "Top bar",
RelativeSizeAxes = Axes.X,
Height = 40,
Child = menuBar = new EditorMenuBar
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
RelativeSizeAxes = Axes.Both,
Mode = { Value = isNewBeatmap ? EditorScreenMode.SongSetup : EditorScreenMode.Compose },
Items = new[]
2018-04-13 17:19:50 +08:00
{
new MenuItem("File")
{
2020-02-25 17:59:16 +08:00
Items = fileMenuItems
},
new MenuItem("Edit")
{
Items = new[]
{
2020-04-22 17:14:21 +08:00
undoMenuItem = new EditorMenuItem("Undo", MenuItemType.Standard, Undo),
redoMenuItem = new EditorMenuItem("Redo", MenuItemType.Standard, Redo),
new EditorMenuItemSpacer(),
cutMenuItem = new EditorMenuItem("Cut", MenuItemType.Standard, Cut),
copyMenuItem = new EditorMenuItem("Copy", MenuItemType.Standard, Copy),
pasteMenuItem = new EditorMenuItem("Paste", MenuItemType.Standard, Paste),
}
},
new MenuItem("View")
{
Items = new[]
{
new WaveformOpacityMenu(config)
}
}
2018-04-13 17:19:50 +08:00
}
}
},
new Container
2018-04-13 17:19:50 +08:00
{
Name = "Bottom bar",
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Height = 60,
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
bottomBackground = new Box { RelativeSizeAxes = Axes.Both },
new Container
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Vertical = 5, Horizontal = 10 },
Child = new GridContainer
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
ColumnDimensions = new[]
2018-04-13 17:19:50 +08:00
{
new Dimension(GridSizeMode.Absolute, 220),
new Dimension(),
new Dimension(GridSizeMode.Absolute, 220)
},
Content = new[]
{
new Drawable[]
2018-04-13 17:19:50 +08:00
{
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = 10 },
Child = new TimeInfoContainer { RelativeSizeAxes = Axes.Both },
},
new SummaryTimeline
{
RelativeSizeAxes = Axes.Both,
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Left = 10 },
Child = new PlaybackControl { RelativeSizeAxes = Axes.Both },
}
2018-04-13 17:19:50 +08:00
},
}
},
}
2018-04-13 17:19:50 +08:00
}
},
}
});
2018-04-13 17:19:50 +08:00
changeHandler.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler.CanRedo.BindValueChanged(v => redoMenuItem.Action.Disabled = !v.NewValue, true);
2020-09-11 21:53:03 +08:00
editorBeatmap.SelectedHitObjects.BindCollectionChanged((_, __) =>
{
var hasObjects = editorBeatmap.SelectedHitObjects.Count > 0;
cutMenuItem.Action.Disabled = !hasObjects;
copyMenuItem.Action.Disabled = !hasObjects;
2020-09-11 21:53:03 +08:00
}, true);
clipboard.BindValueChanged(content => pasteMenuItem.Action.Disabled = string.IsNullOrEmpty(content.NewValue));
2018-04-13 17:19:50 +08:00
menuBar.Mode.ValueChanged += onModeChanged;
bottomBackground.Colour = colours.Gray2;
}
/// <summary>
/// If the beatmap's track has changed, this method must be called to keep the editor in a valid state.
/// </summary>
public void UpdateClockSource()
{
var sourceClock = (IAdjustableClock)Beatmap.Value.Track ?? new StopwatchClock();
clock.ChangeSource(sourceClock);
}
2020-09-09 18:57:28 +08:00
protected void Save()
{
// no longer new after first user-triggered save.
isNewBeatmap = false;
2020-09-09 18:57:28 +08:00
// apply any set-level metadata changes.
beatmapManager.Update(playableBeatmap.BeatmapInfo.BeatmapSet);
// save the loaded beatmap's data stream.
beatmapManager.Save(playableBeatmap.BeatmapInfo, editorBeatmap, editorBeatmap.BeatmapSkin);
updateLastSavedHash();
}
protected override void Update()
{
base.Update();
clock.ProcessFrame();
}
public bool OnPressed(PlatformAction action)
{
switch (action.ActionType)
{
2020-09-11 18:55:41 +08:00
case PlatformActionType.Cut:
Cut();
return true;
case PlatformActionType.Copy:
Copy();
return true;
case PlatformActionType.Paste:
Paste();
return true;
case PlatformActionType.Undo:
2020-04-22 17:14:21 +08:00
Undo();
return true;
case PlatformActionType.Redo:
2020-04-22 17:14:21 +08:00
Redo();
return true;
case PlatformActionType.Save:
2020-09-09 18:57:28 +08:00
Save();
return true;
}
return false;
}
public void OnReleased(PlatformAction action)
{
}
protected override bool OnKeyDown(KeyDownEvent e)
2018-04-13 17:19:50 +08:00
{
switch (e.Key)
2018-04-13 17:19:50 +08:00
{
case Key.Left:
seek(e, -1);
return true;
2019-04-01 11:16:05 +08:00
case Key.Right:
seek(e, 1);
return true;
2018-04-13 17:19:50 +08:00
}
return base.OnKeyDown(e);
2018-04-13 17:19:50 +08:00
}
private double scrollAccumulation;
2018-10-02 11:02:47 +08:00
protected override bool OnScroll(ScrollEvent e)
2018-04-13 17:19:50 +08:00
{
if (e.ControlPressed || e.AltPressed || e.SuperPressed)
return false;
const double precision = 1;
double scrollComponent = e.ScrollDelta.X + e.ScrollDelta.Y;
double scrollDirection = Math.Sign(scrollComponent);
// this is a special case to handle the "pivot" scenario.
// if we are precise scrolling in one direction then change our mind and scroll backwards,
// the existing accumulation should be applied in the inverse direction to maintain responsiveness.
if (scrollAccumulation != 0 && Math.Sign(scrollAccumulation) != scrollDirection)
scrollAccumulation = scrollDirection * (precision - Math.Abs(scrollAccumulation));
scrollAccumulation += scrollComponent * (e.IsPrecise ? 0.1 : 1);
// because we are doing snapped seeking, we need to add up precise scrolls until they accumulate to an arbitrary cut-off.
while (Math.Abs(scrollAccumulation) >= precision)
{
if (scrollAccumulation > 0)
seek(e, -1);
else
seek(e, 1);
scrollAccumulation = scrollAccumulation < 0 ? Math.Min(0, scrollAccumulation + precision) : Math.Max(0, scrollAccumulation - precision);
}
2018-04-13 17:19:50 +08:00
return true;
}
2019-06-30 18:31:31 +08:00
public bool OnPressed(GlobalAction action)
{
switch (action)
2019-06-30 18:31:31 +08:00
{
case GlobalAction.Back:
// as we don't want to display the back button, manual handling of exit action is required.
this.Exit();
return true;
2019-06-30 18:31:31 +08:00
case GlobalAction.EditorComposeMode:
menuBar.Mode.Value = EditorScreenMode.Compose;
return true;
case GlobalAction.EditorDesignMode:
menuBar.Mode.Value = EditorScreenMode.Design;
return true;
case GlobalAction.EditorTimingMode:
menuBar.Mode.Value = EditorScreenMode.Timing;
return true;
case GlobalAction.EditorSetupMode:
menuBar.Mode.Value = EditorScreenMode.SongSetup;
return true;
default:
return false;
}
2019-06-30 18:31:31 +08:00
}
public void OnReleased(GlobalAction action)
{
}
2019-06-30 18:31:31 +08:00
2019-01-23 19:52:00 +08:00
public override void OnEntering(IScreen last)
2018-04-13 17:19:50 +08:00
{
base.OnEntering(last);
2019-07-10 23:22:40 +08:00
ApplyToBackground(b =>
{
// todo: temporary. we want to be applying dim using the UserDimContainer eventually.
b.FadeColour(Color4.DarkGray, 500);
b.EnableUserDim.Value = false;
b.BlurAmount.Value = 0;
});
2019-12-12 12:04:32 +08:00
resetTrack(true);
2018-04-13 17:19:50 +08:00
}
2019-01-23 19:52:00 +08:00
public override bool OnExiting(IScreen next)
2018-04-13 17:19:50 +08:00
{
if (!exitConfirmed)
{
// if the confirm dialog is already showing (or we can't show it, ie. in tests) exit without save.
if (dialogOverlay == null || dialogOverlay.CurrentDialog is PromptForSaveDialog)
{
confirmExit();
return base.OnExiting(next);
}
if (isNewBeatmap || HasUnsavedChanges)
{
dialogOverlay?.Push(new PromptForSaveDialog(() =>
{
confirmExit();
this.Exit();
}, () =>
{
confirmExitWithSave();
this.Exit();
}));
return true;
}
}
ApplyToBackground(b => b.FadeColour(Color4.White, 500));
2019-07-10 23:22:40 +08:00
resetTrack();
2019-07-10 16:43:02 +08:00
Beatmap.Value = beatmapManager.GetWorkingBeatmap(Beatmap.Value.BeatmapInfo);
2018-04-13 17:19:50 +08:00
return base.OnExiting(next);
}
2019-07-10 23:22:40 +08:00
private void confirmExitWithSave()
{
exitConfirmed = true;
2020-09-09 18:57:28 +08:00
Save();
}
private void confirmExit()
{
// stop the track if playing to allow the parent screen to choose a suitable playback mode.
Beatmap.Value.Track.Stop();
if (isNewBeatmap)
{
// confirming exit without save means we should delete the new beatmap completely.
beatmapManager.Delete(playableBeatmap.BeatmapInfo.BeatmapSet);
// eagerly clear contents before restoring default beatmap to prevent value change callbacks from firing.
ClearInternal();
// in theory this shouldn't be required but due to EF core not sharing instance states 100%
// MusicController is unaware of the changed DeletePending state.
Beatmap.SetDefault();
}
exitConfirmed = true;
}
private readonly Bindable<string> clipboard = new Bindable<string>();
protected void Cut()
{
Copy();
editorBeatmap.RemoveRange(editorBeatmap.SelectedHitObjects.ToArray());
}
protected void Copy()
{
if (editorBeatmap.SelectedHitObjects.Count == 0)
return;
clipboard.Value = new ClipboardContent(editorBeatmap).Serialize();
}
protected void Paste()
{
if (string.IsNullOrEmpty(clipboard.Value))
return;
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();
2020-09-14 13:45:49 +08:00
2020-09-11 22:02:23 +08:00
editorBeatmap.SelectedHitObjects.Clear();
editorBeatmap.AddRange(objects);
2020-09-11 22:02:23 +08:00
editorBeatmap.SelectedHitObjects.AddRange(objects);
2020-09-14 13:45:49 +08:00
editorBeatmap.EndChange();
}
2020-04-22 17:14:21 +08:00
protected void Undo() => changeHandler.RestoreState(-1);
2020-04-22 17:14:21 +08:00
protected void Redo() => changeHandler.RestoreState(1);
private void resetTrack(bool seekToStart = false)
2019-07-10 23:22:40 +08:00
{
Beatmap.Value.Track?.Stop();
if (seekToStart)
{
double targetTime = 0;
if (Beatmap.Value.Beatmap.HitObjects.Count > 0)
{
// seek to one beat length before the first hitobject
targetTime = Beatmap.Value.Beatmap.HitObjects[0].StartTime;
targetTime -= Beatmap.Value.Beatmap.ControlPointInfo.TimingPointAt(targetTime).BeatLength;
}
clock.Seek(Math.Max(0, targetTime));
}
2019-07-10 23:22:40 +08:00
}
2019-02-21 17:56:34 +08:00
private void onModeChanged(ValueChangedEvent<EditorScreenMode> e)
{
var lastScreen = currentScreen;
lastScreen?
.ScaleTo(0.98f, 200, Easing.OutQuint)
.FadeOut(200, Easing.OutQuint);
try
{
if ((currentScreen = screenContainer.SingleOrDefault(s => s.Type == e.NewValue)) != null)
{
screenContainer.ChangeChildDepth(currentScreen, lastScreen?.Depth + 1 ?? 0);
currentScreen
.ScaleTo(1, 200, Easing.OutQuint)
.FadeIn(200, Easing.OutQuint);
return;
}
switch (e.NewValue)
{
case EditorScreenMode.SongSetup:
currentScreen = new SetupScreen();
break;
case EditorScreenMode.Compose:
currentScreen = new ComposeScreen();
break;
2019-04-01 11:16:05 +08:00
case EditorScreenMode.Design:
currentScreen = new DesignScreen();
break;
2019-04-01 11:16:05 +08:00
case EditorScreenMode.Timing:
currentScreen = new TimingScreen();
break;
}
LoadComponentAsync(currentScreen, newScreen =>
{
if (newScreen == currentScreen)
screenContainer.Add(newScreen);
});
}
finally
{
updateSampleDisabledState();
}
}
private void updateSampleDisabledState()
{
samplePlaybackDisabled.Value = clock.SeekingOrStopped.Value || !(currentScreen is ComposeScreen);
}
private void seek(UIEvent e, int direction)
{
double amount = e.ShiftPressed ? 4 : 1;
bool trackPlaying = clock.IsRunning;
if (trackPlaying)
{
// generally users are not looking to perform tiny seeks when the track is playing,
// so seeks should always be by one full beat, bypassing the beatDivisor.
// this multiplication undoes the division that will be applied in the underlying seek operation.
amount *= beatDivisor.Value;
}
if (direction < 1)
clock.SeekBackward(!trackPlaying, amount);
else
clock.SeekForward(!trackPlaying, amount);
}
2020-01-14 18:05:52 +08:00
2020-01-15 12:48:28 +08:00
private void exportBeatmap()
{
2020-09-09 18:57:28 +08:00
Save();
2020-01-15 12:48:28 +08:00
beatmapManager.Export(Beatmap.Value.BeatmapSetInfo);
}
private void updateLastSavedHash()
{
lastSavedHash = changeHandler.CurrentStateHash;
}
public double SnapTime(double time, double? referenceTime) => editorBeatmap.SnapTime(time, referenceTime);
2020-01-23 14:31:56 +08:00
public double GetBeatLengthAtTime(double referenceTime) => editorBeatmap.GetBeatLengthAtTime(referenceTime);
public int BeatDivisor => beatDivisor.Value;
2018-04-13 17:19:50 +08:00
}
}