-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualWires.inc
194 lines (176 loc) · 6.18 KB
/
visualWires.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
{$ifdef includeInterface}
{ T_visualWire }
T_visualWire=object
source:P_visualGate;
sourceOutputIndex:longint;
sink:array of record
gate:P_visualGate;
gateInputIndex:longint;
path:T_wirePath;
hoveringOver:boolean;
end;
PROCEDURE dropWiresAssociatedWith(CONST gate:P_visualGate);
FUNCTION simulateStep:boolean;
PROCEDURE paint(CONST zoom:longint; CONST Canvas:TBGRACanvas);
FUNCTION isWirePosition(CONST gridX,gridY:longint; CONST markHover:boolean; VAR hoverChanged:boolean; OUT horizontalWire:boolean):boolean;
PROCEDURE dropWiresTouchingPosition(CONST gridX,gridY:longint; CONST horizontalWire:boolean);
PROCEDURE setPaths(CONST paths:T_wirePathArray);
FUNCTION unhoverAll:boolean;
FUNCTION equals(CONST myBoard,otherBoard:P_visualBoard; CONST other:T_visualWire):boolean;
end;
{$endif}
{$ifdef includeImplementation}
PROCEDURE paintPreviewWire(CONST zoom:longint; CONST Canvas:TBGRACanvas; CONST bitWidth:byte; CONST path:T_wirePath);
VAR w:longint=1;
run:longint;
j:longint;
surround:longint;
begin
if length(path)<=1 then exit;
surround:=zoom div 2;
case bitWidth of
1..3: w:=(1*zoom) shr 4;
4..7: w:=(2*zoom) shr 4;
8..15: w:=(3*zoom) shr 4;
else w:=(4*zoom) shr 4;
end;
if w<1 then w:=1; w+=surround;
for run:=0 to 1 do begin
Canvas.Pen.width:=w;
w+=1-surround;
if run=0
then Canvas.Pen.color:=colorScheme.BOARD_COLOR
else Canvas.Pen.color:=colorScheme.MARK_COLOR;
Canvas.MoveTo(path[0,0]*zoom,path[0,1]*zoom);
for j:=1 to length(path)-1 do Canvas.LineTo(path[j,0]*zoom,path[j,1]*zoom);
end;
end;
PROCEDURE T_visualWire.dropWiresAssociatedWith(CONST gate: P_visualGate);
VAR i,j:longint;
begin
if source=gate then begin
for i:=0 to length(sink)-1 do setLength(sink[i].path,0);
setLength(sink,0);
end else begin
j:=0;
for i:=0 to length(sink)-1 do if sink[i].gate=gate
then setLength(sink[i].path,0)
else begin
sink[j]:=sink[i]; inc(j);
end;
setLength(sink,j);
end;
end;
FUNCTION T_visualWire.simulateStep: boolean;
VAR value:T_wireValue;
i:longint;
begin
value:=source^.behavior^.getOutput(sourceOutputIndex);
result:=false;
for i:=0 to length(sink)-1 do if sink[i].gate^.behavior^.setInput(sink[i].gateInputIndex,value) then result:=true;
end;
PROCEDURE T_visualWire.paint(CONST zoom: longint; CONST Canvas: TBGRACanvas);
VAR w:longint=1;
run, bitWidth:byte;
i,j:longint;
surround:longint;
begin
surround:=zoom div 2;
bitWidth:=source^.behavior^.outputWidth(sourceOutputIndex);
case bitWidth of
1..3: w:=(1*zoom) shr 4;
4..7: w:=(2*zoom) shr 4;
8..15: w:=(3*zoom) shr 4;
else w:=(4*zoom) shr 4;
end;
if w<1 then w:=1;
for run:=0 to 2 do begin
case run of
0: begin
Canvas.Pen.width:=w+surround;
Canvas.Pen.color:=colorScheme.BOARD_COLOR;
end;
1: begin
Canvas.Pen.color:=colorScheme.WIRE_COLOR;
Canvas.Pen.width:=w;
end;
2: begin
Canvas.Pen.color:=colorScheme.MARK_COLOR;
Canvas.Pen.width:=w+1;
end;
end;
for i:=0 to length(sink)-1 do
if (length(sink[i].path)>1) and
((run=0) or
(run=1) and not(sink[i].hoveringOver) or
(run=2) and (sink[i].hoveringOver))
then begin
Canvas.MoveTo(sink[i].path[0,0]*zoom,sink[i].path[0,1]*zoom);
for j:=1 to length(sink[i].path)-1 do Canvas.LineTo(sink[i].path[j,0]*zoom,sink[i].path[j,1]*zoom);
end;
end;
end;
FUNCTION T_visualWire.isWirePosition(CONST gridX, gridY: longint; CONST markHover:boolean; VAR hoverChanged:boolean; OUT horizontalWire: boolean): boolean;
VAR i:longint;
orientation: T_wireDirection;
begin
result:=false;
for i:=0 to length(sink)-1 do if pathContains(sink[i].path,gridX,gridY,orientation) then begin
horizontalWire:=orientation in [wd_left,wd_right];
hoverChanged:=hoverChanged or markHover and not sink[i].hoveringOver;
sink[i].hoveringOver:=markHover;
result:=true;
end else begin
hoverChanged:=hoverChanged or sink[i].hoveringOver;
sink[i].hoveringOver:=false;
end;
end;
PROCEDURE T_visualWire.dropWiresTouchingPosition(CONST gridX, gridY: longint; CONST horizontalWire: boolean);
VAR i:longint;
j:longint=0;
orientation: T_wireDirection;
begin
for i:=0 to length(sink)-1 do if pathContains(sink[i].path,gridX,gridY,orientation) and (horizontalWire=(orientation in [wd_left,wd_right])) then begin
setLength(sink[i].path,0);
end else begin
if i<>j then sink[j]:=sink[i];
inc(j);
end;
setLength(sink,j);
end;
PROCEDURE T_visualWire.setPaths(CONST paths: T_wirePathArray);
VAR i:longint;
begin
for i:=0 to min(length(paths),length(sink))-1 do sink[i].path:=paths[i];
end;
FUNCTION T_visualWire.unhoverAll:boolean;
VAR i:longint;
begin
result:=false;
for i:=0 to length(sink)-1 do begin
result:=result or sink[i].hoveringOver;
sink[i].hoveringOver:=false;
end;
end;
FUNCTION T_visualWire.equals(CONST myBoard,otherBoard:P_visualBoard; CONST other:T_visualWire):boolean;
FUNCTION getGateIndex(CONST gate:P_visualGate; CONST board:P_visualBoard):longint;
VAR g:P_visualGate;
i:longint=0;
begin
for g in board^.inputs do if gate=g then exit(i) else inc(i);
for g in board^.gates do if gate=g then exit(i) else inc(i);
for g in board^.outputs do if gate=g then exit(i) else inc(i);
result:=-1;
end;
VAR i:longint;
begin
result:=(sourceOutputIndex=other.sourceOutputIndex) and
(length(sink)=length(other.sink)) and
(getGateIndex(source,myBoard)=getGateIndex(other.source,otherBoard));
if not(result) then exit(false);
for i:=0 to length(sink)-1 do begin
if ( sink[i].gateInputIndex<> other.sink[i].gateInputIndex) or
(getGateIndex(sink[i].gate,myBoard) <>getGateIndex(other.sink[i].gate,otherBoard)) then exit(false);
end;
end;
{$endif}