From acea7b2e728787a0d83bbf83c8c1f042d2c32e7e Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Mon, 3 Jun 2024 10:15:45 +0800 Subject: + plugins project --- .../source/MonoGame.Extended.Gui/ControlStyle.cs | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ControlStyle.cs (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ControlStyle.cs') diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ControlStyle.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ControlStyle.cs new file mode 100644 index 0000000..1593da3 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ControlStyle.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using MonoGame.Extended.Gui.Controls; + +namespace MonoGame.Extended.Gui +{ + public class ControlStyle : IDictionary + { + private readonly Dictionary> _previousStates = new Dictionary>(); + + public ControlStyle() + : this(typeof(Element)) + { + } + + public ControlStyle(Type targetType) + : this(targetType.FullName, targetType) + { + } + + public ControlStyle(string name, Type targetType) + { + Name = name; + TargetType = targetType; + _setters = new Dictionary(); + } + + public string Name { get; } + public Type TargetType { get; set; } + + private readonly Dictionary _setters; + + public void ApplyIf(Control control, bool predicate) + { + if (predicate) + Apply(control); + else + Revert(control); + } + + public void Apply(Control control) + { + _previousStates[control.Id] = _setters + .ToDictionary(i => i.Key, i => TargetType.GetRuntimeProperty(i.Key)?.GetValue(control)); + + ChangePropertyValues(control, _setters); + } + + public void Revert(Control control) + { + if (_previousStates.ContainsKey(control.Id) && _previousStates[control.Id] != null) + ChangePropertyValues(control, _previousStates[control.Id]); + + _previousStates[control.Id] = null; + } + + private static void ChangePropertyValues(Control control, Dictionary setters) + { + var targetType = control.GetType(); + + foreach (var propertyName in setters.Keys) + { + var propertyInfo = targetType.GetRuntimeProperty(propertyName); + var value = setters[propertyName]; + + if (propertyInfo != null) + { + if(propertyInfo.CanWrite) + propertyInfo.SetValue(control, value); + + // special case when we have a list of items as objects (like on a list box) + if (propertyInfo.PropertyType == typeof(List)) + { + var items = (List)value; + var addMethod = propertyInfo.PropertyType.GetRuntimeMethod("Add", new[] { typeof(object) }); + + foreach (var item in items) + addMethod.Invoke(propertyInfo.GetValue(control), new[] {item}); + } + } + } + } + + public object this[string key] + { + get { return _setters[key]; } + set { _setters[key] = value; } + } + + public ICollection Keys => _setters.Keys; + public ICollection Values => _setters.Values; + public int Count => _setters.Count; + public bool IsReadOnly => false; + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public IEnumerator> GetEnumerator() => _setters.GetEnumerator(); + public void Add(string key, object value) => _setters.Add(key, value); + public void Add(KeyValuePair item) => _setters.Add(item.Key, item.Value); + public bool Remove(string key) => _setters.Remove(key); + public bool Remove(KeyValuePair item) => _setters.Remove(item.Key); + public void Clear() => _setters.Clear(); + public bool Contains(KeyValuePair item) => _setters.Contains(item); + public bool ContainsKey(string key) => _setters.ContainsKey(key); + public bool TryGetValue(string key, out object value) => _setters.TryGetValue(key, out value); + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + throw new NotSupportedException(); + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0