1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 14:57:25 +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.

221 lines
6.3 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.Diagnostics;
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.Game.Configuration;
2021-04-29 16:20:22 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Input.Bindings;
using osu.Game.Screens;
using osu.Game.Screens.Edit;
using osu.Game.Screens.Edit.Components;
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
[Cached]
public readonly EditorClipboard Clipboard = new EditorClipboard();
2023-01-26 16:46:19 +08:00
[Resolved]
private OsuGame game { 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();
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();
}
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.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.
leasedBeatmapSkins = beatmapSkins.BeginLease(true);
leasedBeatmapSkins.Value = false;
}
private void globallyReenableBeatmapSkinSetting()
{
leasedBeatmapSkins?.Return();
leasedBeatmapSkins = null;
}
2021-04-29 16:20:22 +08:00
}
}