1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 05:27:23 +08:00
osu-lazer/osu.Game/Skinning/SerialisableDrawableExtensions.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
2.3 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.
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osuTK;
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;
if (drawableInfo.Size != Vector2.Zero && (component as CompositeDrawable)?.AutoSizeAxes == Axes.None)
component.Size = drawableInfo.Size;
component.Scale = drawableInfo.Scale;
component.Anchor = drawableInfo.Anchor;
component.Origin = drawableInfo.Origin;
if (component is ISerialisableDrawable serialisableDrawable)
{
serialisableDrawable.UsesFixedAnchor = drawableInfo.UsesFixedAnchor;
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;
}
serialisableDrawable.CopyAdjustedSetting(bindable, settingValue);
}
}
if (component is Container container)
{
foreach (var child in drawableInfo.Children)
container.Add(child.CreateInstance());
}
}
}
}