-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathChipInteractionController.cs
More file actions
1115 lines (952 loc) · 36.6 KB
/
ChipInteractionController.cs
File metadata and controls
1115 lines (952 loc) · 36.6 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Generic;
using System.Linq;
using DLS.Description;
using DLS.Graphics;
using DLS.ModdingAPI;
using DLS.Mods;
using DLS.SaveSystem;
using Seb.Helpers;
using UnityEngine;
using PinBitCount = DLS.Description.PinBitCount;
using PinDescription = DLS.Description.PinDescription;
namespace DLS.Game
{
public class ChipInteractionController
{
public readonly Project project;
// ---- Selection and placement state ----
public readonly List<IMoveable> SelectedElements = new();
public List<WireInstance> DuplicatedWires = new();
public WireInstance WireToPlace;
bool isPlacingNewElements;
float itemPlacementCurrVerticalSpacing;
bool newElementsAreDuplicatedElements;
Vector2 moveElementMouseStartPos;
IMoveable[] Obstacles; // Obstacles are the non-selected items when a group of elements is being moved
public Vector2 SelectionBoxStartPos;
StraightLineMoveState straightLineMoveState;
bool hasExittedMultiModeSincePlacementStart;
// ---- Wire edit state ----
public WireInstance wireToEdit;
public int wireEditPointIndex = -1;
public bool wireEditCanInsertPoint;
Vector2 wireEditPointOld;
public int wireEditPointSelectedIndex;
public bool isMovingWireEditPoint;
public DevChipInstance ActiveDevChip => project.ViewedChip;
public bool IsMovingSelection { get; private set; }
public bool IsCreatingSelectionBox { get; private set; }
public Vector2 SelectionBoxCentre => (SelectionBoxStartPos + InputHelper.MousePosWorld) / 2;
public Vector2 SelectionBoxSize => Maths.Abs(SelectionBoxStartPos - InputHelper.MousePosWorld);
public bool HasControl => !UIDrawer.InInputBlockingMenu() && project.CanEditViewedChip;
// Cannot interact with elements when other elements are being moved, in a menu, or drawing a selection box
bool CanInteract => !IsMovingSelection && !UIDrawer.InInputBlockingMenu() && !IsCreatingSelectionBox && !InteractionState.MouseIsOverUI;
public bool IsCreatingWire => WireToPlace != null;
public bool IsPlacingElements => isPlacingNewElements;
public bool IsPlacingElementOrCreatingWire => isPlacingNewElements || IsCreatingWire;
public bool IsPlacingOrMovingElementOrCreatingWire => isPlacingNewElements || IsMovingSelection || IsCreatingWire || isMovingWireEditPoint;
public bool CanInteractWithPin => CanInteract;
public bool CanInteractWithPinStateDisplay => CanInteract && !IsCreatingWire && Project.ActiveProject.CanEditViewedChip;
public bool CanInteractWithPinHandle => CanInteractWithPinStateDisplay;
public ChipInteractionController(Project project)
{
this.project = project;
}
public void Update()
{
HandleKeyboardInput();
HandleMouseInput();
}
// Don't allow interaction with wire that's currently being placed (this would allow it to try to connect to itself for example...)
public bool CanInteractWithWire(WireInstance wire) => CanInteract && wire != WireToPlace;
public bool CanCompleteWireConnection(WireInstance wireToConnectTo, out PinInstance endPin)
{
// If we're joining this wire to an existing wire, choose the appropriate source/target pin from that wire to connect to
endPin = WireToPlace.FirstPin.IsSourcePin ? wireToConnectTo.TargetPin_BusCorrected : wireToConnectTo.SourcePin;
return CanCompleteWireConnection(endPin, wireToConnectTo);
}
public bool CanCompleteWireConnection(PinInstance endPin, WireInstance wireToConnectTo = null)
{
if (!IsCreatingWire) return false;
PinInstance startPin = WireToPlace.FirstPin;
bool connectingFromWire = WireToPlace.FirstConnectionInfo.IsConnectedAtWire;
bool connectingToWire = wireToConnectTo != null;
WireInstance wireConnection = wireToConnectTo ?? WireToPlace.FirstConnectionInfo.connectedWire;
// Don't allow wire-to-wire connections (ambiguous where to get signal source from, and where to carry it to)
if (connectingFromWire && connectingToWire) return false;
// (Maybe temporary restriction?): Don't allow sourcePin-to-wire connections (unless the wire is a bus wire).
// This is because if the two source pins have different states, then the wire would need to be coloured differently
// from the connection point onwards (depending on which of the conflicting states is chosen)
if (connectingFromWire || connectingToWire)
{
PinInstance pinConnection = connectingToWire ? startPin : endPin;
if (pinConnection.IsSourcePin && !wireConnection.IsBusWire) return false;
}
// Ensure connection is between a source and a target pin
// (note: if connection starts or ends at a wire then it's valid regardless, since we can just pick source/target pin from that wire as needed)
bool hasSourceAndTarget = startPin.IsSourcePin != endPin.IsSourcePin || connectingFromWire || connectingToWire;
if (!hasSourceAndTarget || endPin.bitCount != startPin.bitCount) return false;
// Only allow connecting bus origin and terminus if they are linked together (i.e. were created together at same time; rather than any random pair)
// Note: could consider lifting this restriction, but need to investigate impact on simulation...
if (startPin.IsBusPin && endPin.IsBusPin)
{
SubChipInstance busA = (SubChipInstance)endPin.parent;
SubChipInstance busB = (SubChipInstance)startPin.parent;
return busA.LinkedBusPairID == busB.ID;
}
return true;
}
public static bool IsSelected(IMoveable element) => element.IsSelected;
public void Delete(IMoveable element)
{
DeleteElements(new List<IMoveable>(new[] { element }));
}
void DeleteElements(List<IMoveable> elements, bool clearSelection = true)
{
if (!HasControl) return;
List<IMoveable> elementsToDelete = elements.Concat(GetNonIncludedLinkedBusElements(elements)).ToList();
ActiveDevChip.UndoController.RecordDeleteElements(elementsToDelete);
foreach (IMoveable element in elementsToDelete)
{
if (element is SubChipInstance subChip) ActiveDevChip.DeleteSubChip(subChip);
else if (element is DevPinInstance devPin) ActiveDevChip.DeleteDevPin(devPin);
}
if (clearSelection) SelectedElements.Clear();
}
void DeleteSelected()
{
// Delete selected subchips/pins
if (SelectedElements.Count > 0)
{
DeleteElements(SelectedElements);
}
// Delete wire under mouse
else if (InteractionState.ElementUnderMouse is WireInstance wire && wire != wireToEdit)
{
DeleteWire(wire);
}
// Delete wire point under mouse (in wire edit mode)
else if (wireToEdit != null && wireEditPointIndex != -1)
{
bool isWireToWireConnectionPoint = wireEditPointIndex == 0 || wireEditPointIndex == wireToEdit.WirePointCount - 1;
// Can't delete the point connecting a wire to another wire
if (!isWireToWireConnectionPoint)
{
foreach (WireInstance other in ActiveDevChip.Wires)
{
if (other.ConnectedWire == wireToEdit)
{
other.NotifyParentWirePointWillBeDeleted(wireEditPointIndex);
}
}
wireToEdit.DeleteWirePoint(wireEditPointIndex);
wireEditPointIndex = -1;
isMovingWireEditPoint = false;
}
}
}
public void DeleteWire(WireInstance wire)
{
if (HasControl)
{
ActiveDevChip.UndoController.RecordDeleteWire(wire);
ActiveDevChip.DeleteWire(wire);
}
}
public void ToggleDevPinState(DevPinInstance devPin, int bitIndex)
{
if (HasControl) devPin.ToggleState(bitIndex);
}
void HandleKeyboardInput()
{
// Ignore shortcuts if don't have control
if (!HasControl) return;
if (KeyboardShortcuts.UndoShortcutTriggered) ActiveDevChip.UndoController.TryUndo();
else if (KeyboardShortcuts.RedoShortcutTriggered) ActiveDevChip.UndoController.TryRedo();
if (!KeyboardShortcuts.StraightLineModeHeld) straightLineMoveState = StraightLineMoveState.None;
if (KeyboardShortcuts.SearchShortcutTriggered)
{
UIDrawer.SetActiveMenu(UIDrawer.MenuType.Search);
}
if (KeyboardShortcuts.DuplicateShortcutTriggered)
{
if (SelectedElements.Count > 0 && !IsPlacingOrMovingElementOrCreatingWire)
{
DuplicateSelectedElements();
}
}
if (KeyboardShortcuts.DeleteShortcutTriggered)
{
if (IsCreatingWire)
{
if (!WireToPlace.RemoveLastPoint())
{
CancelEverything();
}
}
else
{
if (isPlacingNewElements) CancelPlacingItems();
else DeleteSelected();
}
}
if (KeyboardShortcuts.ConfirmShortcutTriggered)
{
ExitWireEditMode();
}
if (KeyboardShortcuts.CancelShortcutTriggered)
{
CancelEverything();
}
}
void HandleMouseInput()
{
if (HasControl) UpdatePositionsToMouse();
// --- Mouse button input ---
if (InputHelper.IsMouseDownThisFrame(MouseButton.Left))
{
ModLoader.NotifyMods((mod, args) => mod.OnMouseClick(args), new IMod.InputEventArgs
{
Position = InputHelper.MousePosWorld,
Button = IMod.MouseButton.Left
});
HandleLeftMouseDown();
}
if (InputHelper.IsMouseUpThisFrame(MouseButton.Left)) HandleLeftMouseUp();
if (InputHelper.IsMouseDownThisFrame(MouseButton.Right))
{
ModLoader.NotifyMods((mod, args) => mod.OnMouseClick(args), new IMod.InputEventArgs
{
Position = InputHelper.MousePosWorld,
Button = IMod.MouseButton.Right
});
HandleRightMouseDown();
}
// Shift + scroll to increase vertical spacing between elements when placing multiple at a time
// (disabled if elements were duplicated since then we want to preserve relative positions)
if (isPlacingNewElements && !newElementsAreDuplicatedElements && InputHelper.ShiftIsHeld)
{
itemPlacementCurrVerticalSpacing += InputHelper.MouseScrollDelta.y * DrawSettings.GridSize;
itemPlacementCurrVerticalSpacing = Mathf.Max(0, itemPlacementCurrVerticalSpacing);
}
}
List<IMoveable> GetNonIncludedLinkedBusElements(List<IMoveable> elements)
{
List<IMoveable> nonIncludedBusPairs = new();
HashSet<int> elementIDs = elements.Select(e => e.ID).ToHashSet();
foreach (IMoveable element in elements)
{
if (element is SubChipInstance subChip && subChip.IsBus)
{
if (!elementIDs.Contains(subChip.LinkedBusPairID))
{
ActiveDevChip.TryGetSubChipByID(subChip.LinkedBusPairID, out SubChipInstance pairedBus);
nonIncludedBusPairs.Add(pairedBus);
}
}
}
return nonIncludedBusPairs;
}
// Set the correct LinkedBusPairIDs on duplicated elements (still set to original IDs at this point)
static void LinkDuplicatedBuses(List<IMoveable> duplicatedElements, IMoveable[] originalElements)
{
List<SubChipInstance> busOrigins = new();
List<SubChipInstance> busTerminuses = new();
Dictionary<int, int> lookup = new();
foreach (IMoveable element in originalElements)
{
if (element is not SubChipInstance subChip) continue;
if (ChipTypeHelper.IsBusTerminusType(subChip.ChipType)) lookup[subChip.ID] = subChip.LinkedBusPairID;
}
foreach (IMoveable element in duplicatedElements)
{
if (element is not SubChipInstance subChip) continue;
if (ChipTypeHelper.IsBusTerminusType(subChip.ChipType)) busTerminuses.Add(subChip);
else if (ChipTypeHelper.IsBusOriginType(subChip.ChipType)) busOrigins.Add(subChip);
}
foreach (SubChipInstance busOrigin in busOrigins)
{
int originalBusOriginId = lookup[busOrigin.LinkedBusPairID];
foreach (SubChipInstance busTerminus in busTerminuses)
{
if (busTerminus.LinkedBusPairID == originalBusOriginId)
{
busOrigin.SetLinkedBusPair(busTerminus);
busTerminus.SetLinkedBusPair(busOrigin);
break;
}
}
}
}
void DuplicateElements(List<IMoveable> elements)
{
if (elements.Count == 0) return;
IMoveable[] elementsToDuplicate = elements.Concat(GetNonIncludedLinkedBusElements(elements)).ToArray();
List<IMoveable> duplicatedElements = new(elementsToDuplicate.Length);
Dictionary<int, int> duplicatedElementIDFromOriginalID = new();
// Get description of each element, and start placing a copy of it
foreach (IMoveable element in elementsToDuplicate)
{
IMoveable duplicatedElement = CreateElementFromDuplicationSource(element);
StartPlacing(duplicatedElement, element.Position, true);
duplicatedElement.StraightLineReferencePoint = element.Position;
duplicatedElements.Add(duplicatedElement);
duplicatedElementIDFromOriginalID.Add(element.ID, duplicatedElement.ID);
}
LinkDuplicatedBuses(duplicatedElements, elementsToDuplicate);
// ---- Duplicate wires ----
Dictionary<WireInstance, WireInstance> duplicatedWireFromOriginal = new();
DuplicatedWires.Clear();
foreach (WireInstance wire in ActiveDevChip.Wires)
{
bool wireSourceHasBeenDuplicated = duplicatedElementIDFromOriginalID.TryGetValue(wire.SourcePin.Address.PinOwnerID, out int sourceID);
bool wireTargetHasBeenDuplicated = duplicatedElementIDFromOriginalID.TryGetValue(wire.TargetPin.Address.PinOwnerID, out int targetID);
if (wireSourceHasBeenDuplicated && wireTargetHasBeenDuplicated)
{
PinAddress duplicatedSourcePinAddress = new(sourceID, wire.SourcePin.Address.PinID);
PinAddress duplicatedTargetPinAddress = new(targetID, wire.TargetPin.Address.PinID);
DevChipInstance.TryFindPin(duplicatedElements, duplicatedSourcePinAddress, out PinInstance duplicatedSourcePin);
DevChipInstance.TryFindPin(duplicatedElements, duplicatedTargetPinAddress, out PinInstance duplicatedTargetPin);
Debug.Assert(duplicatedSourcePin != null && duplicatedTargetPin != null, "Pins not found for duplicated wire!");
WireInstance duplicatedConnectedSourceWire = null;
WireInstance duplicatedConnectedTargetWire = null;
if (wire.SourceConnectionInfo.connectedWire != null) duplicatedWireFromOriginal.TryGetValue(wire.SourceConnectionInfo.connectedWire, out duplicatedConnectedSourceWire);
if (wire.TargetConnectionInfo.connectedWire != null) duplicatedWireFromOriginal.TryGetValue(wire.TargetConnectionInfo.connectedWire, out duplicatedConnectedTargetWire);
WireInstance.ConnectionInfo sourceConnectionInfo = new()
{
pin = duplicatedSourcePin,
connectedWire = duplicatedConnectedSourceWire,
connectionPoint = wire.SourceConnectionInfo.connectionPoint,
wireConnectionSegmentIndex = wire.SourceConnectionInfo.wireConnectionSegmentIndex
};
WireInstance.ConnectionInfo targetConnectionInfo = new()
{
pin = duplicatedTargetPin,
connectedWire = duplicatedConnectedTargetWire,
connectionPoint = wire.TargetConnectionInfo.connectionPoint,
wireConnectionSegmentIndex = wire.TargetConnectionInfo.wireConnectionSegmentIndex
};
Vector2[] wirePoints = new Vector2[wire.WirePointCount];
for (int i = 0; i < wirePoints.Length; i++)
{
wirePoints[i] = wire.GetWirePoint(i);
}
WireInstance duplicatedWire = new(sourceConnectionInfo, targetConnectionInfo, wirePoints, ActiveDevChip.Wires.Count + DuplicatedWires.Count);
duplicatedWireFromOriginal.Add(wire, duplicatedWire);
DuplicatedWires.Add(duplicatedWire);
}
}
// Find element closest to mouse to use as origin point for duplicated elements
Vector2 mousePos = InputHelper.MousePosWorld;
Vector2 closestElementPos = Vector2.zero;
float closestDst = float.MaxValue;
foreach (IMoveable element in elementsToDuplicate)
{
Vector2 pos = element is DevPinInstance pin ? pin.HandlePosition : element.Position;
float dst = Vector2.Distance(pos, mousePos);
if (dst < closestDst)
{
closestDst = dst;
closestElementPos = pos;
}
}
Vector2 offset = InputHelper.MousePosWorld - closestElementPos;
moveElementMouseStartPos -= offset;
}
void DuplicateSelectedElements()
{
DuplicateElements(SelectedElements);
}
public void Select(IMoveable element, bool addToCurrentSelection = true)
{
ExitWireEditMode();
if (element.IsSelected)
{
// If in add mode, and element already selected, then remove it from the selection
if (addToCurrentSelection)
{
element.IsSelected = false;
SelectedElements.Remove(element);
}
}
else
{
if (!addToCurrentSelection)
{
ClearSelection();
}
SelectedElements.Add(element);
element.IsSelected = true;
element.IsValidMovePos = true;
}
}
void HandleRightMouseDown()
{
// Cancel placement by right-clicking
if (IsPlacingOrMovingElementOrCreatingWire)
{
CancelEverything();
InputHelper.ConsumeMouseButtonDownEvent(MouseButton.Right);
}
IsCreatingSelectionBox = false;
ClearSelection();
}
void HandleLeftMouseDown()
{
SelectionBoxStartPos = InputHelper.MousePosWorld;
straightLineMoveState = StraightLineMoveState.None;
if (InteractionState.ElementUnderMouse == null) ExitWireEditMode();
if (InteractionState.MouseIsOverUI) return;
// Confirm placement of new item
if (IsPlacingElementOrCreatingWire)
{
// Place wire
if (IsCreatingWire) //
{
if (TryFinishPlacingWire())
{
CancelPlacingItems();
}
else if (CanAddWirePoint())
{
WireToPlace.AddWirePoint(InputHelper.MousePosWorld);
}
}
// Place subchip / devpin
else
{
FinishPlacingNewElements();
}
}
else
{
// Mouse down on pin: start placing wire
if (InteractionState.ElementUnderMouse is PinInstance pin && HasControl)
{
WireInstance.ConnectionInfo connectionInfo = new() { pin = pin };
StartPlacingWire(connectionInfo);
}
// Mouse down on wire
else if (InteractionState.ElementUnderMouse is WireInstance wire && HasControl)
{
// Insert a point on the currently edited wire
if (wire == wireToEdit)
{
if (wireEditCanInsertPoint)
{
(Vector2 point, int segmentIndex) = WireLayoutHelper.GetClosestPointOnWire(wireToEdit, InputHelper.MousePosWorld);
wireToEdit.InsertPoint(point, segmentIndex);
wireEditPointIndex = segmentIndex + 1;
}
}
// Start placing a new wire from this point on the selected wire
else
{
WireInstance.ConnectionInfo connectionInfo = CreateWireToWireConnectionInfo(wire, wire.SourcePin);
StartPlacingWire(connectionInfo);
}
}
// Mouse down on selectable element: select it and prepare to start moving current selection
else if (InteractionState.ElementUnderMouse is IMoveable element)
{
bool addToSelection = KeyboardShortcuts.MultiModeHeld;
Select(element, addToSelection);
StartMovingSelectedItems();
}
// Mouse down over nothing: clear selection
else if (InteractionState.ElementUnderMouse == null && !IsPlacingElementOrCreatingWire)
{
if (!KeyboardShortcuts.MultiModeHeld) ClearSelection(); // don't clear if in 'multi-mode' (to allow box selecting multiple times)
IsCreatingSelectionBox = true;
}
if (wireToEdit != null && wireEditPointIndex != -1)
{
isMovingWireEditPoint = true;
wireEditPointSelectedIndex = wireEditPointIndex;
wireEditPointOld = wireToEdit.GetWirePoint(wireEditPointIndex);
}
}
}
WireInstance.ConnectionInfo CreateWireToWireConnectionInfo(WireInstance wireToConnectTo, PinInstance pin)
{
Vector2 mousePos = InputHelper.MousePosWorld;
if (project.ShouldSnapToGrid) mousePos = GridHelper.SnapToGrid(mousePos, true, true);
// If connecting a new wire to an existing wire, the target connection point is end pos of new wire (this is mouse pos but with snapping options applied)
// Otherwise if creating a new wire from an existing wire, connection point is at mouse pos.
Vector2 targetPoint = WireToPlace?.GetWirePoint(WireToPlace.WirePointCount - 1) ?? mousePos;
// Find where target connection point is closest to the target wire.
(Vector2 bestPoint, int bestSegmentIndex) = WireLayoutHelper.GetClosestPointOnWire(wireToConnectTo, targetPoint);
return new WireInstance.ConnectionInfo
{
pin = pin,
connectedWire = wireToConnectTo,
wireConnectionSegmentIndex = bestSegmentIndex,
connectionPoint = bestPoint
};
}
void StartPlacingWire(WireInstance.ConnectionInfo connectionInfo)
{
ExitWireEditMode();
ClearSelection();
int spawnOrder = project.ViewedChip.Wires.Count > 0 ? project.ViewedChip.Wires[^1].spawnOrder + 1 : 0;
WireToPlace = new WireInstance(connectionInfo, spawnOrder);
}
void FinishMovingElements()
{
// -- If any elements are in invalid position, cancel the movement --
bool hasMoved = false;
foreach (IMoveable element in SelectedElements)
{
if (!element.IsValidMovePos)
{
CancelMovingSelectedItems();
return;
}
hasMoved |= element.MoveStartPosition != element.Position;
if (hasMoved)
{
ModLoader.NotifyMods((mod, args) => mod.OnMoveChip(args, element.Position), new IMod.ChipEventArgs
{
ChipName = element is SubChipInstance subchip ? subchip.Description.Name : string.Empty,
Position = element.MoveStartPosition
});
}
}
if (hasMoved) ActiveDevChip.UndoController.RecordMoveElements(SelectedElements);
// -- Apply movement --
IsMovingSelection = false;
foreach (WireInstance wire in ActiveDevChip.Wires)
{
wire.ApplyMoveOffset();
}
}
void FinishPlacingNewElements()
{
// ---- If any elements are in invalid position, don't allow the placement ----
foreach (IMoveable element in SelectedElements)
{
if (!element.IsValidMovePos)
{
return;
}
}
List<IMoveable> newlyPlacedElements = new(SelectedElements);
// ---- Add newly placed elements to the chip ----
foreach (IMoveable elementToPlace in SelectedElements)
{
if (elementToPlace is SubChipInstance subchip)
{
ActiveDevChip.AddNewSubChip(subchip, false);
ModLoader.NotifyMods((mod, args) => mod.OnPlaceChip(args), new IMod.ChipEventArgs
{
ChipName = subchip.Description.Name,
Position = subchip.Position
});
}
else if (elementToPlace is DevPinInstance devPin)
{
ActiveDevChip.AddNewDevPin(devPin, false);
}
}
foreach (WireInstance wire in DuplicatedWires)
{
ActiveDevChip.AddWire(wire, false);
wire.ApplyMoveOffset();
}
ActiveDevChip.UndoController.RecordAddElements(SelectedElements, DuplicatedWires.Count > 0);
DuplicatedWires.Clear();
OnFinishedPlacingItems();
if (KeyboardShortcuts.MultiModeHeld)
{
DuplicateElements(newlyPlacedElements);
}
}
public void EnterWireEditMode(WireInstance wire)
{
if (wireToEdit == wire) ExitWireEditMode();
else wireToEdit = wire;
}
void ExitWireEditMode()
{
if (wireToEdit != null && isMovingWireEditPoint)
{
wireToEdit.SetWirePoint(wireEditPointOld, wireEditPointSelectedIndex);
ModLoader.NotifyMods((mod, args) => mod.OnEditWire(args), new IMod.WireEventArgs
{
SourcePinName = wireToEdit.SourcePin.Name,
TargetPinName = wireToEdit.TargetPin.Name,
BitCount = (int) wireToEdit.bitCount
});
}
wireToEdit = null;
isMovingWireEditPoint = false;
wireEditPointIndex = -1;
wireEditPointSelectedIndex = -1;
}
void HandleLeftMouseUp()
{
// Place items that are being moved
if (!IsPlacingElementOrCreatingWire)
{
if (IsMovingSelection)
{
FinishMovingElements();
}
}
// Select all selectable elements inside selection box
if (IsCreatingSelectionBox)
{
if (!KeyboardShortcuts.MultiModeHeld) ClearSelection();
IsCreatingSelectionBox = false;
float selectionBoxArea = Mathf.Abs(SelectionBoxSize.x * SelectionBoxSize.y);
if (selectionBoxArea > 0.000001f)
{
foreach (IMoveable element in ActiveDevChip.Elements)
{
if (element.ShouldBeIncludedInSelectionBox(SelectionBoxCentre, SelectionBoxSize))
{
Select(element);
}
}
}
}
if (IsCreatingWire)
{
if (TryFinishPlacingWire())
{
CancelPlacingItems();
}
}
if (wireToEdit != null)
{
wireEditPointSelectedIndex = -1;
isMovingWireEditPoint = false;
}
}
void UpdatePositionsToMouse()
{
Vector2 mousePos = InputHelper.MousePosWorld;
bool snapToGrid = project.ShouldSnapToGrid;
if (IsCreatingWire)
{
WireToPlace.SetLastWirePoint(mousePos);
}
else if (IsMovingSelection || isPlacingNewElements)
{
Vector2 moveOffset = mousePos - moveElementMouseStartPos;
for (int i = 0; i < SelectedElements.Count; i++)
{
IMoveable element = SelectedElements[i];
bool isBusTerminus = element is SubChipInstance s && ChipTypeHelper.IsBusTerminusType(s.ChipType);
Vector2 multiElementOffset = isBusTerminus ? Vector2.zero : Vector2.down * (itemPlacementCurrVerticalSpacing * i);
Vector2 totalOffset = moveOffset + multiElementOffset;
Vector2 targetPos = element.MoveStartPosition + totalOffset;
if (snapToGrid)
{
if (i == 0)
{
targetPos = GridHelper.SnapMovingElementToGrid(element, totalOffset, false, true);
}
// Snap additional selected elements relative to the first one. (Snapping each element independently results in a 'jiggling' effect)
else
{
// Get snap points prior to movement
IMoveable prevElement = SelectedElements[i - 1];
Vector2 snapPointStartA = prevElement.MoveStartPosition + (prevElement.SnapPoint - prevElement.Position);
Vector2 snapPointStartB = element.MoveStartPosition + (element.SnapPoint - element.Position);
// Base curr element's snap pos on prev element, adding the (snapped) difference between their initial snap points
Vector2 placementManualOffset = isBusTerminus ? Vector2.zero : Vector2.down * itemPlacementCurrVerticalSpacing;
Vector2 snappedOffset = GridHelper.SnapToGrid(snapPointStartB - snapPointStartA + placementManualOffset, false, true);
Vector2 elementSnapPointOffset = element.SnapPoint - element.Position;
targetPos = prevElement.SnapPoint + snappedOffset - elementSnapPointOffset;
}
}
// When using shift to duplicate new element, don't use straight line mode unless pressed again
if (isPlacingNewElements && !KeyboardShortcuts.MultiModeHeld) hasExittedMultiModeSincePlacementStart = true;
if (KeyboardShortcuts.StraightLineModeHeld && element.HasReferencePointForStraightLineMovement && (!isPlacingNewElements || hasExittedMultiModeSincePlacementStart))
{
Vector2 offset = targetPos - element.StraightLineReferencePoint;
float ox = Mathf.Abs(offset.x);
float oy = Mathf.Abs(offset.y);
bool canChangeState = straightLineMoveState == StraightLineMoveState.None || isPlacingNewElements;
if (Mathf.Max(ox, oy) > 0.035f && canChangeState)
{
straightLineMoveState = ox > oy ? StraightLineMoveState.Horizontal : StraightLineMoveState.Vertical;
}
if (straightLineMoveState == StraightLineMoveState.Horizontal) offset.y = 0;
else if (straightLineMoveState == StraightLineMoveState.Vertical) offset.x = 0;
targetPos = element.StraightLineReferencePoint + offset;
}
element.Position = targetPos;
// Test if is legal position
bool legal = true;
foreach (IMoveable obstacle in Obstacles)
{
if (element.BoundingBox.Overlaps(obstacle.BoundingBox))
{
legal = false;
break;
}
}
element.IsValidMovePos = legal;
}
// Update wires when their parents are moved
if (isPlacingNewElements)
{
foreach (WireInstance wire in DuplicatedWires)
{
Vector2 delA = wire.SourcePin.parent.Position - wire.SourcePin.parent.MoveStartPosition;
Vector2 delB = wire.TargetPin.parent.Position - wire.TargetPin.parent.MoveStartPosition;
// Parent chips may have been moved by slightly different amounts if snapping is enabled, so just take average
wire.MoveOffset = (delA + delB) / 2;
}
}
else
{
foreach (WireInstance wire in ActiveDevChip.Wires)
{
// If both ends of the wire are being moved, then move the entire wire
if (IsSelected(wire.SourcePin.parent) && IsSelected(wire.TargetPin.parent))
{
Vector2 delA = wire.SourcePin.parent.Position - wire.SourcePin.parent.MoveStartPosition;
Vector2 delB = wire.TargetPin.parent.Position - wire.TargetPin.parent.MoveStartPosition;
// Parent chips may have been moved by slightly different amounts if snapping is enabled, so just take average
wire.MoveOffset = (delA + delB) / 2;
}
}
}
}
else if (isMovingWireEditPoint)
{
wireToEdit.SetWirePointWithSnapping(mousePos, wireEditPointSelectedIndex, wireEditPointOld);
}
}
void StartMovingSelectedItems()
{
IsMovingSelection = true;
moveElementMouseStartPos = InputHelper.MousePosWorld;
foreach (IMoveable moveableElement in SelectedElements)
{
moveableElement.MoveStartPosition = moveableElement.Position;
moveableElement.StraightLineReferencePoint = moveableElement.Position;
moveableElement.HasReferencePointForStraightLineMovement = true;
}
Obstacles = ActiveDevChip.Elements.Where(e => !e.IsSelected).ToArray();
}
bool TryFinishPlacingWire()
{
if (InteractionState.ElementUnderMouse is PinInstance pin)
{
if (CanCompleteWireConnection(pin))
{
WireInstance.ConnectionInfo info = new() { pin = pin };
CompleteConnection(info);
return true;
}
}
else if (InteractionState.ElementUnderMouse is WireInstance connectionWire)
{
if (CanCompleteWireConnection(connectionWire, out PinInstance endPin))
{
WireInstance.ConnectionInfo info = CreateWireToWireConnectionInfo(connectionWire, endPin);
CompleteConnection(info);
return true;
}
}
return false;
void CompleteConnection(WireInstance.ConnectionInfo info)
{
WireToPlace.FinishPlacingWire(info);
ActiveDevChip.AddWire(WireToPlace, false);
ActiveDevChip.UndoController.RecordAddWire(WireToPlace);
ModLoader.NotifyMods((mod, args) => mod.OnPlaceWire(args), new IMod.WireEventArgs
{
SourcePinName = WireToPlace.SourcePin.Name,
TargetPinName = WireToPlace.TargetPin.Name,
BitCount = (int) WireToPlace.bitCount
});
}
}
bool CanAddWirePoint()
{
// Can add wire point if mouse is not over anything else
if (InteractionState.ElementUnderMouse is null) return true;
// Can add wire point if mouse is over an existing wire, but that wire comes from same pin as current wire (might want to trace over existing wire for example)
if (InteractionState.ElementUnderMouse is WireInstance wire)
{
if (wire.SourcePin == WireToPlace.FirstPin || wire.TargetPin == WireToPlace.FirstPin) return true;
}
return false;
}
public void StartPlacing(string name)
{
StartPlacing(project.chipLibrary.GetChipDescription(name));
}
public void StartPlacing(ChipDescription chipDescription)
{
StartPlacing(chipDescription, InputHelper.MousePosWorld, false);
}
public IMoveable StartPlacing(ChipDescription chipDescription, Vector2 position, bool isDuplicating)
{
IMoveable elementToPlace = CreateElementFromChipDescription(chipDescription);
StartPlacing(elementToPlace, position, isDuplicating);
return elementToPlace;
}
void StartPlacing(IMoveable elementToPlace, Vector2 position, bool isDuplicating)
{
const float busPairSpacing = DrawSettings.GridSize * 8;
newElementsAreDuplicatedElements = isDuplicating;
if (!isPlacingNewElements)
{
CancelEverything();
isPlacingNewElements = true;
hasExittedMultiModeSincePlacementStart = false;
StartMovingSelectedItems();
}
ChipType chipType;
if (elementToPlace is DevPinInstance devPin) chipType = ChipTypeHelper.GetPinType(devPin.IsInputPin, devPin.BitCount);
else chipType = ((SubChipInstance)elementToPlace).ChipType;
// Place bus terminus to right of bus origin
if (ChipTypeHelper.IsBusTerminusType(chipType) && !isDuplicating)
{
elementToPlace.MoveStartPosition = SelectedElements[^1].MoveStartPosition + Vector2.right * busPairSpacing;
elementToPlace.HasReferencePointForStraightLineMovement = false;
}
// If placing multiple elements simultaneously, place the new element below the previous one
// (unless duplicating elements, in which case their relative positions should be preserved)
else if (SelectedElements.Count > 0 && !isDuplicating)
{
float spacing = (elementToPlace.SelectionBoundingBox.Size.y + SelectedElements[^1].SelectionBoundingBox.Size.y) / 2;
Vector2 prevElementPos = SelectedElements[^1].MoveStartPosition;
// If prev element was bus terminus, we want the midpoint between the bus origin and terminus pair
if (SelectedElements[^1] is SubChipInstance s && ChipTypeHelper.IsBusTerminusType(s.ChipType))
{
prevElementPos = (prevElementPos + SelectedElements[^2].MoveStartPosition) / 2;
}
elementToPlace.MoveStartPosition = prevElementPos + Vector2.down * spacing;
elementToPlace.HasReferencePointForStraightLineMovement = false;
}
else
{
moveElementMouseStartPos = InputHelper.MousePosWorld;
elementToPlace.MoveStartPosition = position;
elementToPlace.StraightLineReferencePoint = position;
elementToPlace.HasReferencePointForStraightLineMovement = isDuplicating;
}
Select(elementToPlace);
// When placing bus, auto-place the corresponding bus terminus (unless duplicating an existing bus)
if (ChipTypeHelper.IsBusOriginType(chipType) && !isDuplicating)
{
elementToPlace.MoveStartPosition -= Vector2.right * busPairSpacing / 2;
ChipType terminusType = ChipTypeHelper.GetCorrespondingBusTerminusType(chipType);
ChipDescription terminusDescription = Project.ActiveProject.chipLibrary.GetChipDescription(ChipTypeHelper.GetName(terminusType));
SubChipInstance terminus = (SubChipInstance)StartPlacing(terminusDescription, position, false);
SubChipInstance busOrigin = (SubChipInstance)elementToPlace;
busOrigin.SetLinkedBusPair(terminus);
terminus.SetLinkedBusPair(busOrigin);
}
}
IMoveable CreateElementFromChipDescription(ChipDescription chipDescription)
{
IMoveable elementToPlace;
int instanceID = IDGenerator.GenerateNewElementID(ActiveDevChip);
// Input/output dev pins are represented as chips for convenience
(bool isInput, bool isOutput, PinBitCount numBits) ioPinInfo = ChipTypeHelper.IsInputOrOutputPin(chipDescription.ChipType);
if (ioPinInfo.isInput || ioPinInfo.isOutput) // Dev pin
{