1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

Move extension methods closer to serialisation classes

This commit is contained in:
Dean Herbert 2023-02-15 17:51:18 +09:00
parent e61d2d571c
commit b1cf6d83d8
7 changed files with 52 additions and 46 deletions

View File

@ -13,7 +13,6 @@ using osu.Framework.Timing;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Rulesets.Osu; using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Skinning.Legacy; using osu.Game.Rulesets.Osu.Skinning.Legacy;

View File

@ -1,11 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Configuration;
using osu.Game.Skinning;
using osuTK; using osuTK;
namespace osu.Game.Extensions namespace osu.Game.Extensions
@ -47,42 +43,5 @@ namespace osu.Game.Extensions
/// <returns>The delta vector in Parent's coordinates.</returns> /// <returns>The delta vector in Parent's coordinates.</returns>
public static Vector2 ScreenSpaceDeltaToParentSpace(this Drawable drawable, Vector2 delta) => public static Vector2 ScreenSpaceDeltaToParentSpace(this Drawable drawable, Vector2 delta) =>
drawable.Parent.ToLocalSpace(drawable.Parent.ToScreenSpace(Vector2.Zero) + delta); drawable.Parent.ToLocalSpace(drawable.Parent.ToScreenSpace(Vector2.Zero) + delta);
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;
if (component is ISerialisableDrawable skinnable)
{
skinnable.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;
}
skinnable.CopyAdjustedSetting(bindable, settingValue);
}
}
if (component is Container container)
{
foreach (var child in drawableInfo.Children)
container.Add(child.CreateInstance());
}
}
} }
} }

View File

@ -9,7 +9,6 @@ using Newtonsoft.Json;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Extensions;
using osu.Game.Screens.Edit; using osu.Game.Screens.Edit;
using osu.Game.Skinning; using osu.Game.Skinning;

View File

@ -16,7 +16,7 @@ namespace osu.Game.Skinning
/// Attaching this interface to any <see cref="IDrawable"/> will make it serialisable to user skins (see <see cref="SkinImporter.Save"/>). /// Attaching this interface to any <see cref="IDrawable"/> will make it serialisable to user skins (see <see cref="SkinImporter.Save"/>).
/// Adding <see cref="SettingSourceAttribute"/> annotated bindables will also allow serialising settings automatically. /// Adding <see cref="SettingSourceAttribute"/> annotated bindables will also allow serialising settings automatically.
/// ///
/// Serialisation is done via <see cref="SerialisedDrawableInfo"/> using <see cref="osu.Game.Extensions.DrawableExtensions.CreateSerialisedInfo"/>. /// Serialisation is done via <see cref="SerialisedDrawableInfo"/> using <see cref="SerialisableDrawableExtensions.CreateSerialisedInfo"/>.
/// </remarks> /// </remarks>
public interface ISerialisableDrawable : IDrawable public interface ISerialisableDrawable : IDrawable
{ {

View File

@ -5,7 +5,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Extensions;
namespace osu.Game.Skinning namespace osu.Game.Skinning
{ {

View File

@ -0,0 +1,51 @@
// 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;
if (component is ISerialisableDrawable skinnable)
{
skinnable.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;
}
skinnable.CopyAdjustedSetting(bindable, settingValue);
}
}
if (component is Container container)
{
foreach (var child in drawableInfo.Children)
container.Add(child.CreateInstance());
}
}
}
}

View File

@ -8,7 +8,6 @@ using System.Threading;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Skinning.Serialisation;
namespace osu.Game.Skinning namespace osu.Game.Skinning
{ {