loading panels and buttons from skin files

This commit is contained in:
Dylan Wilson
2017-02-05 21:01:12 +10:00
parent f3252492f3
commit 3099efe8f3
5 changed files with 41 additions and 38 deletions
+12 -23
View File
@@ -2,36 +2,25 @@
"Skin": "Content/adventure-gui-skin.json",
"Controls": [
{
"Type": "GuiButton",
"Type": "GuiPanel",
"Skin": "brown-panel",
"Position": "400 240",
"Size": "210 110"
"Size": "700 380",
"Controls": [
]
},
{
"Type": "GuiPanel",
"Skin": "beige-inset-panel",
"Position": "400 240",
"Size": "200 100",
"Controls": [
{
"Type": "GuiButton",
"Name": "PlayButton",
"Text": "Play",
"Position": "400 190"
},
{
"Type": "GuiButton",
"Name": "OptionsButton",
"Text": "Options",
"Position": "400 240"
},
{
"Type": "GuiButton",
"Name": "QuitButton",
"Text": "Quit",
"Position": "400 290"
}
]
"Size": "680 360"
},
{
"Type": "GuiButton",
"Skin": "white-button",
"Position": "400 240",
"Text": "Play"
}
]
}
@@ -32,5 +32,12 @@ namespace MonoGame.Extended.Gui
onCreate(control);
return control;
}
public GuiControl Create(Type type, string template)
{
var control = (GuiControl) Activator.CreateInstance(type);
_skin.Templates[template].Apply(control);
return control;
}
}
}
@@ -32,7 +32,7 @@ namespace MonoGame.Extended.Gui
public void Apply(GuiControl control)
{
_previousState = _setters
.ToDictionary(i => i.Key, i => TargetType.GetRuntimeProperty(i.Key).GetValue(control));
.ToDictionary(i => i.Key, i => TargetType.GetRuntimeProperty(i.Key)?.GetValue(control));
ChangePropertyValues(control, _setters);
}
@@ -54,7 +54,7 @@ namespace MonoGame.Extended.Gui
var propertyInfo = targetType.GetRuntimeProperty(propertyName);
var value = setters[propertyName];
if(propertyInfo.CanWrite)
if(propertyInfo != null && propertyInfo.CanWrite)
propertyInfo.SetValue(control, value);
}
}
@@ -21,8 +21,10 @@ namespace MonoGame.Extended.Gui.Serialization
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var controlFactory = new GuiControlFactory(_guiSkinService.Skin);
var style = _styleConverter.ReadJson(reader, objectType, existingValue, serializer) as GuiControlStyle;
var control = controlFactory.Create<GuiButton>("white-button", c => style?.Apply(c));
var style = (GuiControlStyle) _styleConverter.ReadJson(reader, objectType, existingValue, serializer);
var skin = (string) style["Skin"];
var control = controlFactory.Create(style.TargetType, skin);
style.Apply(control);
return control;
}
@@ -50,23 +50,28 @@ namespace MonoGame.Extended.Gui.Serialization
foreach (var keyValuePair in dictionary.Where(i => i.Key != _typeProperty))
{
var propertyName = keyValuePair.Key;
var rawValue = keyValuePair.Value;
var value = properties.TryGetValue(propertyName, out var propertyInfo)
? DeserializeValueAs(serializer, rawValue, propertyInfo.PropertyType)
: DeserializeValueAs(serializer, rawValue, typeof(object));
if (properties.TryGetValue(propertyName, out var propertyInfo))
{
var json = JsonConvert.SerializeObject(keyValuePair.Value);
using (var textReader = new StringReader(json))
using (var jsonReader = new JsonTextReader(textReader))
{
var value = serializer.Deserialize(jsonReader, propertyInfo.PropertyType);
style.Add(propertyName, value);
}
}
style.Add(propertyName, value);
}
return style;
}
private 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);