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
|
|
|
|
|
2022-03-11 22:08:40 +08:00
|
|
|
using System.Diagnostics;
|
2021-09-29 17:50:55 +08:00
|
|
|
using JetBrains.Annotations;
|
2022-03-15 17:10:30 +08:00
|
|
|
using osu.Framework.Allocation;
|
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;
|
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;
|
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
|
|
|
|
|
|
|
namespace osu.Game.Skinning.Editor
|
|
|
|
{
|
|
|
|
/// <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-03-21 15:52:06 +08:00
|
|
|
public 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;
|
|
|
|
|
2021-09-29 17:50:55 +08:00
|
|
|
[CanBeNull]
|
2021-04-29 16:20:22 +08:00
|
|
|
private SkinEditor skinEditor;
|
|
|
|
|
2022-03-15 17:10:30 +08:00
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
private OsuGame game { get; set; }
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-21 19:06:47 +08:00
|
|
|
protected override void PopOut() => skinEditor?.Hide();
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
{
|
2022-03-15 17:00:32 +08:00
|
|
|
lastTargetScreen = screen;
|
|
|
|
|
2022-03-11 22:08:40 +08:00
|
|
|
if (skinEditor == null) return;
|
|
|
|
|
|
|
|
skinEditor.Save();
|
|
|
|
|
2022-03-16 18:11:17 +08:00
|
|
|
// ensure the toolbar is re-hidden even if a new screen decides to try and show it.
|
|
|
|
updateComponentVisibility();
|
|
|
|
|
2022-03-13 15:05:45 +08:00
|
|
|
// AddOnce with parameter will ensure the newest target is loaded if there is any overlap.
|
2022-03-11 22:08:40 +08:00
|
|
|
Scheduler.AddOnce(setTarget, screen);
|
|
|
|
}
|
2021-05-12 15:07:00 +08:00
|
|
|
|
2022-03-15 17:10:30 +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)
|
|
|
|
skinEditor.UpdateTargetScreen(target);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
skinEditor.Hide();
|
|
|
|
skinEditor.Expire();
|
|
|
|
skinEditor = null;
|
|
|
|
}
|
2021-04-29 16:20:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|