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.
|
|
|
|
|
|
|
|
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;
|
2023-02-16 19:01:38 +08:00
|
|
|
using osu.Game.Rulesets;
|
2021-05-06 14:16:16 +08:00
|
|
|
using osuTK;
|
|
|
|
|
2023-02-15 14:30:29 +08:00
|
|
|
namespace osu.Game.Skinning
|
2021-05-06 14:16:16 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
2023-02-15 15:01:26 +08:00
|
|
|
/// Serialised backing data for <see cref="ISerialisableDrawable"/>s.
|
2023-02-15 14:14:38 +08:00
|
|
|
/// Used for json serialisation in user skins.
|
2021-05-06 14:16:16 +08:00
|
|
|
/// </summary>
|
2023-02-15 15:01:26 +08:00
|
|
|
/// <remarks>
|
|
|
|
/// Can be created using <see cref="SerialisableDrawableExtensions.CreateSerialisedInfo"/>.
|
|
|
|
/// Can also be applied to an existing drawable using <see cref="SerialisableDrawableExtensions.ApplySerialisedInfo"/>.
|
|
|
|
/// </remarks>
|
2021-05-07 18:13:38 +08:00
|
|
|
[Serializable]
|
2023-02-15 14:47:41 +08:00
|
|
|
public sealed class SerialisedDrawableInfo
|
2021-05-06 14:16:16 +08:00
|
|
|
{
|
2023-02-16 19:01:59 +08:00
|
|
|
public Type Type { get; set; } = null!;
|
2021-05-10 21:33:45 +08:00
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2023-02-15 15:01:26 +08:00
|
|
|
/// <inheritdoc cref="ISerialisableDrawable.UsesFixedAnchor"/>
|
2021-06-08 20:22:35 +08:00
|
|
|
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>();
|
|
|
|
|
2023-02-15 14:47:41 +08:00
|
|
|
public List<SerialisedDrawableInfo> Children { get; } = new List<SerialisedDrawableInfo>();
|
2021-05-10 21:33:45 +08:00
|
|
|
|
2021-05-13 11:57:28 +08:00
|
|
|
[JsonConstructor]
|
2023-02-15 14:47:41 +08:00
|
|
|
public SerialisedDrawableInfo()
|
2021-05-07 18:13:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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>
|
2023-02-15 14:47:41 +08:00
|
|
|
public SerialisedDrawableInfo(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
|
|
|
|
2023-02-18 03:09:52 +08:00
|
|
|
if (component is ISerialisableDrawable serialisableDrawable)
|
|
|
|
UsesFixedAnchor = serialisableDrawable.UsesFixedAnchor;
|
2021-05-06 14:16:16 +08:00
|
|
|
|
2022-03-11 22:27:27 +08:00
|
|
|
foreach (var (_, property) in component.GetSettingsSourceProperties())
|
|
|
|
{
|
2022-12-16 17:16:26 +08:00
|
|
|
var bindable = (IBindable)property.GetValue(component)!;
|
2022-03-11 22:27:27 +08:00
|
|
|
|
2023-02-06 14:00:42 +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
|
|
|
{
|
2023-02-15 15:01:26 +08:00
|
|
|
foreach (var child in container.OfType<ISerialisableDrawable>().OfType<Drawable>())
|
2023-02-15 14:47:41 +08:00
|
|
|
Children.Add(child.CreateSerialisedInfo());
|
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
|
|
|
|
{
|
2022-12-16 17:16:26 +08:00
|
|
|
Drawable d = (Drawable)Activator.CreateInstance(Type)!;
|
2023-02-15 14:47:41 +08:00
|
|
|
d.ApplySerialisedInfo(this);
|
2022-04-01 13:30:02 +08:00
|
|
|
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
|
|
|
|
2023-02-16 19:01:38 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Retrieve all types available which support serialisation.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ruleset">The ruleset to filter results to. If <c>null</c>, global components will be returned instead.</param>
|
|
|
|
public static Type[] GetAllAvailableDrawables(RulesetInfo? ruleset = null)
|
2022-08-01 13:03:57 +08:00
|
|
|
{
|
2023-02-16 19:01:38 +08:00
|
|
|
return (ruleset?.CreateInstance().GetType() ?? typeof(OsuGame))
|
|
|
|
.Assembly.GetTypes()
|
2023-02-17 17:22:30 +08:00
|
|
|
.Where(t => !t.IsInterface && !t.IsAbstract && t.IsPublic)
|
2023-02-16 19:01:38 +08:00
|
|
|
.Where(t => typeof(ISerialisableDrawable).IsAssignableFrom(t))
|
|
|
|
.OrderBy(t => t.Name)
|
|
|
|
.ToArray();
|
2022-08-01 13:03:57 +08:00
|
|
|
}
|
2021-05-07 18:13:38 +08:00
|
|
|
}
|
2021-05-06 14:16:16 +08:00
|
|
|
}
|