4 using System.Collections.Immutable;
7 using Microsoft.Xna.Framework;
12 internal enum CircuitBoxResizeDirection
45 internal readonly record
struct NetCircuitBoxHeader(
CircuitBoxOpcode Opcode, ushort ItemID, byte ComponentIndex) : INetSerializableStruct
47 public Option<CircuitBox> FindTarget() => CircuitBox.FindCircuitBox(ItemID, ComponentIndex);
51 internal readonly record
struct CircuitBoxConnectorIdentifier(Identifier SignalConnection, Option<ushort> TargetId) : INetSerializableStruct
53 public static CircuitBoxConnectorIdentifier FromConnection(CircuitBoxConnection connection) =>
56 (CircuitBoxInputConnection or CircuitBoxOutputConnection)
57 =>
new CircuitBoxConnectorIdentifier(connection.Name.ToIdentifier(), Option.None),
59 CircuitBoxNodeConnection nodeConnection
60 =>
new CircuitBoxConnectorIdentifier(connection.Name.ToIdentifier(), Option.Some(nodeConnection.Component.ID)),
62 _ =>
throw new ArgumentOutOfRangeException(nameof(connection))
65 public Option<CircuitBoxConnection> FindConnection(CircuitBox circuitBox)
67 if (!TargetId.TryUnwrap(out var
id))
69 return circuitBox.FindInputOutputConnection(SignalConnection);
72 foreach (CircuitBoxComponent boxNode
in circuitBox.Components)
74 if (boxNode.ID !=
id) {
continue; }
76 foreach (var conn
in boxNode.Connectors)
78 if (conn.Name != SignalConnection) {
continue; }
80 return Option.Some(conn);
87 public XElement Save(
string name) =>
new XElement(name,
88 new XAttribute(
"name", SignalConnection),
89 new XAttribute(
"target", TargetId.TryUnwrap(out var value) ? value.ToString() :
string.Empty));
91 public static CircuitBoxConnectorIdentifier Load(ContentXElement element)
93 string? name = element.GetAttributeString(
"name",
string.Empty);
94 string? target = element.GetAttributeString(
"target",
string.Empty);
96 Option<ushort> targetId = Option.None;
97 if (!
string.IsNullOrWhiteSpace(target))
99 targetId = ushort.TryParse(target, out var value) ? Option.Some(value) : Option.None;
102 return new CircuitBoxConnectorIdentifier(name.ToIdentifier(), targetId);
105 public override string ToString()
106 => $
"{{Name: {SignalConnection}, ID: {(TargetId.TryUnwrap(out var value) ? value.ToString() : "N/A
")}}}";
110 internal readonly record
struct CircuitBoxAddLabelEvent(Vector2 Position, Color Color, NetLimitedString Header, NetLimitedString Body) : INetSerializableStruct;
113 internal readonly record struct CircuitBoxServerAddLabelEvent(ushort ID, Vector2 Position, Vector2 Size, Color Color, NetLimitedString Header, NetLimitedString Body) : INetSerializableStruct;
116 internal readonly record struct CircuitBoxResizeLabelEvent(ushort ID, Vector2 Position, Vector2 Size) : INetSerializableStruct;
119 internal readonly record struct CircuitBoxRemoveLabelEvent(ImmutableArray<ushort> TargetIDs) : INetSerializableStruct;
122 internal readonly record struct CircuitBoxAddComponentEvent(UInt32 PrefabIdentifier, Vector2 Position) : INetSerializableStruct;
125 internal readonly record struct CircuitBoxServerCreateComponentEvent(ushort BackingItemId, UInt32 UsedResource, ushort ComponentId, Vector2 Position) : INetSerializableStruct;
128 internal readonly record struct CircuitBoxRemoveComponentEvent(ImmutableArray<ushort> TargetIDs) : INetSerializableStruct;
131 internal readonly record struct CircuitBoxMoveComponentEvent(ImmutableArray<ushort> TargetIDs, ImmutableArray<CircuitBoxInputOutputNode.Type> IOs, ImmutableArray<ushort> LabelIDs, Vector2 MoveAmount) : INetSerializableStruct;
134 internal readonly record struct CircuitBoxSelectNodesEvent(ImmutableArray<ushort> TargetIDs, ImmutableArray<CircuitBoxInputOutputNode.Type> IOs, ImmutableArray<ushort> LabelIDs, bool Overwrite, ushort CharacterID) : INetSerializableStruct;
137 internal readonly record struct CircuitBoxServerUpdateSelection(ImmutableArray<CircuitBoxIdSelectionPair> ComponentIds, ImmutableArray<CircuitBoxIdSelectionPair> WireIds, ImmutableArray<CircuitBoxTypeSelectionPair> InputOutputs, ImmutableArray<CircuitBoxIdSelectionPair> LabelIds) : INetSerializableStruct;
140 internal readonly record struct CircuitBoxIdSelectionPair(ushort ID, Option<ushort> SelectedBy) : INetSerializableStruct;
143 internal readonly record struct CircuitBoxTypeSelectionPair(CircuitBoxInputOutputNode.Type Type, Option<ushort> SelectedBy) : INetSerializableStruct;
146 internal readonly record struct CircuitBoxSelectWiresEvent(ImmutableArray<ushort> TargetIDs, bool Overwrite, ushort CharacterID) : INetSerializableStruct;
149 internal readonly record struct CircuitBoxClientAddWireEvent(Color Color, CircuitBoxConnectorIdentifier Start, CircuitBoxConnectorIdentifier End, UInt32 SelectedWirePrefabIdentifier) : INetSerializableStruct;
152 internal readonly record struct CircuitBoxServerCreateWireEvent(CircuitBoxClientAddWireEvent Request, ushort WireId, Option<ushort> BackingItemId) : INetSerializableStruct;
155 internal readonly record struct CircuitBoxRemoveWireEvent(ImmutableArray<ushort> TargetIDs) : INetSerializableStruct;
158 internal readonly record struct CircuitBoxRenameLabelEvent(ushort LabelId, Color Color, NetLimitedString NewHeader, NetLimitedString NewBody) : INetSerializableStruct;
161 internal readonly record struct CircuitBoxRenameConnectionLabelsEvent(CircuitBoxInputOutputNode.Type Type, NetDictionary<string, string> Override) : INetSerializableStruct;
165 internal readonly record struct CircuitBoxErrorEvent(string Message) : INetSerializableStruct;
168 internal readonly record struct CircuitBoxInitializeStateFromServerEvent(
169 ImmutableArray<CircuitBoxServerCreateComponentEvent> Components,
170 ImmutableArray<CircuitBoxServerCreateWireEvent> Wires,
171 ImmutableArray<CircuitBoxServerAddLabelEvent> Labels,
172 ImmutableArray<CircuitBoxRenameConnectionLabelsEvent> LabelOverrides,
174 Vector2 OutputPos) : INetSerializableStruct;
176 internal readonly record struct CircuitBoxEventData(INetSerializableStruct Data) :
ItemComponent.IEventData
181 (CircuitBoxAddComponentEvent or CircuitBoxServerCreateComponentEvent)
183 CircuitBoxRemoveComponentEvent
185 CircuitBoxMoveComponentEvent
187 CircuitBoxSelectNodesEvent
189 CircuitBoxSelectWiresEvent
191 CircuitBoxServerUpdateSelection
193 (CircuitBoxClientAddWireEvent or CircuitBoxServerCreateWireEvent)
195 CircuitBoxRemoveWireEvent
197 CircuitBoxInitializeStateFromServerEvent
199 CircuitBoxRenameLabelEvent
201 (CircuitBoxAddLabelEvent or CircuitBoxServerAddLabelEvent)
203 CircuitBoxRemoveLabelEvent
205 CircuitBoxResizeLabelEvent
207 CircuitBoxRenameConnectionLabelsEvent
209 _ =>
throw new ArgumentOutOfRangeException(nameof(Data))
The base class for components holding the different functionalities of the item