Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Items/Components/Power/PowerTransfer.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Xml.Linq;
4 
6 {
7  partial class PowerTransfer : Powered
8  {
9  private GUITickBox powerIndicator;
10  private GUITickBox highVoltageIndicator;
11  private GUITickBox lowVoltageIndicator;
12 
13  private GUITextBlock powerLabel, loadLabel;
14 
15  private LanguageIdentifier prevLanguage;
16 
17  partial void InitProjectSpecific(XElement element)
18  {
19  if (GuiFrame == null) { return; }
20 
21  var paddedFrame = new GUIFrame(new RectTransform(GuiFrame.Rect.Size - GUIStyle.ItemFrameMargin, GuiFrame.RectTransform, Anchor.Center) { AbsoluteOffset = GUIStyle.ItemFrameOffset },
22  style: null)
23  {
24  CanBeFocused = false
25  };
26 
27  var lightsArea = new GUILayoutGroup(new RectTransform(new Vector2(0.4f, 1), paddedFrame.RectTransform, Anchor.CenterLeft))
28  {
29  Stretch = true
30  };
31  powerIndicator = new GUITickBox(new RectTransform(new Vector2(1, 0.33f), lightsArea.RectTransform),
32  TextManager.Get("PowerTransferPowered"), font: GUIStyle.SubHeadingFont, style: "IndicatorLightGreen")
33  {
34  CanBeFocused = false
35  };
36  highVoltageIndicator = new GUITickBox(new RectTransform(new Vector2(1, 0.33f), lightsArea.RectTransform),
37  TextManager.Get("PowerTransferHighVoltage"), font: GUIStyle.SubHeadingFont, style: "IndicatorLightRed")
38  {
39  ToolTip = TextManager.Get("PowerTransferTipOvervoltage"),
40  Enabled = false
41  };
42  lowVoltageIndicator = new GUITickBox(new RectTransform(new Vector2(1, 0.33f), lightsArea.RectTransform),
43  TextManager.Get("PowerTransferLowVoltage"), font: GUIStyle.SubHeadingFont, style: "IndicatorLightRed")
44  {
45  ToolTip = TextManager.Get("PowerTransferTipLowvoltage"),
46  Enabled = false
47  };
48  powerIndicator.TextBlock.OverrideTextColor(GUIStyle.TextColorNormal);
49  highVoltageIndicator.TextBlock.OverrideTextColor(GUIStyle.TextColorNormal);
50  lowVoltageIndicator.TextBlock.OverrideTextColor(GUIStyle.TextColorNormal);
51  GUITextBlock.AutoScaleAndNormalize(powerIndicator.TextBlock, highVoltageIndicator.TextBlock, lowVoltageIndicator.TextBlock);
52 
53  var textContainer = new GUIFrame(new RectTransform(new Vector2(0.58f, 1.0f), paddedFrame.RectTransform, Anchor.CenterRight), style: null);
54  var upperTextArea = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.5f), textContainer.RectTransform, Anchor.TopLeft), isHorizontal: true, childAnchor: Anchor.CenterLeft)
55  {
56  Stretch = true
57  };
58  var lowerTextArea = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.5f), textContainer.RectTransform, Anchor.BottomLeft), isHorizontal: true, childAnchor: Anchor.CenterLeft)
59  {
60  Stretch = true
61  };
62 
63  powerLabel = new GUITextBlock(new RectTransform(new Vector2(0.4f, 1), upperTextArea.RectTransform),
64  TextManager.Get("PowerTransferPowerLabel"), textColor: GUIStyle.TextColorBright, font: GUIStyle.LargeFont, textAlignment: Alignment.CenterRight)
65  {
66  ToolTip = TextManager.Get("PowerTransferTipPower")
67  };
68  loadLabel = new GUITextBlock(new RectTransform(new Vector2(0.4f, 1), lowerTextArea.RectTransform),
69  TextManager.Get("PowerTransferLoadLabel"), textColor: GUIStyle.TextColorBright, font: GUIStyle.LargeFont, textAlignment: Alignment.CenterRight)
70  {
71  ToolTip = TextManager.Get("PowerTransferTipLoad")
72  };
73 
74  var digitalBackground = new GUIFrame(new RectTransform(new Vector2(0.55f, 0.8f), upperTextArea.RectTransform), style: "DigitalFrameDark");
75  var powerText = new GUITextBlock(new RectTransform(new Vector2(0.9f, 0.95f), digitalBackground.RectTransform, Anchor.Center),
76  "", font: GUIStyle.DigitalFont, textColor: GUIStyle.TextColorDark)
77  {
78  TextAlignment = Alignment.CenterRight,
79  ToolTip = TextManager.Get("PowerTransferTipPower"),
80  TextGetter = () => {
81  float currPower = powerLoad < 0 ? -powerLoad: 0;
82  if (this is not RelayComponent && PowerConnections != null && PowerConnections.Count > 0 && PowerConnections[0].Grid != null)
83  {
84  currPower = PowerConnections[0].Grid.Power;
85  }
86  return ((int)Math.Round(currPower)).ToString();
87  }
88  };
89  var kw1 = new GUITextBlock(new RectTransform(new Vector2(0.15f, 0.5f), upperTextArea.RectTransform),
90  TextManager.Get("kilowatt"), textColor: GUIStyle.TextColorNormal, font: GUIStyle.Font)
91  {
92  Padding = Vector4.Zero,
93  TextAlignment = Alignment.BottomCenter
94  };
95 
96  digitalBackground = new GUIFrame(new RectTransform(new Vector2(0.55f, 0.8f), lowerTextArea.RectTransform), style: "DigitalFrameDark");
97  var loadText = new GUITextBlock(new RectTransform(new Vector2(0.9f, 0.95f), digitalBackground.RectTransform, Anchor.Center),
98  "", font: GUIStyle.DigitalFont, textColor: GUIStyle.TextColorDark)
99  {
100  TextAlignment = Alignment.CenterRight,
101  ToolTip = TextManager.Get("PowerTransferTipLoad"),
102  TextGetter = () =>
103  {
104  float load = PowerLoad;
105  if (this is RelayComponent relay)
106  {
107  load = relay.DisplayLoad;
108  }
109  else if (load < 0)
110  {
111  load = 0;
112  }
113  return ((int)Math.Round(load)).ToString();
114  }
115  };
116  var kw2 = new GUITextBlock(new RectTransform(new Vector2(0.15f, 0.5f), lowerTextArea.RectTransform),
117  TextManager.Get("kilowatt"), textColor: GUIStyle.TextColorNormal, font: GUIStyle.Font)
118  {
119  Padding = Vector4.Zero,
120  TextAlignment = Alignment.BottomCenter
121  };
122 
123  GUITextBlock.AutoScaleAndNormalize(powerLabel, loadLabel);
124  GUITextBlock.AutoScaleAndNormalize(true, true, powerText, loadText);
125  GUITextBlock.AutoScaleAndNormalize(kw1, kw2);
126 
127  prevLanguage = GameSettings.CurrentConfig.Language;
128  }
129 
130  public override void UpdateHUDComponentSpecific(Character character, float deltaTime, Camera cam)
131  {
132  if (GuiFrame == null) return;
133 
134  float voltage = (PowerConnections.Count > 0 && PowerConnections[0].Grid != null) ? PowerConnections[0].Grid.Voltage : 0f;
135  powerIndicator.Selected = IsActive && voltage > 0;
136  highVoltageIndicator.Selected = Timing.TotalTime % 0.5f < 0.25f && powerIndicator.Selected && voltage > 1.2f;
137  lowVoltageIndicator.Selected = Timing.TotalTime % 0.5f < 0.25f && powerIndicator.Selected && voltage < 0.8f;
138 
139  if (prevLanguage != GameSettings.CurrentConfig.Language)
140  {
141  GUITextBlock.AutoScaleAndNormalize(powerIndicator.TextBlock, highVoltageIndicator.TextBlock, lowVoltageIndicator.TextBlock);
142  GUITextBlock.AutoScaleAndNormalize(powerLabel, loadLabel);
143  prevLanguage = GameSettings.CurrentConfig.Language;
144  }
145  }
146  }
147 }
virtual Rectangle Rect
RectTransform RectTransform
static void AutoScaleAndNormalize(params GUITextBlock[] textBlocks)
Set the text scale of the GUITextBlocks so that they all use the same scale and can fit the text with...
override void UpdateHUDComponentSpecific(Character character, float deltaTime, Camera cam)