mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 12:06:37 +00:00
62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
using System;
|
|
using MonoGame.Extended.Gui.Controls;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace MonoGame.Extended.Gui.Serialization
|
|
{
|
|
public class ControlJsonConverter : JsonConverter
|
|
{
|
|
private readonly IGuiSkinService _guiSkinService;
|
|
private readonly ControlStyleJsonConverter _styleConverter;
|
|
private const string _styleProperty = "Style";
|
|
|
|
public ControlJsonConverter(IGuiSkinService guiSkinService, params Type[] customControlTypes)
|
|
{
|
|
_guiSkinService = guiSkinService;
|
|
_styleConverter = new ControlStyleJsonConverter(customControlTypes);
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
}
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
var skin = _guiSkinService.Skin;
|
|
var style = (ControlStyle) _styleConverter.ReadJson(reader, objectType, existingValue, serializer);
|
|
var template = GetControlTemplate(style);
|
|
var control = skin.Create(style.TargetType, template);
|
|
|
|
object childControls;
|
|
|
|
if (style.TryGetValue(nameof(Control.Controls), out childControls))
|
|
{
|
|
var controlCollection = childControls as ControlCollection;
|
|
|
|
if (controlCollection != null)
|
|
{
|
|
foreach (var child in controlCollection)
|
|
control.Controls.Add(child);
|
|
}
|
|
}
|
|
|
|
style.Apply(control);
|
|
return control;
|
|
}
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
return objectType == typeof(Control);
|
|
}
|
|
|
|
private static string GetControlTemplate(ControlStyle style)
|
|
{
|
|
object template;
|
|
|
|
if (style.TryGetValue(_styleProperty, out template))
|
|
return template as string;
|
|
|
|
return null;
|
|
}
|
|
}
|
|
} |