Client LuaCsForBarotrauma
UIStyleFile.cs
1 using System;
3 using System.Collections.Immutable;
4 using System.Linq;
5 using System.Reflection;
6 using System.Xml.Linq;
7 
8 namespace Barotrauma
9 {
10 #if CLIENT
11  public sealed class UIStyleFile : HashlessFile
12  {
13  public UIStyleFile(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path) { }
14 
15  public void LoadFromXElement(ContentXElement parentElement, bool overriding)
16  {
17  Identifier elemName = parentElement.NameAsIdentifier();
18  Identifier elemNameWithFontSuffix = elemName.AppendIfMissing("Font");
19  if (parentElement.IsOverride())
20  {
21  foreach (var element in parentElement.Elements())
22  {
23  LoadFromXElement(element, true);
24  }
25  }
26  else if (GUIStyle.Fonts.ContainsKey(elemNameWithFontSuffix))
27  {
28  GUIFontPrefab prefab = new GUIFontPrefab(parentElement, this);
29  GUIStyle.Fonts[elemNameWithFontSuffix].Prefabs.Add(prefab, overriding);
30  }
31  else if (GUIStyle.Sprites.ContainsKey(elemName))
32  {
33  GUISpritePrefab prefab = new GUISpritePrefab(parentElement, this);
34  GUIStyle.Sprites[elemName].Prefabs.Add(prefab, overriding);
35  }
36  else if (GUIStyle.SpriteSheets.ContainsKey(elemName))
37  {
38  GUISpriteSheetPrefab prefab = new GUISpriteSheetPrefab(parentElement, this);
39  GUIStyle.SpriteSheets[elemName].Prefabs.Add(prefab, overriding);
40  }
41  else if (GUIStyle.Colors.ContainsKey(elemName))
42  {
43  GUIColorPrefab prefab = new GUIColorPrefab(parentElement, this);
44  GUIStyle.Colors[elemName].Prefabs.Add(prefab, overriding);
45  }
46  else if (elemName == "cursor")
47  {
48  GUICursorPrefab prefab = new GUICursorPrefab(parentElement, this);
49  GUIStyle.CursorSprite.Prefabs.Add(prefab, overriding);
50  }
51  else if (elemName == "style")
52  {
53  foreach (var element in parentElement.Elements())
54  {
55  LoadFromXElement(element, overriding);
56  }
57  }
58  else
59  {
60  GUIComponentStyle prefab = new GUIComponentStyle(parentElement, this);
61  GUIStyle.ComponentStyles.Add(prefab, overriding);
62  }
63  }
64 
65  public override sealed void LoadFile()
66  {
67  XDocument doc = XMLExtensions.TryLoadXml(Path);
68  if (doc == null) { return; }
69 
70  var rootElement = doc.Root.FromPackage(ContentPackage);
71  LoadFromXElement(rootElement, false);
72  }
73 
74  public override sealed void UnloadFile()
75  {
76  GUIStyle.ComponentStyles.RemoveByFile(this);
77  GUIStyle.CursorSprite.Prefabs.RemoveByFile(this);
78  GUIStyle.Fonts.Values.ForEach(p => p.Prefabs.RemoveByFile(this));
79  GUIStyle.Sprites.Values.ForEach(p => p.Prefabs.RemoveByFile(this));
80  GUIStyle.SpriteSheets.Values.ForEach(p => p.Prefabs.RemoveByFile(this));
81  GUIStyle.Colors.Values.ForEach(p => p.Prefabs.RemoveByFile(this));
82  }
83 
84  public override sealed void Sort()
85  {
86  GUIStyle.ComponentStyles.SortAll();
87  GUIStyle.CursorSprite.Prefabs.Sort();
88  GUIStyle.Fonts.Values.ForEach(p => p.Prefabs.Sort());
89  GUIStyle.Sprites.Values.ForEach(p => p.Prefabs.Sort());
90  GUIStyle.SpriteSheets.Values.ForEach(p => p.Prefabs.Sort());
91  GUIStyle.Colors.Values.ForEach(p => p.Prefabs.Sort());
92  }
93  }
94 #else
95  public sealed class UIStyleFile : OtherFile
96  {
97  public UIStyleFile(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path) { }
98  }
99 #endif
100 }
readonly ContentPackage ContentPackage
Definition: ContentFile.cs:136
Identifier NameAsIdentifier()
IEnumerable< ContentXElement > Elements()
override sealed void LoadFile()
Definition: UIStyleFile.cs:65
override sealed void Sort()
Definition: UIStyleFile.cs:84
void LoadFromXElement(ContentXElement parentElement, bool overriding)
Definition: UIStyleFile.cs:15
UIStyleFile(ContentPackage contentPackage, ContentPath path)
Definition: UIStyleFile.cs:13
override sealed void UnloadFile()
Definition: UIStyleFile.cs:74