1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:17:23 +08:00
osu-lazer/osu.Game/Skinning/Editor/SkinEditorOverlay.cs

135 lines
4.0 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 JetBrains.Annotations;
2021-04-29 16:20:22 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
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;
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 : CompositeDrawable, IKeyBindingHandler<GlobalAction>
2021-04-29 16:20:22 +08:00
{
private readonly ScalingContainer target;
[CanBeNull]
2021-04-29 16:20:22 +08:00
private SkinEditor skinEditor;
public const float VISIBLE_TARGET_SCALE = 0.8f;
2021-04-29 16:20:22 +08:00
public SkinEditorOverlay(ScalingContainer target)
2021-04-29 16:20:22 +08:00
{
this.target = target;
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
case GlobalAction.ToggleSkinEditor:
Toggle();
2021-04-29 16:20:22 +08:00
return true;
}
return false;
}
public void Toggle()
{
if (skinEditor == null)
Show();
else
skinEditor.ToggleVisibility();
}
public override void Hide()
{
// base call intentionally omitted.
skinEditor?.Hide();
}
public override void Show()
{
// base call intentionally omitted.
if (skinEditor == null)
{
var editor = new SkinEditor(target);
editor.State.BindValueChanged(editorVisibilityChanged);
// Schedule ensures that if `Show` is called before this overlay is loaded,
// it will not throw (LoadComponentAsync requires the load target to be in a loaded state).
Schedule(() =>
{
Debug.Assert(editor != null);
LoadComponentAsync(editor, AddInternal);
});
skinEditor = editor;
}
else
skinEditor.Show();
}
2021-04-29 16:20:22 +08:00
private void editorVisibilityChanged(ValueChangedEvent<Visibility> visibility)
{
if (visibility.NewValue == Visibility.Visible)
{
updateMasking();
2021-04-29 16:20:22 +08:00
target.AllowScaling = false;
target.RelativePositionAxes = Axes.Both;
target.ScaleTo(VISIBLE_TARGET_SCALE, SkinEditor.TRANSITION_DURATION, Easing.OutQuint);
target.MoveToX(0.095f, SkinEditor.TRANSITION_DURATION, Easing.OutQuint);
2021-04-29 16:20:22 +08:00
}
else
{
target.AllowScaling = true;
target.ScaleTo(1, SkinEditor.TRANSITION_DURATION, Easing.OutQuint).OnComplete(_ => updateMasking());
target.MoveToX(0f, SkinEditor.TRANSITION_DURATION, Easing.OutQuint);
2021-04-29 16:20:22 +08:00
}
}
private void updateMasking()
{
if (skinEditor == null)
return;
target.Masking = skinEditor.State.Value == Visibility.Visible;
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
2021-04-29 16:20:22 +08:00
{
}
/// <summary>
/// Exit any existing skin editor due to the game state changing.
/// </summary>
public void Reset()
{
skinEditor?.Save();
2021-04-29 16:20:22 +08:00
skinEditor?.Hide();
skinEditor?.Expire();
2021-04-29 16:20:22 +08:00
skinEditor = null;
}
}
}