-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathSubChipInstance.cs
More file actions
398 lines (326 loc) · 11.5 KB
/
SubChipInstance.cs
File metadata and controls
398 lines (326 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Generic;
using System.Linq;
using DLS.Description;
using DLS.Graphics;
using DLS.SaveSystem;
using Seb.Helpers;
using Seb.Types;
using UnityEngine;
using Exception = System.Exception;
namespace DLS.Game
{
public class SubChipInstance : IMoveable
{
public readonly PinInstance[] AllPins;
public readonly ChipType ChipType;
public readonly ChipDescription Description;
public readonly List<DisplayInstance> Displays;
public readonly SubChipDescription InitialSubChipDesc;
public readonly PinInstance[] InputPins;
public readonly uint[] InternalData;
public readonly bool IsBus;
public readonly Vector2 MinSize;
public readonly string MultiLineName;
public readonly PinInstance[] OutputPins;
public string activationKeyString; // input char for the 'key chip' type (stored as string to avoid allocating when drawing)
public string Label;
public SubChipInstance(ChipDescription description, SubChipDescription subChipDesc)
{
InitialSubChipDesc = subChipDesc;
ChipType = description.ChipType;
Description = description;
Position = subChipDesc.Position;
ID = subChipDesc.ID;
Label = subChipDesc.Label;
IsBus = ChipTypeHelper.IsBusType(ChipType);
MultiLineName = CreateMultiLineName(description.Name);
MinSize = CalculateMinChipSize(description.InputPins, description.OutputPins, description.Name);
InputPins = CreatePinInstances(description.InputPins, true);
OutputPins = CreatePinInstances(description.OutputPins, false);
AllPins = InputPins.Concat(OutputPins).ToArray();
LoadOutputPinColours(subChipDesc.OutputPinColourInfo);
// Displays
Displays = CreateDisplayInstances(description);
// Load internal data (or create default in case missing)
if (subChipDesc.InternalData == null || subChipDesc.InternalData.Length == 0)
{
InternalData = DescriptionCreator.CreateDefaultInstanceData(description.ChipType);
}
else
{
InternalData = new uint[subChipDesc.InternalData.Length];
Array.Copy(subChipDesc.InternalData, InternalData, InternalData.Length);
if (ChipType == ChipType.Key)
{
SetKeyChipActivationChar((char)subChipDesc.InternalData[0]);
}
if (IsBus && InternalData.Length > 1)
{
foreach (PinInstance pin in AllPins)
{
pin.SetBusFlip(BusIsFlipped);
}
}
}
PinInstance[] CreatePinInstances(PinDescription[] pinDescriptions, bool isInputPin)
{
int num = pinDescriptions.Length;
PinInstance[] pins = new PinInstance[pinDescriptions.Length];
for (int i = 0; i < num; i++)
{
PinDescription desc = pinDescriptions[i];
PinAddress address = new(subChipDesc.ID, desc.ID);
pins[i] = new PinInstance(desc, address, this, !isInputPin);
}
CalculatePinLayout(pins);
return pins;
}
}
public int LinkedBusPairID => IsBus ? (int)InternalData[0] : -1;
public bool BusIsFlipped => IsBus && InternalData.Length > 1 && InternalData[1] == 1;
public Vector2 Size => Description.Size;
public Vector2 Position { get; set; }
public Vector2 MoveStartPosition { get; set; }
public Vector2 StraightLineReferencePoint { get; set; }
public int ID { get; }
public bool IsSelected { get; set; }
public bool HasReferencePointForStraightLineMovement { get; set; }
public bool IsValidMovePos { get; set; }
public Bounds2D BoundingBox => CreateBoundingBox(0);
public Bounds2D SelectionBoundingBox => CreateBoundingBox(DrawSettings.SelectionBoundsPadding);
public Vector2 SnapPoint
{
get
{
if (IsBus) return Position; // Snap bus chips to their centre to avoid misalignment when flipping
if (InputPins.Length != 0) return InputPins[0].GetWorldPos();
if (OutputPins.Length != 0) return OutputPins[0].GetWorldPos();
return Position;
}
}
public bool ShouldBeIncludedInSelectionBox(Vector2 selectionCentre, Vector2 selectionSize) => Maths.PointInBox2D(Position, selectionCentre, selectionSize);
public void SetLinkedBusPair(SubChipInstance busPair)
{
if (!IsBus) throw new Exception("Can't link bus to non-bus chip");
InternalData[0] = (uint)busPair.ID;
}
public void SetKeyChipActivationChar(char c)
{
if (ChipType != ChipType.Key) throw new Exception("Expected KeyChip type, but instead got: " + ChipType);
activationKeyString = c.ToString();
InternalData[0] = c;
}
public void UpdatePinLayout()
{
CalculatePinLayout(InputPins);
CalculatePinLayout(OutputPins);
}
void CalculatePinLayout(PinInstance[] pins)
{
// If only one pin, it should be placed in the centre
if (pins.Length == 1)
{
pins[0].LocalPosY = 0;
return;
}
float chipTop = Size.y / 2;
float startY = chipTop;
(float chipHeight, float[] pinGridY) info = CalculateDefaultPinLayout(pins.Select(s => s.bitCount).ToArray());
// ---- First pass: layout pins without any spacing between them ----
for (int i = 0; i < pins.Length; i++)
{
PinInstance pin = pins[i];
float pinGridY = info.pinGridY[i];
pin.LocalPosY = startY + pinGridY * DrawSettings.GridSize;
}
// ---- Second pass: evenly distribute the remaining space between the pins ----
float spaceRemaining = Size.y - info.chipHeight;
if (spaceRemaining > 0)
{
float spacingBetweenPins = spaceRemaining / (pins.Length - 1);
for (int i = 1; i < pins.Length; i++)
{
pins[i].LocalPosY -= spacingBetweenPins * i;
}
}
}
// Min chip height based on input and output pins
public static float MinChipHeightForPins(PinDescription[] inputs, PinDescription[] outputs) => Mathf.Max(MinChipHeightForPins(inputs), MinChipHeightForPins(outputs));
public static float MinChipHeightForPins(PinDescription[] pins)
{
if (pins == null || pins.Length == 0) return 0;
return CalculateDefaultPinLayout(pins.Select(p => p.BitCount).ToArray()).chipHeight;
}
// Calculate minimal height of chip to fit the given pins, and calculate their y positions (in grid space)
public static (float chipHeight, float[] pinGridY) CalculateDefaultPinLayout(PinBitCount[] pins)
{
int gridY = 0; // top
float[] pinGridYVals = new float[pins.Length];
for (int i = 0; i < pins.Length; i++)
{
PinBitCount pinBitCount = pins[i];
int pinGridHeight = pinBitCount switch
{
PinBitCount.Bit1 => 2,
PinBitCount.Bit4 => 3,
_ => 4
};
pinGridYVals[i] = gridY - pinGridHeight / 2f;
gridY -= pinGridHeight;
}
float height = Mathf.Abs(gridY) * DrawSettings.GridSize;
return (height, pinGridYVals);
}
static List<DisplayInstance> CreateDisplayInstances(ChipDescription chipDesc)
{
List<DisplayInstance> list = new();
if (chipDesc.HasDisplay())
{
foreach (DisplayDescription displayDesc in chipDesc.Displays)
{
try
{
list.Add(CreateDisplayInstance(displayDesc, chipDesc));
}
catch (Exception e)
{
Debug.Log("Failed to create display (this is expected if display has been deleted by player). Error: " + e.Message);
}
}
}
return list;
}
static DisplayInstance CreateDisplayInstance(DisplayDescription displayDesc, ChipDescription chipDesc)
{
DisplayInstance instance = new();
instance.Desc = displayDesc;
instance.DisplayType = chipDesc.ChipType;
if (chipDesc.ChipType == ChipType.Custom)
{
ChipDescription childDesc = GetDescriptionOfDisplayedSubChip(chipDesc, displayDesc.SubChipID);
instance.ChildDisplays = CreateDisplayInstances(childDesc);
}
return instance;
}
static ChipDescription GetDescriptionOfDisplayedSubChip(ChipDescription chipDesc, int subchipID)
{
ChipLibrary library = Project.ActiveProject.chipLibrary;
foreach (SubChipDescription subchipDesc in chipDesc.SubChips)
{
if (subchipDesc.ID == subchipID)
{
return library.GetChipDescription(subchipDesc.Name);
}
}
throw new Exception("Chip for display not found " + chipDesc.Name + " subchip id: " + subchipID);
}
Bounds2D CreateBoundingBox(float pad)
{
float pinWidthPad = 0;
float offsetX = 0;
bool inputsHidden = ChipTypeHelper.IsBusOriginType(ChipType);
float flipX = BusIsFlipped ? -1 : 1;
if (InputPins.Length > 0 && !inputsHidden)
{
pinWidthPad += DrawSettings.PinRadius;
offsetX -= DrawSettings.PinRadius / 2 * flipX;
}
if (OutputPins.Length > 0)
{
pinWidthPad += DrawSettings.PinRadius;
offsetX += DrawSettings.PinRadius / 2 * flipX;
}
Vector2 padFinal = new(pinWidthPad + DrawSettings.ChipOutlineWidth + pad, DrawSettings.ChipOutlineWidth + pad);
return Bounds2D.CreateFromCentreAndSize(Position + Vector2.right * offsetX, Size + padFinal);
}
public static Vector2 CalculateMinChipSize(PinDescription[] inputPins, PinDescription[] outputPins, string unformattedName)
{
float minHeightForPins = MinChipHeightForPins(inputPins, outputPins);
string multiLineName = CreateMultiLineName(unformattedName);
bool hasMultiLineName = multiLineName != unformattedName;
float minNameHeight = DrawSettings.GridSize * (hasMultiLineName ? 4 : 3);
Vector2 nameDrawBoundsSize = DevSceneDrawer.CalculateChipNameBounds(multiLineName);
float sizeX = Mathf.Max(nameDrawBoundsSize.x + DrawSettings.GridSize, DrawSettings.PinRadius * 4);
float sizeY = Mathf.Max(minNameHeight, minHeightForPins);
return new Vector2(sizeX, sizeY);
}
public static float PinHeightFromBitCount(PinBitCount bitCount)
{
return bitCount switch
{
PinBitCount.Bit1 => DrawSettings.PinRadius * 2,
PinBitCount.Bit4 => DrawSettings.PinHeight4Bit,
PinBitCount.Bit8 => DrawSettings.PinHeight8Bit,
_ => throw new Exception("Bit count not implemented " + bitCount)
};
}
// Split chip name into two lines (if contains a space character)
static string CreateMultiLineName(string name)
{
// If name is short, or contains no spaces, then just keep on single line
if (name.Length <= 6 || !name.Contains(' ')) return name;
string[] lines = { name };
float bestSplitPenalty = float.MaxValue;
for (int i = 0; i < name.Length; i++)
{
if (name[i] == ' ')
{
string lineA = name.Substring(0, i).Trim();
string lineB = name.Substring(i).Trim();
int lenDiff = lineA.Length - lineB.Length;
float splitPenalty = Mathf.Abs(lenDiff);
if (splitPenalty < bestSplitPenalty)
{
lines = new[] { lineA, lineB };
bestSplitPenalty = splitPenalty;
}
}
}
// Pad lines with spaces to centre justify
string formatted = "";
int longestLine = lines.Max(l => l.Length);
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
int numPadChars = longestLine - line.Length;
int numPadLeft = numPadChars / 2;
int numPadRight = numPadChars - numPadLeft;
line = line.PadLeft(line.Length + numPadLeft, ' ');
line = line.PadRight(line.Length + numPadRight, ' ');
// Add half space tag to center if padding is uneven
if (numPadLeft < numPadRight)
{
line = "<halfSpace>" + line;
}
formatted += line;
if (i < lines.Length - 1) formatted += "\n";
}
return formatted;
}
public void FlipBus()
{
if (!IsBus) throw new Exception("Can't flip non-bus type");
bool isFlipped = !BusIsFlipped;
InternalData[1] = isFlipped ? 1u : 0;
foreach (PinInstance pin in AllPins)
{
pin.SetBusFlip(isFlipped);
}
}
void LoadOutputPinColours(OutputPinColourInfo[] cols)
{
if (cols == null) return;
foreach (PinInstance pin in OutputPins)
{
foreach (OutputPinColourInfo colInfo in cols)
{
if (colInfo.PinID == pin.Address.PinID)
{
pin.Colour = colInfo.PinColour;
}
}
}
}
}
}