a few tweaks to the gui skinning system

This commit is contained in:
Dylan Wilson
2017-02-15 23:03:26 +10:00
parent 258a12f2f9
commit 719a779e85
10 changed files with 138 additions and 38 deletions
@@ -20,8 +20,9 @@
"Padding": "10"
}
],
"Templates": {
"close-button": {
"Styles": [
{
"Name": "close-button",
"Type": "GuiButton",
"TextureRegion": "buttonRound_close",
"HoverStyle": {
@@ -29,8 +30,8 @@
"Color": "LightGray"
}
},
"white-button": {
{
"Name": "white-button",
"Type": "GuiButton",
"TextureRegion": "buttonLong_grey",
"TextOffset": "0 -2",
@@ -47,42 +48,43 @@
"TextColor": "#ffffffff"
}
},
"checkbox": {
{
"Name": "checkbox",
"Type": "GuiCheckBox",
"TextureRegion": "checkBox_beige_unchecked",
"CheckedStyle": {
"Type": "GuiCheckBox",
"TextureRegion": "checkBox_beige_checked"
}
}
},
"brown-panel": {
{
"Name": "brown-panel",
"Type": "GuiPanel",
"TextureRegion": "panel_brown",
"Size": "400 300"
},
"beige-inset-panel": {
{
"Name": "beige-inset-panel",
"Type": "GuiPanel",
"TextureRegion": "panelInset_beige",
"Size": "385 280"
},
"label": {
{
"Name": "label",
"Type": "GuiLabel",
"TextColor": "#444444"
},
"monogame-title": {
{
"Name": "monogame-title",
"Type": "GuiImage",
"TextureRegion": "monogame-title"
},
"text-box": {
{
"Name": "text-box",
"Type": "GuiTextBox",
"TextureRegion": "grey_textBox",
"TextColor": "#444444"
}
}
}
]
}
@@ -4,44 +4,44 @@
{
"Type": "GuiPanel",
"Name": "MainPanel",
"Skin": "brown-panel",
"Style": "brown-panel",
"Position": "400 240",
"Size": "700 380",
"Controls": [
{
"Type": "GuiPanel",
"Skin": "beige-inset-panel",
"Style": "beige-inset-panel",
"Position": "350 190",
"Size": "680 360"
},
{
"Name": "CloseButton",
"Type": "GuiButton",
"Skin": "close-button",
"Style": "close-button",
"Position": "690 10"
},
{
"Type": "GuiImage",
"Skin": "monogame-title",
"Style": "monogame-title",
"Position": "350 100"
},
{
"Name": "PlayButton",
"Type": "GuiButton",
"Skin": "white-button",
"Style": "white-button",
"Position": "350 190",
"Text": "Play"
},
{
"Name": "QuitButton",
"Type": "GuiButton",
"Skin": "white-button",
"Style": "white-button",
"Position": "350 240",
"Text": "Quit"
},
{
"Name": "EmailLabel",
"Skin": "label",
"Style": "label",
"Type": "GuiLabel",
"Position": "350 315",
"Text": "email"
@@ -49,14 +49,14 @@
{
"Name": "EmailTextBox",
"Type": "GuiTextBox",
"Skin": "text-box",
"Style": "text-box",
"Position": "350 345",
"Size": "300 42"
},
{
"Name": "CheckBox",
"Type": "GuiCheckBox",
"Skin": "checkbox",
"Style": "checkbox",
"Position": "350 285",
"Size": "250 35",
"Text": "sign me up to all the things",
@@ -17,7 +17,7 @@ namespace MonoGame.Extended.Gui
where T : GuiControl, new()
{
var control = new T();
_skin.Templates[template].Apply(control);
_skin.GetStyle(template).Apply(control);
control.Name = name;
control.Position = position;
control.Text = text;
@@ -28,7 +28,7 @@ namespace MonoGame.Extended.Gui
where T : GuiControl, new()
{
var control = new T();
_skin.Templates[template].Apply(control);
_skin.GetStyle(template).Apply(control);
onCreate(control);
return control;
}
@@ -36,7 +36,7 @@ namespace MonoGame.Extended.Gui
public GuiControl Create(Type type, string template)
{
var control = (GuiControl) Activator.CreateInstance(type);
_skin.Templates[template].Apply(control);
_skin.GetStyle(template).Apply(control);
return control;
}
}
@@ -11,12 +11,14 @@ namespace MonoGame.Extended.Gui
{
private Dictionary<string, object> _previousState;
public GuiControlStyle(Type targetType)
public GuiControlStyle(string name, Type targetType)
{
Name = name;
TargetType = targetType;
_setters = new Dictionary<string, object>();
}
public string Name { get; }
public Type TargetType { get; set; }
private readonly Dictionary<string, object> _setters;
+75 -2
View File
@@ -1,3 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MonoGame.Extended.BitmapFonts;
using System.Linq;
@@ -6,6 +8,70 @@ using Newtonsoft.Json;
namespace MonoGame.Extended.Gui
{
public class KeyedCollection<TKey, TValue> : ICollection<TValue>
{
private readonly Func<TValue, TKey> _getKey;
private readonly Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
public KeyedCollection(Func<TValue, TKey> getKey)
{
_getKey = getKey;
}
public TValue this[TKey key] => _dictionary[key];
public ICollection<TKey> Keys => _dictionary.Keys;
public ICollection<TValue> Values => _dictionary.Values;
public int Count => _dictionary.Count;
public bool IsReadOnly => false;
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<TValue> GetEnumerator()
{
return _dictionary.Values.GetEnumerator();
}
public void Add(TValue item)
{
_dictionary.Add(_getKey(item), item);
}
public void Clear()
{
_dictionary.Clear();
}
public bool Contains(TValue item)
{
return _dictionary.ContainsKey(_getKey(item));
}
public void CopyTo(TValue[] array, int arrayIndex)
{
throw new NotSupportedException();
}
public bool Remove(TValue item)
{
return _dictionary.Remove(_getKey(item));
}
public bool ContainsKey(TKey key)
{
return _dictionary.ContainsKey(key);
}
public bool TryGetValue(TKey key, out TValue value)
{
return _dictionary.TryGetValue(key, out value);
}
}
public class GuiSkin
{
public GuiSkin()
@@ -13,9 +79,11 @@ namespace MonoGame.Extended.Gui
TextureAtlases = new List<TextureAtlas>();
Fonts = new List<BitmapFont>();
NinePatches = new List<NinePatchRegion2D>();
Templates = new Dictionary<string, GuiControlStyle>();
_styles = new KeyedCollection<string, GuiControlStyle>(s => s.Name);
}
private readonly KeyedCollection<string, GuiControlStyle> _styles;
[JsonProperty(Order = 0)]
public string Name { get; set; }
@@ -35,6 +103,11 @@ namespace MonoGame.Extended.Gui
public GuiCursor Cursor { get; set; }
[JsonProperty(Order = 6)]
public IDictionary<string, GuiControlStyle> Templates { get; }
public ICollection<GuiControlStyle> Styles => _styles;
public GuiControlStyle GetStyle(string name)
{
return _styles[name];
}
}
}
@@ -8,6 +8,7 @@ namespace MonoGame.Extended.Gui.Serialization
{
private readonly IGuiSkinService _guiSkinService;
private readonly GuiControlStyleJsonConverter _styleConverter = new GuiControlStyleJsonConverter();
private const string _styleProperty = "Style";
public GuiControlJsonConverter(IGuiSkinService guiSkinService)
{
@@ -22,7 +23,7 @@ namespace MonoGame.Extended.Gui.Serialization
{
var controlFactory = new GuiControlFactory(_guiSkinService.Skin);
var style = (GuiControlStyle) _styleConverter.ReadJson(reader, objectType, existingValue, serializer);
var skin = (string) style["Skin"];
var skin = (string) style[_styleProperty];
var control = controlFactory.Create(style.TargetType, skin);
object childControls;
@@ -3,6 +3,7 @@ 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;
@@ -12,6 +13,7 @@ namespace MonoGame.Extended.Gui.Serialization
{
private readonly Dictionary<string, Type> _controlTypes;
private const string _typeProperty = "Type";
private const string _nameProperty = "Name";
public GuiControlStyleJsonConverter()
{
@@ -40,12 +42,13 @@ namespace MonoGame.Extended.Gui.Serialization
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var dictionary = serializer.Deserialize<Dictionary<string, object>>(reader);
var typeName = (string)dictionary[_typeProperty];
var targetType = _controlTypes[typeName];
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(targetType);
var style = new GuiControlStyle(name, targetType);
foreach (var keyValuePair in dictionary.Where(i => i.Key != _typeProperty))
{
@@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace MonoGame.Extended.Collections
{
public static class DictionaryExtensions
{
public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
{
return GetValueOrDefault(dictionary, key, default(TValue));
}
public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : defaultValue;
}
}
}
@@ -19,4 +19,4 @@ namespace MonoGame.Extended.Collections
return list;
}
}
}
}
@@ -39,6 +39,7 @@
<ItemGroup>
<Compile Include="Animations\Animation.cs" />
<Compile Include="Animations\AnimationComponent.cs" />
<Compile Include="Collections\DictionaryExtensions.cs" />
<Compile Include="FloatHelper.cs" />
<Compile Include="Serialization\NinePatchRegion2DJsonConverter.cs" />
<Compile Include="Serialization\ThicknessJsonConverter.cs" />