summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/ControlJsonConverter.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-06-03 10:15:45 +0800
committerchai <215380520@qq.com>2024-06-03 10:15:45 +0800
commitacea7b2e728787a0d83bbf83c8c1f042d2c32e7e (patch)
tree0bfec05c1ca2d71be2c337bcd110a0421f19318b /Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/ControlJsonConverter.cs
parent88febcb02bf127d961c6471d9e846c0e1315f5c3 (diff)
+ plugins project
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/ControlJsonConverter.cs')
-rw-r--r--Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/ControlJsonConverter.cs67
1 files changed, 67 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/ControlJsonConverter.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/ControlJsonConverter.cs
new file mode 100644
index 0000000..a9c71f8
--- /dev/null
+++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/ControlJsonConverter.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+using MonoGame.Extended.Gui.Controls;
+
+namespace MonoGame.Extended.Gui.Serialization
+{
+ public class ControlJsonConverter : JsonConverter<Control>
+ {
+ 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);
+ }
+
+ /// <inheritdoc />
+ public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Control);
+
+ /// <inheritdoc />
+ public override Control Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ var style = _styleConverter.Read(ref reader, typeToConvert, options);
+ var template = GetControlTemplate(style);
+ var skin = _guiSkinService.Skin;
+ var control = skin.Create(style.TargetType, template);
+
+ var itemsControl = control as ItemsControl;
+ if (itemsControl != null)
+ {
+ object childControls;
+
+ if (style.TryGetValue(nameof(ItemsControl.Items), out childControls))
+ {
+ var controlCollection = childControls as ControlCollection;
+
+ if (controlCollection != null)
+ {
+ foreach (var child in controlCollection)
+ itemsControl.Items.Add(child);
+ }
+ }
+ }
+
+ style.Apply(control);
+ return control;
+ }
+
+ /// <inheritdoc />
+ public override void Write(Utf8JsonWriter writer, Control value, JsonSerializerOptions options) { }
+
+
+
+ private static string GetControlTemplate(ControlStyle style)
+ {
+ object template;
+
+ if (style.TryGetValue(_styleProperty, out template))
+ return template as string;
+
+ return null;
+ }
+ }
+}