3 using System.Collections.Generic;
4 using System.Collections.Immutable;
7 using Microsoft.Xna.Framework;
11 internal sealed
partial class CircuitBoxInputOutputNode : CircuitBoxNode
20 public readonly Type NodeType;
22 private const int MaxConnectionLabelLength = 32;
23 private const string ConnectionLabelOverrideElementName =
"ConnectionLabelOverride";
25 public Dictionary<string, string> ConnectionLabelOverrides =
new();
27 public CircuitBoxInputOutputNode(IReadOnlyList<CircuitBoxConnection> conns, Vector2 initialPosition, Type type, CircuitBox circuitBox): base(circuitBox)
30 Connectors = conns.ToImmutableArray();
31 Position = initialPosition;
36 public void ReplaceAllConnectionLabelOverrides(Dictionary<string, string> replace)
38 foreach (var (_, value) in replace)
40 if (value.Length > MaxConnectionLabelLength)
42 DebugConsole.ThrowError($
"Label override value \"{value}\" is too long (max {MaxConnectionLabelLength} characters)");
47 foreach (var (name, value) in replace)
49 if (
string.IsNullOrWhiteSpace(value))
51 ConnectionLabelOverrides.Remove(name);
55 ConnectionLabelOverrides[name] = value;
63 private void InitSize(IReadOnlyList<CircuitBoxConnection> conns)
66 foreach (CircuitBoxConnection conn
in conns)
68 if (ConnectionLabelOverrides.TryGetValue(conn.Name, out
string? value))
70 LocalizedString newLabel =
71 string.IsNullOrWhiteSpace(value)
72 ? conn.Connection.DisplayName
73 : TextManager.Get(value).Fallback(value);
75 conn.SetLabel(newLabel,
this);
79 conn.SetLabel(conn.Connection.DisplayName,
this);
83 Size = CalculateSize(conns);
86 public XElement Save()
88 XElement element =
new XElement($
"{NodeType}Node",
new XAttribute(
"pos", XMLExtensions.Vector2ToString(Position)));
90 foreach (var (name, value) in ConnectionLabelOverrides)
92 element.Add(
new XElement(ConnectionLabelOverrideElementName,
93 new XAttribute(
"name", name),
94 new XAttribute(
"value", value)));
100 public void Load(ContentXElement element)
102 Position = element.GetAttributeVector2(
"pos", Vector2.Zero);
104 Dictionary<string, string> loadedOverrides =
new();
105 foreach (var subElement
in element.Elements())
107 if (subElement.Name != ConnectionLabelOverrideElementName) {
continue; }
109 string name = subElement.GetAttributeString(
"name",
string.Empty);
110 string value = subElement.GetAttributeString(
"value",
string.Empty);
112 loadedOverrides[name] = value;
115 ConnectionLabelOverrides = loadedOverrides;
116 InitSize(Connectors);