1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 23:07:26 +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.

307 lines
9.1 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;
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.Screens;
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;
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.Utils;
using osuTK;
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 IBindable<RulesetInfo> ruleset { get; set; } = null!;
[Resolved]
private Bindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;
2023-01-26 16:46:19 +08:00
private OsuScreen? lastTargetScreen;
private Vector2 lastDrawSize;
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;
}
[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();
if (lastTargetScreen is MainMenu)
PresentGameplay();
2021-12-30 15:03:16 +08:00
if (skinEditor != null)
{
2021-12-30 15:03:16 +08:00
skinEditor.Show();
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);
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();
globallyReenableBeatmapSkinSetting();
}
public void PresentGameplay()
{
performer?.PerformFromScreen(screen =>
{
// If we're playing the intro, switch away to another beatmap.
if (beatmap.Value.BeatmapSetInfo.Protected)
{
music.NextTrack();
Schedule(PresentGameplay);
return;
}
if (screen is Player)
return;
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 (game.DrawSize != lastDrawSize)
{
lastDrawSize = game.DrawSize;
updateScreenSizing();
}
}
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
{
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)
{
Scheduler.AddOnce(setTarget, target);
return;
}
if (skinEditor.State.Value == Visibility.Visible)
{
skinEditor.Save(false);
skinEditor.UpdateTargetScreen(target);
}
else
{
skinEditor.Hide();
skinEditor.Expire();
skinEditor = null;
}
2021-04-29 16:20:22 +08:00
}
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
{
public EndlessPlayer(Func<IBeatmap, IReadOnlyList<Mod>, Score> createScore)
: base(createScore, new PlayerConfiguration
{
ShowResults = false,
AutomaticallySkipIntro = true,
})
{
}
protected override void Update()
{
base.Update();
if (GameplayState.HasPassed)
GameplayClockContainer.Seek(0);
}
}
2021-04-29 16:20:22 +08:00
}
}