2023-02-15 16:51:18 +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 osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Extensions;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
public static class SerialisableDrawableExtensions
|
|
|
|
{
|
|
|
|
public static SerialisedDrawableInfo CreateSerialisedInfo(this Drawable component) => new SerialisedDrawableInfo(component);
|
|
|
|
|
|
|
|
public static void ApplySerialisedInfo(this Drawable component, SerialisedDrawableInfo drawableInfo)
|
|
|
|
{
|
|
|
|
// todo: can probably make this better via deserialisation directly using a common interface.
|
|
|
|
component.Position = drawableInfo.Position;
|
|
|
|
component.Rotation = drawableInfo.Rotation;
|
|
|
|
component.Scale = drawableInfo.Scale;
|
|
|
|
component.Anchor = drawableInfo.Anchor;
|
|
|
|
component.Origin = drawableInfo.Origin;
|
|
|
|
|
2023-02-16 14:34:41 +08:00
|
|
|
if (component is ISerialisableDrawable serialisableDrawable)
|
2023-02-15 16:51:18 +08:00
|
|
|
{
|
2023-02-16 14:34:41 +08:00
|
|
|
serialisableDrawable.UsesFixedAnchor = drawableInfo.UsesFixedAnchor;
|
2023-02-15 16:51:18 +08:00
|
|
|
|
|
|
|
foreach (var (_, property) in component.GetSettingsSourceProperties())
|
|
|
|
{
|
|
|
|
var bindable = ((IBindable)property.GetValue(component)!);
|
|
|
|
|
|
|
|
if (!drawableInfo.Settings.TryGetValue(property.Name.ToSnakeCase(), out object? settingValue))
|
|
|
|
{
|
|
|
|
// TODO: We probably want to restore default if not included in serialisation information.
|
|
|
|
// This is not simple to do as SetDefault() is only found in the typed Bindable<T> interface right now.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-16 14:34:41 +08:00
|
|
|
serialisableDrawable.CopyAdjustedSetting(bindable, settingValue);
|
2023-02-15 16:51:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (component is Container container)
|
|
|
|
{
|
|
|
|
foreach (var child in drawableInfo.Children)
|
|
|
|
container.Add(child.CreateInstance());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|