1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 08:22:56 +08:00
osu-lazer/osu.Game/Overlays/SkinEditor/SkinEditorOverlay.cs

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

375 lines
12 KiB
C#
Raw Normal View History

2021-04-29 16:20:22 +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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2021-04-29 16:20:22 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Input;
2021-04-29 16:20:22 +08:00
using osu.Framework.Input.Bindings;
2021-09-16 17:26:12 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Layout;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
2021-04-29 16:20:22 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Input.Bindings;
Forcibly change ruleset to correct one before entering gameplay from main menu Closes #25663 (again). As it turns out, in some scenarios it can be the case that the current game-global `Beatmap` is not valid for the current game-global `Ruleset`. The validity of one and the other in conjunction is only really validated by the song select screen; elsewhere there is no guarantee that the global beatmap is playable using the global ruleset. However, this only comes up in very specific circumstances, namely one: when trying to autoplay a catch beatmap with osu! ruleset globally active via the skin editor flow. `Player` is responsible for retrieving the beatmap to be played. It does so by invoking the appropriate beatmap converter and asking it if the beatmap can be converted: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game/Beatmaps/WorkingBeatmap.cs#L262-L266 If the code above throws, `Player` actually silently covers for this, by trying the beatmap's default ruleset instead: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game/Screens/Play/Player.cs#L529-L536 However, for the pairing of osu! ruleset and catch beatmap, this fails, as `OsuBeatmapConverter`'s condition necessary for permitting conversion is that the objects have a defined position: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game.Rulesets.Osu/Beatmaps/OsuBeatmapConverter.cs#L25 which they will do, due to the fact that all catch beatmaps are really just osu! beatmaps but with conversion steps applied, and thus `Player` succeeds to load the catch beatmap in osu! ruleset. In the skin editor scenario, this would lead to the secondary failure of the skin editor trying to apply `CatchModAutoplay` on top of all of that, which would fail at the hard-cast of the beatmap to `CatchBeatmap`.
2023-12-06 17:12:25 +08:00
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Scoring;
using osu.Game.Screens;
using osu.Game.Screens.Edit;
using osu.Game.Screens.Edit.Components;
using osu.Game.Screens.Menu;
using osu.Game.Screens.Play;
using osu.Game.Screens.Select;
using osu.Game.Users;
using osu.Game.Utils;
2021-04-29 16:20:22 +08:00
2023-01-26 17:21:04 +08:00
namespace osu.Game.Overlays.SkinEditor
2021-04-29 16:20:22 +08:00
{
/// <summary>
/// A container which handles loading a skin editor on user request for a specified target.
/// This also handles the scaling / positioning adjustment of the target.
2021-04-29 16:20:22 +08:00
/// </summary>
public partial class SkinEditorOverlay : OverlayContainer, IKeyBindingHandler<GlobalAction>
2021-04-29 16:20:22 +08:00
{
private readonly ScalingContainer scalingContainer;
protected override bool BlockNonPositionalInput => true;
2023-01-26 16:46:19 +08:00
private SkinEditor? skinEditor;
2021-04-29 16:20:22 +08:00
[Resolved]
private IPerformFromScreenRunner? performer { get; set; }
[Cached]
public readonly EditorClipboard Clipboard = new EditorClipboard();
2023-01-26 16:46:19 +08:00
[Resolved]
private OsuGame game { get; set; } = null!;
[Resolved]
private MusicController music { get; set; } = null!;
[Resolved]
private Bindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
Forcibly change ruleset to correct one before entering gameplay from main menu Closes #25663 (again). As it turns out, in some scenarios it can be the case that the current game-global `Beatmap` is not valid for the current game-global `Ruleset`. The validity of one and the other in conjunction is only really validated by the song select screen; elsewhere there is no guarantee that the global beatmap is playable using the global ruleset. However, this only comes up in very specific circumstances, namely one: when trying to autoplay a catch beatmap with osu! ruleset globally active via the skin editor flow. `Player` is responsible for retrieving the beatmap to be played. It does so by invoking the appropriate beatmap converter and asking it if the beatmap can be converted: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game/Beatmaps/WorkingBeatmap.cs#L262-L266 If the code above throws, `Player` actually silently covers for this, by trying the beatmap's default ruleset instead: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game/Screens/Play/Player.cs#L529-L536 However, for the pairing of osu! ruleset and catch beatmap, this fails, as `OsuBeatmapConverter`'s condition necessary for permitting conversion is that the objects have a defined position: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game.Rulesets.Osu/Beatmaps/OsuBeatmapConverter.cs#L25 which they will do, due to the fact that all catch beatmaps are really just osu! beatmaps but with conversion steps applied, and thus `Player` succeeds to load the catch beatmap in osu! ruleset. In the skin editor scenario, this would lead to the secondary failure of the skin editor trying to apply `CatchModAutoplay` on top of all of that, which would fail at the hard-cast of the beatmap to `CatchBeatmap`.
2023-12-06 17:12:25 +08:00
[Resolved]
private Bindable<RulesetInfo> ruleset { get; set; } = null!;
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;
2023-01-26 16:46:19 +08:00
private OsuScreen? lastTargetScreen;
private InvokeOnDisposal? nestedInputManagerDisable;
2024-05-06 18:23:11 +08:00
private readonly LayoutValue drawSizeLayout;
public SkinEditorOverlay(ScalingContainer scalingContainer)
2021-04-29 16:20:22 +08:00
{
this.scalingContainer = scalingContainer;
2021-04-29 16:20:22 +08:00
RelativeSizeAxes = Axes.Both;
AddLayout(drawSizeLayout = new LayoutValue(Invalidation.DrawSize));
2021-04-29 16:20:22 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
config.BindWith(OsuSetting.BeatmapSkins, beatmapSkins);
}
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
2021-04-29 16:20:22 +08:00
{
2021-09-16 17:26:12 +08:00
switch (e.Action)
2021-04-29 16:20:22 +08:00
{
case GlobalAction.Back:
if (skinEditor?.State.Value != Visibility.Visible)
break;
Hide();
return true;
2021-04-29 16:20:22 +08:00
}
return false;
}
protected override void PopIn()
{
globallyDisableBeatmapSkinSetting();
2021-12-30 15:03:16 +08:00
if (skinEditor != null)
{
disableNestedInputManagers();
2021-12-30 15:03:16 +08:00
skinEditor.Show();
if (lastTargetScreen is MainMenu)
PresentGameplay();
2021-12-30 15:03:16 +08:00
return;
}
var editor = new SkinEditor();
2022-03-21 22:54:47 +08:00
2022-06-24 20:25:23 +08:00
editor.State.BindValueChanged(_ => updateComponentVisibility());
2021-12-30 15:03:16 +08:00
skinEditor = editor;
2022-03-21 22:54:47 +08:00
LoadComponentAsync(editor, _ =>
2021-12-30 15:03:16 +08:00
{
if (editor != skinEditor)
return;
2022-03-21 22:54:47 +08:00
AddInternal(editor);
if (lastTargetScreen is MainMenu)
PresentGameplay();
2023-01-26 16:46:19 +08:00
Debug.Assert(lastTargetScreen != null);
2022-03-21 22:54:47 +08:00
SetTarget(lastTargetScreen);
2021-12-30 15:03:16 +08:00
});
}
protected override void PopOut()
{
skinEditor?.Save(false);
skinEditor?.Hide();
nestedInputManagerDisable?.Dispose();
nestedInputManagerDisable = null;
globallyReenableBeatmapSkinSetting();
}
public void PresentGameplay() => presentGameplay(false);
private void presentGameplay(bool attemptedBeatmapSwitch)
{
performer?.PerformFromScreen(screen =>
{
if (State.Value != Visibility.Visible)
return;
if (beatmap.Value is DummyWorkingBeatmap)
{
// presume we don't have anything good to play and just bail.
return;
}
// If we're playing the intro, switch away to another beatmap.
if (beatmap.Value.BeatmapSetInfo.Protected)
{
if (!attemptedBeatmapSwitch)
{
music.NextTrack();
Schedule(() => presentGameplay(true));
}
return;
}
if (screen is Player)
return;
Forcibly change ruleset to correct one before entering gameplay from main menu Closes #25663 (again). As it turns out, in some scenarios it can be the case that the current game-global `Beatmap` is not valid for the current game-global `Ruleset`. The validity of one and the other in conjunction is only really validated by the song select screen; elsewhere there is no guarantee that the global beatmap is playable using the global ruleset. However, this only comes up in very specific circumstances, namely one: when trying to autoplay a catch beatmap with osu! ruleset globally active via the skin editor flow. `Player` is responsible for retrieving the beatmap to be played. It does so by invoking the appropriate beatmap converter and asking it if the beatmap can be converted: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game/Beatmaps/WorkingBeatmap.cs#L262-L266 If the code above throws, `Player` actually silently covers for this, by trying the beatmap's default ruleset instead: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game/Screens/Play/Player.cs#L529-L536 However, for the pairing of osu! ruleset and catch beatmap, this fails, as `OsuBeatmapConverter`'s condition necessary for permitting conversion is that the objects have a defined position: https://github.com/ppy/osu/blob/6d64538d7a3130df63574eb75a8ebe044154c799/osu.Game.Rulesets.Osu/Beatmaps/OsuBeatmapConverter.cs#L25 which they will do, due to the fact that all catch beatmaps are really just osu! beatmaps but with conversion steps applied, and thus `Player` succeeds to load the catch beatmap in osu! ruleset. In the skin editor scenario, this would lead to the secondary failure of the skin editor trying to apply `CatchModAutoplay` on top of all of that, which would fail at the hard-cast of the beatmap to `CatchBeatmap`.
2023-12-06 17:12:25 +08:00
// the validity of the current game-wide beatmap + ruleset combination is enforced by song select.
// if we're anywhere else, the state is unknown and may not make sense, so forcibly set something that does.
if (screen is not PlaySongSelect)
ruleset.Value = beatmap.Value.BeatmapInfo.Ruleset;
var replayGeneratingMod = ruleset.Value.CreateInstance().GetAutoplayMod();
IReadOnlyList<Mod> usableMods = mods.Value;
if (replayGeneratingMod != null)
usableMods = usableMods.Append(replayGeneratingMod).ToArray();
if (!ModUtils.CheckCompatibleSet(usableMods, out var invalid))
mods.Value = mods.Value.Except(invalid).ToArray();
if (replayGeneratingMod != null)
screen.Push(new EndlessPlayer((beatmap, mods) => replayGeneratingMod.CreateScoreFromReplayData(beatmap, mods)));
}, new[] { typeof(Player), typeof(PlaySongSelect) });
}
protected override void Update()
{
base.Update();
if (!drawSizeLayout.IsValid)
{
updateScreenSizing();
drawSizeLayout.Validate();
}
}
private void updateScreenSizing()
{
if (skinEditor?.State.Value != Visibility.Visible) return;
const float padding = 10;
float relativeSidebarWidth = (EditorSidebar.WIDTH + padding) / DrawWidth;
float relativeToolbarHeight = (SkinEditorSceneLibrary.HEIGHT + SkinEditor.MENU_HEIGHT + padding) / DrawHeight;
var rect = new RectangleF(
relativeSidebarWidth,
relativeToolbarHeight,
1 - relativeSidebarWidth * 2,
1f - relativeToolbarHeight - padding / DrawHeight);
scalingContainer.SetCustomRect(rect, true);
}
private void updateComponentVisibility()
2021-04-29 16:20:22 +08:00
{
Debug.Assert(skinEditor != null);
if (skinEditor.State.Value == Visibility.Visible)
2021-04-29 16:20:22 +08:00
{
Scheduler.AddOnce(updateScreenSizing);
2023-01-26 16:46:19 +08:00
game.Toolbar.Hide();
game.CloseAllOverlays();
2021-04-29 16:20:22 +08:00
}
else
{
scalingContainer.SetCustomRect(null);
if (lastTargetScreen?.HideOverlaysOnEnter != true)
2023-01-26 16:46:19 +08:00
game.Toolbar.Show();
2021-04-29 16:20:22 +08:00
}
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
2021-04-29 16:20:22 +08:00
{
}
/// <summary>
/// Set a new target screen which will be used to find skinnable components.
2021-04-29 16:20:22 +08:00
/// </summary>
public void SetTarget(OsuScreen screen)
2021-04-29 16:20:22 +08:00
{
nestedInputManagerDisable?.Dispose();
nestedInputManagerDisable = null;
lastTargetScreen = screen;
if (skinEditor == null) return;
// ensure the toolbar is re-hidden even if a new screen decides to try and show it.
updateComponentVisibility();
// AddOnce with parameter will ensure the newest target is loaded if there is any overlap.
Scheduler.AddOnce(setTarget, screen);
}
2023-01-26 16:46:19 +08:00
private void setTarget(OsuScreen? target)
{
if (target == null)
return;
Debug.Assert(skinEditor != null);
if (!target.IsLoaded || !skinEditor.IsLoaded)
{
Scheduler.AddOnce(setTarget, target);
return;
}
if (skinEditor.State.Value == Visibility.Visible)
{
skinEditor.Save(false);
skinEditor.UpdateTargetScreen(target);
disableNestedInputManagers();
}
else
{
skinEditor.Hide();
skinEditor.Expire();
skinEditor = null;
}
2021-04-29 16:20:22 +08:00
}
private void disableNestedInputManagers()
{
if (lastTargetScreen == null)
return;
var nestedInputManagers = lastTargetScreen.ChildrenOfType<PassThroughInputManager>().Where(manager => manager.UseParentInput).ToArray();
foreach (var inputManager in nestedInputManagers)
inputManager.UseParentInput = false;
nestedInputManagerDisable = new InvokeOnDisposal(() =>
{
foreach (var inputManager in nestedInputManagers)
inputManager.UseParentInput = true;
});
}
private readonly Bindable<bool> beatmapSkins = new Bindable<bool>();
private LeasedBindable<bool>? leasedBeatmapSkins;
private void globallyDisableBeatmapSkinSetting()
{
if (beatmapSkins.Disabled)
return;
// The skin editor doesn't work well if beatmap skins are being applied to the player screen.
// To keep things simple, disable the setting game-wide while using the skin editor.
2023-11-23 15:39:05 +08:00
//
// This causes a full reload of the skin, which is pretty ugly.
// TODO: Investigate if we can avoid this when a beatmap skin is not being applied by the current beatmap.
leasedBeatmapSkins = beatmapSkins.BeginLease(true);
leasedBeatmapSkins.Value = false;
}
private void globallyReenableBeatmapSkinSetting()
{
leasedBeatmapSkins?.Return();
leasedBeatmapSkins = null;
}
private partial class EndlessPlayer : ReplayPlayer
{
protected override UserActivity? InitialActivity => null;
2023-12-06 16:18:35 +08:00
public override bool DisallowExternalBeatmapRulesetChanges => true;
public override bool? AllowGlobalTrackControl => false;
public EndlessPlayer(Func<IBeatmap, IReadOnlyList<Mod>, Score> createScore)
: base(createScore, new PlayerConfiguration
{
ShowResults = false,
AutomaticallySkipIntro = true,
})
{
}
protected override void LoadComplete()
{
base.LoadComplete();
if (!LoadedBeatmapSuccessfully)
2023-12-27 04:46:50 +08:00
Scheduler.AddDelayed(this.Exit, 1000);
}
protected override void Update()
{
base.Update();
if (!LoadedBeatmapSuccessfully)
return;
if (GameplayState.HasPassed)
GameplayClockContainer.Seek(0);
}
}
2021-04-29 16:20:22 +08:00
}
}