Add basic support for child serialisation

This commit is contained in:
Dean Herbert 2021-05-10 22:33:45 +09:00
parent 1742ee89e0
commit 6a88b8888b
2 changed files with 31 additions and 13 deletions

View File

@ -3,6 +3,7 @@
using System;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings;
using osu.Framework.Threading;
using osu.Game.Screens.Play.HUD;
@ -54,6 +55,12 @@ namespace osu.Game.Extensions
component.Rotation = info.Rotation;
component.Scale = info.Scale;
component.Anchor = info.Anchor;
if (component is Container container)
{
foreach (var child in info.Children)
container.Add(child.CreateInstance());
}
}
}
}

View File

@ -2,9 +2,12 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Extensions;
using osu.Game.IO.Serialization;
using osu.Game.Skinning;
@ -18,6 +21,21 @@ namespace osu.Game.Screens.Play.HUD
[Serializable]
public class SkinnableInfo : IJsonSerializable
{
public Type Type { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public SkinnableTarget? Target { get; set; }
public Vector2 Position { get; set; }
public float Rotation { get; set; }
public Vector2 Scale { get; set; }
public Anchor Anchor { get; set; }
public List<SkinnableInfo> Children { get; } = new List<SkinnableInfo>();
public SkinnableInfo()
{
}
@ -34,21 +52,14 @@ namespace osu.Game.Screens.Play.HUD
Rotation = component.Rotation;
Scale = component.Scale;
Anchor = component.Anchor;
if (component is Container container)
{
foreach (var child in container.Children.OfType<ISkinnableComponent>().OfType<Drawable>())
Children.Add(child.CreateSerialisedInformation());
}
}
public Type Type { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public SkinnableTarget? Target { get; set; }
public Vector2 Position { get; set; }
public float Rotation { get; set; }
public Vector2 Scale { get; set; }
public Anchor Anchor { get; set; }
public Drawable CreateInstance()
{
Drawable d = (Drawable)Activator.CreateInstance(Type);