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.
|
|
|
|
|
2022-03-11 22:08:40 +08:00
|
|
|
using System.Diagnostics;
|
2022-03-15 17:10:30 +08:00
|
|
|
using osu.Framework.Allocation;
|
2023-06-20 18:18:17 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-04-29 16:20:22 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-03-02 19:04:53 +08:00
|
|
|
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;
|
2023-06-20 18:18:17 +08:00
|
|
|
using osu.Game.Configuration;
|
2021-04-29 16:20:22 +08:00
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
using osu.Game.Input.Bindings;
|
2022-03-15 17:10:30 +08:00
|
|
|
using osu.Game.Screens;
|
2023-02-15 18:35:37 +08:00
|
|
|
using osu.Game.Screens.Edit;
|
2022-06-06 17:02:42 +08:00
|
|
|
using osu.Game.Screens.Edit.Components;
|
2022-06-06 17:27:41 +08:00
|
|
|
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>
|
2021-05-03 13:58:23 +08:00
|
|
|
/// 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>
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class SkinEditorOverlay : OverlayContainer, IKeyBindingHandler<GlobalAction>
|
2021-04-29 16:20:22 +08:00
|
|
|
{
|
2022-03-15 17:00:32 +08:00
|
|
|
private readonly ScalingContainer scalingContainer;
|
2021-09-29 17:50:55 +08:00
|
|
|
|
2022-03-21 15:52:06 +08:00
|
|
|
protected override bool BlockNonPositionalInput => true;
|
|
|
|
|
2023-01-26 16:46:19 +08:00
|
|
|
private SkinEditor? skinEditor;
|
2021-04-29 16:20:22 +08:00
|
|
|
|
2023-02-15 18:35:37 +08:00
|
|
|
[Cached]
|
|
|
|
public readonly EditorClipboard Clipboard = new EditorClipboard();
|
|
|
|
|
2023-01-26 16:46:19 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuGame game { get; set; } = null!;
|
2022-03-15 17:10:30 +08:00
|
|
|
|
2023-01-26 16:46:19 +08:00
|
|
|
private OsuScreen? lastTargetScreen;
|
2022-03-15 17:00:32 +08:00
|
|
|
|
2022-06-06 17:27:41 +08:00
|
|
|
private Vector2 lastDrawSize;
|
|
|
|
|
2022-03-15 17:00:32 +08:00
|
|
|
public SkinEditorOverlay(ScalingContainer scalingContainer)
|
2021-04-29 16:20:22 +08:00
|
|
|
{
|
2022-03-15 17:00:32 +08:00
|
|
|
this.scalingContainer = scalingContainer;
|
2021-04-29 16:20:22 +08:00
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
|
2023-06-20 18:18:17 +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
|
|
|
{
|
2021-04-29 16:40:58 +08:00
|
|
|
case GlobalAction.Back:
|
2021-07-22 14:59:00 +08:00
|
|
|
if (skinEditor?.State.Value != Visibility.Visible)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Hide();
|
2021-07-20 18:36:12 +08:00
|
|
|
return true;
|
2021-04-29 16:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-21 15:52:06 +08:00
|
|
|
protected override void PopIn()
|
2021-07-20 18:36:12 +08:00
|
|
|
{
|
2023-06-21 15:11:19 +08:00
|
|
|
globallyDisableBeatmapSkinSetting();
|
|
|
|
|
2021-12-30 15:03:16 +08:00
|
|
|
if (skinEditor != null)
|
2021-07-20 18:36:12 +08:00
|
|
|
{
|
2021-12-30 15:03:16 +08:00
|
|
|
skinEditor.Show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-15 17:00:32 +08:00
|
|
|
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-09-29 17:50:55 +08:00
|
|
|
|
2021-12-30 15:03:16 +08:00
|
|
|
skinEditor = editor;
|
2021-12-30 15:02:08 +08:00
|
|
|
|
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);
|
2022-03-15 17:00:32 +08:00
|
|
|
|
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
|
|
|
});
|
2021-07-20 18:36:12 +08:00
|
|
|
}
|
|
|
|
|
2023-06-21 15:11:19 +08:00
|
|
|
protected override void PopOut()
|
|
|
|
{
|
|
|
|
skinEditor?.Save(false);
|
|
|
|
skinEditor?.Hide();
|
|
|
|
|
|
|
|
globallyReenableBeatmapSkinSetting();
|
|
|
|
}
|
2022-03-21 15:52:06 +08:00
|
|
|
|
2022-06-06 17:27:41 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
if (game.DrawSize != lastDrawSize)
|
|
|
|
{
|
|
|
|
lastDrawSize = game.DrawSize;
|
|
|
|
updateScreenSizing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:02:42 +08:00
|
|
|
private void updateScreenSizing()
|
|
|
|
{
|
|
|
|
if (skinEditor?.State.Value != Visibility.Visible) return;
|
|
|
|
|
2022-06-06 17:27:41 +08:00
|
|
|
const float padding = 10;
|
|
|
|
|
|
|
|
float relativeSidebarWidth = (EditorSidebar.WIDTH + padding) / DrawWidth;
|
|
|
|
float relativeToolbarHeight = (SkinEditorSceneLibrary.HEIGHT + SkinEditor.MENU_HEIGHT + padding) / DrawHeight;
|
2022-06-06 17:02:42 +08:00
|
|
|
|
|
|
|
var rect = new RectangleF(
|
|
|
|
relativeSidebarWidth,
|
|
|
|
relativeToolbarHeight,
|
|
|
|
1 - relativeSidebarWidth * 2,
|
2022-06-06 17:27:41 +08:00
|
|
|
1f - relativeToolbarHeight - padding / DrawHeight);
|
2022-06-06 17:02:42 +08:00
|
|
|
|
|
|
|
scalingContainer.SetCustomRect(rect, true);
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:11:17 +08:00
|
|
|
private void updateComponentVisibility()
|
2021-04-29 16:20:22 +08:00
|
|
|
{
|
2022-03-15 17:10:30 +08:00
|
|
|
Debug.Assert(skinEditor != null);
|
|
|
|
|
2022-03-16 18:11:17 +08:00
|
|
|
if (skinEditor.State.Value == Visibility.Visible)
|
2021-04-29 16:20:22 +08:00
|
|
|
{
|
2022-06-06 17:02:42 +08:00
|
|
|
Scheduler.AddOnce(updateScreenSizing);
|
2022-03-15 17:10:30 +08:00
|
|
|
|
2023-01-26 16:46:19 +08:00
|
|
|
game.Toolbar.Hide();
|
|
|
|
game.CloseAllOverlays();
|
2021-04-29 16:20:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-15 17:00:32 +08:00
|
|
|
scalingContainer.SetCustomRect(null);
|
2022-03-15 17:10:30 +08:00
|
|
|
|
|
|
|
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>
|
2022-03-11 22:08:40 +08:00
|
|
|
/// Set a new target screen which will be used to find skinnable components.
|
2021-04-29 16:20:22 +08:00
|
|
|
/// </summary>
|
2022-03-15 17:10:30 +08:00
|
|
|
public void SetTarget(OsuScreen screen)
|
2021-04-29 16:20:22 +08:00
|
|
|
{
|
2023-06-21 15:11:19 +08:00
|
|
|
lastTargetScreen = screen;
|
2022-03-15 17:00:32 +08:00
|
|
|
|
2023-06-21 15:11:19 +08:00
|
|
|
if (skinEditor == null) return;
|
2022-03-11 22:08:40 +08:00
|
|
|
|
2023-06-21 15:11:19 +08:00
|
|
|
// ensure the toolbar is re-hidden even if a new screen decides to try and show it.
|
|
|
|
updateComponentVisibility();
|
2022-03-11 22:08:40 +08:00
|
|
|
|
2023-06-21 15:11:19 +08:00
|
|
|
// AddOnce with parameter will ensure the newest target is loaded if there is any overlap.
|
|
|
|
Scheduler.AddOnce(setTarget, screen);
|
2022-03-11 22:08:40 +08:00
|
|
|
}
|
2021-05-12 15:07:00 +08:00
|
|
|
|
2023-01-26 16:46:19 +08:00
|
|
|
private void setTarget(OsuScreen? target)
|
2022-03-11 22:08:40 +08:00
|
|
|
{
|
2022-06-06 17:26:52 +08:00
|
|
|
if (target == null)
|
|
|
|
return;
|
|
|
|
|
2022-03-11 22:08:40 +08:00
|
|
|
Debug.Assert(skinEditor != null);
|
|
|
|
|
|
|
|
if (!target.IsLoaded)
|
|
|
|
{
|
|
|
|
Scheduler.AddOnce(setTarget, target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skinEditor.State.Value == Visibility.Visible)
|
2023-11-21 16:49:56 +08:00
|
|
|
{
|
|
|
|
skinEditor.Save(false);
|
2022-03-11 22:08:40 +08:00
|
|
|
skinEditor.UpdateTargetScreen(target);
|
2023-11-21 16:49:56 +08:00
|
|
|
}
|
2022-03-11 22:08:40 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
skinEditor.Hide();
|
|
|
|
skinEditor.Expire();
|
|
|
|
skinEditor = null;
|
|
|
|
}
|
2021-04-29 16:20:22 +08:00
|
|
|
}
|
2023-06-20 18:18:17 +08:00
|
|
|
|
|
|
|
private readonly Bindable<bool> beatmapSkins = new Bindable<bool>();
|
2023-06-21 15:09:54 +08:00
|
|
|
private LeasedBindable<bool>? leasedBeatmapSkins;
|
2023-06-20 18:18:17 +08:00
|
|
|
|
|
|
|
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.
|
2023-06-21 15:09:54 +08:00
|
|
|
leasedBeatmapSkins = beatmapSkins.BeginLease(true);
|
|
|
|
leasedBeatmapSkins.Value = false;
|
2023-06-20 18:18:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void globallyReenableBeatmapSkinSetting()
|
|
|
|
{
|
2023-06-21 15:09:54 +08:00
|
|
|
leasedBeatmapSkins?.Return();
|
|
|
|
leasedBeatmapSkins = null;
|
2023-06-20 18:18:17 +08:00
|
|
|
}
|
2021-04-29 16:20:22 +08:00
|
|
|
}
|
|
|
|
}
|