CrabUI
Loading...
Searching...
No Matches
CrabUI.CUIGlobalStyleResolver Class Reference

Contains all logic for resolving styles in the framework. More...

Static Public Member Functions

static void OnComponentStyleChanged (CUIComponent host)
 
static void OnComponentStylePropChanged (CUIComponent host, string key)
 
static void OnPaletteChange (CUIPalette palette)
 
static void OnDefaultStyleChanged (Type CUIType)
 
static void OnDefaultStylePropChanged (Type CUIType, string key)
 
static void ApplyStyleOn (CUIStyle style, CUIComponent target)
 
static void ApplyStylePropOn (CUIStyle style, string name, CUIComponent target, CUITypeMetaData meta=null)
 Applies 1 prop with name on the target.
 

Static Public Attributes

static string CUIPalettePrefix = "CUIPalette."
 

Detailed Description

Contains all logic for resolving styles in the framework.

Definition at line 17 of file CUIGlobalStyleResolver.cs.

Member Function Documentation

◆ ApplyStyleOn()

static void CrabUI.CUIGlobalStyleResolver.ApplyStyleOn ( CUIStyle style,
CUIComponent target )
static

Definition at line 125 of file CUIGlobalStyleResolver.cs.

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 }
static void ApplyStylePropOn(CUIStyle style, string name, CUIComponent target, CUITypeMetaData meta=null)
Applies 1 prop with name on the target.

◆ ApplyStylePropOn()

static void CrabUI.CUIGlobalStyleResolver.ApplyStylePropOn ( CUIStyle style,
string name,
CUIComponent target,
CUITypeMetaData meta = null )
static

Applies 1 prop with name on the target.

Definition at line 146 of file CUIGlobalStyleResolver.cs.

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 }

◆ OnComponentStyleChanged()

static void CrabUI.CUIGlobalStyleResolver.OnComponentStyleChanged ( CUIComponent host)
static

Definition at line 21 of file CUIGlobalStyleResolver.cs.

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 }

◆ OnComponentStylePropChanged()

static void CrabUI.CUIGlobalStyleResolver.OnComponentStylePropChanged ( CUIComponent host,
string key )
static

Definition at line 29 of file CUIGlobalStyleResolver.cs.

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 }

◆ OnDefaultStyleChanged()

static void CrabUI.CUIGlobalStyleResolver.OnDefaultStyleChanged ( Type CUIType)
static

Definition at line 58 of file CUIGlobalStyleResolver.cs.

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 }

◆ OnDefaultStylePropChanged()

static void CrabUI.CUIGlobalStyleResolver.OnDefaultStylePropChanged ( Type CUIType,
string key )
static

Definition at line 86 of file CUIGlobalStyleResolver.cs.

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 }

◆ OnPaletteChange()

static void CrabUI.CUIGlobalStyleResolver.OnPaletteChange ( CUIPalette palette)
static

Definition at line 47 of file CUIGlobalStyleResolver.cs.

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 }

Member Data Documentation

◆ CUIPalettePrefix

string CrabUI.CUIGlobalStyleResolver.CUIPalettePrefix = "CUIPalette."
static

Definition at line 142 of file CUIGlobalStyleResolver.cs.


The documentation for this class was generated from the following file: