1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 11:42:58 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/SkinnableInfo.cs

112 lines
3.5 KiB
C#
Raw Normal View History

// 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
using System;
using System.Collections.Generic;
using System.Linq;
2021-05-13 11:57:28 +08:00
using Newtonsoft.Json;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Skinning;
using osuTK;
namespace osu.Game.Screens.Play.HUD
{
/// <summary>
/// Serialised information governing custom changes to an <see cref="ISkinnableDrawable"/>.
/// </summary>
[Serializable]
public class SkinnableInfo
{
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; }
/// <inheritdoc cref="ISkinnableDrawable.UsesFixedAnchor"/>
public bool UsesFixedAnchor { get; set; }
public Dictionary<string, object> Settings { get; set; } = new Dictionary<string, object>();
public List<SkinnableInfo> Children { get; } = new List<SkinnableInfo>();
2021-05-13 11:57:28 +08:00
[JsonConstructor]
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>
public SkinnableInfo(Drawable component)
{
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 13:08:39 +08:00
if (component is ISkinnableDrawable skinnable)
UsesFixedAnchor = skinnable.UsesFixedAnchor;
foreach (var (_, property) in component.GetSettingsSourceProperties())
{
var bindable = (IBindable)property.GetValue(component);
if (!bindable.IsDefault)
Settings.Add(property.Name.ToSnakeCase(), bindable.GetUnderlyingSettingValue());
}
2021-05-13 11:57:28 +08:00
if (component is Container<Drawable> container)
{
foreach (var child in container.OfType<ISkinnableDrawable>().OfType<Drawable>())
Children.Add(child.CreateSkinnableInfo());
}
}
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>
public Drawable CreateInstance()
{
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();
}
}
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();
}
}
}