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;
|
2023-11-10 17:13:36 +08:00
|
|
|
using osu.Framework.Extensions.EnumExtensions;
|
2023-02-15 16:51:18 +08:00
|
|
|
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;
|
2023-11-10 19:00:20 +08:00
|
|
|
if (drawableInfo.Width is float width && width != 0 && (component as CompositeDrawable)?.AutoSizeAxes.HasFlagFast(Axes.X) != true)
|
2023-11-10 17:13:36 +08:00
|
|
|
component.Width = width;
|
2023-11-10 19:00:20 +08:00
|
|
|
if (drawableInfo.Height is float height && height != 0 && (component as CompositeDrawable)?.AutoSizeAxes.HasFlagFast(Axes.Y) != true)
|
2023-11-10 17:13:36 +08:00
|
|
|
component.Height = height;
|
2023-02-15 16:51:18 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|