Files
MonoGame.Extended/Source/MonoGame.Extended.Gui/Serialization/GuiControlJsonConverter.cs
T
tspayne87andDylan Wilson 46f3f99b39 [GUI] Adding support for parent propegation, the idea of forms and textbox highlighting (#439)
* Adding in changes to deal with custom control types for loading from a GuiScreen, also adding in block text if a width for the element exists.  Adding in Padding to some elements to give the developer some more options in styles.  Adding in some changes to the textbox to deal with selecting a section of text.

* Adding in a few changes to deal with propagation down the GuiControl tree, this was added to most events.  Also, adding in the idea of a form and submit button to deal with keyboard controls when inside of a form.  This gives simalar behavier to how forms work in the browser.

* Adding in the enter key skip for the textbox.
2017-11-21 10:50:24 +10:00

62 lines
2.0 KiB
C#

using System;
using MonoGame.Extended.Gui.Controls;
using Newtonsoft.Json;
namespace MonoGame.Extended.Gui.Serialization
{
public class GuiControlJsonConverter : JsonConverter
{
private readonly IGuiSkinService _guiSkinService;
private readonly GuiControlStyleJsonConverter _styleConverter;
private const string _styleProperty = "Style";
public GuiControlJsonConverter(IGuiSkinService guiSkinService, params Type[] customControlTypes)
{
_guiSkinService = guiSkinService;
_styleConverter = new GuiControlStyleJsonConverter(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 = (GuiControlStyle) _styleConverter.ReadJson(reader, objectType, existingValue, serializer);
var template = GetControlTemplate(style);
var control = skin.Create(style.TargetType, template);
object childControls;
if (style.TryGetValue(nameof(GuiControl.Controls), out childControls))
{
var controlCollection = childControls as GuiControlCollection;
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(GuiControl);
}
private static string GetControlTemplate(GuiControlStyle style)
{
object template;
if (style.TryGetValue(_styleProperty, out template))
return template as string;
return null;
}
}
}