2021-04-28 14:14:48 +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.
|
|
|
|
|
2021-04-30 11:35:58 +08:00
|
|
|
using System;
|
2021-05-14 15:03:22 +08:00
|
|
|
using System.Collections.Generic;
|
2021-04-30 11:35:58 +08:00
|
|
|
using System.Linq;
|
2021-04-29 12:53:01 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-05-11 16:00:56 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-04-28 14:14:48 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-04-29 16:20:22 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2021-04-30 11:35:58 +08:00
|
|
|
using osu.Framework.Testing;
|
2021-04-29 16:26:55 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Containers;
|
2021-04-28 14:14:48 +08:00
|
|
|
using osu.Game.Graphics.Cursor;
|
2021-05-10 21:43:48 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2021-05-11 10:57:12 +08:00
|
|
|
using osuTK;
|
2021-04-28 14:14:48 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning.Editor
|
|
|
|
{
|
2021-05-11 16:49:00 +08:00
|
|
|
[Cached(typeof(SkinEditor))]
|
2021-05-20 18:51:07 +08:00
|
|
|
public class SkinEditor : VisibilityContainer
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
2021-04-29 16:20:22 +08:00
|
|
|
public const double TRANSITION_DURATION = 500;
|
|
|
|
|
2021-05-13 16:06:00 +08:00
|
|
|
public readonly BindableList<ISkinnableDrawable> SelectedComponents = new BindableList<ISkinnableDrawable>();
|
2021-05-13 12:04:17 +08:00
|
|
|
|
|
|
|
protected override bool StartHidden => true;
|
|
|
|
|
2021-05-11 10:57:12 +08:00
|
|
|
private readonly Drawable targetScreen;
|
2021-04-29 12:53:01 +08:00
|
|
|
|
2021-04-29 16:26:55 +08:00
|
|
|
private OsuTextFlowContainer headerText;
|
|
|
|
|
2021-05-13 12:04:17 +08:00
|
|
|
private Bindable<Skin> currentSkin;
|
2021-05-11 16:49:00 +08:00
|
|
|
|
2021-05-10 21:43:48 +08:00
|
|
|
[Resolved]
|
|
|
|
private SkinManager skins { get; set; }
|
|
|
|
|
2021-05-11 17:07:58 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
2021-05-11 16:49:00 +08:00
|
|
|
|
2021-05-12 16:42:04 +08:00
|
|
|
private bool hasBegunMutating;
|
|
|
|
|
2021-05-11 10:57:12 +08:00
|
|
|
public SkinEditor(Drawable targetScreen)
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
2021-05-11 10:57:12 +08:00
|
|
|
this.targetScreen = targetScreen;
|
2021-04-28 14:14:48 +08:00
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:53:01 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2021-05-11 17:07:58 +08:00
|
|
|
private void load()
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
|
|
|
InternalChild = new OsuContextMenuContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2021-04-29 12:53:01 +08:00
|
|
|
Children = new Drawable[]
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
2021-04-29 17:23:22 +08:00
|
|
|
headerText = new OsuTextFlowContainer
|
2021-04-29 16:26:55 +08:00
|
|
|
{
|
|
|
|
TextAnchor = Anchor.TopCentre,
|
|
|
|
Padding = new MarginPadding(20),
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
RelativeSizeAxes = Axes.X
|
|
|
|
},
|
2021-05-11 17:37:41 +08:00
|
|
|
new GridContainer
|
2021-04-30 11:35:58 +08:00
|
|
|
{
|
2021-05-11 17:37:41 +08:00
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
ColumnDimensions = new[]
|
2021-05-10 21:43:48 +08:00
|
|
|
{
|
2021-05-11 17:37:41 +08:00
|
|
|
new Dimension(GridSizeMode.AutoSize),
|
|
|
|
new Dimension()
|
2021-05-10 21:43:48 +08:00
|
|
|
},
|
2021-05-11 17:37:41 +08:00
|
|
|
Content = new[]
|
2021-05-11 10:57:12 +08:00
|
|
|
{
|
2021-05-11 17:37:41 +08:00
|
|
|
new Drawable[]
|
2021-05-11 10:57:12 +08:00
|
|
|
{
|
2021-05-11 17:37:41 +08:00
|
|
|
new SkinComponentToolbox(600)
|
|
|
|
{
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
RequestPlacement = placeComponent
|
|
|
|
},
|
|
|
|
new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new SkinBlueprintContainer(targetScreen),
|
|
|
|
new FillFlowContainer
|
|
|
|
{
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
|
|
|
Spacing = new Vector2(5),
|
|
|
|
Padding = new MarginPadding
|
|
|
|
{
|
|
|
|
Top = 10,
|
|
|
|
Left = 10,
|
|
|
|
},
|
|
|
|
Margin = new MarginPadding
|
|
|
|
{
|
|
|
|
Right = 10,
|
|
|
|
Bottom = 10,
|
|
|
|
},
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new TriangleButton
|
|
|
|
{
|
|
|
|
Text = "Save Changes",
|
|
|
|
Width = 140,
|
2021-05-12 15:07:00 +08:00
|
|
|
Action = Save,
|
2021-05-11 17:37:41 +08:00
|
|
|
},
|
|
|
|
new DangerousTriangleButton
|
|
|
|
{
|
|
|
|
Text = "Revert to default",
|
|
|
|
Width = 140,
|
|
|
|
Action = revert,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-05-11 10:57:12 +08:00
|
|
|
}
|
2021-05-11 17:37:41 +08:00
|
|
|
}
|
2021-04-28 14:14:48 +08:00
|
|
|
}
|
|
|
|
};
|
2021-05-11 17:07:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
Show();
|
2021-04-29 16:26:55 +08:00
|
|
|
|
2021-05-11 17:07:58 +08:00
|
|
|
// as long as the skin editor is loaded, let's make sure we can modify the current skin.
|
|
|
|
currentSkin = skins.CurrentSkin.GetBoundCopy();
|
|
|
|
|
|
|
|
// schedule ensures this only happens when the skin editor is visible.
|
|
|
|
// also avoid some weird endless recursion / bindable feedback loop (something to do with tracking skins across three different bindable types).
|
|
|
|
// probably something which will be factored out in a future database refactor so not too concerning for now.
|
2021-05-12 16:42:04 +08:00
|
|
|
currentSkin.BindValueChanged(skin =>
|
|
|
|
{
|
|
|
|
hasBegunMutating = false;
|
|
|
|
Scheduler.AddOnce(skinChanged);
|
|
|
|
}, true);
|
2021-05-11 17:07:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void skinChanged()
|
|
|
|
{
|
|
|
|
headerText.Clear();
|
|
|
|
|
|
|
|
headerText.AddParagraph("Skin editor", cp => cp.Font = OsuFont.Default.With(size: 24));
|
|
|
|
headerText.NewParagraph();
|
|
|
|
headerText.AddText("Currently editing ", cp =>
|
2021-04-29 16:26:55 +08:00
|
|
|
{
|
|
|
|
cp.Font = OsuFont.Default.With(size: 12);
|
|
|
|
cp.Colour = colours.Yellow;
|
|
|
|
});
|
2021-05-11 17:07:58 +08:00
|
|
|
|
|
|
|
headerText.AddText($"{currentSkin.Value.SkinInfo}", cp =>
|
|
|
|
{
|
|
|
|
cp.Font = OsuFont.Default.With(size: 12, weight: FontWeight.Bold);
|
|
|
|
cp.Colour = colours.Yellow;
|
|
|
|
});
|
|
|
|
|
|
|
|
skins.EnsureMutableSkin();
|
2021-05-12 16:42:04 +08:00
|
|
|
hasBegunMutating = true;
|
2021-04-28 14:14:48 +08:00
|
|
|
}
|
2021-04-29 12:53:01 +08:00
|
|
|
|
2021-04-30 11:35:58 +08:00
|
|
|
private void placeComponent(Type type)
|
|
|
|
{
|
2021-05-12 13:11:40 +08:00
|
|
|
var targetContainer = getTarget(SkinnableTarget.MainHUDComponents);
|
|
|
|
|
|
|
|
if (targetContainer == null)
|
|
|
|
return;
|
|
|
|
|
2021-05-13 16:06:00 +08:00
|
|
|
if (!(Activator.CreateInstance(type) is ISkinnableDrawable component))
|
2021-05-13 18:06:58 +08:00
|
|
|
throw new InvalidOperationException($"Attempted to instantiate a component for placement which was not an {typeof(ISkinnableDrawable)}.");
|
2021-04-30 11:35:58 +08:00
|
|
|
|
2021-05-12 13:11:40 +08:00
|
|
|
var drawableComponent = (Drawable)component;
|
|
|
|
|
|
|
|
// give newly added components a sane starting location.
|
|
|
|
drawableComponent.Origin = Anchor.TopCentre;
|
|
|
|
drawableComponent.Anchor = Anchor.TopCentre;
|
|
|
|
drawableComponent.Y = targetContainer.DrawSize.Y / 2;
|
2021-04-30 11:35:58 +08:00
|
|
|
|
2021-05-12 13:11:40 +08:00
|
|
|
targetContainer.Add(component);
|
2021-05-11 16:49:00 +08:00
|
|
|
|
|
|
|
SelectedComponents.Clear();
|
2021-05-12 13:02:20 +08:00
|
|
|
SelectedComponents.Add(component);
|
2021-05-11 10:57:12 +08:00
|
|
|
}
|
2021-04-30 11:35:58 +08:00
|
|
|
|
2021-05-14 15:03:22 +08:00
|
|
|
private IEnumerable<ISkinnableTarget> availableTargets => targetScreen.ChildrenOfType<ISkinnableTarget>();
|
|
|
|
|
2021-05-11 10:57:12 +08:00
|
|
|
private ISkinnableTarget getTarget(SkinnableTarget target)
|
|
|
|
{
|
2021-05-14 15:03:22 +08:00
|
|
|
return availableTargets.FirstOrDefault(c => c.Target == target);
|
2021-04-30 11:35:58 +08:00
|
|
|
}
|
|
|
|
|
2021-05-11 10:57:12 +08:00
|
|
|
private void revert()
|
|
|
|
{
|
2021-05-14 15:03:22 +08:00
|
|
|
ISkinnableTarget[] targetContainers = availableTargets.ToArray();
|
2021-05-11 10:57:12 +08:00
|
|
|
|
|
|
|
foreach (var t in targetContainers)
|
|
|
|
{
|
2021-05-11 16:00:56 +08:00
|
|
|
currentSkin.Value.ResetDrawableTarget(t);
|
2021-05-11 10:57:12 +08:00
|
|
|
|
|
|
|
// add back default components
|
|
|
|
getTarget(t.Target).Reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:07:00 +08:00
|
|
|
public void Save()
|
2021-05-10 21:43:48 +08:00
|
|
|
{
|
2021-05-12 16:42:04 +08:00
|
|
|
if (!hasBegunMutating)
|
|
|
|
return;
|
|
|
|
|
2021-05-14 15:03:22 +08:00
|
|
|
ISkinnableTarget[] targetContainers = availableTargets.ToArray();
|
2021-05-10 21:43:48 +08:00
|
|
|
|
|
|
|
foreach (var t in targetContainers)
|
2021-05-11 16:00:56 +08:00
|
|
|
currentSkin.Value.UpdateDrawableTarget(t);
|
2021-05-10 21:43:48 +08:00
|
|
|
|
|
|
|
skins.Save(skins.CurrentSkin.Value);
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:20:22 +08:00
|
|
|
protected override bool OnHover(HoverEvent e) => true;
|
|
|
|
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e) => true;
|
2021-04-29 12:53:01 +08:00
|
|
|
|
|
|
|
protected override void PopIn()
|
|
|
|
{
|
2021-04-29 16:20:22 +08:00
|
|
|
this.FadeIn(TRANSITION_DURATION, Easing.OutQuint);
|
2021-04-29 12:53:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void PopOut()
|
|
|
|
{
|
2021-04-29 16:20:22 +08:00
|
|
|
this.FadeOut(TRANSITION_DURATION, Easing.OutQuint);
|
2021-04-29 12:53:01 +08:00
|
|
|
}
|
2021-05-14 15:03:22 +08:00
|
|
|
|
|
|
|
public void DeleteItems(ISkinnableDrawable[] items)
|
|
|
|
{
|
2021-05-15 04:33:26 +08:00
|
|
|
foreach (var item in items)
|
2021-05-14 15:03:22 +08:00
|
|
|
availableTargets.FirstOrDefault(t => t.Components.Contains(item))?.Remove(item);
|
|
|
|
}
|
2021-04-28 14:14:48 +08:00
|
|
|
}
|
|
|
|
}
|