CrabUI
Loading...
Searching...
No Matches
CUIGlobalStyleResolver.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;
11
12namespace CrabUI
13{
14 /// <summary>
15 /// Contains all logic for resolving styles in the framework
16 /// </summary>
17 public static class CUIGlobalStyleResolver
18 {
19
20
21 public static void OnComponentStyleChanged(CUIComponent host)
22 {
23 CUITypeMetaData meta = CUITypeMetaData.Get(host.GetType());
24 host.ResolvedStyle = CUIStyle.Merge(meta.ResolvedDefaultStyle, host.Style);
25 ApplyStyleOn(host.ResolvedStyle, host);
26 host.InvokeOnStyleApplied();
27 }
28
29 public static void OnComponentStylePropChanged(CUIComponent host, string key)
30 {
31 CUITypeMetaData meta = CUITypeMetaData.Get(host.GetType());
32
33 if (meta.ResolvedDefaultStyle.Props.ContainsKey(key))
34 {
35 host.ResolvedStyle[key] = meta.ResolvedDefaultStyle[key];
36 }
37
38 if (host.Style.Props.ContainsKey(key))
39 {
40 host.ResolvedStyle[key] = host.Style[key];
41 }
42
43 ApplyStylePropOn(host.ResolvedStyle, key, host, meta);
44 host.InvokeOnStyleApplied();
45 }
46
47 public static void OnPaletteChange(CUIPalette palette)
48 {
49 foreach (Type CUIType in CUIReflection.CUITypes.Values)
50 {
51 foreach (CUIComponent c in CUIComponent.ComponentsByType.GetPage(CUIType))
52 {
53 ApplyStyleOn(c.ResolvedStyle, c);
54 }
55 }
56 }
57
58 public static void OnDefaultStyleChanged(Type CUIType)
59 {
60 try
61 {
62 // Merge default styles
63 CUIReflection.CUITypeTree[CUIType].RunRecursive((node) =>
64 {
65 node.Meta.ResolvedDefaultStyle = CUIStyle.Merge(
66 node.Parent?.Meta.ResolvedDefaultStyle,
67 node.Meta.DefaultStyle
68 );
69 });
70
71 // Apply default styles
72 CUIReflection.CUITypeTree[CUIType].RunRecursive((node) =>
73 {
74 foreach (CUIComponent c in CUIComponent.ComponentsByType.GetPage(node.T))
75 {
76 OnComponentStyleChanged(c);
77 }
78 });
79 }
80 catch (Exception e)
81 {
82 CUI.Warning($"OnDefaultStyleChanged| {e}");
83 }
84 }
85
86 public static void OnDefaultStylePropChanged(Type CUIType, string key)
87 {
88 try
89 {
90 // Merge default styles
91 CUIReflection.CUITypeTree[CUIType].RunRecursive((node) =>
92 {
93 if (node.Parent != null)
94 {
95 if (node.Parent.Meta.ResolvedDefaultStyle.Props.ContainsKey(key))
96 {
97 node.Meta.ResolvedDefaultStyle[key] = node.Parent.Meta.ResolvedDefaultStyle[key];
98 }
99 }
100
101 if (node.Meta.DefaultStyle.Props.ContainsKey(key))
102 {
103 node.Meta.ResolvedDefaultStyle[key] = node.Meta.DefaultStyle[key];
104 }
105 });
106
107 // Apply default styles
108 CUIReflection.CUITypeTree[CUIType].RunRecursive((node) =>
109 {
110 foreach (CUIComponent c in CUIComponent.ComponentsByType.GetPage(node.T))
111 {
112 OnComponentStylePropChanged(c, key);
113 }
114 });
115 }
116 catch (Exception e)
117 {
118 CUI.Warning(e);
119 }
120 }
121
122
123
124
125 public static void ApplyStyleOn(CUIStyle style, CUIComponent target)
126 {
127 if (target == null)
128 {
129 CUI.Warning($"Style target is null");
130 return;
131 }
132
133 CUITypeMetaData meta = CUITypeMetaData.Get(target.GetType());
134
135 foreach (string name in style.Props.Keys)
136 {
137 ApplyStylePropOn(style, name, target, meta);
138 }
139 }
140
141
142 public static string CUIPalettePrefix = "CUIPalette.";
143 /// <summary>
144 /// Applies 1 prop with name on the target
145 /// </summary>
146 public static void ApplyStylePropOn(CUIStyle style, string name, CUIComponent target, CUITypeMetaData meta = null)
147 {
148 if (target.Unreal) return;
149
150 if (target == null) { CUI.Warning($"Style target is null"); return; }
151
152 meta ??= CUITypeMetaData.Get(target.GetType());
153
154 PropertyInfo pi = meta.Assignable.GetValueOrDefault(name);
155
156 if (pi == null)
157 {
158 if (CUIPalette.NotifyExcessivePropStyles) CUI.Warning($"Can't apply style: Couldn't find {name} prop in {target}");
159
160 return;
161 }
162
163 string raw = style[name];
164
165 if (raw.StartsWith(CUIPalettePrefix))
166 {
167 PaletteExtractResult result = CUIPalette.Extract(raw.Substring(CUIPalettePrefix.Length), target.Palette);
168 if (result.Ok)
169 {
170 raw = result.Value;
171 }
172 else
173 {
174 if (CUIPalette.NotifiMissingPropStyles)
175 {
176 CUI.Warning($"Can't find {raw.Substring(CUIPalettePrefix.Length)} palette style for {target}");
177 }
178 return;
179 }
180 }
181
182 MethodInfo parse = CUIExtensions.Parse.GetValueOrDefault(pi.PropertyType);
183
184 parse ??= pi.PropertyType.GetMethod(
185 "Parse",
186 BindingFlags.Public | BindingFlags.Static,
187 new Type[] { typeof(string) }
188 );
189
190 if (parse == null)
191 {
192 CUI.Warning($"Can't parse style prop {name} for {target} because it's type {pi.PropertyType.Name} is missing Parse method");
193 return;
194 }
195
196 try
197 {
198 pi.SetValue(target, parse.Invoke(null, new object[] { raw }));
199 }
200 catch (Exception e)
201 {
202 CUI.Warning($"Can't parse {raw} into {pi.PropertyType.Name} for {target}");
203 CUI.Warning(e);
204 }
205 }
206 }
207
208
209
210}
Base class for all components.
PaletteOrder Palette
This palette will be used to resolve palette styles Primary, Secondary, Tertiary,...
CUIStyle Style
Allows you to assing parsable string or link to CUIPalette to any prop It's indexable,...
Contains all logic for resolving styles in the framework.
static void ApplyStylePropOn(CUIStyle style, string name, CUIComponent target, CUITypeMetaData meta=null)
Applies 1 prop with name on the target.
In fact a static class managing static things.
Definition CUIErrors.cs:16
In Fact just an observable dict.
Definition CUIStyle.cs:21
Dictionary< string, string > Props
Prop name -> value.
Definition CUIStyle.cs:42