2021-04-30 11:09:58 +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;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-04-30 11:35:58 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2022-03-17 17:40:26 +08:00
|
|
|
using osu.Framework.Logging;
|
2021-04-30 11:35:58 +08:00
|
|
|
using osu.Game.Graphics;
|
2021-04-30 11:09:58 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2021-04-30 14:22:51 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2023-01-17 00:55:28 +08:00
|
|
|
using osu.Game.Localisation;
|
2022-03-15 15:36:04 +08:00
|
|
|
using osu.Game.Screens.Edit.Components;
|
2022-08-01 13:03:57 +08:00
|
|
|
using osu.Game.Screens.Play.HUD;
|
2023-01-26 17:21:04 +08:00
|
|
|
using osu.Game.Skinning;
|
2021-04-30 11:09:58 +08:00
|
|
|
using osuTK;
|
|
|
|
|
2023-01-26 17:21:04 +08:00
|
|
|
namespace osu.Game.Overlays.SkinEditor
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class SkinComponentToolbox : EditorSidebarSection
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2022-08-01 13:03:57 +08:00
|
|
|
public Action<Type>? RequestPlacement;
|
|
|
|
|
|
|
|
private readonly CompositeDrawable? target;
|
2021-04-30 11:35:58 +08:00
|
|
|
|
2022-08-01 13:03:57 +08:00
|
|
|
private FillFlowContainer fill = null!;
|
2021-05-10 17:15:39 +08:00
|
|
|
|
2022-08-01 13:03:57 +08:00
|
|
|
public SkinComponentToolbox(CompositeDrawable? target = null)
|
2023-01-17 00:55:28 +08:00
|
|
|
: base(SkinEditorStrings.Components)
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2022-03-15 17:57:53 +08:00
|
|
|
this.target = target;
|
2021-04-30 11:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2021-04-30 13:37:49 +08:00
|
|
|
Child = fill = new FillFlowContainer
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2021-04-30 13:37:49 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Direction = FillDirection.Vertical,
|
2022-03-15 16:19:20 +08:00
|
|
|
Spacing = new Vector2(2)
|
2021-04-30 11:09:58 +08:00
|
|
|
};
|
|
|
|
|
2022-03-15 17:57:53 +08:00
|
|
|
reloadComponents();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reloadComponents()
|
|
|
|
{
|
|
|
|
fill.Clear();
|
|
|
|
|
2022-08-01 13:03:57 +08:00
|
|
|
var skinnableTypes = SkinnableInfo.GetAllAvailableDrawables();
|
2021-04-30 11:09:58 +08:00
|
|
|
foreach (var type in skinnableTypes)
|
2022-03-15 17:57:53 +08:00
|
|
|
attemptAddComponent(type);
|
2021-04-30 11:09:58 +08:00
|
|
|
}
|
|
|
|
|
2022-03-15 17:57:53 +08:00
|
|
|
private void attemptAddComponent(Type type)
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-12-16 17:16:26 +08:00
|
|
|
Drawable instance = (Drawable)Activator.CreateInstance(type)!;
|
2021-04-30 11:09:58 +08:00
|
|
|
|
2022-03-15 17:57:53 +08:00
|
|
|
if (!((ISkinnableDrawable)instance).IsEditable) return;
|
2021-05-13 16:03:17 +08:00
|
|
|
|
2022-03-15 17:57:53 +08:00
|
|
|
fill.Add(new ToolboxComponentButton(instance, target)
|
|
|
|
{
|
|
|
|
RequestPlacement = t => RequestPlacement?.Invoke(t)
|
|
|
|
});
|
2021-04-30 11:09:58 +08:00
|
|
|
}
|
2022-03-17 17:40:26 +08:00
|
|
|
catch (DependencyNotRegisteredException)
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2022-03-17 17:40:26 +08:00
|
|
|
// This loading code relies on try-catching any dependency injection errors to know which components are valid for the current target screen.
|
|
|
|
// If a screen can't provide the required dependencies, a skinnable component should not be displayed in the list.
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Error(e, $"Skin component {type} could not be loaded in the editor component list due to an error");
|
2021-04-30 11:09:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class ToolboxComponentButton : OsuButton
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2022-08-01 13:03:57 +08:00
|
|
|
public Action<Type>? RequestPlacement;
|
|
|
|
|
2021-04-30 11:35:58 +08:00
|
|
|
private readonly Drawable component;
|
2022-08-01 13:03:57 +08:00
|
|
|
private readonly CompositeDrawable? dependencySource;
|
2021-04-30 11:35:58 +08:00
|
|
|
|
2022-08-01 13:03:57 +08:00
|
|
|
private Container innerContainer = null!;
|
2021-04-30 14:22:51 +08:00
|
|
|
|
2022-03-15 16:33:06 +08:00
|
|
|
private const float contracted_size = 60;
|
|
|
|
private const float expanded_size = 120;
|
|
|
|
|
2022-08-01 13:03:57 +08:00
|
|
|
public ToolboxComponentButton(Drawable component, CompositeDrawable? dependencySource)
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2021-04-30 11:35:58 +08:00
|
|
|
this.component = component;
|
2022-03-15 17:57:53 +08:00
|
|
|
this.dependencySource = dependencySource;
|
2021-04-30 14:22:51 +08:00
|
|
|
|
|
|
|
Enabled.Value = true;
|
2021-04-30 11:09:58 +08:00
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
2022-03-15 16:33:06 +08:00
|
|
|
Height = contracted_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
|
|
|
this.Delay(300).ResizeHeightTo(expanded_size, 500, Easing.OutQuint);
|
|
|
|
return base.OnHover(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
{
|
|
|
|
base.OnHoverLost(e);
|
|
|
|
this.ResizeHeightTo(contracted_size, 500, Easing.OutQuint);
|
2021-04-30 14:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-03-15 16:19:20 +08:00
|
|
|
private void load(OverlayColourProvider colourProvider, OsuColour colours)
|
2021-04-30 14:22:51 +08:00
|
|
|
{
|
2022-03-15 16:19:20 +08:00
|
|
|
BackgroundColour = colourProvider.Background3;
|
2021-04-30 11:09:58 +08:00
|
|
|
|
2021-04-30 14:22:51 +08:00
|
|
|
AddRange(new Drawable[]
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2022-03-15 16:19:20 +08:00
|
|
|
new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Padding = new MarginPadding(10) { Bottom = 20 },
|
|
|
|
Masking = true,
|
2022-03-15 17:57:53 +08:00
|
|
|
Child = innerContainer = new DependencyBorrowingContainer(dependencySource)
|
2022-03-15 16:19:20 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Child = component
|
|
|
|
},
|
|
|
|
},
|
2021-04-30 14:22:51 +08:00
|
|
|
new OsuSpriteText
|
2021-04-30 11:09:58 +08:00
|
|
|
{
|
2021-04-30 14:22:51 +08:00
|
|
|
Text = component.GetType().Name,
|
2022-03-15 16:19:20 +08:00
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Margin = new MarginPadding(5),
|
2021-04-30 14:22:51 +08:00
|
|
|
},
|
|
|
|
});
|
2021-04-30 11:09:58 +08:00
|
|
|
|
|
|
|
// adjust provided component to fit / display in a known state.
|
2021-04-30 11:35:58 +08:00
|
|
|
component.Anchor = Anchor.Centre;
|
|
|
|
component.Origin = Anchor.Centre;
|
2021-04-30 11:09:58 +08:00
|
|
|
}
|
2021-04-30 11:35:58 +08:00
|
|
|
|
2022-03-15 16:19:20 +08:00
|
|
|
protected override void Update()
|
2021-04-30 11:35:58 +08:00
|
|
|
{
|
2022-03-15 16:19:20 +08:00
|
|
|
base.Update();
|
2021-04-30 11:35:58 +08:00
|
|
|
|
2022-03-15 16:19:20 +08:00
|
|
|
if (component.DrawSize != Vector2.Zero)
|
2021-04-30 14:22:51 +08:00
|
|
|
{
|
2022-03-15 16:19:20 +08:00
|
|
|
float bestScale = Math.Min(
|
|
|
|
innerContainer.DrawWidth / component.DrawWidth,
|
|
|
|
innerContainer.DrawHeight / component.DrawHeight);
|
|
|
|
|
|
|
|
innerContainer.Scale = new Vector2(bestScale);
|
2021-04-30 14:22:51 +08:00
|
|
|
}
|
2021-04-30 11:35:58 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 14:22:51 +08:00
|
|
|
protected override bool OnClick(ClickEvent e)
|
2021-04-30 11:35:58 +08:00
|
|
|
{
|
2021-04-30 14:22:51 +08:00
|
|
|
RequestPlacement?.Invoke(component.GetType());
|
|
|
|
return true;
|
2021-04-30 11:35:58 +08:00
|
|
|
}
|
2021-04-30 11:09:58 +08:00
|
|
|
}
|
2022-03-14 14:51:10 +08:00
|
|
|
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class DependencyBorrowingContainer : Container
|
2022-03-14 14:51:10 +08:00
|
|
|
{
|
2022-10-21 03:48:44 +08:00
|
|
|
protected override bool ShouldBeConsideredForInput(Drawable child) => false;
|
|
|
|
|
|
|
|
public override bool PropagateNonPositionalInputSubTree => false;
|
|
|
|
|
2022-08-01 13:03:57 +08:00
|
|
|
private readonly CompositeDrawable? donor;
|
2022-03-15 17:57:53 +08:00
|
|
|
|
2022-08-01 13:03:57 +08:00
|
|
|
public DependencyBorrowingContainer(CompositeDrawable? donor)
|
2022-03-15 17:57:53 +08:00
|
|
|
{
|
|
|
|
this.donor = donor;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) =>
|
|
|
|
new DependencyContainer(donor?.Dependencies ?? base.CreateChildDependencies(parent));
|
2022-03-14 14:51:10 +08:00
|
|
|
}
|
2021-04-30 11:09:58 +08:00
|
|
|
}
|
|
|
|
}
|