Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/CircuitBox/CircuitBoxConnection.cs
1 #nullable enable
2 
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
6 
7 namespace Barotrauma
8 {
9  internal abstract partial class CircuitBoxConnection
10  {
11  public string Name => Connection.Name;
12 
13  public CircuitBoxLabel Label { get; private set; }
14 
15  private Sprite? knobSprite,
16  screwSprite,
17  connectorSprite;
18 
19  private static int Padding => GUI.IntScale(8);
20 
21  private Option<LocalizedString> tooltip = Option.None;
22 
23  private partial void InitProjSpecific(CircuitBox circuitBox)
24  {
25  Label = new CircuitBoxLabel(Connection.DisplayName, GUIStyle.SubHeadingFont);
26  knobSprite = circuitBox.ConnectionSprite;
27  screwSprite = circuitBox.ConnectionScrewSprite;
28  connectorSprite = circuitBox.WireConnectorSprite;
29  Length = Rect.Width + Padding + Label.Size.X;
30  }
31 
32  public void SetLabel(LocalizedString label, CircuitBoxNode node)
33  {
34  Label = new CircuitBoxLabel(label, GUIStyle.SubHeadingFont);
35  Length = Rect.Width + Padding + Label.Size.X;
36  }
37 
38  public void Draw(SpriteBatch spriteBatch, Vector2 drawPos, Vector2 parentPos, Color color)
39  {
40  if (CircuitBox.UI is not { } circuitBoxUi) { return; }
41  var drawRect = CircuitBoxNode.OverrideRectLocation(Rect, drawPos, parentPos);
42 
43  Vector2 cursorPos = circuitBoxUi.GetCursorPosition();
44  cursorPos.Y = -cursorPos.Y;
45 
46  bool isMouseOver = drawRect.Contains(cursorPos);
47 
48  float xPos;
49  if (IsOutput)
50  {
51  xPos = drawRect.Left - Padding - Label.Size.X;
52  }
53  else
54  {
55  xPos = drawRect.Right + Padding;
56  }
57 
58  Vector2 stringPos = new Vector2(xPos, drawRect.Center.Y - Label.Size.Y / 2f);
59  GUI.DrawString(spriteBatch, stringPos, Label.Value, GUIStyle.TextColorNormal, font: Label.Font);
60 
61  if (knobSprite is null)
62  {
63  CircuitBoxUI.DrawRectangleWithBorder(spriteBatch, drawRect, GUIStyle.Blue * 0.3f, GUIStyle.Blue);
64  }
65  else
66  {
67  float scale = drawRect.Height / knobSprite.size.Y;
68  knobSprite?.Draw(spriteBatch, drawRect.Center, color, 0f, scale);
69  }
70 
71  bool isScrewed = this switch
72  {
73  CircuitBoxOutputConnection output => output.ExternallyConnectedFrom.Count > 0,
74  CircuitBoxInputConnection input => input.ExternallyConnectedTo.Count > 0,
75  _ => Connection.Wires.Count > 0 ||
77  ExternallyConnectedFrom.Count > 0
78  };
79 
80  if (isMouseOver)
81  {
82  var glowSprite = GUIStyle.UIGlowCircular.Value?.Sprite;
83  if (glowSprite is not null)
84  {
85  float glowScale = 40f / glowSprite.size.X;
86  if (isScrewed)
87  {
88  glowScale *= 1.2f;
89  }
90  glowSprite.Draw(spriteBatch, position, GUIStyle.Yellow, glowSprite.size / 2, scale: glowScale);
91  }
92  }
93 
94  tooltip = Option.None;
96  {
97  Connection.DrawConnectionDebugInfo(spriteBatch, Connection, drawRect.Center, isScrewed ? 1.1f : 0.9f, out var tooltipText);
98 
99  if (isMouseOver && !tooltipText.IsNullOrEmpty())
100  {
101  tooltip = Option.Some(tooltipText);
102  }
103  }
104 
105  if (!isScrewed) { return; }
106 
107  if (screwSprite is not null)
108  {
109  float screwScale = drawRect.Height / screwSprite.size.Y;
110  screwSprite.Draw(spriteBatch, drawRect.Center, color, 0f, screwScale);
111  }
112 
113  if (connectorSprite is not null)
114  {
115  float screwScale = drawRect.Height / connectorSprite.size.Y * 2f;
116  Vector2 pos = drawRect.Center;
117 
118  connectorSprite.Draw(spriteBatch,
119  pos: pos,
120  color: Color.White,
121  origin: connectorSprite.Origin,
122  rotate: MathHelper.Pi / (IsOutput ? -2f : 2f),
123  scale: screwScale,
124  spriteEffect: SpriteEffects.None);
125  }
126  }
127 
128  public void DrawHUD(SpriteBatch spriteBatch, Camera camera)
129  {
130  if (!tooltip.TryUnwrap(out var text)) { return; }
131 
132  var drawPos = camera.WorldToScreen(new Vector2(Rect.Right, -Rect.Bottom));
133 
134  GUIComponent.DrawToolTip(spriteBatch, text, drawPos);
135  }
136  }
137 }
List< CircuitBoxConnection > CircuitBoxConnections
Circuit box input and output connections that are linked to this connection.
static void DrawConnectionDebugInfo(SpriteBatch spriteBatch, Connection c, Vector2 position, float scale, out LocalizedString tooltip)