2021-04-29 08:20:22 +00: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.
|
|
|
|
|
2021-09-29 09:50:55 +00:00
|
|
|
using JetBrains.Annotations;
|
2021-04-29 08:20:22 +00:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Input.Bindings;
|
2021-09-16 09:26:12 +00:00
|
|
|
using osu.Framework.Input.Events;
|
2021-04-29 08:20:22 +00:00
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
using osu.Game.Input.Bindings;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning.Editor
|
|
|
|
{
|
|
|
|
/// <summary>
|
2021-05-03 05:58:23 +00: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 08:20:22 +00:00
|
|
|
/// </summary>
|
2021-05-03 05:58:23 +00:00
|
|
|
public class SkinEditorOverlay : CompositeDrawable, IKeyBindingHandler<GlobalAction>
|
2021-04-29 08:20:22 +00:00
|
|
|
{
|
|
|
|
private readonly ScalingContainer target;
|
2021-09-29 09:50:55 +00:00
|
|
|
|
|
|
|
[CanBeNull]
|
2021-04-29 08:20:22 +00:00
|
|
|
private SkinEditor skinEditor;
|
|
|
|
|
2021-04-30 03:35:58 +00:00
|
|
|
public const float VISIBLE_TARGET_SCALE = 0.8f;
|
2021-04-29 08:20:22 +00:00
|
|
|
|
2021-05-03 05:58:23 +00:00
|
|
|
public SkinEditorOverlay(ScalingContainer target)
|
2021-04-29 08:20:22 +00:00
|
|
|
{
|
|
|
|
this.target = target;
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
|
2021-09-16 09:26:12 +00:00
|
|
|
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
|
2021-04-29 08:20:22 +00:00
|
|
|
{
|
2021-09-16 09:26:12 +00:00
|
|
|
switch (e.Action)
|
2021-04-29 08:20:22 +00:00
|
|
|
{
|
2021-04-29 08:40:58 +00:00
|
|
|
case GlobalAction.Back:
|
2021-07-22 06:59:00 +00:00
|
|
|
if (skinEditor?.State.Value != Visibility.Visible)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Hide();
|
2021-07-20 10:36:12 +00:00
|
|
|
return true;
|
2021-04-29 08:40:58 +00:00
|
|
|
|
2021-04-29 08:20:22 +00:00
|
|
|
case GlobalAction.ToggleSkinEditor:
|
2021-07-20 10:36:12 +00:00
|
|
|
Toggle();
|
2021-04-29 08:20:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-20 10:36:12 +00:00
|
|
|
public void Toggle()
|
|
|
|
{
|
|
|
|
if (skinEditor == null)
|
|
|
|
Show();
|
|
|
|
else
|
|
|
|
skinEditor.ToggleVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Hide()
|
|
|
|
{
|
2021-07-22 06:57:47 +00:00
|
|
|
// base call intentionally omitted.
|
2021-09-29 09:58:43 +00:00
|
|
|
skinEditor?.Hide();
|
2021-07-20 10:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Show()
|
|
|
|
{
|
2021-12-30 07:03:16 +00:00
|
|
|
// base call intentionally omitted as we have custom behaviour.
|
|
|
|
|
|
|
|
if (skinEditor != null)
|
2021-07-20 10:36:12 +00:00
|
|
|
{
|
2021-12-30 07:03:16 +00:00
|
|
|
skinEditor.Show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var editor = new SkinEditor(target);
|
|
|
|
editor.State.BindValueChanged(editorVisibilityChanged);
|
2021-09-29 09:50:55 +00:00
|
|
|
|
2021-12-30 07:03:16 +00:00
|
|
|
skinEditor = editor;
|
2021-12-30 07:02:08 +00:00
|
|
|
|
2021-12-30 07:03:16 +00:00
|
|
|
// 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(() =>
|
|
|
|
{
|
|
|
|
if (editor != skinEditor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LoadComponentAsync(editor, _ =>
|
2021-12-29 12:17:44 +00:00
|
|
|
{
|
2021-12-30 07:02:08 +00:00
|
|
|
if (editor != skinEditor)
|
|
|
|
return;
|
2021-12-29 12:46:34 +00:00
|
|
|
|
2021-12-30 07:03:16 +00:00
|
|
|
AddInternal(editor);
|
2021-12-30 07:02:08 +00:00
|
|
|
});
|
2021-12-30 07:03:16 +00:00
|
|
|
});
|
2021-07-20 10:36:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 08:20:22 +00:00
|
|
|
private void editorVisibilityChanged(ValueChangedEvent<Visibility> visibility)
|
|
|
|
{
|
|
|
|
if (visibility.NewValue == Visibility.Visible)
|
|
|
|
{
|
2021-09-17 04:48:19 +00:00
|
|
|
updateMasking();
|
2021-04-29 08:20:22 +00:00
|
|
|
target.AllowScaling = false;
|
2021-04-30 03:35:58 +00:00
|
|
|
target.RelativePositionAxes = Axes.Both;
|
|
|
|
|
|
|
|
target.ScaleTo(VISIBLE_TARGET_SCALE, SkinEditor.TRANSITION_DURATION, Easing.OutQuint);
|
2021-05-11 09:17:05 +00:00
|
|
|
target.MoveToX(0.095f, SkinEditor.TRANSITION_DURATION, Easing.OutQuint);
|
2021-04-29 08:20:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target.AllowScaling = true;
|
|
|
|
|
2021-09-17 04:48:19 +00:00
|
|
|
target.ScaleTo(1, SkinEditor.TRANSITION_DURATION, Easing.OutQuint).OnComplete(_ => updateMasking());
|
2021-04-30 03:35:58 +00:00
|
|
|
target.MoveToX(0f, SkinEditor.TRANSITION_DURATION, Easing.OutQuint);
|
2021-04-29 08:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 09:50:55 +00:00
|
|
|
private void updateMasking()
|
|
|
|
{
|
|
|
|
if (skinEditor == null)
|
|
|
|
return;
|
|
|
|
|
2021-09-17 04:48:19 +00:00
|
|
|
target.Masking = skinEditor.State.Value == Visibility.Visible;
|
2021-09-29 09:50:55 +00:00
|
|
|
}
|
2021-09-17 04:48:19 +00:00
|
|
|
|
2021-09-16 09:26:12 +00:00
|
|
|
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
|
2021-04-29 08:20:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Exit any existing skin editor due to the game state changing.
|
|
|
|
/// </summary>
|
|
|
|
public void Reset()
|
|
|
|
{
|
2021-05-12 07:07:00 +00:00
|
|
|
skinEditor?.Save();
|
2021-04-29 08:20:22 +00:00
|
|
|
skinEditor?.Hide();
|
|
|
|
skinEditor?.Expire();
|
2021-05-12 07:07:00 +00:00
|
|
|
|
2021-04-29 08:20:22 +00:00
|
|
|
skinEditor = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|