Client LuaCsForBarotrauma
ComponentStyle.cs
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Xml.Linq;
7 
8 namespace Barotrauma
9 {
10  public enum SpriteFallBackState
11  {
12  None,
13  Hover,
14  Pressed,
15  Selected,
17  Toggle
18  }
19 
21  {
22  public readonly Vector4 Padding;
23 
24  public readonly Color Color;
25  public readonly Color HoverColor;
26  public readonly Color SelectedColor;
27  public readonly Color PressedColor;
28  public readonly Color DisabledColor;
29 
30  public readonly Color TextColor;
31  public readonly Color HoverTextColor;
32  public readonly Color SelectedTextColor;
33  public readonly Color DisabledTextColor;
34 
35  public readonly float SpriteCrossFadeTime;
36  public readonly float ColorCrossFadeTime;
37  public readonly TransitionMode TransitionMode;
38 
39  public readonly Identifier Font;
40  public readonly bool ForceUpperCase;
41 
42  public readonly Color OutlineColor;
43 
44  public readonly ContentXElement Element;
45 
46  public readonly Dictionary<GUIComponent.ComponentState, List<UISprite>> Sprites;
47 
49 
50  public readonly GUIComponentStyle ParentStyle;
51  public readonly Dictionary<Identifier, GUIComponentStyle> ChildStyles;
52 
53  public static GUIComponentStyle FromHierarchy(IReadOnlyList<Identifier> hierarchy)
54  {
55  if (hierarchy is null || hierarchy.None()) { return null; }
56  GUIStyle.ComponentStyles.TryGet(hierarchy[0], out GUIComponentStyle style);
57  for (int i = 1; i < hierarchy.Count; i++)
58  {
59  if (style is null) { return null; }
60  style.ChildStyles.TryGetValue(hierarchy[i], out style);
61  }
62  return style;
63  }
64 
65  public static Identifier[] ToHierarchy(GUIComponentStyle style)
66  {
67  List<Identifier> ids = new List<Identifier>();
68  while (style != null)
69  {
70  ids.Insert(0, style.Identifier);
71  style = style.ParentStyle;
72  }
73 
74  return ids.ToArray();
75  }
76 
77  public readonly string Name;
78 
79  public int? Width { get; private set; }
80  public int? Height { get; private set; }
81 
82  public GUIComponentStyle(ContentXElement element, UIStyleFile file, GUIComponentStyle parent = null) : base(element, file)
83  {
84  Name = element.Name.LocalName;
85 
86  Element = element;
87 
88  Sprites = new Dictionary<GUIComponent.ComponentState, List<UISprite>>();
89  foreach (GUIComponent.ComponentState state in Enum.GetValues(typeof(GUIComponent.ComponentState)))
90  {
91  Sprites[state] = new List<UISprite>();
92  }
93 
94  ParentStyle = parent;
95  ChildStyles = new Dictionary<Identifier, GUIComponentStyle>();
96 
97  Padding = element.GetAttributeVector4("padding", Vector4.Zero);
98 
99  Color = element.GetAttributeColor("color", Color.Transparent);
100  HoverColor = element.GetAttributeColor("hovercolor", Color);
101  SelectedColor = element.GetAttributeColor("selectedcolor", Color);
102  DisabledColor = element.GetAttributeColor("disabledcolor", Color);
103  PressedColor = element.GetAttributeColor("pressedcolor", Color);
104  OutlineColor = element.GetAttributeColor("outlinecolor", Color.Transparent);
105 
106  TextColor = element.GetAttributeColor("textcolor", Color.Black);
107  HoverTextColor = element.GetAttributeColor("hovertextcolor", TextColor);
108  DisabledTextColor = element.GetAttributeColor("disabledtextcolor", TextColor);
109  SelectedTextColor = element.GetAttributeColor("selectedtextcolor", TextColor);
110  SpriteCrossFadeTime = element.GetAttributeFloat("spritefadetime", SpriteCrossFadeTime);
111  ColorCrossFadeTime = element.GetAttributeFloat("colorfadetime", ColorCrossFadeTime);
112 
113  if (Enum.TryParse(element.GetAttributeString("colortransition", string.Empty), ignoreCase: true, out TransitionMode transition))
114  {
115  TransitionMode = transition;
116  }
117  if (Enum.TryParse(element.GetAttributeString("fallbackstate", GUIComponent.ComponentState.None.ToString()), ignoreCase: true, out SpriteFallBackState s))
118  {
119  FallBackState = s;
120  }
121 
122  Font = element.GetAttributeIdentifier("font", "");
123  ForceUpperCase = element.GetAttributeBool("forceuppercase", false);
124 
125  foreach (var subElement in element.Elements())
126  {
127  switch (subElement.Name.ToString().ToLowerInvariant())
128  {
129  case "sprite":
130  UISprite newSprite = new UISprite(subElement);
131  Rectangle sourceRect = newSprite.Sprite.SourceRect;
132  if ((sourceRect.Width <= 1 || sourceRect.Height <= 1) && newSprite.Tile)
133  {
134  DebugConsole.AddWarning($"Sprite \"{subElement.GetAttributeString("name", Name)}\" has a size of 1 or less which may cause performance problems.", contentPackage: element.ContentPackage);
135  }
136 
138  if (subElement.GetAttribute("state") != null)
139  {
140  string stateStr = subElement.GetAttributeString("state", "None");
141  Enum.TryParse(stateStr, out spriteState);
142  Sprites[spriteState].Add(newSprite);
143  //use the same sprite for Hover and HoverSelected if latter is not specified
144  if (spriteState == GUIComponent.ComponentState.HoverSelected && !Sprites.ContainsKey(GUIComponent.ComponentState.HoverSelected))
145  {
146  Sprites[GUIComponent.ComponentState.HoverSelected].Add(newSprite);
147  }
148  }
149  else
150  {
151  foreach (GUIComponent.ComponentState state in Enum.GetValues(typeof(GUIComponent.ComponentState)))
152  {
153  Sprites[state].Add(newSprite);
154  }
155  }
156  break;
157  case "size":
158  break;
159  default:
160  Identifier styleName = subElement.NameAsIdentifier();
161  if (ChildStyles.ContainsKey(styleName))
162  {
163  DebugConsole.ThrowError("UI style \"" + element.Name.ToString() + "\" contains multiple child styles with the same name (\"" + styleName + "\")!");
164  ChildStyles[styleName] = new GUIComponentStyle(subElement, file, this);
165  }
166  else
167  {
168  ChildStyles.Add(styleName, new GUIComponentStyle(subElement, file, this));
169  }
170  break;
171  }
172  }
173 
174  GetSize(element);
175  }
176 
178  {
180  }
182  {
183  return Sprites.ContainsKey(state) ? Sprites[state]?.First()?.Sprite : null;
184  }
185 
186  public void RefreshSize()
187  {
188  Width = null;
189  Height = null;
190  GetSize(Element);
191  foreach (var childStyle in ChildStyles.Values)
192  {
193  childStyle.RefreshSize();
194  }
195  }
196 
197  private void GetSize(XElement element)
198  {
199  Point size = new Point(0, 0);
200  foreach (var subElement in element.Elements())
201  {
202  if (!subElement.Name.ToString().Equals("size", StringComparison.OrdinalIgnoreCase)) { continue; }
203  Point maxResolution = subElement.GetAttributePoint("maxresolution", new Point(int.MaxValue, int.MaxValue));
204  if (GameMain.GraphicsWidth <= maxResolution.X && GameMain.GraphicsHeight <= maxResolution.Y)
205  {
206  size = new Point(
207  ParseSize(subElement, "width"),
208  ParseSize(subElement, "height"));
209  break;
210  }
211  }
212  if (size.X > 0) { Width = size.X; }
213  if (size.Y > 0) { Height = size.Y; }
214  }
215 
216  public override void Dispose() { }
217  }
218 }
string? GetAttributeString(string key, string? def)
Vector4 GetAttributeVector4(string key, in Vector4 def)
Color GetAttributeColor(string key, in Color def)
float GetAttributeFloat(string key, float def)
bool GetAttributeBool(string key, bool def)
Identifier GetAttributeIdentifier(string key, string def)
readonly TransitionMode TransitionMode
readonly Dictionary< GUIComponent.ComponentState, List< UISprite > > Sprites
readonly Color SelectedTextColor
static Identifier[] ToHierarchy(GUIComponentStyle style)
readonly GUIComponentStyle ParentStyle
SpriteFallBackState FallBackState
readonly Identifier Font
readonly float ColorCrossFadeTime
static GUIComponentStyle FromHierarchy(IReadOnlyList< Identifier > hierarchy)
GUIComponentStyle(ContentXElement element, UIStyleFile file, GUIComponentStyle parent=null)
Sprite GetSprite(GUIComponent.ComponentState state)
readonly float SpriteCrossFadeTime
readonly Dictionary< Identifier, GUIComponentStyle > ChildStyles
readonly Color DisabledTextColor
readonly ContentXElement Element
int ParseSize(XElement element, string attributeName)
Definition: GUIPrefab.cs:23
readonly Identifier Identifier
Definition: Prefab.cs:34
TransitionMode
Definition: Enums.cs:6