-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualBoards.inc
2175 lines (1974 loc) · 80.9 KB
/
visualBoards.inc
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
{$ifdef includeInterface}
{ T_visualBoard }
P_watcher=^T_watcher;
{ T_watcher }
T_watcher=object
gate:P_visualGate;
input:boolean;
ioIndex:longint;
CONSTRUCTOR create(CONST gate_:P_visualGate; CONST input_:boolean; CONST ioIndex_:longint);
DESTRUCTOR destroy;
FUNCTION getCanvasPos(CONST uiAdapter:P_uiAdapter):T_point;
PROCEDURE paint(CONST uiAdapter:P_uiAdapter; CONST Canvas:TCanvas);
FUNCTION isAtCanvasPos(CONST uiAdapter:P_uiAdapter; CONST x,y:longint):boolean;
end;
T_visualBoard=object(T_captionedAndIndexed)
private
wiresCs:TRTLCriticalSection;
associatedPalette:P_abstractPrototypeSource;
myIndex:longint;
captionString:shortstring;
descriptionString:string;
simStepCounter:longint;
wiresUpToDate:boolean;
ui:P_uiAdapter;
wiringTask:P_wiringTask;
gridX0,gridX1,gridY0,gridY1:longint;
PROCEDURE ScrollbarScroll(Sender: TObject; ScrollCode: TScrollCode; VAR ScrollPos: integer);
PROCEDURE boardMouseDown(Sender: TObject; button: TMouseButton; Shift: TShiftState; X, Y: integer);
PROCEDURE boardMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
PROCEDURE boardMouseUp(Sender: TObject; button: TMouseButton; Shift: TShiftState; X, Y: integer);
PROCEDURE reshapeGrid(CONST newGridX1,newGridY1:longint; CONST byMouseEvent:boolean);
PROCEDURE dropWatchersAssociatedWith(CONST gate:P_visualGate);
PROCEDURE deleteMarkedGates;
PROCEDURE unhoverAllWires;
public
underlyingPrototype:P_visualBoard;
modified:boolean;
inputs : T_visualGateArray;
outputs: T_visualGateArray;
gates : T_visualGateArray;
wires : array of T_visualWire;
watchers:array of P_watcher;
wiresHaveChangedInBackground:boolean;
CONSTRUCTOR create(CONST palette:P_abstractPrototypeSource);
PROCEDURE clear;
DESTRUCTOR destroy;
PROPERTY isModified:boolean read modified;
PROCEDURE handleInputKey(CONST key:word; CONST shiftPressed:boolean);
PROCEDURE setIndexInPalette(CONST index:longint; CONST dropBackReference:boolean);
PROCEDURE checkBoardExtend(CONST movingLeft,movingBottom:boolean; CONST autoshrink:boolean=false);
PROCEDURE detachUI;
PROCEDURE attachUI(CONST uiAdapter:P_uiAdapter);
PROCEDURE elementAdded(CONST newElement:P_visualGate; CONST mouseIsInBoard:boolean);
PROCEDURE repositionElement(CONST element:P_visualGate ; CONST skipSaveUndo,mouseIsInBoard:boolean);
PROCEDURE repositionElement(CONST elements: T_visualGateArray; CONST skipSaveUndo,mouseIsInBoard:boolean);
PROCEDURE remove(CONST element:P_visualGate ; CONST skipSaveUndo:boolean);
PROCEDURE remove(CONST elements: T_visualGateArray; CONST skipSaveUndo:boolean);
FUNCTION loadFromStream(VAR stream:T_bufferedInputStreamWrapper; CONST includeFixing:boolean):boolean;
PROCEDURE saveToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST includeFixing:boolean);
PROCEDURE savePaletteEntryToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST paletteIndex:longint);
PROCEDURE loadPaletteEntryFromStream(VAR stream:T_bufferedInputStreamWrapper; CONST paletteIndex:longint);
FUNCTION extractBehavior:P_compoundGate;
PROCEDURE extractChallenge(CONST challengePalette: P_abstractPrototypeSource; OUT expectedBehavior: P_visualBoard);
FUNCTION getInterfaces:T_gateInterfaces;
FUNCTION cloneAsTrueCopy(CONST fixProperties:boolean=false):P_visualBoard;
FUNCTION cloneWithBackReference:P_visualBoard;
PROCEDURE enumerateIo;
FUNCTION getWireGraph(CONST dropExistingWires:boolean):P_wireGraph;
PROCEDURE rewire(CONST replaceExisting,expensiveScan:boolean);
PROCEDURE updateWiresFromTask;
PROCEDURE paintWires(CONST init:boolean=true; CONST draw:boolean=true);
PROCEDURE forceWireRepaint;
PROCEDURE paintWirePreview(CONST wireStart:T_point; CONST screenX,screenY:longint);
FUNCTION getCaption: shortstring; virtual;
PROCEDURE setCaption(CONST s: string); virtual;
FUNCTION getDescription: string; virtual;
PROCEDURE setDescription(CONST s: string); virtual;
FUNCTION getIndexInPalette: longint; virtual;
PROCEDURE afterGatePropertiesEdited(CONST editedGate:P_visualGate; CONST editedIsBoardElement:boolean);
FUNCTION isInputConnected(CONST gate:P_visualGate; CONST gateInputIndex:longint):boolean;
FUNCTION isOutputputConnected(CONST gate:P_visualGate; CONST gateOutputIndex:longint):boolean;
PROCEDURE addWire(CONST sourceGate:P_visualGate; CONST sourceOutputIndex:longint;
CONST sinkGate:P_visualGate; CONST sinkInputIndex:longint);
PROCEDURE reset(CONST includeInputs:boolean=false);
FUNCTION coSimulateSteps(CONST count,timeOutInTicks: longint; CONST expectedBehavior:P_abstractGate):longint;
FUNCTION simulateSteps(CONST count,timeOutInTicks: longint):longint;
FUNCTION outputsMatch(CONST expected:P_compoundGate):boolean;
FUNCTION interfacesMatch(CONST expected:P_compoundGate):boolean;
PROCEDURE setInputs(CONST inputWires:T_wireValueArray);
FUNCTION startMultiDrag(CONST primaryGate:P_visualGate):boolean;
FUNCTION isVisualBoard: boolean; virtual;
FUNCTION usesPrototype(CONST p:P_captionedAndIndexed; CONST transitiveScan:boolean):boolean;
PROCEDURE prototypeUpdated(CONST oldPrototype,newPrototype:P_captionedAndIndexed);
PROCEDURE copySelectionToClipboard;
PROCEDURE pasteFromClipboard(CONST tgtX,tgtY:longint);
PROCEDURE paintBoard(CONST exludeDragged:boolean=false);
FUNCTION getGateCount:T_gateCount;
FUNCTION getInfoLabelText:string;
PROCEDURE moveToPalette(CONST newPalette:P_abstractPrototypeSource);
FUNCTION equals(CONST other:P_visualBoard):boolean;
FUNCTION hash:word;
FUNCTION interfaceHash:word;
FUNCTION newGateFromPrototype: P_abstractGate; virtual;
end;
{$endif}
{$ifdef includeImplementation}
CONSTRUCTOR T_visualBoard.create(CONST palette: P_abstractPrototypeSource);
begin
initCriticalSection(wiresCs);
associatedPalette:=palette;
clear;
wiringTask:=nil;
end;
PROCEDURE T_visualBoard.clear;
VAR i:longint;
begin
myIndex:=-1;
underlyingPrototype:=nil;
gridX0:=0;
gridX1:=32;
gridY0:=0;
gridY1:=32;
captionString:='';
descriptionString:='';
for i:=0 to length(inputs)-1 do dispose(inputs[i],destroy);
setLength(inputs,0);
for i:=0 to length(outputs)-1 do dispose(outputs[i],destroy);
setLength(outputs,0);
for i:=0 to length(gates)-1 do dispose(gates[i],destroy);
setLength(gates,0);
setLength(wires,0);
wiresUpToDate:=false;
if ui<>nil then begin
ui^.hideIoEdit;
ui^.updateTitleLayer;
end;
setLength(watchers,0);
simStepCounter:=0;
modified:=false;
disposeTask(wiringTask);
end;
DESTRUCTOR T_visualBoard.destroy;
begin
detachUI;
clear;
enterCriticalSection(wiresCs);
leaveCriticalSection(wiresCs);
doneCriticalSection(wiresCs);
end;
PROCEDURE T_visualBoard.handleInputKey(CONST key: word;
CONST shiftPressed: boolean);
CONST DELETE_KEY=46;
VAR i :longint;
idx:longint=-1;
inputGate:P_visualGate;
begin
if key=DELETE_KEY then begin
deleteMarkedGates;
exit;
end;
if key>ord('Z') then exit;
for i:=0 to min(length(C_inputKey),length(inputs))-1 do if ord(C_inputKey[i])=key then idx:=i;
if (idx<0) then exit;
inputGate:=inputs[idx];
if shiftPressed
then ui^.showIoEdit(inputGate)
else inputGate^.flipInputBits;
end;
PROCEDURE T_visualBoard.setIndexInPalette(CONST index: longint; CONST dropBackReference:boolean);
begin
if dropBackReference then underlyingPrototype:=nil;
myIndex:=index;
end;
PROCEDURE T_visualBoard.checkBoardExtend(CONST movingLeft,
movingBottom: boolean; CONST autoshrink: boolean);
VAR nonInX0,nonInY0:longint;
PROCEDURE growIfNecessary(CONST gate:P_visualGate);
VAR corner:T_point;
begin
if gate^.gridPos[0]<0 then gate^.gridPos[0]:=0;
if gate^.gridPos[1]<0 then gate^.gridPos[1]:=0;
corner:=gate^.gridPos;
if gate^.behavior^.gateType<>gt_input then begin
if corner[0]<nonInX0 then nonInX0:=corner[0];
if corner[1]<nonInY0 then nonInY0:=corner[1];
end;
if gate^.behavior^.gateType=gt_output then begin
if gate^.ioLocations.numberOfTopInputs>0
then begin corner[0]+=gate^.gridWidth; if movingBottom or autoshrink then corner[1]:=0; end
else begin corner[1]+=gate^.gridHeight; if movingLeft or autoshrink then corner[0]:=0; end;
end else begin
corner[0]+=gate^.gridWidth; if gate^.ioLocations.numberOfRightOutputs >0 then inc(corner[0]);
corner[1]+=gate^.gridHeight; if gate^.ioLocations.numberOfBottomOutputs>0 then inc(corner[1]);
end;
if gate^.behavior^.gateType=gt_input then begin
if gate^.ioLocations.numberOfRightOutputs>0
then gridX0:=4
else gridY0:=4;
end;
if (corner[0]>gridX1) then gridX1:=corner[0];
if (corner[1]>gridY1) then gridY1:=corner[1];
end;
VAR gate:P_visualGate;
oldGridX1,oldGridY1,
oldGridX0,oldGridY0:longint;
delta: T_point;
begin
nonInX0:=maxLongint;
nonInY0:=maxLongint;
oldGridX0:=gridX0; gridX0:=0;
oldGridY0:=gridY0; gridY0:=0;
oldGridX1:=gridX1;
oldGridY1:=gridY1;
if autoshrink then begin
gridX1:=gridX0;
gridY1:=gridY0;
end;
for gate in inputs do growIfNecessary(gate);
for gate in outputs do growIfNecessary(gate);
for gate in gates do growIfNecessary(gate);
if autoshrink then begin
gridX1+=1;
gridY1+=1;
end;
if (gridX0<>oldGridX0) or (gridY0<>oldGridY0) then begin
delta:=pointOf(gridX0-oldGridX0,gridY0-oldGridY0);
if nonInX0>=gridX0 then delta[0]:=0;
if nonInY0>=gridY0 then delta[1]:=0;
for gate in gates do gate^.gridPos+=delta;
for gate in outputs do gate^.gridPos+=delta;
gridX1+=delta[0];
gridY1+=delta[1];
end;
for gate in outputs do begin
if P_outputGate(gate^.behavior)^.onLeftOrRightSide
then gate^.gridPos[0]:=gridX1
else gate^.gridPos[1]:=gridY1;
end;
if ((gridX1<>oldGridX1) or (gridY1<>oldGridY1)) and (ui<>nil) then begin
ui^.updateBoardScrollbars;
rewire(false,true);
end;
wiresUpToDate:=false;
end;
PROCEDURE T_visualBoard.detachUI;
VAR e:P_visualGate;
begin
if ui<>nil then begin
ui^.activeBoard:=nil;
ui^.hideIoEdit;
end;
for e in inputs do begin e^.uiAdapter:=nil; end;
for e in outputs do begin e^.uiAdapter:=nil; end;
for e in gates do begin e^.uiAdapter:=nil; end;
ui:=nil;
end;
PROCEDURE T_visualBoard.attachUI(CONST uiAdapter: P_uiAdapter);
VAR e:P_visualGate;
begin
if uiAdapter^.activeBoard<>nil then uiAdapter^.activeBoard^.detachUI;
wiresUpToDate:=false;
ui:=uiAdapter;
uiAdapter^.activeBoard:=@self;
for e in inputs do begin e^.uiAdapter:=uiAdapter; end;
for e in outputs do begin e^.uiAdapter:=uiAdapter; end;
for e in gates do begin e^.uiAdapter:=uiAdapter; end;
checkBoardExtend(false,false);
rewire(false,true); //only missing wires...
ui^.resetState;
ui^.uiElement.boardImage.OnMouseMove:=@boardMouseMove;
ui^.uiElement.boardImage.OnMouseDown:=@boardMouseDown;
ui^.uiElement.boardImage.OnMouseUp :=@boardMouseUp;
ui^.uiElement.boardHorizontalScrollBar.OnScroll:=@ScrollbarScroll;
ui^.uiElement.boardVerticalScrollBar .OnScroll:=@ScrollbarScroll;
ui^.updateTitleLayer;
end;
PROCEDURE T_visualBoard.elementAdded(CONST newElement: P_visualGate; CONST mouseIsInBoard: boolean);
VAR element:P_visualGate;
begin
modified:=true;
element:=newElement;
if not(mouseIsInBoard)
then dispose(element,destroy)
else begin
if ui<>nil then ui^.saveStateToUndoList;
case element^.behavior^.gateType of
gt_input: begin setLength(inputs ,length(inputs )+1); inputs [length(inputs )-1]:=element; end;
gt_output: begin setLength(outputs,length(outputs)+1); outputs[length(outputs)-1]:=element; end;
else begin setLength(gates ,length(gates )+1); gates [length(gates )-1]:=element; end;
end;
if element^.behavior^.gateType in [gt_input,gt_output] then enumerateIo;
if ui<>nil then repositionElement(element,true,true);
end;
if ui<>nil then ui^.callback.boardModifiedCallback();
end;
PROCEDURE T_visualBoard.repositionElement(CONST element: P_visualGate;
CONST skipSaveUndo, mouseIsInBoard: boolean);
VAR wrap:T_visualGateArray;
begin
setLength(wrap,1);
wrap[0]:=element;
repositionElement(wrap,skipSaveUndo,mouseIsInBoard);
end;
PROCEDURE T_visualBoard.repositionElement(CONST elements: T_visualGateArray;
CONST skipSaveUndo, mouseIsInBoard: boolean);
VAR element:P_visualGate;
elementIndex:longint=0;
FUNCTION overlapsAnyOther:boolean;
FUNCTION isInElementsStillInNeedOfPositioning(CONST g:P_visualGate):boolean;
VAR i:longint;
begin
for i:=elementIndex to length(elements)-1 do if elements[i]=g then exit(true);
result:=false;
end;
VAR other:P_visualGate;
begin
for other in inputs do if not(isInElementsStillInNeedOfPositioning(other)) and element^.overlaps(other) then exit(true);
for other in outputs do if not(isInElementsStillInNeedOfPositioning(other)) and element^.overlaps(other) then exit(true);
for other in gates do if not(isInElementsStillInNeedOfPositioning(other)) and element^.overlaps(other) then exit(true);
result:=false;
end;
PROCEDURE repositionVertically;
VAR stepY:longint=1;
begin
if element^.gridPos[1]<4 then element^.gridPos[1]:=4;
if element^.gridPos[1]+4>gridY1 then reshapeGrid(gridX1,element^.gridPos[1]+4,false);
while overlapsAnyOther do begin
element^.gridPos[1]+=stepY;
if stepY<0 then stepY:=-1-stepY else stepY:=1-stepY;
if element^.gridPos[1]<element^.gridHeight then element^.gridPos[1]:=element^.gridHeight;
if element^.gridPos[1]> gridY1 then element^.gridPos[1]:=gridY1;
end;
end;
PROCEDURE repositionHorizontally;
VAR stepX:longint=1;
begin
if element^.gridPos[0]<4 then element^.gridPos[0]:=4;
if element^.gridPos[0]+4>gridX1 then reshapeGrid(element^.gridPos[0]+4,gridY1,false);
while overlapsAnyOther do begin
element^.gridPos[0]+=stepX;
if stepX<0 then stepX:=-1-stepX else stepX:=1-stepX;
if element^.gridPos[0]<element^.gridWidth then element^.gridPos[0]:=element^.gridWidth;
if element^.gridPos[0]>gridX1 then element^.gridPos[0]:=gridX1;
end;
end;
PROCEDURE reposition;
VAR stepX:longint=1;
stepY:longint=0;
initialX,initialY:longint;
begin
if element^.gridPos[1]<gridY0 then element^.gridPos[1]:=gridY0;
if element^.gridPos[0]<gridX0 then element^.gridPos[0]:=gridX0;
if element^.gridPos[1]+4>gridY1 then reshapeGrid(gridX1,element^.gridPos[1]+4,false);
if element^.gridPos[0]+4>gridX1 then reshapeGrid(element^.gridPos[0]+4,gridY1,false);
initialX:=element^.gridPos[0];
initialY:=element^.gridPos[1];
while (overlapsAnyOther or
(element^.gridPos[0]<gridX0) or (element^.gridPos[0]+element^.gridWidth>gridX1) or
(element^.gridPos[1]<gridY0) or (element^.gridPos[1]+element^.gridHeight>gridY1)) and (stepX<100) do begin
element^.gridPos[0]+=stepX;
element^.gridPos[1]+=stepY;
if stepX>0 then begin stepY:= stepX; stepX:=0; end
else if stepY>0 then begin stepX:=-1-stepY; stepY:=0; end
else if stepX<0 then begin stepY:= stepX; stepX:=0; end
else begin stepX:= 1-stepY; stepY:=0; end;
end;
if not(overlapsAnyOther) then exit;
element^.gridPos[0]:=initialX;
element^.gridPos[1]:=initialY;
while (overlapsAnyOther or
(element^.gridPos[0]<gridX0) or
(element^.gridPos[1]<gridY0)) and (stepX<100) do begin
element^.gridPos[0]+=stepX;
element^.gridPos[1]+=stepY;
if stepX>0 then begin stepY:= stepX; stepX:=0; end
else if stepY>0 then begin stepX:=-1-stepY; stepY:=0; end
else if stepX<0 then begin stepY:= stepX; stepX:=0; end
else begin stepX:= 1-stepY; stepY:=0; end;
end;
end;
begin
if not skipSaveUndo then ui^.saveStateToUndoList;
if not(mouseIsInBoard) then begin
remove(elements,true);
exit;
end;
for element in elements do begin
element^.gridPos:=ui^.canvasToGrid(element^.canvasPos);
case element^.behavior^.gateType of
gt_input: begin
if element^.gridPos[0]-gridX0<=element^.gridPos[1]-gridY0 then begin
element^.gridPos[0]:=0;
repositionVertically;
P_inputGate(element^.behavior)^.onLeftOrRightSide:=true;
end else begin
element^.gridPos[1]:=0;
repositionHorizontally;
P_inputGate(element^.behavior)^.onLeftOrRightSide:=false;
end;
element^.setupVisuals;
end;
gt_output: begin
if gridX1-element^.gridPos[0]<gridY1-element^.gridPos[1] then begin
element^.gridPos[0]:=gridX1;
repositionVertically;
P_outputGate(element^.behavior)^.onLeftOrRightSide:=true;
end else begin
element^.gridPos[1]:=gridY1;
repositionHorizontally;
P_outputGate(element^.behavior)^.onLeftOrRightSide:=false;
end;
element^.setupVisuals;
end;
else begin
checkBoardExtend(false,false);
reposition;
end;
end;
inc(elementIndex);
end;
checkBoardExtend(false,false);
rewire(true,true);
enumerateIo;
ui^.paintAll;
ui^.uiElement.boardImage.repaint;
end;
PROCEDURE T_visualBoard.remove(CONST element: P_visualGate;
CONST skipSaveUndo: boolean);
VAR wrap:T_visualGateArray;
begin
setLength(wrap,1);
wrap[0]:=element;
remove(wrap,skipSaveUndo);
end;
PROCEDURE T_visualBoard.remove(CONST elements: T_visualGateArray;
CONST skipSaveUndo: boolean);
VAR
element: P_visualGate;
j, i: integer;
begin
if not(skipSaveUndo) then ui^.saveStateToUndoList;
for element in elements do if not element^.fixedPosition then begin
//For challenges with fixed number of gates, this has to be added to the palette again.
associatedPalette^.countUpGate(element^.behavior);
j:=0;
enterCriticalSection(wiresCs);
for i:=0 to length(wires)-1 do begin
wires[i].dropWiresAssociatedWith(element);
if length(wires[i].sink)>0 then begin
wires[j]:=wires[i];
inc(j);
end;
end;
setLength(wires,j);
leaveCriticalSection(wiresCs);
dropWatchersAssociatedWith(element);
j:=0; for i:=0 to length(inputs )-1 do if inputs [i]=element then dispose(inputs [i],destroy) else begin inputs [j]:=inputs [i]; inc(j); end; setLength(inputs ,j);
j:=0; for i:=0 to length(outputs)-1 do if outputs[i]=element then dispose(outputs[i],destroy) else begin outputs[j]:=outputs[i]; inc(j); end; setLength(outputs,j);
j:=0; for i:=0 to length(gates )-1 do if gates [i]=element then dispose(gates [i],destroy) else begin gates [j]:=gates [i]; inc(j); end; setLength(gates ,j);
end;
enumerateIo;
rewire(true,true);
ui^.callback.boardModifiedCallback();
end;
FUNCTION T_visualBoard.loadFromStream(VAR stream: T_bufferedInputStreamWrapper; CONST includeFixing: boolean): boolean;
FUNCTION gateByIndex(index:longint):P_visualGate;
begin
if index<length(inputs) then exit(inputs[index]); dec(index,length(inputs));
if index<length(gates) then exit(gates [index]); dec(index,length(gates));
result:=outputs[index];
end;
VAR i,j:longint;
inputGate :P_inputGate;
outputGate:P_outputGate;
behavior :P_abstractGate;
begin
try
result:=true;
captionString:=stream.readAnsiString;
descriptionString:=stream.readAnsiString;
myIndex:=stream.readInteger;
if not(stream.allOkay) then exit(false);
gridX1:=0;
gridY1:=0;
setLength(inputs,stream.readNaturalNumber);
for i:=0 to length(inputs)-1 do begin
new(inputGate,create);
inputGate^.readMetaDataFromStream(stream);
new(inputs[i],create(inputGate));
inputs[i]^.gridPos:=readPoint(stream);
if includeFixing then begin
inputs[i]^.fixedPosition:=stream.readBoolean;
inputs[i]^.fixedProperties:=stream.readBoolean;
end;
end;
if not(stream.allOkay) then exit(false);
setLength(gates,stream.readNaturalNumber);
for i:=0 to length(gates)-1 do begin
behavior:=associatedPalette^.readGate(stream);
new(gates[i],create(behavior));
gates[i]^.gridPos:=readPoint(stream);
if includeFixing then begin
gates[i]^.fixedPosition:=stream.readBoolean;
gates[i]^.fixedProperties:=stream.readBoolean;
end;
end;
if not(stream.allOkay) then exit(false);
setLength(outputs,stream.readNaturalNumber);
for i:=0 to length(outputs)-1 do begin
new(outputGate,create);
outputGate^.readMetaDataFromStream(stream);
new(outputs[i],create(outputGate));
outputs[i]^.gridPos:=readPoint(stream);
if includeFixing then begin
outputs[i]^.fixedPosition:=stream.readBoolean;
outputs[i]^.fixedProperties:=stream.readBoolean;
end;
end;
if not(stream.allOkay) then exit(false);
setLength(wires,stream.readNaturalNumber);
for i:=0 to length(wires)-1 do with wires[i] do begin
source:=gateByIndex(stream.readNaturalNumber);
sourceOutputIndex:= stream.readNaturalNumber;
setLength(sink,stream.readNaturalNumber);
for j:=0 to length(sink)-1 do with sink[j] do begin
gate:=gateByIndex(stream.readNaturalNumber);
gateInputIndex:= stream.readNaturalNumber;
end;
end;
except
stream.logWrongTypeError;
end;
result:=stream.allOkay;
end;
PROCEDURE T_visualBoard.saveToStream(VAR stream: T_bufferedOutputStreamWrapper;
CONST includeFixing: boolean);
FUNCTION gateToIndex(CONST g:P_visualGate):longint;
VAR i:longint;
offset:longint=0;
begin
result:=0;
for i:=0 to length(inputs )-1 do if inputs [i]=g then exit(offset+i); inc(offset,length(inputs));
for i:=0 to length(gates )-1 do if gates [i]=g then exit(offset+i); inc(offset,length(gates));
for i:=0 to length(outputs)-1 do if outputs[i]=g then exit(offset+i);
end;
VAR i,j:longint;
begin
stream.writeAnsiString(captionString);
stream.writeAnsiString(descriptionString);
stream.writeInteger(getIndexInPalette);
stream.writeNaturalNumber(length(inputs));
for i:=0 to length(inputs)-1 do begin
inputs[i]^.behavior^.writeToStream(stream,true);
writePointToStream(stream,inputs[i]^.gridPos);
if includeFixing then begin
stream.writeBoolean(inputs[i]^.fixedPosition);
stream.writeBoolean(inputs[i]^.fixedProperties);
end;
end;
stream.writeNaturalNumber(length(gates));
for i:=0 to length(gates)-1 do begin
gates[i]^.behavior^.writeToStream(stream);
writePointToStream(stream,gates[i]^.gridPos);
if includeFixing then begin
stream.writeBoolean(gates[i]^.fixedPosition);
stream.writeBoolean(gates[i]^.fixedProperties);
end;
end;
stream.writeNaturalNumber(length(outputs));
for i:=0 to length(outputs)-1 do begin
outputs[i]^.behavior^.writeToStream(stream,true);
writePointToStream(stream,outputs[i]^.gridPos);
if includeFixing then begin
stream.writeBoolean(outputs[i]^.fixedPosition);
stream.writeBoolean(outputs[i]^.fixedProperties);
end;
end;
stream.writeNaturalNumber(length(wires));
for i:=0 to length(wires)-1 do with wires[i] do begin
stream.writeNaturalNumber(gateToIndex(source));
stream.writeNaturalNumber(sourceOutputIndex);
stream.writeNaturalNumber(length(sink));
for j:=0 to length(sink)-1 do with sink[j] do begin
stream.writeNaturalNumber(gateToIndex(gate));
stream.writeNaturalNumber(gateInputIndex);
end;
end;
end;
PROCEDURE T_visualBoard.savePaletteEntryToStream(VAR stream: T_bufferedOutputStreamWrapper; CONST paletteIndex: longint);
begin
underlyingPrototype:=nil;
myIndex:=paletteIndex;
saveToStream(stream,false);
end;
PROCEDURE T_visualBoard.loadPaletteEntryFromStream(
VAR stream: T_bufferedInputStreamWrapper; CONST paletteIndex: longint);
begin
loadFromStream(stream,false);
myIndex:=paletteIndex;
end;
FUNCTION T_visualBoard.extractBehavior: P_compoundGate;
VAR cloned:P_compoundGate;
i,j:longint;
FUNCTION gateInClone(CONST gate:P_visualGate):P_abstractGate;
VAR i:longint;
begin
result:=nil;
for i:=0 to length(inputs )-1 do if inputs [i]=gate then exit(cloned^.inputs[i]);
for i:=0 to length(outputs)-1 do if outputs[i]=gate then exit(cloned^.outputs[i]);
for i:=0 to length(gates )-1 do if gates [i]=gate then exit(cloned^.gates[i]);
assert(result<>nil,'Cloning of T_circuitBoard failed');
end;
begin
new(cloned,create(associatedPalette));
cloned^.prototype:=@self;
cloned^.captionString:=captionString;
cloned^.descriptionString:=descriptionString;
setLength(cloned^.inputs ,length(inputs )); for i:=0 to length(inputs )-1 do cloned^.inputs [i]:=P_inputGate (inputs [i]^.behavior^.clone(false));
setLength(cloned^.outputs,length(outputs)); for i:=0 to length(outputs)-1 do cloned^.outputs[i]:=P_outputGate(outputs[i]^.behavior^.clone(false));
setLength(cloned^.gates ,length(gates)); for i:=0 to length(gates )-1 do cloned^.gates [i]:= gates [i]^.behavior^.clone(false);
setLength(cloned^.wires,length(wires));
for i:=0 to length(wires)-1 do begin
cloned^.wires[i].source:=gateInClone(wires[i].source);
cloned^.wires[i].sourceOutputIndex:= wires[i].sourceOutputIndex;
setLength(cloned^.wires[i].sink,length(wires[i].sink));
for j:=0 to length(wires[i].sink)-1 do begin
cloned^.wires[i].sink[j].gate:=gateInClone(wires[i].sink[j].gate);
cloned^.wires[i].sink[j].gateInputIndex:= wires[i].sink[j].gateInputIndex;
end;
end;
result:=cloned;
end;
FUNCTION T_visualBoard.newGateFromPrototype: P_abstractGate;
begin
result:=extractBehavior;
end;
PROCEDURE T_visualBoard.extractChallenge(CONST challengePalette: P_abstractPrototypeSource; OUT expectedBehavior: P_visualBoard);
FUNCTION gateInClone(CONST gate:P_visualGate):P_visualGate;
VAR i:longint;
begin
result:=nil;
for i:=0 to length(inputs )-1 do if inputs [i]=gate then exit(expectedBehavior^.inputs[i]);
for i:=0 to length(outputs)-1 do if outputs[i]=gate then exit(expectedBehavior^.outputs[i]);
for i:=0 to length(gates )-1 do if gates [i]=gate then exit(expectedBehavior^.gates[i]);
assert(result<>nil,'extractChallenge of T_circuitBoard failed');
end;
VAR i,j:longint;
behavior: P_abstractGate;
visual : P_visualGate;
begin
new(expectedBehavior,create(challengePalette));
expectedBehavior^.myIndex:=-1;
expectedBehavior^.captionString:=captionString;
expectedBehavior^.descriptionString:=descriptionString;
setLength(expectedBehavior^.inputs ,length(inputs ));
for i:=0 to length(inputs )-1 do begin
expectedBehavior^.inputs [i]:=inputs[i]^.clone;
end;
setLength(expectedBehavior^.outputs,length(outputs));
for i:=0 to length(outputs)-1 do begin
expectedBehavior^.outputs[i]:=outputs[i]^.clone;
end;
setLength(expectedBehavior^.gates,length(gates));
for i:=0 to length(gates)-1 do begin
if gates[i]^.behavior^.gateType=gt_compound
then begin
behavior:=P_compoundGate(gates[i]^.behavior)^.exportForChallenge(challengePalette);
new(visual,create(behavior));
visual^.gridPos :=gates[i]^.gridPos;
visual^.uiAdapter :=gates[i]^.uiAdapter;
visual^.canvasPos :=gates[i]^.canvasPos;
visual^.ioMode :=gates[i]^.ioMode;
visual^.fixedProperties:=false;
visual^.fixedPosition :=false;
expectedBehavior^.gates[i]:=visual;
end
else begin
expectedBehavior^.gates[i]:=gates[i]^.clone;
end
end;
setLength(expectedBehavior^.wires,length(wires));
for i:=0 to length(wires)-1 do begin
expectedBehavior^.wires[i].source:=gateInClone(wires[i].source);
expectedBehavior^.wires[i].sourceOutputIndex:= wires[i].sourceOutputIndex;
setLength(expectedBehavior^.wires[i].sink,length(wires[i].sink));
for j:=0 to length(wires[i].sink)-1 do begin
expectedBehavior^.wires[i].sink[j].gate:=gateInClone(wires[i].sink[j].gate);
expectedBehavior^.wires[i].sink[j].gateInputIndex:= wires[i].sink[j].gateInputIndex;
end;
end;
end;
FUNCTION T_visualBoard.getInterfaces: T_gateInterfaces;
VAR i:longint;
begin
initialize(result);
setLength(result.inputs,length(inputs));
for i:=0 to length(inputs)-1 do begin
result.inputs[i].name :=inputs[i]^.behavior^.getCaption;
result.inputs[i].wireWidth:=inputs[i]^.behavior^.outputWidth(0);
result.inputs[i].representation:=inputs[i]^.ioMode;
end;
setLength(result.outputs,length(outputs));
for i:=0 to length(outputs)-1 do begin
result.outputs[i].name :=outputs[i]^.behavior^.getCaption;
result.outputs[i].wireWidth:=outputs[i]^.behavior^.inputWidth(0);
result.outputs[i].representation:=outputs[i]^.ioMode;
end;
end;
FUNCTION T_visualBoard.equals(CONST other: P_visualBoard): boolean;
VAR k:longint;
begin
result:=(length(inputs )=length(other^.inputs )) and
(length(outputs)=length(other^.outputs)) and
(length(gates )=length(other^.gates )) and
(length(wires )=length(other^.wires ));
if not(result) then exit(false);
for k:=0 to length(inputs )-1 do if not(inputs [k]^.behavior^.equals(other^.inputs [k]^.behavior)) then exit(false);
for k:=0 to length(outputs)-1 do if not(outputs[k]^.behavior^.equals(other^.outputs[k]^.behavior)) then exit(false);
for k:=0 to length(gates )-1 do if not(gates [k]^.behavior^.equals(other^.gates [k]^.behavior)) then exit(false);
for k:=0 to length(wires)-1 do if not(wires[k].equals(@self,other,other^.wires[k])) then exit(false);
end;
FUNCTION T_visualBoard.hash: word;
VAR g:P_visualGate;
begin
{$Q-}{$R-}
result:= length(inputs);
result:=result*31+length(outputs);
result:=result*31+length(gates);
result:=result*31+length(wires);
for g in gates do result:=result*31+byte(g^.behavior^.gateType);
{$Q+}{$R+}
end;
FUNCTION T_visualBoard.interfaceHash: word;
VAR i:longint;
Interfaces: T_gateInterfaces;
begin
Interfaces:=getInterfaces;
{$Q-}{$R-}
result:= length(Interfaces.inputs);
result:=result*31+length(Interfaces.outputs);
for i:=0 to length(Interfaces.inputs) -1 do result:=result*31+Interfaces.inputs[i].wireWidth;
for i:=0 to length(Interfaces.outputs)-1 do result:=result*31+Interfaces.outputs[i].wireWidth;
{$Q+}{$R+}
end;
FUNCTION T_visualBoard.cloneAsTrueCopy(CONST fixProperties: boolean): P_visualBoard;
VAR cloned:P_visualBoard;
i,j,k:longint;
FUNCTION gateInClone(CONST gate:P_visualGate):P_visualGate;
VAR i:longint;
begin
result:=nil;
for i:=0 to length(inputs )-1 do if inputs [i]=gate then exit(cloned^.inputs[i]);
for i:=0 to length(outputs)-1 do if outputs[i]=gate then exit(cloned^.outputs[i]);
for i:=0 to length(gates )-1 do if gates [i]=gate then exit(cloned^.gates[i]);
assert(result<>nil,'Cloning of T_circuitBoard failed');
end;
begin
new(cloned,create(associatedPalette));
cloned^.wiresHaveChangedInBackground:=false;
cloned^.captionString:=captionString;
cloned^.descriptionString:=descriptionString;
cloned^.myIndex:=-1;
cloned^.underlyingPrototype:=nil;
cloned^.gridX0:=gridX0;
cloned^.gridX1:=gridX1;
cloned^.gridY0:=gridY0;
cloned^.gridY1:=gridY1;
cloned^.modified:=false;
setLength(cloned^.inputs ,length(inputs )); for i:=0 to length(inputs )-1 do begin cloned^.inputs [i]:=inputs [i]^.clone; if fixProperties then cloned^.inputs [i]^.fixedProperties:=true; end;
setLength(cloned^.outputs,length(outputs)); for i:=0 to length(outputs)-1 do begin cloned^.outputs[i]:=outputs[i]^.clone; if fixProperties then cloned^.outputs[i]^.fixedProperties:=true; end;
setLength(cloned^.gates ,length(gates)); for i:=0 to length(gates )-1 do begin cloned^.gates [i]:=gates [i]^.clone; if fixProperties then cloned^.gates [i]^.fixedProperties:=true; end;
setLength(cloned^.wires ,length(wires));
for i:=0 to length(wires)-1 do begin
cloned^.wires[i].source:=gateInClone(wires[i].source);
cloned^.wires[i].sourceOutputIndex:= wires[i].sourceOutputIndex;
setLength(cloned^.wires[i].sink,length(wires[i].sink));
for j:=0 to length(wires[i].sink)-1 do begin
cloned^.wires[i].sink[j].gate:=gateInClone(wires[i].sink[j].gate);
cloned^.wires[i].sink[j].gateInputIndex:= wires[i].sink[j].gateInputIndex;
setLength(cloned^.wires[i].sink[j].path,length(wires[i].sink[j].path));
for k:=0 to length(wires[i].sink[j].path)-1 do cloned^.wires[i].sink[j].path[k]:=wires[i].sink[j].path[k];
end;
end;
result:=cloned;
end;
FUNCTION T_visualBoard.cloneWithBackReference:P_visualBoard;
begin
result:=cloneAsTrueCopy;
result^.underlyingPrototype:=@self;
end;
PROCEDURE T_visualBoard.enumerateIo;
VAR i:longint;
axis:longint;
gateSet:T_visualGateArray;
PROCEDURE initGateSet(CONST axis:longint; CONST baseSet:T_visualGateArray);
VAR k:longint;
ko:longint=0;
begin
setLength(gateSet,length(baseSet));
for k:=0 to length(baseSet)-1 do if P_inputGate(baseSet[k]^.behavior)^.onLeftOrRightSide=(axis=1)
then begin
gateSet[ko]:=baseSet[k];
inc(ko);
end;
setLength(gateSet,ko);
end;
FUNCTION assignMinInGateSetAndRemove(CONST axis,ioIndexToAssign:longint):boolean;
VAR i:longint;
iMin:longint=0;
minCoord:longint=maxLongint;
begin
if length(gateSet)=0 then exit(false);
for i:=0 to length(gateSet)-1 do if gateSet[i]^.gridPos[axis]<minCoord then begin
iMin:=i;
minCoord:=gateSet[i]^.gridPos[axis];
end;
P_inputGate(gateSet[iMin]^.behavior)^.positionIndex:=ioIndexToAssign;
for i:=iMin to length(gateSet)-2 do gateSet[i]:=gateSet[i+1];
setLength(gateSet,length(gateSet)-1);
result:=true;
end;
begin
for i:=0 to length(inputs )-1 do P_inputGate (inputs[i]^.behavior)^.ioIndex:=i;
for i:=0 to length(outputs)-1 do P_outputGate(outputs[i]^.behavior)^.ioIndex:=i;
for axis:=0 to 1 do begin
initGateSet(axis,inputs);
i:=0;
while assignMinInGateSetAndRemove(axis,i) do inc(i);
end;
for axis:=0 to 1 do begin
initGateSet(axis,outputs);
i:=0;
while assignMinInGateSetAndRemove(axis,i) do inc(i);
end;
end;
FUNCTION T_visualBoard.getWireGraph(CONST dropExistingWires: boolean
): P_wireGraph;
VAR graph:P_wireGraph;
PROCEDURE punchOutGate(CONST gate:P_visualGate);
VAR x,y:longint;
i:longint;
p: T_point;
begin
for y:=gate^.gridPos[1] to gate^.gridPos[1]+gate^.gridHeight do
for x:=gate^.gridPos[0] to gate^.gridPos[0]+gate^.gridWidth do graph^.dropNode(pointOf(x,y));
for i:=0 to gate^.behavior^.numberOfOutputs-1 do begin
p:=gate^.getOutputPositionInGridSize(i)+gate^.gridPos;
if p[0]=gate^.gridPos[0]+gate^.gridWidth
then graph^.addUnidirectionalEdge(p,wd_right)
else graph^.addUnidirectionalEdge(p,wd_down);
end;
for i:=0 to gate^.behavior^.numberOfInputs-1 do begin
p:=gate^.getInputPositionInGridSize(i)+gate^.gridPos;
if p[0]=gate^.gridPos[0]
then graph^.addUnidirectionalEdge(p+wd_left,wd_right)
else graph^.addUnidirectionalEdge(p+wd_up ,wd_down);
end;
end;
VAR gate:P_visualGate;
i,j:longint;
begin
new(graph,create(gridX1+1,gridY1+1));
graph^.initDirections;
for gate in inputs do punchOutGate(gate);
for gate in outputs do punchOutGate(gate);
for gate in gates do punchOutGate(gate);
if not(dropExistingWires) then exit(graph);
enterCriticalSection(wiresCs);
for i:=0 to length(wires)-1 do
for j:=0 to length(wires[i].sink)-1 do
graph^.dropWire(wires[i].sink[j].path);
leaveCriticalSection(wiresCs);
result:=graph;
end;
PROCEDURE T_visualBoard.rewire(CONST replaceExisting,expensiveScan: boolean);
PROCEDURE dropWiresReferringToNonexitentPorts;
VAR io, i, jo, j: integer;
begin
io:=0;
for i:=0 to length(wires)-1 do begin
with wires[i] do begin
unhoverAll;
if sourceOutputIndex>=source^.behavior^.numberOfOutputs then setLength(sink,0);
jo:=0;
for j:=0 to length(sink)-1 do
if (sink[j].gateInputIndex<sink[j].gate^.behavior^.numberOfInputs) and
(sink[j].gate^.behavior^.inputWidth(sink[j].gateInputIndex)=source^.behavior^.outputWidth(sourceOutputIndex))
then begin
if j<>jo then sink[jo]:=sink[j];
inc(jo);
end;
setLength(sink,jo);
end;
if jo>0 then begin
if i<>io then wires[io]:=wires[i];
inc(io);
end;
end;
setLength(wires,io);
end;
PROCEDURE sortWires;
VAR gateRank:array of longint;
FUNCTION getGateRank(CONST gate:P_visualGate):longint;
VAR k:longint;
begin
result:=maxLongint;
for k:=0 to length(gates)-1 do if gates[k]=gate then exit(gateRank[k]);
end;