Client LuaCsForBarotrauma
HUDLayoutSettings.cs
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System;
4 using System.Linq;
5 
6 namespace Barotrauma
7 {
8  static class HUDLayoutSettings
9  {
10  public static bool DebugDraw;
11 
12  private static int inventoryTopY;
13  public static int InventoryTopY
14  {
15  get { return inventoryTopY; }
16  set
17  {
18  if (value == inventoryTopY) { return; }
19  inventoryTopY = value;
20  CreateAreas();
21  }
22  }
23 
24  public static Rectangle ButtonAreaTop
25  {
26  get; private set;
27  }
28  public static Rectangle TutorialObjectiveListArea
29  {
30  get; private set;
31  }
32 
33  public static Rectangle MessageAreaTop
34  {
35  get; private set;
36  }
37 
38  public static Rectangle CrewArea
39  {
40  get; private set;
41  }
42 
43  public static Rectangle ChatBoxArea
44  {
45  get; private set;
46  }
47 
48  public static Rectangle ObjectiveAnchor
49  {
50  get; private set;
51  }
52 
53  public static Rectangle InventoryAreaLower
54  {
55  get; private set;
56  }
57 
58  public static Rectangle HealthBarArea
59  {
60  get; private set;
61  }
62 
63  public static Rectangle BottomRightInfoArea
64  {
65  get; private set;
66  }
67 
68  public static Rectangle HealthBarAfflictionArea
69  {
70  get; private set;
71  }
72 
73  public static Rectangle HealthWindowAreaLeft
74  {
75  get; private set;
76  }
77 
78  public static Rectangle PortraitArea
79  {
80  get; private set;
81  }
82 
83  public static Rectangle VotingArea
84  {
85  get; private set;
86  }
87 
88  public static Rectangle ItemHUDArea
89  {
90  get; private set;
91  }
92 
93  public static int Padding
94  {
95  get; private set;
96  }
97 
98  static HUDLayoutSettings()
99  {
100  if (GameMain.Instance != null)
101  {
102  GameMain.Instance.ResolutionChanged += CreateAreas;
103  CreateAreas();
104  CharacterInfo.Init();
105  }
106  }
107 
108  public static RectTransform ToRectTransform(Rectangle rect, RectTransform parent)
109  {
110  return new RectTransform(new Vector2(rect.Width / (float)GameMain.GraphicsWidth, rect.Height / (float)GameMain.GraphicsHeight), parent)
111  {
112  RelativeOffset = new Vector2(rect.X / (float)GameMain.GraphicsWidth, rect.Y / (float)GameMain.GraphicsHeight)
113  };
114  }
115 
116  public static void CreateAreas()
117  {
118  Padding = (int)(11 * GUI.Scale);
119 
120  if (inventoryTopY == 0) { inventoryTopY = GameMain.GraphicsHeight - 30; }
121 
122  //slice from the top of the screen for misc buttons (info, end round, server controls)
123  ButtonAreaTop = new Rectangle(Padding, Padding, GameMain.GraphicsWidth - Padding * 2, (int)(50 * GUI.Scale));
124 
125  int infoAreaWidth = (int)(142 * GUI.Scale);
126  int infoAreaHeight = (int)(98 * GUI.Scale);
127  int portraitSize = (int)(infoAreaHeight * 0.95f);
128  BottomRightInfoArea = new Rectangle(GameMain.GraphicsWidth - Padding * 2 - infoAreaWidth, GameMain.GraphicsHeight - Padding * 2 - infoAreaHeight, infoAreaWidth, infoAreaHeight);
129  PortraitArea = new Rectangle(GameMain.GraphicsWidth - portraitSize, BottomRightInfoArea.Bottom - portraitSize + Padding / 2, portraitSize, portraitSize);
130 
131  //horizontal slices at the corners of the screen for health bar and affliction icons
132  int afflictionAreaHeight = (int)(50 * GUI.Scale);
133  int healthBarWidth = BottomRightInfoArea.Width;
134 
135  var healthBarChildStyles = GUIStyle.GetComponentStyle("CharacterHealthBar")?.ChildStyles;
136  if (healthBarChildStyles!= null && healthBarChildStyles.TryGetValue("GUIFrame".ToIdentifier(), out var style))
137  {
138  if (style.Sprites.TryGetValue(GUIComponent.ComponentState.None, out var uiSprites) && uiSprites.FirstOrDefault() is { } uiSprite)
139  {
140  // The default health bar uses a sliced sprite so let's make sure the health bar area is calculated accordingly
141  healthBarWidth += (int)(uiSprite.NonSliceSize.X * Math.Min(GUI.Scale, 1f));
142  }
143  }
144  int healthBarHeight = (int)(50f * GUI.Scale);
145  HealthBarArea = new Rectangle(BottomRightInfoArea.Right - healthBarWidth + (int)Math.Floor(1 / GUI.Scale), BottomRightInfoArea.Y - healthBarHeight + GUI.IntScale(10), healthBarWidth, healthBarHeight);
146  HealthBarAfflictionArea = new Rectangle(HealthBarArea.X, HealthBarArea.Y - Padding - afflictionAreaHeight, HealthBarArea.Width, afflictionAreaHeight);
147 
148 
149  int messageAreaWidth = GameMain.GraphicsWidth / 3;
150  MessageAreaTop = new Rectangle((GameMain.GraphicsWidth - messageAreaWidth) / 2, ButtonAreaTop.Bottom + ButtonAreaTop.Height, messageAreaWidth, ButtonAreaTop.Height);
151 
152  int chatBoxWidth = (int)(475 * GUI.Scale * GUI.AspectRatioAdjustment);
153  int chatBoxHeight = (int)Math.Max(GameMain.GraphicsHeight * 0.25f, 150);
154  ChatBoxArea = new Rectangle(Padding, GameMain.GraphicsHeight - Padding - chatBoxHeight, chatBoxWidth, chatBoxHeight);
155 
156  int objectiveAnchorWidth = (int)(250 * GUI.Scale);
157  int objectiveAnchorOffsetY = (int)(150 * GUI.Scale);
158  ObjectiveAnchor = new Rectangle(Padding, ChatBoxArea.Y - objectiveAnchorOffsetY, objectiveAnchorWidth, 0);
159 
160  int crewAreaY = ButtonAreaTop.Bottom + Padding;
161  int crewAreaHeight = ObjectiveAnchor.Top - Padding - crewAreaY;
162 
163  CrewArea = new Rectangle(Padding, crewAreaY,
164  (int)MathHelper.Clamp(400 * GUI.Scale, 220, GameMain.GraphicsHeight * 0.4f),
165  crewAreaHeight);
166  InventoryAreaLower = new Rectangle(ChatBoxArea.Right + Padding * 7, inventoryTopY, GameMain.GraphicsWidth - Padding * 9 - ChatBoxArea.Width, GameMain.GraphicsHeight - inventoryTopY);
167 
168  int healthWindowWidth = (int)(GameMain.GraphicsWidth * 0.5f);
169  int healthWindowHeight = (int)(GameMain.GraphicsWidth * 0.5f * 0.65f);
170  int healthWindowX = GameMain.GraphicsWidth / 2 - healthWindowWidth / 2;
171  int healthWindowY = GameMain.GraphicsHeight / 2 - healthWindowHeight / 2;
172 
173  HealthWindowAreaLeft = new Rectangle(healthWindowX, healthWindowY, healthWindowWidth, healthWindowHeight);
174 
175  int objectiveListAreaX = HealthWindowAreaLeft.Right + Padding;
176  int objectiveListAreaY = ButtonAreaTop.Bottom + Padding;
177  TutorialObjectiveListArea = new Rectangle(objectiveListAreaX, objectiveListAreaY, (GameMain.GraphicsWidth - Padding) - objectiveListAreaX, (HealthBarAfflictionArea.Top - Padding) - objectiveListAreaY);
178 
179  int votingAreaWidth = (int)(400 * GUI.Scale);
180  int votingAreaX = GameMain.GraphicsWidth - Padding - votingAreaWidth;
181  int votingAreaY = Padding + ButtonAreaTop.Height;
182 
183  // Height is based on text content
184  VotingArea = new Rectangle(votingAreaX, votingAreaY, votingAreaWidth, 0);
185 
186  ItemHUDArea = new Rectangle(0, ButtonAreaTop.Bottom, GameMain.GraphicsWidth, GameMain.GraphicsHeight - ButtonAreaTop.Bottom - InventoryAreaLower.Height);
187  }
188 
189  public static void Draw(SpriteBatch spriteBatch)
190  {
191  DrawRectangle(nameof(ButtonAreaTop), ButtonAreaTop, Color.White * 0.5f);
192  DrawRectangle(nameof(TutorialObjectiveListArea), TutorialObjectiveListArea, GUIStyle.Blue * 0.5f);
193  DrawRectangle(nameof(MessageAreaTop), MessageAreaTop, GUIStyle.Orange * 0.5f);
194  DrawRectangle(nameof(CrewArea), CrewArea, Color.Blue * 0.5f);
195  DrawRectangle(nameof(ChatBoxArea), ChatBoxArea, Color.Cyan * 0.5f);
196  DrawRectangle(nameof(HealthBarArea), HealthBarArea, Color.Red * 0.5f);
197  DrawRectangle(nameof(HealthBarAfflictionArea), HealthBarAfflictionArea, Color.Red * 0.5f);
198  DrawRectangle(nameof(InventoryAreaLower), InventoryAreaLower, Color.Yellow * 0.5f);
199  DrawRectangle(nameof(HealthWindowAreaLeft), HealthWindowAreaLeft, Color.Red * 0.5f);
200  DrawRectangle(nameof(BottomRightInfoArea), BottomRightInfoArea, Color.Green * 0.5f);
201  DrawRectangle(nameof(ItemHUDArea), ItemHUDArea, Color.Magenta * 0.3f);
202 
203  void DrawRectangle(string label, Rectangle r, Color c)
204  {
205  if (!label.IsNullOrEmpty())
206  {
207  GUI.DrawString(spriteBatch, r.Location.ToVector2() + Vector2.One * 3, label, c, font: GUIStyle.SmallFont);
208  }
209  GUI.DrawRectangle(spriteBatch, r, c);
210  }
211  }
212  }
213 
214  public static class HUD
215  {
216  public static bool CloseHUD(Rectangle rect)
217  {
218  // Always close when hitting escape
219  if (PlayerInput.KeyHit(Microsoft.Xna.Framework.Input.Keys.Escape)) { return true; }
220 
221  //don't close when the cursor is on a UI element
222  if (GUI.MouseOn != null) { return false; }
223 
224  //don't close when hovering over an inventory element
225  if (Inventory.IsMouseOnInventory) { return false; }
226 
227  bool input = PlayerInput.PrimaryMouseButtonDown() || PlayerInput.SecondaryMouseButtonClicked();
228  return input && !rect.Contains(PlayerInput.MousePosition);
229  }
230  }
231 }