CrabUI
Loading...
Searching...
No Matches
CUIStyleLoader.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Text;
6using System.Diagnostics;
7
8using Barotrauma;
9using Microsoft.Xna.Framework;
10using Microsoft.Xna.Framework.Input;
11using Microsoft.Xna.Framework.Graphics;
12using System.Xml;
13using System.Xml.Linq;
14using System.IO;
15using System.Diagnostics;
16namespace CrabUI
17{
18 public class CUIStyleLoader
19 {
20 internal static void InitStatic()
21 {
22 CUI.OnInit += () => LoadDefaultStyles();
23 }
24
25 public static string DefaultStylesPath => Path.Combine(CUI.AssetsPath, "Default Styles.xml");
26
27
28 public static void LoadDefaultStyles()
29 {
30 Stopwatch sw = Stopwatch.StartNew();
31
32 if (CUI.AssetsPath == null) return;
33 if (!File.Exists(DefaultStylesPath)) return;
34
35
36 Dictionary<Type, CUIStyle> DefaultStyles = new();
37
38 XDocument xdoc = XDocument.Load(DefaultStylesPath);
39 XElement root = xdoc.Element("DefaultStyles");
40 foreach (XElement componentStyle in root.Elements())
41 {
42 Type componentType = CUIReflection.GetComponentTypeByName(componentStyle.Name.ToString());
43 if (componentType == null)
44 {
45 CUI.Warning($"Couldn't find type for default style {componentStyle.Name}");
46 continue;
47 }
48
49 DefaultStyles[componentType] = CUIStyle.FromXML(componentStyle);
50 }
51 sw.Stop();
52 CUIDebug.Log($"Parsing default styles took {sw.ElapsedMilliseconds}ms");
53 sw.Restart();
54
55 // It's heavy because CUITypeMetaData.Get creates defaults here
56 foreach (Type T in DefaultStyles.Keys)
57 {
58 CUITypeMetaData.Get(T).DefaultStyle = DefaultStyles[T];
59 }
60
61 sw.Stop();
62 CUIDebug.Log($"Applying default styles took {sw.ElapsedMilliseconds}ms");
63 }
64 }
65
66
67
68
69}