-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateproperties.pas
395 lines (371 loc) · 17 KB
/
gateproperties.pas
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
UNIT gateProperties;
{$mode objfpc}{$H+}
INTERFACE
USES logicalGates,compoundGates, myGenerics,paletteHandling,ValEdit,Classes,Grids,Controls;
TYPE
T_gatePropertyType=(pt_number,pt_string,pt_wireWidth,pt_connectionCount,pt_enumWithOptionForNewEntry,pt_data);
T_gatePropertyEnum=(gpe_captionReadOnly,
gpe_caption,
gpe_editableLabel,
gpe_subpaletteReadOnly,
gpe_subPalette,
gpe_descriptionReadOnly,
gpe_description,
gpe_intervalGreaterZero,
gpe_inputWidth,
gpe_outputWidth,
gpe_inputCount,
gpe_romData);
T_gatePropertyEnums=set of T_gatePropertyEnum;
T_gateProperty=record
name:string;
typ:T_gatePropertyType;
minValue,maxValue:longint;
readonly:boolean;
end;
CONST
C_gateProperty:array[T_gatePropertyEnum] of T_gateProperty=
((name:'Name' ; typ:pt_string; minValue:0; maxValue: 0; readonly: true),
(name:'Name' ; typ:pt_string; minValue:0; maxValue: 0; readonly:false),
(name:'Label'; typ:pt_string; minValue:0; maxValue: 0; readonly:false),
(name:'Palette' ; typ:pt_string; minValue:0; maxValue: 0; readonly: true),
(name:'Palette' ; typ:pt_enumWithOptionForNewEntry; minValue:0; maxValue: 0; readonly:false),
(name:'Beschreibung'; typ:pt_string; minValue:0; maxValue: 0; readonly: true),
(name:'Beschreibung'; typ:pt_string; minValue:0; maxValue: 0; readonly:false),
(name:'Intervall'; typ:pt_number; minValue:1; maxValue: maxLongint; readonly:false),
(name:'Breite Eingang (bits)'; typ:pt_wireWidth; minValue:1; maxValue:WIRE_MAX_WIDTH; readonly:false),
(name:'Breite Ausgang (bits)'; typ:pt_wireWidth; minValue:1; maxValue:WIRE_MAX_WIDTH; readonly:false),
(name:'Anzahl Eingänge'; typ:pt_connectionCount; minValue:2; maxValue:WIRE_MAX_WIDTH; readonly:false),
(name:'Speicherinhalt'; typ:pt_data; minValue:0; maxValue: 0; readonly:false));
C_availableProperies:array[T_gateType,false..true] of T_gatePropertyEnums=
{gt_notGate} (([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly]),
{gt_andGate} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputCount]),
{gt_orGate} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputCount]),
{gt_xorGate} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputCount]),
{gt_nandGate} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputCount]),
{gt_norGate} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputCount]),
{gt_nxorGate} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputCount]),
{gt_input} ([gpe_captionReadOnly,gpe_subPalette], [gpe_editableLabel,gpe_subpaletteReadOnly,gpe_outputWidth]),
{gt_output} ([gpe_captionReadOnly,gpe_subPalette], [gpe_editableLabel,gpe_subpaletteReadOnly,gpe_inputWidth]),
{gt_compound} ([gpe_caption,gpe_description,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_descriptionReadOnly]),
{gt_clock} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_intervalGreaterZero]),
{gt_adapter} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputWidth,gpe_outputWidth]),
([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly]),
([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly]),
{gt_gatedCl..}([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_intervalGreaterZero]),
{1/2->1} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputWidth]),
{1/2->0} ([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_inputWidth]),
([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_romData]),
([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly,gpe_romData]),
([gpe_captionReadOnly,gpe_subPalette], [gpe_captionReadOnly,gpe_subpaletteReadOnly]));
TYPE
T_gatePropertyValue=record
s:string;
n:longint;
romContents:T_romContents;
end;
T_gateProperties=array of T_gateProperty;
{ T_gatePropertyValues }
T_gatePropertyValues=object
private
onAccept: TNotifyEvent;
gate:P_abstractGate;
palette:P_workspacePalette;
entry:array of record
prop :T_gatePropertyEnum;
value :T_gatePropertyValue;
modified:boolean;
end;
editingBoard:boolean;
myEditor:TValueListEditor;
PROCEDURE EditButtonClick(Sender: TObject; aCol, aRow: integer);
PROCEDURE EditorSelectEditor(Sender: TObject; aCol, aRow: integer; VAR editor: TWinControl);
FUNCTION fetchValue(CONST prop:T_gatePropertyEnum):T_gatePropertyValue;
PROCEDURE applyValue(CONST prop:T_gatePropertyEnum; CONST value:T_gatePropertyValue);
FUNCTION isPropertyValidInCurrentContext(CONST gatePropertyEnum:T_gatePropertyEnum):boolean;
PROCEDURE ValueListEditorValidateEntry(Sender: TObject; aCol, aRow: integer; CONST oldValue: string; VAR newValue: string);
PROCEDURE connectEditor(editor:TValueListEditor);
public
CONSTRUCTOR createForPaletteEntry(editor:TValueListEditor; onModify:TNotifyEvent; CONST gate_:P_abstractGate; CONST palette_:P_palette);
CONSTRUCTOR createForBoardEntry (editor:TValueListEditor; onModify:TNotifyEvent; CONST gate_:P_abstractGate; CONST palette_:P_palette);
DESTRUCTOR destroy;
FUNCTION acceptNewValue(CONST index:longint; CONST newValue:string):boolean;
FUNCTION applyValues:boolean;
PROPERTY arePropertiesForBoard:boolean read editingBoard;
end;
IMPLEMENTATION
USES sysutils,romEditorUnit,StdCtrls,visuals;
{ T_gatePropertyValues }
FUNCTION T_gatePropertyValues.fetchValue(CONST prop: T_gatePropertyEnum): T_gatePropertyValue;
begin
result.n:=0;
result.s:='';
initialize(result.romContents);
setLength(result.romContents,0);
case prop of
gpe_caption,gpe_captionReadOnly:
result.s:=StringReplace(gate^.getCaption,LineEnding,'\n',[rfReplaceAll]);
gpe_description,gpe_descriptionReadOnly:
result.s:=StringReplace(gate^.getDescription,LineEnding,'\n',[rfReplaceAll]);
gpe_editableLabel:
if gate^.gateType in [gt_input,gt_output]
then result.s:=P_inputGate(gate)^.getCaption;
gpe_intervalGreaterZero:
if gate^.gateType in [gt_clock,gt_gatedClock]
then result.n:=P_clock(gate)^.interval;
gpe_inputWidth:
case gate^.gateType of
gt_output: result.n:=P_outputGate(gate)^.width;
gt_adapter: result.n:=P_adapter(gate)^.inputWidth(0);
gt_undeterminedToFalse: result.n:=P_tendToFalse(gate)^.input.width;
gt_undeterminedToTrue : result.n:=P_tendToTrue (gate)^.input.width;
end;
gpe_outputWidth:
case gate^.gateType of
gt_input: result.n:=P_inputGate(gate)^.width;
gt_adapter: result.n:=P_adapter(gate)^.outputWidth(0);
end;
gpe_inputCount :
if gate^.gateType in [gt_andGate,gt_orGate,gt_xorGate,gt_nandGate,gt_norGate,gt_nxorGate]
then result.n:=P_binaryBaseGate(gate)^.inputCount;
gpe_subPalette,gpe_subpaletteReadOnly: begin
assert(palette<>nil);
result.n:=palette^.findEntry(gate);
if (result.n>=0) and (result.n<length(palette^.paletteEntries))
then begin
result.n:=palette^.paletteEntries[result.n].subPaletteIndex;
if (result.n>=0) and (result.n<length(palette^.paletteNames))
then result.s:=palette^.paletteNames[result.n]
else begin
result.s:='?';
result.n:=-1;
end;
end else begin
result.s:='?';
result.n:=-1;
end;
end;
gpe_romData: result.romContents:=P_RomGate(gate)^.data;
else assert(false);
end;
end;
PROCEDURE T_gatePropertyValues.EditButtonClick(Sender: TObject; aCol,
aRow: integer);
VAR i:longint=0;
begin
i:=aRow-1;
if (i<0) or (i>=length(entry)) or (entry[i].prop<>gpe_romData) then exit;
if RomEditorForm.showFor(entry[i].value.romContents) then begin
entry[i].modified:=true;
onAccept(Sender);
end;
end;
PROCEDURE T_gatePropertyValues.EditorSelectEditor(Sender: TObject; aCol, aRow: integer; VAR editor: TWinControl);
begin
if (editor is TPickListCellEditor) then begin
TPickListCellEditor(editor).color:=colorScheme.editorBackgroundColor;
TPickListCellEditor(editor).Font.color:=colorScheme.ENABLED_TEXT_COLOR;
TPickListCellEditor(editor).style:=csOwnerDrawEditableFixed;
TPickListCellEditor(editor).AutoComplete:=true;
TPickListCellEditor(editor).AutoCompleteText:=[cbactEnabled,cbactEndOfLineComplete,cbactSearchAscending];
end;
end;
PROCEDURE T_gatePropertyValues.applyValue(CONST prop: T_gatePropertyEnum;
CONST value: T_gatePropertyValue);
begin
case prop of
gpe_caption: begin
P_compoundGate(gate)^.prototype^.setCaption(StringReplace(value.s,'\n',LineEnding,[rfReplaceAll]));
end;
gpe_description: begin
P_compoundGate(gate)^.prototype^.setDescription(StringReplace(value.s,'\n',LineEnding,[rfReplaceAll]));
end;
gpe_subPalette: begin
//we could fetch the value first, so all should be okay...
palette^.reassignEntry(gate,value.s);
end;
gpe_editableLabel: if gate^.gateType in [gt_input,gt_output] then begin
P_inputGate(gate)^.ioLabel:=value.s;
end;
gpe_intervalGreaterZero: if gate^.gateType in [gt_clock,gt_gatedClock] then begin
P_clock(gate)^.interval:=value.n;
end;
gpe_inputWidth:
case gate^.gateType of
gt_output: begin
P_outputGate(gate)^.width:=value.n;
P_outputGate(gate)^.reset;
end;
gt_adapter: P_adapter(gate)^.setIoWidth(value.n,gate^.outputWidth(0));
gt_undeterminedToFalse: begin P_tendToFalse(gate)^.input.width:=value.n; P_tendToFalse(gate)^.output.width:=value.n; gate^.reset; end;
gt_undeterminedToTrue : begin P_tendToTrue (gate)^.input.width:=value.n; P_tendToTrue (gate)^.output.width:=value.n; gate^.reset; end;
end;
gpe_outputWidth:
case gate^.gateType of
gt_input: begin
P_inputGate(gate)^.width:=value.n;
P_inputGate(gate)^.reset;
end;
gt_adapter: P_adapter(gate)^.setIoWidth(gate^.inputWidth(0),value.n);
end;
gpe_inputCount :
if gate^.gateType in [gt_andGate,gt_orGate,gt_xorGate,gt_nandGate,gt_norGate,gt_nxorGate]
then P_binaryBaseGate(gate)^.inputCount:=value.n;
gpe_romData: begin
if gate^.gateType in [gt_ram,gt_rom]
then P_RomGate(gate)^.data:=value.romContents;
end;
end;
end;
FUNCTION T_gatePropertyValues.isPropertyValidInCurrentContext(CONST gatePropertyEnum:T_gatePropertyEnum):boolean;
begin
case gatePropertyEnum of
gpe_subPalette,gpe_subpaletteReadOnly: result:=(palette<>nil);
else result:=true;
end;
end;
PROCEDURE T_gatePropertyValues.ValueListEditorValidateEntry(Sender: TObject;
aCol, aRow: integer; CONST oldValue: string; VAR newValue: string);
begin
if aCol=0 then begin
newValue:=oldValue;
end else begin
if acceptNewValue(aRow-1,newValue)
then onAccept(Sender)
else newValue:=oldValue;
end;
end;
PROCEDURE T_gatePropertyValues.connectEditor(editor: TValueListEditor);
VAR i: integer;
s: string;
begin
myEditor:=editor;
editor.OnValidateEntry:=@ValueListEditorValidateEntry;
editor.OnButtonClick:=@EditButtonClick;
editor.clear;
editor.rowCount:=length(entry);
for i:=0 to length(entry)-1 do begin
editor.Cells[0,i+1]:=C_gateProperty[entry[i].prop].name;
case C_gateProperty[entry[i].prop].typ of
pt_number,pt_wireWidth,pt_connectionCount: begin
editor.Cells[1,i+1]:=intToStr(entry[i].value.n);
editor.ItemProps[i].EditStyle:=esSimple;
end;
pt_string: begin
editor.Cells[1,i+1]:=entry[i].value.s;
editor.ItemProps[i].EditStyle:=esSimple;
end;
pt_enumWithOptionForNewEntry: begin
editor.Cells[1,i+1]:=entry[i].value.s;
editor.ItemProps[i].EditStyle:=esPickList;
editor.ItemProps[i].PickList.clear;
for s in palette^.subPaletteNames do editor.ItemProps[i].PickList.add(s);
end;
pt_data: begin
editor.Cells[1,i+1]:='<data>';
editor.ItemProps[i].EditStyle:=esEllipsis;
end;
end;
end;
editor.editor.color:=colorScheme.editorBackgroundColor;
editor.editor.Font.color:=colorScheme.ENABLED_TEXT_COLOR;
editor.OnSelectEditor:=@EditorSelectEditor;
editor.AutoSizeColumn(0);
end;
CONSTRUCTOR T_gatePropertyValues.createForPaletteEntry( editor: TValueListEditor; onModify: TNotifyEvent; CONST gate_: P_abstractGate; CONST palette_: P_palette);
VAR p:T_gatePropertyEnum;
i:longint=0;
begin
editingBoard:=false;
onAccept:=onModify;
gate:=gate_;
palette:=P_workspacePalette(palette_);
setLength(entry,0);
for p in C_availableProperies[gate^.gateType,false] do begin
setLength(entry,i+1);
entry[i].prop:=p;
entry[i].value:=fetchValue(p);
entry[i].modified:=false;
inc(i);
end;
connectEditor(editor);
end;
CONSTRUCTOR T_gatePropertyValues.createForBoardEntry(editor: TValueListEditor; onModify: TNotifyEvent; CONST gate_: P_abstractGate; CONST palette_: P_palette);
VAR p:T_gatePropertyEnum;
i:longint=0;
begin
editingBoard:=true;
onAccept:=onModify;
gate:=gate_;
if palette_^.isWorkspacePalette
then palette:=P_workspacePalette(palette_)
else palette:=nil;
setLength(entry,0);
for p in C_availableProperies[gate^.gateType,true] do
if isPropertyValidInCurrentContext(p) then begin
setLength(entry,i+1);
entry[i].prop:=p;
entry[i].value:=fetchValue(p);
entry[i].modified:=false;
inc(i);
end;
connectEditor(editor);
end;
DESTRUCTOR T_gatePropertyValues.destroy;
begin
setLength(entry,0);
end;
FUNCTION T_gatePropertyValues.acceptNewValue(CONST index: longint;
CONST newValue: string): boolean;
VAR newNumber:int64;
begin
if C_gateProperty[entry[index].prop].readonly then exit(false);
case C_gateProperty[entry[index].prop].typ of
pt_number: begin
newNumber:=StrToInt64Def(newValue,int64(maxLongint)+1);
if (newNumber>C_gateProperty[entry[index].prop].maxValue) or
(newNumber<C_gateProperty[entry[index].prop].minValue) then exit(false);
entry[index].modified:=entry[index].modified or (entry[index].value.n<>newNumber);
entry[index].value.n:=longint(newNumber);
result:=true;
end;
pt_wireWidth: begin
newNumber:=StrToInt64Def(newValue,int64(maxLongint)+1);
if not((newNumber=1) or (newNumber=4) or (newNumber=8) or (newNumber=16)) then exit(false);
entry[index].modified:=entry[index].modified or (entry[index].value.n<>newNumber);
entry[index].value.n:=byte(newNumber);
result:=true;
end;
pt_connectionCount: begin
newNumber:=StrToInt64Def(newValue,int64(maxLongint)+1);
if (newNumber<2) or (newNumber>WIRE_MAX_WIDTH) then exit(false);
entry[index].modified:=entry[index].modified or (entry[index].value.n<>newNumber);
entry[index].value.n:=byte(newNumber);
result:=true;
end;
pt_string: begin
entry[index].modified:=entry[index].modified or (entry[index].value.s<>newValue);
entry[index].value.s:=newValue;
result:=true;
end;
pt_enumWithOptionForNewEntry: begin
entry[index].modified:=entry[index].modified or (entry[index].value.s<>newValue);
entry[index].value.s:=newValue;
result:=true;
end
else result:=false;
end;
end;
FUNCTION T_gatePropertyValues.applyValues: boolean;
VAR i:longint;
begin
result:=false;
for i:=0 to length(entry)-1 do
with entry[i] do
if modified
then begin
applyValue(prop,value);
result:=true;
end;
end;
end.