Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/CircuitBox/CircuitBoxComponent.cs
1 #nullable enable
2 
3 using System;
4 using System.Linq;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Graphics;
8 
9 namespace Barotrauma
10 {
11  internal partial class CircuitBoxComponent
12  {
13  public static Option<GUIComponent> EditingHUD = Option.None;
14 
15  private Sprite Sprite => Item.Prefab.InventoryIcon ?? Item.Prefab.Sprite;
16 
17  private CircuitBoxLabel? label;
18  private CircuitBoxLabel Label
19  {
20  get
21  {
22  if (label is { } l)
23  {
24  return l;
25  }
26 
27  var name = TextManager.Get($"circuitboxnode.{Item.Prefab.Identifier}").Fallback($"[FALLBACK] {Item.Name}");
28  label = new CircuitBoxLabel(name, GUIStyle.LargeFont);
29  return label.Value;
30  }
31  }
32 
33  public void UpdateEditing(RectTransform parent)
34  {
35  if (EditingHUD.TryUnwrap(out var editor))
36  {
37  if (editor.UserData == this) { return; }
38  RemoveEditingHUD();
39  }
40  EditingHUD = Option.Some(CreateEditingHUD(parent));
41  }
42 
43  public static void RemoveEditingHUD()
44  {
45  if (!EditingHUD.TryUnwrap(out var editor)) { return; }
46 
47  editor.RectTransform.Parent = null;
48  EditingHUD = Option.None;
49  }
50 
51  public GUIComponent CreateEditingHUD(RectTransform parent)
52  {
53  GUIFrame frame = new(new RectTransform(new Vector2(0.4f, 0.3f), parent, Anchor.TopRight))
54  {
55  UserData = this
56  };
57 
58  GUIListBox listBox = new(new RectTransform(ToolBox.PaddingSizeParentRelative(frame.RectTransform, 0.8f), frame.RectTransform, Anchor.Center))
59  {
60  KeepSpaceForScrollBar = true,
61  AutoHideScrollBar = false,
62  CanTakeKeyBoardFocus = false
63  };
64 
65  bool isEditor = Screen.Selected is { IsEditor: true };
66 
67  GUILayoutGroup titleHolder = new GUILayoutGroup(new RectTransform(new Vector2(1f, 0.3f), listBox.Content.RectTransform));
68  new GUITextBlock(new RectTransform(Vector2.One, titleHolder.RectTransform), Item.Prefab.Name, font: GUIStyle.LargeFont)
69  {
70  TextColor = Color.White,
71  Color = Color.Black
72  };
73  int fieldCount = 0;
74 
75  foreach (ItemComponent ic in Item.Components)
76  {
77  if (ic is Holdable) { continue; }
78  if (!ic.AllowInGameEditing && Screen.Selected is not { IsEditor: true }) { continue; }
79  if (SerializableProperty.GetProperties<InGameEditable>(ic).Count == 0 &&
80  !SerializableProperty.GetProperties<ConditionallyEditable>(ic).Any(p => p.GetAttribute<ConditionallyEditable>().IsEditable(ic)))
81  {
82  continue;
83  }
84 
85  new GUIFrame(new RectTransform(new Vector2(1.0f, 0.02f), listBox.Content.RectTransform), style: "HorizontalLine");
86 
87  var componentEditor = new SerializableEntityEditor(listBox.Content.RectTransform, ic, inGame: !isEditor, showName: false, titleFont: GUIStyle.SubHeadingFont)
88  {
89  Readonly = CircuitBox.Locked
90  };
91  fieldCount += componentEditor.Fields.Count;
92 
93  ic.CreateEditingHUD(componentEditor);
94  componentEditor.Recalculate();
95  }
96 
97  if (fieldCount == 0)
98  {
99  frame.Visible = false;
100  }
101 
102  return frame;
103  }
104 
105  public override void DrawHeader(SpriteBatch spriteBatch, RectangleF drawRect, Color color)
106  {
107  // scale to topRect height
108  Vector2 scale = new(drawRect.Height / MathF.Min(Sprite.size.X, Sprite.size.Y)),
109  spritePosition = new(drawRect.Left, drawRect.Top);
110 
111  float spriteWidth = Sprite.size.X * scale.X;
112 
113  Sprite.Draw(spriteBatch, spritePosition, Color.White, Vector2.Zero, 0f, scale);
114  GUI.DrawString(spriteBatch, new Vector2(spritePosition.X + spriteWidth + CircuitBoxSizes.NodeHeaderTextPadding, drawRect.Center.Y - Label.Size.Y / 2f), Label.Value, GUIStyle.TextColorNormal, font: GUIStyle.LargeFont);
115  }
116  }
117 }
The base class for components holding the different functionalities of the item
bool IsEditable(ISerializableEntity entity)