Client LuaCsForBarotrauma
HUDProgressBar.cs
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System;
4 
5 namespace Barotrauma
6 {
8  {
9  private float progress;
10 
11  public float Progress
12  {
13  get { return progress; }
14  set { progress = MathHelper.Clamp(value, 0.0f, 1.0f); }
15  }
16 
17  public float FadeTimer;
18 
19  private Color fullColor, emptyColor;
20 
21  private Vector2 worldPosition;
22 
23  public Vector2 WorldPosition
24  {
25  get
26  {
27  return worldPosition;
28  }
29  set
30  {
31  worldPosition = value;
32  if (parentSub != null)
33  {
34  worldPosition -= parentSub.DrawPosition;
35  }
36  }
37  }
38 
39  public Vector2 Size;
40 
41  private readonly Submarine parentSub;
43  {
44  get;
45  private set;
46  }
47 
48  private string textTag;
49  public string TextTag
50  {
51  get { return textTag; }
52  set
53  {
54  if (textTag == value) { return; }
55  textTag = value;
56  Text = string.IsNullOrEmpty(textTag) ? string.Empty : TextManager.Get(textTag);
57  }
58  }
59 
60  public HUDProgressBar(Vector2 worldPosition, string textTag, Submarine parentSubmarine = null)
61  : this(worldPosition, parentSubmarine, GUIStyle.Red, GUIStyle.Green, textTag)
62  {
63  }
64 
65  public HUDProgressBar(Vector2 worldPosition, Submarine parentSubmarine, Color emptyColor, Color fullColor, string textTag)
66  {
67  this.emptyColor = emptyColor;
68  this.fullColor = fullColor;
69  parentSub = parentSubmarine;
70  WorldPosition = worldPosition;
71  Size = new Vector2(100.0f, 20.0f);
72  FadeTimer = 1.0f;
73  if (!string.IsNullOrEmpty(textTag))
74  {
75  this.textTag = textTag;
76  Text = TextManager.Get(textTag).Fallback(textTag);
77  }
78  }
79 
80  public void Update(float deltatime)
81  {
82  FadeTimer -= deltatime;
83  }
84 
85  public void Draw(SpriteBatch spriteBatch, Camera cam)
86  {
87  float a = Math.Min(FadeTimer, 1.0f);
88 
89  Vector2 pos = new Vector2(WorldPosition.X - Size.X / 2, WorldPosition.Y + Size.Y / 2);
90 
91  if (parentSub != null)
92  {
93  pos += parentSub.DrawPosition;
94  }
95 
96  pos = cam.WorldToScreen(pos);
97  Color color = Color.Lerp(emptyColor, fullColor, progress);
98  GUI.DrawProgressBar(spriteBatch,
99  new Vector2(pos.X, -pos.Y),
100  Size, progress,
101  color * a,
102  Color.White * a * 0.8f);
103 
104  if (!Text.IsNullOrEmpty())
105  {
106  Vector2 textSize = GUIStyle.SmallFont.MeasureString(Text);
107  Vector2 textPos = new Vector2(pos.X + (Size.X - textSize.X) / 2, pos.Y - textSize.Y * 1.2f);
108  GUI.DrawString(spriteBatch, textPos - Vector2.One, Text, Color.Black * a, font: GUIStyle.SmallFont);
109  GUI.DrawString(spriteBatch, textPos, Text, Color.White * a, font: GUIStyle.SmallFont);
110  }
111 
112  }
113  }
114 }
Vector2 WorldToScreen(Vector2 coords)
Definition: Camera.cs:416
HUDProgressBar(Vector2 worldPosition, Submarine parentSubmarine, Color emptyColor, Color fullColor, string textTag)
void Draw(SpriteBatch spriteBatch, Camera cam)
void Update(float deltatime)
HUDProgressBar(Vector2 worldPosition, string textTag, Submarine parentSubmarine=null)
LocalizedString Fallback(LocalizedString fallback, bool useDefaultLanguageIfFound=true)
Use this text instead if the original text cannot be found.