CrabUI
Loading...
Searching...
No Matches
CUIStyle.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Text;
6
7using Barotrauma;
8using Microsoft.Xna.Framework;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Graphics;
11using System.Xml;
12using System.Xml.Linq;
13
14namespace CrabUI
15{
16
17 /// <summary>
18 /// In Fact just an observable dict
19 /// </summary>
20 public partial class CUIStyle : IEnumerable<KeyValuePair<string, string>>, ICloneable
21 {
22 public static CUIStyle DefaultFor(Type T) => CUITypeMetaData.Get(T).DefaultStyle;
23 public static CUIStyle DefaultFor<T>() where T : CUIComponent => CUITypeMetaData.Get(typeof(T)).DefaultStyle;
24
25 IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, string>>.GetEnumerator()
26 => Props.GetEnumerator();
27
28 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
29 => Props.GetEnumerator();
30
31 public void Add(string key, string value)
32 {
33 Props[key] = value;
34 OnPropChanged?.Invoke(key, value);
35 }
36
37 public void Clear() => Props.Clear();
38
39 /// <summary>
40 /// Prop name -> value
41 /// </summary>
42 public Dictionary<string, string> Props = new();
43
44 public event Action<string, string> OnPropChanged;
45 public event Action<CUIStyle> OnUse;
46
47 public virtual string this[string name]
48 {
49 get => Props.ContainsKey(name) ? Props[name] : "";
50 set => Add(name, value);
51 }
52
53 public object Clone()
54 {
55 CUIStyle style = new CUIStyle();
56 style.Props = new Dictionary<string, string>(Props);
57 return style;
58 }
59
60 public static CUIStyle Merge(CUIStyle baseStyle, CUIStyle addedStyle)
61 {
62 CUIStyle result = new CUIStyle();
63
64 if (baseStyle != null)
65 {
66 foreach (string key in baseStyle.Props.Keys)
67 {
68 result[key] = baseStyle.Props[key];
69 }
70 }
71
72 if (addedStyle != null)
73 {
74 foreach (string key in addedStyle.Props.Keys)
75 {
76 result[key] = addedStyle.Props[key];
77 }
78 }
79
80 return result;
81 }
82
83 public void Use(CUIStyle source)
84 {
85 Props = new Dictionary<string, string>(source.Props);
86 OnUse?.Invoke(this);
87 }
88
89 public void UseString(string raw)
90 {
91 Clear();
92
93 try
94 {
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());
98
99 foreach (var pair in pairs)
100 {
101 Props[pair[0]] = pair[1];
102 }
103 }
104 catch (Exception e) { CUI.Warning($"Style.UseString failed: {e.Message}"); }
105 OnUse?.Invoke(this);
106 }
107
108 public void UseXML(XElement element)
109 {
110 Clear();
111 foreach (XElement prop in element.Elements())
112 {
113 Props[prop.Name.ToString()] = prop.Value;
114 }
115 OnUse?.Invoke(this);
116 }
117
118 public static CUIStyle FromXML(XElement element)
119 {
120 CUIStyle style = new CUIStyle();
121 style.UseXML(element);
122 return style;
123 }
124
125 public override string ToString()
126 {
127 return "{ " + String.Join(", ", Props.Select(kvp => $"{kvp.Key} : {kvp.Value}")) + " }";
128 }
129
130 public static CUIStyle Parse(string raw)
131 {
132 CUIStyle style = new CUIStyle();
133 style.UseString(raw);
134 return style;
135 }
136
137 public override bool Equals(object obj)
138 {
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)
146 {
147 if (!styleB.Props.ContainsKey(key)) return false;
148 if (styleA[key] != styleB[key]) return false;
149 }
150 return true;
151 }
152
153 public static CUIStyle operator +(CUIStyle styleA, CUIStyle styleB) => Merge(styleA, styleB);
154
155 public static bool operator ==(CUIStyle styleA, CUIStyle styleB)
156 {
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)
162 {
163 if (!styleB.Props.ContainsKey(key)) return false;
164 if (styleA[key] != styleB[key]) return false;
165 }
166 return true;
167 }
168
169 public static bool operator !=(CUIStyle styleA, CUIStyle styleB)
170 {
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)
176 {
177 if (!styleB.Props.ContainsKey(key)) return true;
178 if (styleA[key] != styleB[key]) return true;
179 }
180 return false;
181 }
182
183
184
185 }
186
187
188
189}
Base class for all components.
virtual CUIComponent Get(string name)
Returns memorized component by name.
In Fact just an observable dict.
Definition CUIStyle.cs:21
Dictionary< string, string > Props
Prop name -> value.
Definition CUIStyle.cs:42