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 Radiation = new GUISprite("Radiation");
56  public readonly static GUISpriteSheet RadiationAnimSpriteSheet = new GUISpriteSheet("RadiationAnimSpriteSheet");
57 
58  public readonly static GUISpriteSheet SavingIndicator = new GUISpriteSheet("SavingIndicator");
59  public readonly static GUISpriteSheet GenericThrobber = new GUISpriteSheet("GenericThrobber");
60 
61  public readonly static GUISprite UIGlow = new GUISprite("UIGlow");
62  public readonly static GUISprite TalentGlow = new GUISprite("TalentGlow");
63  public readonly static GUISprite PingCircle = new GUISprite("PingCircle");
64  public readonly static GUISprite UIGlowCircular = new GUISprite("UIGlowCircular");
65  public readonly static GUISprite UIGlowSolidCircular = new GUISprite("UIGlowSolidCircular");
66  public readonly static GUISprite UIThermalGlow = new GUISprite("UIGlowSolidCircular");
67  public readonly static GUISprite ButtonPulse = new GUISprite("ButtonPulse");
68  public readonly static GUISprite WalletPortraitBG = new GUISprite("WalletPortraitBG");
69  public readonly static GUISprite CrewWalletIconSmall = new GUISprite("CrewWalletIconSmall");
70 
71  public readonly static GUISprite EndRoundButtonPulse = new GUISprite("EndRoundButtonPulse");
72 
73  public readonly static GUISpriteSheet FocusIndicator = new GUISpriteSheet("FocusIndicator");
74 
75  public readonly static GUISprite IconOverflowIndicator = new GUISprite("IconOverflowIndicator");
76 
80  public readonly static GUIColor Green = new GUIColor("Green", new Color(154, 213, 163, 255));
81 
85  public readonly static GUIColor Orange = new GUIColor("Orange", new Color(243, 162, 50, 255));
86 
90  public readonly static GUIColor Red = new GUIColor("Red", new Color(245, 105, 105, 255));
91 
95  public readonly static GUIColor Blue = new GUIColor("Blue", new Color(126, 211, 224, 255));
96 
100  public readonly static GUIColor Yellow = new GUIColor("Yellow", new Color(255, 255, 0, 255));
101 
105  public readonly static GUIColor ModdedServerColor = new GUIColor("ModdedServerColor", new Color(154, 185, 160, 255));
106 
107  public readonly static GUIColor ColorInventoryEmpty = new GUIColor("ColorInventoryEmpty", new Color(245, 105, 105, 255));
108  public readonly static GUIColor ColorInventoryHalf = new GUIColor("ColorInventoryHalf", new Color(243, 162, 50, 255));
109  public readonly static GUIColor ColorInventoryFull = new GUIColor("ColorInventoryFull", new Color(96, 222, 146, 255));
110  public readonly static GUIColor ColorInventoryBackground = new GUIColor("ColorInventoryBackground", new Color(56, 56, 56, 255));
111  public readonly static GUIColor ColorInventoryEmptyOverlay = new GUIColor("ColorInventoryEmptyOverlay", new Color(125, 125, 125, 255));
112 
113  public readonly static GUIColor TextColorNormal = new GUIColor("TextColorNormal", new Color(228, 217, 167, 255));
114  public readonly static GUIColor TextColorBright = new GUIColor("TextColorBright", new Color(255, 255, 255, 255));
115  public readonly static GUIColor TextColorDark = new GUIColor("TextColorDark", new Color(0, 0, 0, 230));
116  public readonly static GUIColor TextColorDim = new GUIColor("TextColorDim", new Color(153, 153, 153, 153));
117 
118  public readonly static GUIColor ItemQualityColorPoor = new GUIColor("ItemQualityColorPoor", new Color(128, 128, 128, 255));
119  public readonly static GUIColor ItemQualityColorNormal = new GUIColor("ItemQualityColorNormal", new Color(255, 255, 255, 255));
120  public readonly static GUIColor ItemQualityColorGood = new GUIColor("ItemQualityColorGood", new Color(144, 238, 144, 255));
121  public readonly static GUIColor ItemQualityColorExcellent = new GUIColor("ItemQualityColorExcellent", new Color(173, 216, 230, 255));
122  public readonly static GUIColor ItemQualityColorMasterwork = new GUIColor("ItemQualityColorMasterwork", new Color(147, 112, 219, 255));
123 
124  public readonly static GUIColor ColorReputationVeryLow = new GUIColor("ColorReputationVeryLow", new Color(192, 60, 60, 255));
125  public readonly static GUIColor ColorReputationLow = new GUIColor("ColorReputationLow", new Color(203, 145, 23, 255));
126  public readonly static GUIColor ColorReputationNeutral = new GUIColor("ColorReputationNeutral", new Color(228, 217, 167, 255));
127  public readonly static GUIColor ColorReputationHigh = new GUIColor("ColorReputationHigh", new Color(51, 152, 64, 255));
128  public readonly static GUIColor ColorReputationVeryHigh = new GUIColor("ColorReputationVeryHigh", new Color(71, 160, 164, 255));
129 
130  public readonly static GUIColor InteractionLabelColor = new GUIColor("InteractionLabelColor", new Color(255, 255, 255, 255));
131  public readonly static GUIColor InteractionLabelHoverColor = new GUIColor("InteractionLabelHoverColor", new Color(0, 255, 255, 255));
132 
133  // Inventory
134  public readonly static GUIColor EquipmentSlotIconColor = new GUIColor("EquipmentSlotIconColor", new Color(99, 70, 64, 255));
135 
136  // Health HUD
137  public readonly static GUIColor BuffColorLow = new GUIColor("BuffColorLow", new Color(66, 170, 73, 255));
138  public readonly static GUIColor BuffColorMedium = new GUIColor("BuffColorMedium", new Color(110, 168, 118, 255));
139  public readonly static GUIColor BuffColorHigh = new GUIColor("BuffColorHigh", new Color(154, 213, 163, 255));
140 
141  public readonly static GUIColor DebuffColorLow = new GUIColor("DebuffColorLow", new Color(243, 162, 50, 255));
142  public readonly static GUIColor DebuffColorMedium = new GUIColor("DebuffColorMedium", new Color(155, 55, 55, 255));
143  public readonly static GUIColor DebuffColorHigh = new GUIColor("DebuffColorHigh", new Color(228, 27, 27, 255));
144 
145  public readonly static GUIColor HealthBarColorLow = new GUIColor("HealthBarColorLow", new Color(255, 0, 0, 255));
146  public readonly static GUIColor HealthBarColorMedium = new GUIColor("HealthBarColorMedium", new Color(255, 165, 0, 255));
147  public readonly static GUIColor HealthBarColorHigh = new GUIColor("HealthBarColorHigh", new Color(78, 114, 88));
148  public readonly static GUIColor HealthBarColorPoisoned = new GUIColor("HealthBarColorPoisoned", new Color(100, 150, 0, 255));
149 
150  private readonly static Point defaultItemFrameMargin = new Point(50, 56);
151 
152  public static Point ItemFrameMargin
153  {
154  get
155  {
156  Point size = defaultItemFrameMargin.Multiply(GUI.SlicedSpriteScale);
157 
158  var style = GetComponentStyle("ItemUI");
159  var sprite = style?.Sprites[GUIComponent.ComponentState.None].First();
160  if (sprite != null)
161  {
162  size.X = Math.Min(sprite.Slices[0].Width + sprite.Slices[2].Width, size.X);
163  size.Y = Math.Min(sprite.Slices[0].Height + sprite.Slices[6].Height, size.Y);
164  }
165  return size;
166  }
167  }
168 
169  public static int ItemFrameTopBarHeight
170  {
171  get
172  {
173  var style = GetComponentStyle("ItemUI");
174  var sprite = style?.Sprites[GUIComponent.ComponentState.None].First();
175  return (int)Math.Min(sprite?.Slices[0].Height ?? 0, defaultItemFrameMargin.Y / 2 * GUI.SlicedSpriteScale);
176  }
177  }
178 
179  public static Point ItemFrameOffset => new Point(0, 3).Multiply(GUI.SlicedSpriteScale);
180 
181  public static GUIComponentStyle GetComponentStyle(string styleName)
182  {
183  return GetComponentStyle(styleName.ToIdentifier());
184  }
185 
186  public static GUIComponentStyle GetComponentStyle(Identifier identifier)
187  => ComponentStyles.TryGet(identifier, out var style) ? style : null;
188 
189  public static void Apply(GUIComponent targetComponent, string styleName = "", GUIComponent parent = null)
190  {
191  Apply(targetComponent, styleName.ToIdentifier(), parent);
192  }
193 
194  public static void Apply(GUIComponent targetComponent, Identifier styleName, GUIComponent parent = null)
195  {
196  GUIComponentStyle componentStyle;
197  if (parent != null)
198  {
199  GUIComponentStyle parentStyle = parent.Style;
200 
201  if (parentStyle == null)
202  {
203  Identifier parentStyleName = ReflectionUtils.GetTypeNameWithoutGenericArity(parent.GetType());
204  if (!ComponentStyles.ContainsKey(parentStyleName))
205  {
206  DebugConsole.ThrowError($"Couldn't find a GUI style \"{parentStyleName}\"");
207  return;
208  }
209  parentStyle = ComponentStyles[parentStyleName];
210  }
211  Identifier childStyleName = styleName.IsEmpty ? ReflectionUtils.GetTypeNameWithoutGenericArity(targetComponent.GetType()) : styleName;
212  parentStyle.ChildStyles.TryGetValue(childStyleName, out componentStyle);
213  }
214  else
215  {
216  Identifier styleIdentifier = styleName.ToIdentifier();
217  if (styleIdentifier == Identifier.Empty)
218  {
219  styleIdentifier = ReflectionUtils.GetTypeNameWithoutGenericArity(targetComponent.GetType());
220  }
221  if (!ComponentStyles.ContainsKey(styleIdentifier))
222  {
223  DebugConsole.ThrowError($"Couldn't find a GUI style \"{styleIdentifier}\"");
224  return;
225  }
226  componentStyle = ComponentStyles[styleIdentifier];
227  }
228 
229  targetComponent.ApplyStyle(componentStyle);
230  }
231 
232  public static GUIColor GetQualityColor(int quality)
233  {
234  switch (quality)
235  {
236  case 1:
237  return ItemQualityColorGood;
238  case 2:
239  return ItemQualityColorExcellent;
240  case 3:
241  return ItemQualityColorMasterwork;
242  case -1:
243  return ItemQualityColorPoor;
244  default:
245  return ItemQualityColorNormal;
246  }
247  }
248 
249  public static void RecalculateFonts()
250  {
251  foreach (var font in Fonts.Values)
252  {
253  font.Prefabs.ForEach(p => p.LoadFont());
254  }
255  }
256 
257  public static void RecalculateSizeRestrictions()
258  {
259  foreach (var componentStyle in ComponentStyles)
260  {
261  componentStyle.RefreshSize();
262  }
263  }
264  }
265 }