1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 13:32:55 +08:00
osu-lazer/osu.Game/Skinning/Editor/SkinEditorOverlay.cs

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

185 lines
5.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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Diagnostics;
using JetBrains.Annotations;
using osu.Framework.Allocation;
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;
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.Components;
using osuTK;
2021-04-29 16:20:22 +08:00
namespace osu.Game.Skinning.Editor
{
/// <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 class SkinEditorOverlay : OverlayContainer, IKeyBindingHandler<GlobalAction>
2021-04-29 16:20:22 +08:00
{
private readonly ScalingContainer scalingContainer;
protected override bool BlockNonPositionalInput => true;
[CanBeNull]
2021-04-29 16:20:22 +08:00
private SkinEditor skinEditor;
[Resolved(canBeNull: true)]
private OsuGame game { get; set; }
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;
}
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()
{
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
editor.State.BindValueChanged(visibility => 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);
2022-03-21 22:54:47 +08:00
SetTarget(lastTargetScreen);
2021-12-30 15:03:16 +08:00
});
}
2022-03-21 19:06:47 +08:00
protected override void PopOut() => skinEditor?.Hide();
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);
game?.Toolbar.Hide();
game?.CloseAllOverlays();
2021-04-29 16:20:22 +08:00
}
else
{
scalingContainer.SetCustomRect(null);
if (lastTargetScreen?.HideOverlaysOnEnter != true)
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;
skinEditor.Save();
// 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);
}
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
}
}
}