2021-05-06 06:16:16 +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.
|
|
|
|
|
|
|
|
using System;
|
2021-05-10 13:33:45 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-05-13 03:57:28 +00:00
|
|
|
using Newtonsoft.Json;
|
2022-03-11 14:27:27 +00:00
|
|
|
using osu.Framework.Bindables;
|
2021-05-06 06:16:16 +00:00
|
|
|
using osu.Framework.Graphics;
|
2021-05-10 13:33:45 +00:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-04-01 05:30:02 +00:00
|
|
|
using osu.Framework.Logging;
|
2022-03-11 14:27:27 +00:00
|
|
|
using osu.Game.Configuration;
|
2021-05-07 10:13:38 +00:00
|
|
|
using osu.Game.Extensions;
|
2023-02-16 11:01:38 +00:00
|
|
|
using osu.Game.Rulesets;
|
2021-05-06 06:16:16 +00:00
|
|
|
using osuTK;
|
|
|
|
|
2023-02-15 06:30:29 +00:00
|
|
|
namespace osu.Game.Skinning
|
2021-05-06 06:16:16 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2023-02-15 07:01:26 +00:00
|
|
|
/// Serialised backing data for <see cref="ISerialisableDrawable"/>s.
|
2023-02-15 06:14:38 +00:00
|
|
|
/// Used for json serialisation in user skins.
|
2021-05-06 06:16:16 +00:00
|
|
|
/// </summary>
|
2023-02-15 07:01:26 +00: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 10:13:38 +00:00
|
|
|
[Serializable]
|
2023-02-15 06:47:41 +00:00
|
|
|
public sealed class SerialisedDrawableInfo
|
2021-05-06 06:16:16 +00:00
|
|
|
{
|
2023-02-16 11:01:59 +00:00
|
|
|
public Type Type { get; set; } = null!;
|
2021-05-10 13:33:45 +00:00
|
|
|
|
|
|
|
public Vector2 Position { get; set; }
|
|
|
|
|
|
|
|
public float Rotation { get; set; }
|
|
|
|
|
|
|
|
public Vector2 Scale { get; set; }
|
|
|
|
|
|
|
|
public Anchor Anchor { get; set; }
|
|
|
|
|
2021-05-11 04:39:32 +00:00
|
|
|
public Anchor Origin { get; set; }
|
|
|
|
|
2023-02-15 07:01:26 +00:00
|
|
|
/// <inheritdoc cref="ISerialisableDrawable.UsesFixedAnchor"/>
|
2021-06-08 12:22:35 +00:00
|
|
|
public bool UsesFixedAnchor { get; set; }
|
2021-06-06 11:18:08 +00:00
|
|
|
|
2022-03-11 14:27:27 +00:00
|
|
|
public Dictionary<string, object> Settings { get; set; } = new Dictionary<string, object>();
|
|
|
|
|
2023-02-15 06:47:41 +00:00
|
|
|
public List<SerialisedDrawableInfo> Children { get; } = new List<SerialisedDrawableInfo>();
|
2021-05-10 13:33:45 +00:00
|
|
|
|
2021-05-13 03:57:28 +00:00
|
|
|
[JsonConstructor]
|
2023-02-15 06:47:41 +00:00
|
|
|
public SerialisedDrawableInfo()
|
2021-05-07 10:13:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-13 03:57:28 +00: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 06:47:41 +00:00
|
|
|
public SerialisedDrawableInfo(Drawable component)
|
2021-05-06 06:16:16 +00:00
|
|
|
{
|
|
|
|
Type = component.GetType();
|
|
|
|
|
|
|
|
Position = component.Position;
|
|
|
|
Rotation = component.Rotation;
|
|
|
|
Scale = component.Scale;
|
|
|
|
Anchor = component.Anchor;
|
2021-05-11 04:39:32 +00:00
|
|
|
Origin = component.Origin;
|
2021-06-07 04:04:53 +00:00
|
|
|
|
2023-02-17 19:09:52 +00:00
|
|
|
if (component is ISerialisableDrawable serialisableDrawable)
|
|
|
|
UsesFixedAnchor = serialisableDrawable.UsesFixedAnchor;
|
2021-05-06 06:16:16 +00:00
|
|
|
|
2022-03-11 14:27:27 +00:00
|
|
|
foreach (var (_, property) in component.GetSettingsSourceProperties())
|
|
|
|
{
|
2022-12-16 09:16:26 +00:00
|
|
|
var bindable = (IBindable)property.GetValue(component)!;
|
2022-03-11 14:27:27 +00:00
|
|
|
|
2023-02-06 06:00:42 +00:00
|
|
|
Settings.Add(property.Name.ToSnakeCase(), bindable.GetUnderlyingSettingValue());
|
2022-03-11 14:27:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 03:57:28 +00:00
|
|
|
if (component is Container<Drawable> container)
|
2021-05-10 13:33:45 +00:00
|
|
|
{
|
2023-02-15 07:01:26 +00:00
|
|
|
foreach (var child in container.OfType<ISerialisableDrawable>().OfType<Drawable>())
|
2023-02-15 06:47:41 +00:00
|
|
|
Children.Add(child.CreateSerialisedInfo());
|
2021-05-10 13:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 10:13:38 +00:00
|
|
|
|
2021-05-13 03:57:28 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Construct an instance of the drawable with all attributes applied.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The new instance.</returns>
|
2021-05-10 06:31:14 +00:00
|
|
|
public Drawable CreateInstance()
|
2021-05-07 10:13:38 +00:00
|
|
|
{
|
2022-04-01 05:30:02 +00:00
|
|
|
try
|
|
|
|
{
|
2022-12-16 09:16:26 +00:00
|
|
|
Drawable d = (Drawable)Activator.CreateInstance(Type)!;
|
2023-02-15 06:47:41 +00:00
|
|
|
d.ApplySerialisedInfo(this);
|
2022-04-01 05:30:02 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Error(e, $"Unable to create skin component {Type.Name}");
|
|
|
|
return Drawable.Empty();
|
|
|
|
}
|
2021-05-07 10:13:38 +00:00
|
|
|
}
|
2022-08-01 05:03:57 +00:00
|
|
|
|
2023-02-16 11:01:38 +00: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 05:03:57 +00:00
|
|
|
{
|
2023-02-16 11:01:38 +00:00
|
|
|
return (ruleset?.CreateInstance().GetType() ?? typeof(OsuGame))
|
|
|
|
.Assembly.GetTypes()
|
2023-02-17 09:22:30 +00:00
|
|
|
.Where(t => !t.IsInterface && !t.IsAbstract && t.IsPublic)
|
2023-02-16 11:01:38 +00:00
|
|
|
.Where(t => typeof(ISerialisableDrawable).IsAssignableFrom(t))
|
|
|
|
.OrderBy(t => t.Name)
|
|
|
|
.ToArray();
|
2022-08-01 05:03:57 +00:00
|
|
|
}
|
2021-05-07 10:13:38 +00:00
|
|
|
}
|
2021-05-06 06:16:16 +00:00
|
|
|
}
|