2021-05-06 14:16:16 +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
|
|
|
|
|
2021-05-06 14:16:16 +08:00
|
|
|
using System;
|
2021-05-10 21:33:45 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-05-13 11:57:28 +08:00
|
|
|
using Newtonsoft.Json;
|
2022-03-11 22:27:27 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-05-06 14:16:16 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-05-10 21:33:45 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-04-01 13:30:02 +08:00
|
|
|
using osu.Framework.Logging;
|
2022-03-11 22:27:27 +08:00
|
|
|
using osu.Game.Configuration;
|
2021-05-07 18:13:38 +08:00
|
|
|
using osu.Game.Extensions;
|
|
|
|
using osu.Game.Skinning;
|
2021-05-06 14:16:16 +08:00
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play.HUD
|
|
|
|
{
|
|
|
|
/// <summary>
|
2021-05-13 16:06:00 +08:00
|
|
|
/// Serialised information governing custom changes to an <see cref="ISkinnableDrawable"/>.
|
2021-05-06 14:16:16 +08:00
|
|
|
/// </summary>
|
2021-05-07 18:13:38 +08:00
|
|
|
[Serializable]
|
2021-08-31 13:38:35 +08:00
|
|
|
public class SkinnableInfo
|
2021-05-06 14:16:16 +08:00
|
|
|
{
|
2021-05-10 21:33:45 +08:00
|
|
|
public Type Type { get; set; }
|
|
|
|
|
|
|
|
public Vector2 Position { get; set; }
|
|
|
|
|
|
|
|
public float Rotation { get; set; }
|
|
|
|
|
|
|
|
public Vector2 Scale { get; set; }
|
|
|
|
|
|
|
|
public Anchor Anchor { get; set; }
|
|
|
|
|
2021-05-11 12:39:32 +08:00
|
|
|
public Anchor Origin { get; set; }
|
|
|
|
|
2021-06-08 20:22:35 +08:00
|
|
|
/// <inheritdoc cref="ISkinnableDrawable.UsesFixedAnchor"/>
|
|
|
|
public bool UsesFixedAnchor { get; set; }
|
2021-06-06 19:18:08 +08:00
|
|
|
|
2022-03-11 22:27:27 +08:00
|
|
|
public Dictionary<string, object> Settings { get; set; } = new Dictionary<string, object>();
|
|
|
|
|
2021-05-10 21:33:45 +08:00
|
|
|
public List<SkinnableInfo> Children { get; } = new List<SkinnableInfo>();
|
|
|
|
|
2021-05-13 11:57:28 +08:00
|
|
|
[JsonConstructor]
|
2021-05-07 18:13:38 +08:00
|
|
|
public SkinnableInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-13 11:57:28 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Construct a new instance populating all attributes from the provided drawable.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="component">The drawable which attributes should be sourced from.</param>
|
2021-05-07 18:13:38 +08:00
|
|
|
public SkinnableInfo(Drawable component)
|
2021-05-06 14:16:16 +08:00
|
|
|
{
|
|
|
|
Type = component.GetType();
|
|
|
|
|
|
|
|
Position = component.Position;
|
|
|
|
Rotation = component.Rotation;
|
|
|
|
Scale = component.Scale;
|
|
|
|
Anchor = component.Anchor;
|
2021-05-11 12:39:32 +08:00
|
|
|
Origin = component.Origin;
|
2021-06-07 12:04:53 +08:00
|
|
|
|
2021-06-07 13:08:39 +08:00
|
|
|
if (component is ISkinnableDrawable skinnable)
|
2021-06-08 20:22:35 +08:00
|
|
|
UsesFixedAnchor = skinnable.UsesFixedAnchor;
|
2021-05-06 14:16:16 +08:00
|
|
|
|
2022-03-11 22:27:27 +08:00
|
|
|
foreach (var (_, property) in component.GetSettingsSourceProperties())
|
|
|
|
{
|
|
|
|
var bindable = (IBindable)property.GetValue(component);
|
|
|
|
|
|
|
|
if (!bindable.IsDefault)
|
2022-06-20 21:02:15 +08:00
|
|
|
Settings.Add(property.Name.ToSnakeCase(), bindable.GetUnderlyingSettingValue());
|
2022-03-11 22:27:27 +08:00
|
|
|
}
|
|
|
|
|
2021-05-13 11:57:28 +08:00
|
|
|
if (component is Container<Drawable> container)
|
2021-05-10 21:33:45 +08:00
|
|
|
{
|
2021-05-13 16:06:00 +08:00
|
|
|
foreach (var child in container.OfType<ISkinnableDrawable>().OfType<Drawable>())
|
2021-05-13 12:14:49 +08:00
|
|
|
Children.Add(child.CreateSkinnableInfo());
|
2021-05-10 21:33:45 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 18:13:38 +08:00
|
|
|
|
2021-05-13 11:57:28 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Construct an instance of the drawable with all attributes applied.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The new instance.</returns>
|
2021-05-10 14:31:14 +08:00
|
|
|
public Drawable CreateInstance()
|
2021-05-07 18:13:38 +08:00
|
|
|
{
|
2022-04-01 13:30:02 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Drawable d = (Drawable)Activator.CreateInstance(Type);
|
|
|
|
d.ApplySkinnableInfo(this);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Error(e, $"Unable to create skin component {Type.Name}");
|
|
|
|
return Drawable.Empty();
|
|
|
|
}
|
2021-05-07 18:13:38 +08:00
|
|
|
}
|
2022-08-01 13:03:57 +08:00
|
|
|
|
|
|
|
public static Type[] GetAllAvailableDrawables()
|
|
|
|
{
|
|
|
|
return typeof(OsuGame).Assembly.GetTypes()
|
|
|
|
.Where(t => !t.IsInterface && !t.IsAbstract)
|
|
|
|
.Where(t => typeof(ISkinnableDrawable).IsAssignableFrom(t))
|
|
|
|
.OrderBy(t => t.Name)
|
|
|
|
.ToArray();
|
|
|
|
}
|
2021-05-07 18:13:38 +08:00
|
|
|
}
|
2021-05-06 14:16:16 +08:00
|
|
|
}
|