20 public partial class CUIStyle : IEnumerable<KeyValuePair<string, string>>, ICloneable
22 public static CUIStyle DefaultFor(Type T) => CUITypeMetaData.Get(T).DefaultStyle;
25 IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, string>>.GetEnumerator()
26 =>
Props.GetEnumerator();
28 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
29 =>
Props.GetEnumerator();
31 public void Add(
string key,
string value)
34 OnPropChanged?.Invoke(key, value);
37 public void Clear() =>
Props.Clear();
42 public Dictionary<string, string>
Props =
new();
44 public event Action<string, string> OnPropChanged;
45 public event Action<CUIStyle> OnUse;
47 public virtual string this[
string name]
49 get =>
Props.ContainsKey(name) ?
Props[name] :
"";
50 set => Add(name, value);
56 style.Props =
new Dictionary<string, string>(
Props);
60 public static CUIStyle Merge(CUIStyle baseStyle, CUIStyle addedStyle)
62 CUIStyle result =
new CUIStyle();
64 if (baseStyle !=
null)
66 foreach (
string key
in baseStyle.Props.Keys)
68 result[key] = baseStyle.Props[key];
72 if (addedStyle !=
null)
74 foreach (
string key
in addedStyle.Props.Keys)
76 result[key] = addedStyle.Props[key];
83 public void Use(CUIStyle source)
85 Props =
new Dictionary<string, string>(source.Props);
89 public void UseString(
string raw)
95 string content = raw.Split(
'{',
'}')[1];
96 if (content.Trim() ==
"")
return;
97 var pairs = content.Split(
',').Select(s => s.Split(
':').Select(sub => sub.Trim()).ToArray());
99 foreach (var pair
in pairs)
101 Props[pair[0]] = pair[1];
104 catch (Exception e) { CUI.Warning($
"Style.UseString failed: {e.Message}"); }
108 public void UseXML(XElement element)
111 foreach (XElement prop
in element.Elements())
113 Props[prop.Name.ToString()] = prop.Value;
118 public static CUIStyle FromXML(XElement element)
120 CUIStyle style =
new CUIStyle();
121 style.UseXML(element);
125 public override string ToString()
127 return "{ " + String.Join(
", ",
Props.Select(kvp => $
"{kvp.Key} : {kvp.Value}")) +
" }";
130 public static CUIStyle Parse(
string raw)
132 CUIStyle style =
new CUIStyle();
133 style.UseString(raw);
137 public override bool Equals(
object obj)
139 if (!(obj is CUIStyle styleB))
return false;
140 CUIStyle styleA =
this;
141 if (styleA is
null && styleB is
null)
return true;
142 if (styleA is
null || styleB is
null)
return false;
143 if (styleA.Props is
null || styleB.Props is
null)
return false;
144 if (styleA.Props.Count != styleB.Props.Count)
return false;
145 foreach (var (key, value) in styleA.Props)
147 if (!styleB.Props.ContainsKey(key))
return false;
148 if (styleA[key] != styleB[key])
return false;
153 public static CUIStyle operator +(CUIStyle styleA, CUIStyle styleB) => Merge(styleA, styleB);
155 public static bool operator ==(CUIStyle styleA, CUIStyle styleB)
157 if (styleA is
null && styleB is
null)
return true;
158 if (styleA is
null || styleB is
null)
return false;
159 if (styleA.Props is
null || styleB.Props is
null)
return false;
160 if (styleA.Props.Count != styleB.Props.Count)
return false;
161 foreach (var (key, value) in styleA.Props)
163 if (!styleB.Props.ContainsKey(key))
return false;
164 if (styleA[key] != styleB[key])
return false;
169 public static bool operator !=(CUIStyle styleA, CUIStyle styleB)
171 if (styleA is
null && styleB is
null)
return false;
172 if (styleA is
null || styleB is
null)
return true;
173 if (styleA.Props is
null || styleB.Props is
null)
return true;
174 if (styleA.Props.Count != styleB.Props.Count)
return true;
175 foreach (var (key, value) in styleA.Props)
177 if (!styleB.Props.ContainsKey(key))
return true;
178 if (styleA[key] != styleB[key])
return true;