mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 12:06:37 +00:00
* started refactoring for gui windows * a bit more work on gui windows * fixed some bugs and broken build stuff * fixed some bugs in gui controls * a little code cleanup * got gui dialogs positioning and experimenting with named styles by type * apply control styles directly in the constructor of controls * got everything compiling again * all gui controls have skinnable constructors * rework uniform grid desired size calculations * turns out gui layouts are complicated * include pdb files in nuget packages * fixed a bug in find nearest glyph * custom control styling * added a range constructor overload that takes one value * rethinking control desired size behaviour * made particle emitters more editable at runtime * moved the auto trigger code to the particle effect instead of the emitters * tweaks * changed the particle modifer interface to an abstract base with an optional name * changed the particle modifer interface to an abstract base with an optional name * an attempt at creating a gui combo box * rolled back the nested control experiment * pretty good (but not perfect) combo boxes * tweaks * name property bindings on lists * removed and cleaned up the hacks from the gui combo box * minor combo box styling tweaks * minor bug fix in gui combo box * experiments with data binding * fixed a mistake on the texture region service interface * working on the ability to serialize particles * finished particle serialization * more robust range deserialization * added editor browable attributes * fixed some broken demos * made the particle modifiers editable at runtime * fixed some issues with particle serialization * fixed some issues with interpolators
83 lines
3.1 KiB
C#
83 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using MonoGame.Extended.Collections;
|
|
using MonoGame.Extended.Gui.Controls;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace MonoGame.Extended.Gui.Serialization
|
|
{
|
|
public class GuiControlStyleJsonConverter : JsonConverter
|
|
{
|
|
private readonly Dictionary<string, Type> _controlTypes;
|
|
private const string _typeProperty = "Type";
|
|
private const string _nameProperty = "Name";
|
|
|
|
public GuiControlStyleJsonConverter(params Type[] customControlTypes)
|
|
{
|
|
_controlTypes = typeof(GuiControl)
|
|
.GetTypeInfo()
|
|
.Assembly
|
|
.ExportedTypes
|
|
.Concat(customControlTypes)
|
|
.Where(t => t.GetTypeInfo().IsSubclassOf(typeof(GuiControl)))
|
|
.ToDictionary(t => t.Name);
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
var style = (GuiControlStyle)value;
|
|
var dictionary = new Dictionary<string, object> { [_typeProperty] = style.TargetType.Name };
|
|
|
|
foreach (var keyValuePair in style)
|
|
dictionary.Add(keyValuePair.Key, keyValuePair.Value);
|
|
|
|
serializer.Serialize(writer, dictionary);
|
|
}
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
var dictionary = serializer.Deserialize<Dictionary<string, object>>(reader);
|
|
var name = dictionary.GetValueOrDefault(_nameProperty) as string;
|
|
var typeName = dictionary.GetValueOrDefault(_typeProperty) as string;
|
|
var targetType = typeName != null ? _controlTypes[typeName] : typeof(GuiControl);
|
|
var properties = targetType
|
|
.GetRuntimeProperties()
|
|
.ToDictionary(p => p.Name);
|
|
var style = new GuiControlStyle(name, targetType);
|
|
|
|
foreach (var keyValuePair in dictionary.Where(i => i.Key != _typeProperty))
|
|
{
|
|
var propertyName = keyValuePair.Key;
|
|
var rawValue = keyValuePair.Value;
|
|
|
|
PropertyInfo propertyInfo;
|
|
var value = properties.TryGetValue(propertyName, out propertyInfo)
|
|
? DeserializeValueAs(serializer, rawValue, propertyInfo.PropertyType)
|
|
: DeserializeValueAs(serializer, rawValue, typeof(object));
|
|
|
|
style.Add(propertyName, value);
|
|
}
|
|
|
|
return style;
|
|
}
|
|
|
|
private static object DeserializeValueAs(JsonSerializer serializer, object value, Type type)
|
|
{
|
|
var json = JsonConvert.SerializeObject(value);
|
|
|
|
using (var textReader = new StringReader(json))
|
|
using (var jsonReader = new JsonTextReader(textReader))
|
|
{
|
|
return serializer.Deserialize(jsonReader, type);
|
|
}
|
|
}
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
return objectType == typeof(GuiControlStyle);
|
|
}
|
|
}
|
|
} |