Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Items/Components/Power/PowerContainer.cs
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using System;
5 
7 {
8  partial class PowerContainer : Powered, IDrawableComponent, IServerSerializable, IClientSerializable
9  {
10  private GUIProgressBar chargeIndicator;
11  private GUIScrollBar rechargeSpeedSlider;
12 
13  [Serialize(0.0f, IsPropertySaveable.Yes)]
14  public float RechargeWarningIndicatorLow { get; set; }
15 
16  [Serialize(0.0f, IsPropertySaveable.Yes)]
17  public float RechargeWarningIndicatorHigh { get; set; }
18 
19  public Vector2 DrawSize
20  {
21  //use the extents of the item as the draw size
22  get { return Vector2.Zero; }
23  }
24 
25  partial void InitProjSpecific()
26  {
27  if (GuiFrame == null) { return; }
28 
29  var paddedFrame = new GUIFrame(new RectTransform(new Vector2(0.75f, 0.75f), GuiFrame.RectTransform, Anchor.Center)
30  {
31  //RelativeOffset = new Vector2(0, 0.05f)
32  }, style: null);
33 
34  var upperArea = new GUIFrame(new RectTransform(new Vector2(1, 0.4f), paddedFrame.RectTransform, Anchor.TopCenter), style: null);
35  var lowerArea = new GUIFrame(new RectTransform(new Vector2(1, 0.6f), paddedFrame.RectTransform, Anchor.BottomCenter), style: null);
36 
37  var rechargeRateContainer = new GUIFrame(new RectTransform(new Vector2(1, 0.4f), upperArea.RectTransform), style: null);
38  var rechargeLabel = new GUITextBlock(new RectTransform(new Vector2(0.4f, 0.0f), rechargeRateContainer.RectTransform, Anchor.CenterLeft),
39  TextManager.Get("rechargerate"), textColor: GUIStyle.TextColorNormal, font: GUIStyle.SubHeadingFont, textAlignment: Alignment.CenterLeft);
40  LocalizedString kW = TextManager.Get("kilowatt");
41  var rechargeText = new GUITextBlock(new RectTransform(new Vector2(0.6f, 1), rechargeRateContainer.RectTransform, Anchor.CenterRight),
42  "", textColor: GUIStyle.TextColorNormal, font: GUIStyle.Font, textAlignment: Alignment.CenterRight)
43  {
44  TextGetter = () => $"{(int)MathF.Round(currPowerConsumption)} {kW} ({(int)MathF.Round(RechargeRatio * 100)} %)"
45  };
46  if (rechargeText.TextSize.X > rechargeText.Rect.Width) { rechargeText.Font = GUIStyle.SmallFont; }
47 
48  var rechargeSliderContainer = new GUIFrame(new RectTransform(new Vector2(0.9f, 0.4f), upperArea.RectTransform, Anchor.BottomCenter));
49 
51  {
52  var rechargeSliderFill = new GUICustomComponent(new RectTransform(new Vector2(0.95f, 0.9f), rechargeSliderContainer.RectTransform, Anchor.Center), (SpriteBatch sb, GUICustomComponent c) =>
53  {
54  if (RechargeWarningIndicatorLow > 0.0f)
55  {
56  float warningLow = c.Rect.Width * RechargeWarningIndicatorLow;
57  GUI.DrawRectangle(sb, new Vector2(c.Rect.X + warningLow, c.Rect.Y), new Vector2(c.Rect.Width - warningLow, c.Rect.Height), GUIStyle.Orange, isFilled: true);
58  }
60  {
61  float warningHigh = c.Rect.Width * RechargeWarningIndicatorHigh;
62  GUI.DrawRectangle(sb, new Vector2(c.Rect.X + warningHigh, c.Rect.Y), new Vector2(c.Rect.Width - warningHigh, c.Rect.Height), GUIStyle.Red, isFilled: true);
63  }
64  });
65  }
66 
67  rechargeSpeedSlider = new GUIScrollBar(new RectTransform(Vector2.One, rechargeSliderContainer.RectTransform, Anchor.Center),
68  barSize: 0.15f, style: "DeviceSliderSeeThrough")
69  {
70  Step = 0.1f,
71  OnMoved = (GUIScrollBar scrollBar, float barScroll) =>
72  {
73  float newRechargeSpeed = maxRechargeSpeed * barScroll;
74  if (Math.Abs(newRechargeSpeed - rechargeSpeed) < 0.1f) { return false; }
75 
76  RechargeSpeed = newRechargeSpeed;
77  if (GameMain.Client != null)
78  {
79  item.CreateClientEvent(this);
81  }
82  return true;
83  }
84  };
85  rechargeSpeedSlider.Bar.RectTransform.MaxSize = new Point(rechargeSpeedSlider.Bar.Rect.Height);
86  rechargeSpeedSlider.Frame.UserData = UIHighlightAction.ElementId.RechargeSpeedSlider;
87 
88  // lower area --------------------------
89 
90  var chargeTextContainer = new GUIFrame(new RectTransform(new Vector2(1, 0.4f), lowerArea.RectTransform), style: null);
91  var chargeLabel = new GUITextBlock(new RectTransform(new Vector2(0.4f, 0.0f), chargeTextContainer.RectTransform, Anchor.CenterLeft),
92  TextManager.Get("charge"), textColor: GUIStyle.TextColorNormal, font: GUIStyle.SubHeadingFont, textAlignment: Alignment.CenterLeft)
93  {
94  ToolTip = TextManager.Get("PowerTransferTipPower")
95  };
96  LocalizedString kWmin = TextManager.Get("kilowattminute");
97  var chargeText = new GUITextBlock(new RectTransform(new Vector2(0.6f, 1), chargeTextContainer.RectTransform, Anchor.CenterRight),
98  "", textColor: GUIStyle.TextColorNormal, font: GUIStyle.Font, textAlignment: Alignment.CenterRight)
99  {
100  TextGetter = () => $"{(int)MathF.Round(charge)}/{(int)adjustedCapacity} {kWmin} ({(int)MathF.Round(MathUtils.Percentage(charge, adjustedCapacity))} %)"
101  };
102  if (chargeText.TextSize.X > chargeText.Rect.Width) { chargeText.Font = GUIStyle.SmallFont; }
103 
104  chargeIndicator = new GUIProgressBar(new RectTransform(new Vector2(1.1f, 0.5f), lowerArea.RectTransform, Anchor.BottomCenter)
105  {
106  RelativeOffset = new Vector2(0, 0.1f)
107  }, barSize: 0.0f, style: "DeviceProgressBar")
108  {
109  ProgressGetter = () =>
110  {
111  return adjustedCapacity <= 0.0f ? 1.0f : charge / adjustedCapacity;
112  }
113  };
114  }
115 
116  public override void OnItemLoaded()
117  {
118  base.OnItemLoaded();
119  if (rechargeSpeedSlider != null)
120  {
121  rechargeSpeedSlider.BarScroll = rechargeSpeed / MaxRechargeSpeed;
122  }
123  }
124 
125  public override void UpdateHUDComponentSpecific(Character character, float deltaTime, Camera cam)
126  {
127  if (chargeIndicator != null)
128  {
129  float chargeRatio = charge / adjustedCapacity;
130  chargeIndicator.Color = ToolBox.GradientLerp(chargeRatio, Color.Red, Color.Orange, Color.Green);
131  }
132  }
133 
134  public void Draw(SpriteBatch spriteBatch, bool editing = false, float itemDepth = -1, Color? overrideColor = null)
135  {
136  Vector2 scaledIndicatorSize = indicatorSize * item.Scale;
137  if (scaledIndicatorSize.X <= 2.0f || scaledIndicatorSize.Y <= 2.0f) { return; }
138 
139  const float outlineThickness = 1.0f;
140  Vector2 itemSize = new Vector2(item.Sprite.SourceRect.Width, item.Sprite.SourceRect.Height) * item.Scale;
141  Vector2 indicatorPos = -itemSize / 2.0f + indicatorPosition * item.Scale;
142  Vector2 itemPosition = new Vector2(item.DrawPosition.X, -item.DrawPosition.Y);
143  Vector2 flip = new Vector2(item.FlippedX && item.Prefab.CanSpriteFlipX ? -1.0f : 1.0f, item.FlippedY && item.Prefab.CanSpriteFlipY ? -1.0f : 1.0f);
144  Matrix rotate = Matrix.CreateRotationZ(item.RotationRad);
145  Vector2 center = Vector2.Transform((indicatorPos + (scaledIndicatorSize * 0.5f)) * flip, rotate) + itemPosition;
146 
147  if (charge > 0 && adjustedCapacity > 0)
148  {
149  float chargeRatio = MathHelper.Clamp(charge / adjustedCapacity, 0.0f, 1.0f);
150  Color indicatorColor = ToolBox.GradientLerp(chargeRatio, Color.Red, Color.Orange, Color.Green);
151  Vector2 indicatorCenter = (indicatorPos + (scaledIndicatorSize * 0.5f)) * flip;
152  Vector2 indicatorSize;
153 
154  if (isHorizontal)
155  {
156  float indicatorLength = (scaledIndicatorSize.X - outlineThickness * 2.0f) * chargeRatio;
157  indicatorCenter.X += -scaledIndicatorSize.X * 0.5f + (flipIndicator ? scaledIndicatorSize.X - outlineThickness - indicatorLength * 0.5f : outlineThickness + indicatorLength * 0.5f);
158  indicatorSize = new Vector2(indicatorLength, scaledIndicatorSize.Y);
159  }
160  else
161  {
162  float indicatorLength = (scaledIndicatorSize.Y - outlineThickness * 2.0f) * chargeRatio;
163  indicatorCenter.Y += -scaledIndicatorSize.Y * 0.5f + (flipIndicator ? outlineThickness + indicatorLength * 0.5f : scaledIndicatorSize.Y - outlineThickness - indicatorLength * 0.5f);
164  indicatorSize = new Vector2(scaledIndicatorSize.X, indicatorLength);
165  }
166 
167  indicatorCenter = Vector2.Transform(indicatorCenter, rotate) + itemPosition;
168 
169  GUI.DrawFilledRectangle(spriteBatch, indicatorCenter, indicatorSize, indicatorSize * 0.5f, item.RotationRad, indicatorColor, item.SpriteDepth - 0.00001f);
170  }
171 
172  GUI.DrawRectangle(spriteBatch, center, scaledIndicatorSize, scaledIndicatorSize * 0.5f, item.RotationRad, Color.Black, item.SpriteDepth - 0.000015f, outlineThickness, GUI.OutlinePosition.Inside);
173  }
174 
176  {
177  msg.WriteRangedInteger((int)(rechargeSpeed / MaxRechargeSpeed * 10), 0, 10);
178  }
179 
180  public void ClientEventRead(IReadMessage msg, float sendingTime)
181  {
182  if (correctionTimer > 0.0f)
183  {
184  StartDelayedCorrection(msg.ExtractBits(4 + 8), sendingTime);
185  return;
186  }
187 
188  float rechargeRate = msg.ReadRangedInteger(0, 10) / 10.0f;
189  RechargeSpeed = rechargeRate * MaxRechargeSpeed;
190 #if CLIENT
191  if (rechargeSpeedSlider != null)
192  {
193  rechargeSpeedSlider.BarScroll = rechargeRate;
194  }
195 #endif
196  Charge = msg.ReadRangedSingle(0.0f, 1.0f, 8) * adjustedCapacity;
197  }
198  }
199 }
RectTransform RectTransform
override GUIFont Font
Definition: GUITextBlock.cs:66
override void UpdateHUDComponentSpecific(Character character, float deltaTime, Camera cam)
void Draw(SpriteBatch spriteBatch, bool editing=false, float itemDepth=-1, Color? overrideColor=null)
override void OnItemLoaded()
Called when all the components of the item have been loaded. Use to initialize connections between co...
Highlights an UI element of some kind. Generally used in tutorials.
Interface for entities that the clients can send events to the server
int ReadRangedInteger(int min, int max)
Single ReadRangedSingle(Single min, Single max, int bitCount)
Interface for entities that the server can send events to the clients
void WriteRangedInteger(int val, int min, int max)