Client LuaCsForBarotrauma
GUIStyle.cs
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Immutable;
5 using System.Linq;
6 using System.Reflection;
7 
8 namespace Barotrauma
9 {
10  public static class GUIStyle
11  {
12  public readonly static ImmutableDictionary<Identifier, GUIFont> Fonts;
13  public readonly static ImmutableDictionary<Identifier, GUISprite> Sprites;
14  public readonly static ImmutableDictionary<Identifier, GUISpriteSheet> SpriteSheets;
15  public readonly static ImmutableDictionary<Identifier, GUIColor> Colors;
16  static GUIStyle()
17  {
18  var guiClassProperties = typeof(GUIStyle).GetFields(BindingFlags.Public | BindingFlags.Static);
19 
20  ImmutableDictionary<Identifier, T> getPropertiesOfType<T>() where T : class
21  {
22  return guiClassProperties
23  .Where(p => p.FieldType == typeof(T))
24  .Select(p => (p.Name.ToIdentifier(), p.GetValue(null) as T))
25  .ToImmutableDictionary();
26  }
27 
28  Fonts = getPropertiesOfType<GUIFont>();
29  Sprites = getPropertiesOfType<GUISprite>();
30  SpriteSheets = getPropertiesOfType<GUISpriteSheet>();
31  Colors = getPropertiesOfType<GUIColor>();
32  }
33 
34  public readonly static PrefabCollection<GUIComponentStyle> ComponentStyles = new PrefabCollection<GUIComponentStyle>();
35 
36  public readonly static GUIFont Font = new GUIFont("Font");
37  public readonly static GUIFont UnscaledSmallFont = new GUIFont("UnscaledSmallFont");
38  public readonly static GUIFont SmallFont = new GUIFont("SmallFont");
39  public readonly static GUIFont LargeFont = new GUIFont("LargeFont");
40  public readonly static GUIFont SubHeadingFont = new GUIFont("SubHeadingFont");
41  public readonly static GUIFont DigitalFont = new GUIFont("DigitalFont");
42  public readonly static GUIFont HotkeyFont = new GUIFont("HotkeyFont");
43  public readonly static GUIFont MonospacedFont = new GUIFont("MonospacedFont");
44 
45  public readonly static GUICursor CursorSprite = new GUICursor("Cursor");
46 
47  public readonly static GUISprite SubmarineLocationIcon = new GUISprite("SubmarineLocationIcon");
48  public readonly static GUISprite Arrow = new GUISprite("Arrow");
49  public readonly static GUISprite SpeechBubbleIcon = new GUISprite("SpeechBubbleIcon");
50  public readonly static GUISprite SpeechBubbleIconSliced = new GUISprite("SpeechBubbleIconSliced");
51  public readonly static GUISprite InteractionLabelBackground = new GUISprite("InteractionLabelBackground");
52  public readonly static GUISprite BrokenIcon = new GUISprite("BrokenIcon");
53  public readonly static GUISprite YouAreHereCircle = new GUISprite("YouAreHereCircle");
54 
55  public readonly static GUISprite SubLocationIcon = new GUISprite("SubLocationIcon");
56  public readonly static GUISprite ShuttleIcon = new GUISprite("ShuttleIcon");
57  public readonly static GUISprite WreckIcon = new GUISprite("WreckIcon");
58  public readonly static GUISprite CaveIcon = new GUISprite("CaveIcon");
59  public readonly static GUISprite OutpostIcon = new GUISprite("OutpostIcon");
60  public readonly static GUISprite RuinIcon = new GUISprite("RuinIcon");
61  public readonly static GUISprite EnemyIcon = new GUISprite("EnemyIcon");
62  public readonly static GUISprite CorpseIcon = new GUISprite("CorpseIcon");
63  public readonly static GUISprite BeaconIcon = new GUISprite("BeaconIcon");
64 
65  public readonly static GUISprite Radiation = new GUISprite("Radiation");
66  public readonly static GUISpriteSheet RadiationAnimSpriteSheet = new GUISpriteSheet("RadiationAnimSpriteSheet");
67 
68  public readonly static GUISpriteSheet SavingIndicator = new GUISpriteSheet("SavingIndicator");
69  public readonly static GUISpriteSheet GenericThrobber = new GUISpriteSheet("GenericThrobber");
70 
71  public readonly static GUISprite UIGlow = new GUISprite("UIGlow");
72  public readonly static GUISprite TalentGlow = new GUISprite("TalentGlow");
73  public readonly static GUISprite PingCircle = new GUISprite("PingCircle");
74  public readonly static GUISprite UIGlowCircular = new GUISprite("UIGlowCircular");
75  public readonly static GUISprite UIGlowSolidCircular = new GUISprite("UIGlowSolidCircular");
76  public readonly static GUISprite UIThermalGlow = new GUISprite("UIGlowSolidCircular");
77  public readonly static GUISprite ButtonPulse = new GUISprite("ButtonPulse");
78  public readonly static GUISprite WalletPortraitBG = new GUISprite("WalletPortraitBG");
79  public readonly static GUISprite CrewWalletIconSmall = new GUISprite("CrewWalletIconSmall");
80 
81  public readonly static GUISprite EndRoundButtonPulse = new GUISprite("EndRoundButtonPulse");
82 
83  public readonly static GUISpriteSheet FocusIndicator = new GUISpriteSheet("FocusIndicator");
84 
85  public readonly static GUISprite IconOverflowIndicator = new GUISprite("IconOverflowIndicator");
86 
90  public readonly static GUIColor Green = new GUIColor("Green", new Color(154, 213, 163, 255));
91 
95  public readonly static GUIColor Orange = new GUIColor("Orange", new Color(243, 162, 50, 255));
96 
100  public readonly static GUIColor Red = new GUIColor("Red", new Color(245, 105, 105, 255));
101 
105  public readonly static GUIColor Blue = new GUIColor("Blue", new Color(126, 211, 224, 255));
106 
110  public readonly static GUIColor Yellow = new GUIColor("Yellow", new Color(255, 255, 0, 255));
111 
115  public readonly static GUIColor ModdedServerColor = new GUIColor("ModdedServerColor", new Color(154, 185, 160, 255));
116 
117  public readonly static GUIColor ColorInventoryEmpty = new GUIColor("ColorInventoryEmpty", new Color(245, 105, 105, 255));
118  public readonly static GUIColor ColorInventoryHalf = new GUIColor("ColorInventoryHalf", new Color(243, 162, 50, 255));
119  public readonly static GUIColor ColorInventoryFull = new GUIColor("ColorInventoryFull", new Color(96, 222, 146, 255));
120  public readonly static GUIColor ColorInventoryBackground = new GUIColor("ColorInventoryBackground", new Color(56, 56, 56, 255));
121  public readonly static GUIColor ColorInventoryEmptyOverlay = new GUIColor("ColorInventoryEmptyOverlay", new Color(125, 125, 125, 255));
122 
123  public readonly static GUIColor TextColorNormal = new GUIColor("TextColorNormal", new Color(228, 217, 167, 255));
124  public readonly static GUIColor TextColorBright = new GUIColor("TextColorBright", new Color(255, 255, 255, 255));
125  public readonly static GUIColor TextColorDark = new GUIColor("TextColorDark", new Color(0, 0, 0, 230));
126  public readonly static GUIColor TextColorDim = new GUIColor("TextColorDim", new Color(153, 153, 153, 153));
127 
128  public readonly static GUIColor ItemQualityColorPoor = new GUIColor("ItemQualityColorPoor", new Color(128, 128, 128, 255));
129  public readonly static GUIColor ItemQualityColorNormal = new GUIColor("ItemQualityColorNormal", new Color(255, 255, 255, 255));
130  public readonly static GUIColor ItemQualityColorGood = new GUIColor("ItemQualityColorGood", new Color(144, 238, 144, 255));
131  public readonly static GUIColor ItemQualityColorExcellent = new GUIColor("ItemQualityColorExcellent", new Color(173, 216, 230, 255));
132  public readonly static GUIColor ItemQualityColorMasterwork = new GUIColor("ItemQualityColorMasterwork", new Color(147, 112, 219, 255));
133 
134  public readonly static GUIColor ColorReputationVeryLow = new GUIColor("ColorReputationVeryLow", new Color(192, 60, 60, 255));
135  public readonly static GUIColor ColorReputationLow = new GUIColor("ColorReputationLow", new Color(203, 145, 23, 255));
136  public readonly static GUIColor ColorReputationNeutral = new GUIColor("ColorReputationNeutral", new Color(228, 217, 167, 255));
137  public readonly static GUIColor ColorReputationHigh = new GUIColor("ColorReputationHigh", new Color(51, 152, 64, 255));
138  public readonly static GUIColor ColorReputationVeryHigh = new GUIColor("ColorReputationVeryHigh", new Color(71, 160, 164, 255));
139 
140  public readonly static GUIColor InteractionLabelColor = new GUIColor("InteractionLabelColor", new Color(255, 255, 255, 255));
141  public readonly static GUIColor InteractionLabelHoverColor = new GUIColor("InteractionLabelHoverColor", new Color(0, 255, 255, 255));
142 
143  // Inventory
144  public readonly static GUIColor EquipmentSlotIconColor = new GUIColor("EquipmentSlotIconColor", new Color(99, 70, 64, 255));
145 
146  // Health HUD
147  public readonly static GUIColor BuffColorLow = new GUIColor("BuffColorLow", new Color(66, 170, 73, 255));
148  public readonly static GUIColor BuffColorMedium = new GUIColor("BuffColorMedium", new Color(110, 168, 118, 255));
149  public readonly static GUIColor BuffColorHigh = new GUIColor("BuffColorHigh", new Color(154, 213, 163, 255));
150 
151  public readonly static GUIColor DebuffColorLow = new GUIColor("DebuffColorLow", new Color(243, 162, 50, 255));
152  public readonly static GUIColor DebuffColorMedium = new GUIColor("DebuffColorMedium", new Color(155, 55, 55, 255));
153  public readonly static GUIColor DebuffColorHigh = new GUIColor("DebuffColorHigh", new Color(228, 27, 27, 255));
154 
155  public readonly static GUIColor HealthBarColorLow = new GUIColor("HealthBarColorLow", new Color(255, 0, 0, 255));
156  public readonly static GUIColor HealthBarColorMedium = new GUIColor("HealthBarColorMedium", new Color(255, 165, 0, 255));
157  public readonly static GUIColor HealthBarColorHigh = new GUIColor("HealthBarColorHigh", new Color(78, 114, 88));
158  public readonly static GUIColor HealthBarColorPoisoned = new GUIColor("HealthBarColorPoisoned", new Color(100, 150, 0, 255));
159 
160  private readonly static Point defaultItemFrameMargin = new Point(50, 56);
161 
162  public static Point ItemFrameMargin
163  {
164  get
165  {
166  Point size = defaultItemFrameMargin.Multiply(GUI.SlicedSpriteScale);
167 
168  var style = GetComponentStyle("ItemUI");
169  var sprite = style?.Sprites[GUIComponent.ComponentState.None].First();
170  if (sprite != null)
171  {
172  size.X = Math.Min(sprite.Slices[0].Width + sprite.Slices[2].Width, size.X);
173  size.Y = Math.Min(sprite.Slices[0].Height + sprite.Slices[6].Height, size.Y);
174  }
175  return size;
176  }
177  }
178 
179  public static int ItemFrameTopBarHeight
180  {
181  get
182  {
183  var style = GetComponentStyle("ItemUI");
184  var sprite = style?.Sprites[GUIComponent.ComponentState.None].First();
185  return (int)Math.Min(sprite?.Slices[0].Height ?? 0, defaultItemFrameMargin.Y / 2 * GUI.SlicedSpriteScale);
186  }
187  }
188 
189  public static Point ItemFrameOffset => new Point(0, 3).Multiply(GUI.SlicedSpriteScale);
190 
191  public static GUIComponentStyle GetComponentStyle(string styleName)
192  {
193  return GetComponentStyle(styleName.ToIdentifier());
194  }
195 
196  public static GUIComponentStyle GetComponentStyle(Identifier identifier)
197  => ComponentStyles.TryGet(identifier, out var style) ? style : null;
198 
199  public static void Apply(GUIComponent targetComponent, string styleName = "", GUIComponent parent = null)
200  {
201  Apply(targetComponent, styleName.ToIdentifier(), parent);
202  }
203 
204  public static void Apply(GUIComponent targetComponent, Identifier styleName, GUIComponent parent = null)
205  {
206  GUIComponentStyle componentStyle;
207  if (parent != null)
208  {
209  GUIComponentStyle parentStyle = parent.Style;
210 
211  if (parentStyle == null)
212  {
213  Identifier parentStyleName = ReflectionUtils.GetTypeNameWithoutGenericArity(parent.GetType());
214  if (!ComponentStyles.ContainsKey(parentStyleName))
215  {
216  DebugConsole.ThrowError($"Couldn't find a GUI style \"{parentStyleName}\"");
217  return;
218  }
219  parentStyle = ComponentStyles[parentStyleName];
220  }
221  Identifier childStyleName = styleName.IsEmpty ? ReflectionUtils.GetTypeNameWithoutGenericArity(targetComponent.GetType()) : styleName;
222  parentStyle.ChildStyles.TryGetValue(childStyleName, out componentStyle);
223  }
224  else
225  {
226  Identifier styleIdentifier = styleName.ToIdentifier();
227  if (styleIdentifier == Identifier.Empty)
228  {
229  styleIdentifier = ReflectionUtils.GetTypeNameWithoutGenericArity(targetComponent.GetType());
230  }
231  if (!ComponentStyles.ContainsKey(styleIdentifier))
232  {
233  DebugConsole.ThrowError($"Couldn't find a GUI style \"{styleIdentifier}\"");
234  return;
235  }
236  componentStyle = ComponentStyles[styleIdentifier];
237  }
238 
239  targetComponent.ApplyStyle(componentStyle);
240  }
241 
242  public static GUIColor GetQualityColor(int quality)
243  {
244  switch (quality)
245  {
246  case 1:
247  return ItemQualityColorGood;
248  case 2:
249  return ItemQualityColorExcellent;
250  case 3:
251  return ItemQualityColorMasterwork;
252  case -1:
253  return ItemQualityColorPoor;
254  default:
255  return ItemQualityColorNormal;
256  }
257  }
258 
259  public static void RecalculateFonts()
260  {
261  foreach (var font in Fonts.Values)
262  {
263  font.Prefabs.ForEach(p => p.LoadFont());
264  }
265  }
266 
267  public static void RecalculateSizeRestrictions()
268  {
269  foreach (var componentStyle in ComponentStyles)
270  {
271  componentStyle.RefreshSize();
272  }
273  }
274  }
275 }