-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunBlockVar.pas
413 lines (339 loc) · 10.1 KB
/
unBlockVar.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
unit unBlockVar;
interface
uses unBlock,stdctrls,System.Classes,System.UITypes,Winapi.Messages,VCL.Controls;
const WM_REFVARS_MSG = WM_USER + 252;
Type
TDspVarBlock=Class(TDspBlock)
private
DelCbox:TCheckBox;
function GetToDelete: Boolean;
procedure SetToDelete(const Value: Boolean);
protected
procedure Setprototype(const Value: boolean);Override;
function createNewBlock: TDspBlock;override;
procedure CheckAttach;override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override;
procedure setParent(AParent:TWinControl);Override;
procedure AssignTo(Dest:TPersistent);override;
public
constructor Create(AOwner: TComponent); override;
Procedure DelCboxClick(Sender:TObject);
procedure SetBounds( pLeft , pTop , pWidth , pHeight:integer );Override;
Property ToDelete:Boolean read GetToDelete write SetToDelete;
function GetArduinoCommand(idnt: Integer=1): String;Override;
End;
TDspCommandVarBlock=Class(TDspBlock)
private
procedure OnComboChange(Sender: tObject);
procedure OnRefVarRequest(var Msg: TMessage); message WM_REFVARS_MSG;
protected
procedure SetParent(AParent: TWinControl); override;
function getParam2: Integer;override;
procedure setfParam2(const Value: Integer);override;
procedure AssignTo(Dest:TPersistent);override;
function createNewBlock: TDspBlock;override;
Procedure FillVarCombo;
Procedure CreateCtrl2;Override;
function getParam2Control(x, y: integer): integer;Override;
Constructor Create(AOwner: TComponent);Override;
function Param2Visible: Boolean;Override;
Public
Function varCombo:TComboBox;
function GetArduinoCommand(idnt: Integer=1): String;Override;
End;
implementation
uses Types,unVariables,DspUtils,Sysutils,vcl.graphics,math;
{ TDspVarBlock }
procedure TDspVarBlock.AssignTo(Dest: TPersistent);
begin
inherited;
if dest is TDspVarBlock then
TDspVarBlock(dest).height:=height-2;
end;
procedure TDspVarBlock.CheckAttach;
Var tmpctrl:TDspBlock;
i:Integer;
OtherParamRect:TRect;
MyRect:TRect;
begin
if Param1toAttach<>nil then
Begin
Param1toAttach.Param1Attaching:=false;
Param1toAttach.Invalidate;
End;
if Param2toAttach<>nil then
Begin
Param2toAttach.Param2Attaching:=false;
Param2toAttach.Invalidate;
End;
Param1toAttach:=nil;
Param2toAttach:=nil;
if (parent<>nil) and (parent.Tag=995) then
Begin
for i := 0 to parent.ControlCount-1 do
Begin
if parent.Controls[i] is TDspBlock and (parent.Controls[i].Tag<>2000 ) and (parent.Controls[i]<>self) then
Begin
tmpctrl:=TDspBlock(parent.Controls[i]);
MyRect:=BoundsRect;
OtherParamRect:=tmpctrl.GetParam1Rect; //check param1
if (not OtherParamRect.IsEmpty) and tmpctrl.Param1Visible and
IntersectRect(OtherParamRect,MyRect) then
Begin
Param1toAttach:= tmpctrl;
Param1toAttach.Param1Attaching:=True;
Repaint;
Break;//attach to 1st block we found
End;
OtherParamRect:=tmpctrl.GetParam2Rect; //check param2
if (not OtherParamRect.IsEmpty) and tmpctrl.Param2Visible and
IntersectRect(OtherParamRect,MyRect) then
Begin
Param2toAttach:= tmpctrl;
Param2toAttach.Param2Attaching:=True;
Repaint;
Break;//attach to 1st block we found
End;
End;
End;
End;
end;
constructor TDspVarBlock.Create(AOwner: TComponent);
begin
inherited;
Tag:=2000;//we are var block
topnose:=False;
botnose:=False;
DelCbox:=TCheckBox.Create(self);
Delcbox.Left:=8;
Delcbox.Top:=Height div 2-10;
Delcbox.OnClick:=DelCboxClick;
Delcbox.Caption:='';
Delcbox.Hint:='ÅðÝëåîå ãéá äéáãñáöÞ';
Delcbox.ShowHint:=true;
Delcbox.Width:=16;
Delcbox.parent:=self;
Margins.Left:=Delcbox.Left+Delcbox.width;
Margins.Top:=0;
end;
function TDspVarBlock.createNewBlock: TDspBlock;
begin
result:= TDspVarBlock.CreateFloat(Owner);
result.Height:=result.Height-2;
end;
procedure TDspVarBlock.DelCboxClick(Sender: TObject);
begin
end;
function TDspVarBlock.GetToDelete: Boolean;
begin
Result:= DelCbox.Checked;
end;
procedure TDspVarBlock.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
if assigned(Param1toAttach) then
Begin
//Attach self variable block to Param1toAttach
Param1toAttach.VarParam1:=Self;
Param1toAttach.Param1Attaching:=false;
End;
if assigned(Param2toAttach) then
Begin
//Attach self variable block to Param1toAttach
Param2toAttach.VarParam2:=Self;
Param2toAttach.Param2Attaching:=false;
End;
end;
procedure TDspVarBlock.SetBounds(pLeft, pTop, pWidth, pHeight: integer);
begin
inherited SetBounds( pLeft , pTop , pWidth , pHeight );
end;
procedure TDspVarBlock.setParent(AParent: TWinControl);
begin
inherited;
end;
procedure TDspVarBlock.Setprototype(const Value: boolean);
begin
inherited;
if assigned(DelCbox) then
Begin
DelCbox.Free;
DelCbox:=nil;
Margins.Left:=3;
End;
end;
function TDspVarBlock.GetArduinoCommand(idnt:Integer=1): String;
Begin
if DeviceOnlyCommandID=-1 then
result:= ArduVars.GetRealVarName(ArduCmd) //simple vars
else
result:= inherited GetArduinoCommand(idnt); //fixed vars device vars
End;
procedure TDspVarBlock.SetToDelete(const Value: Boolean);
begin
DelCbox.Checked:=value;
end;
{ TDspCommandVarBlock }
function TDspCommandVarBlock.GetArduinoCommand(idnt:Integer=1): String;
function getVarName(p1:integer):string;
Begin
result:=ArduVars.GetRealVarName(ArduVars.GetVarByID(p1).VarName);
End;
Begin
if fArduinoCommand='' then fArduinoCommand:=ArduCmd;
if pos('%v1',fArduinoCommand)>0 then
fArduinoCommand:=stringReplace(fArduinoCommand,'%v1',getVarName(param1),[rfReplaceAll]);
if pos('%v2',fArduinoCommand)>0 then
fArduinoCommand:=stringReplace(fArduinoCommand,'%v2',getVarName(param2),[rfReplaceAll]);
result:= inherited GetArduinoCommand(idnt);
End;
procedure TDspCommandVarBlock.AssignTo(Dest: TPersistent);
begin
inherited;
end;
constructor TDspCommandVarBlock.Create(AOwner: TComponent);
begin
inherited;
TotalParams:=2;
end;
procedure TDspCommandVarBlock.OnComboChange(Sender:tObject);
Var pName:string;
Begin
if assigned(ctrl2) then
Begin
pname:='';
if TComboBox(ctrl2).itemindex>-1 then
pname:=TComboBox(ctrl2).Items[TComboBox(ctrl2).itemindex];
if assigned(arduvars.GetVarByName(pName)) then
fParam2:=arduvars.GetVarByName(pname).VarID
else
fParam2:=-1;
End;
End;
procedure TDspCommandVarBlock.OnRefVarRequest(var Msg: TMessage);
begin
FillVarCombo;
end;
function TDspCommandVarBlock.Param2Visible: Boolean;
begin
Result:=False;
end;
procedure TDspCommandVarBlock.setfParam2(const Value: Integer);
Var pName:string;
begin
fParam2 := Value; //variable id
if not (csLoading in componentstate) then
begin
RecalcWidth;
if assigned(ctrl2) then
if TComboBox(ctrl2).items.Count>0 then
Begin
//find itemindex
pname:='';
if assigned(arduvars.GetVarByID(fparam2)) then
pname:=arduvars.GetVarByID(fparam2).VarName;
TComboBox(ctrl2).itemindex:=TComboBox(ctrl2).Items.IndexOf(pname);
End;
end;
end;
procedure TDspCommandVarBlock.SetParent(AParent: TWinControl);
begin
inherited;
if AParent<>nil then
Begin
FillVarCombo;
if assigned(ctrl2) then
Param2:=fParam2;
End;
end;
function TDspCommandVarBlock.varCombo: TComboBox;
begin
result:= TComboBox(Ctrl2);
end;
procedure TDspCommandVarBlock.CreateCtrl2;
begin
ctrl2:=TComboBox.create(self);
ctrl2.Parent:=self;
with TComboBox(Ctrl2) do
Begin
Font.Size:=10;
Font.Color:=ctrlcolor2;
Font.Style:=[fsbold];
Visible:=False;
Width:=40;
onchange:=OnComboChange;
BevelInner:=bvnone;
Bevelkind:=bkFlat;
BevelOuter:=bvSpace;
StyleElements:=[];
height:=18;
End;
TComboBox(Ctrl2).text:='';
end;
function TDspCommandVarBlock.createNewBlock: TDspBlock;
begin
Result:=TDspCommandVarBlock.CreateFloat(Owner);
end;
procedure TDspCommandVarBlock.FillVarCombo;
var
i,w: Integer;
oldidx:Integer;
begin
if prototype or (csloading in componentstate) then exit;
oldidx:=varCombo.itemindex;
varCombo.Items.Assign(ArduVars);
w:=0;
for i := 0 to Pred(varCombo.Items.Count) do
Begin
w:=max(w,varCombo.Canvas.TextWidth(varCombo.Items[i]+'00'));
End;
// if VarCombo.Width<w then
VarCombo.Width:=w+48; //then pulldown
varCombo.itemindex:=oldidx;
if VarCombo.text='' then
if (varcombo.Itemindex<>-1) and (varcombo.Items.Count>0) then
varcombo.text:=varcombo.Items[varcombo.Itemindex];
{ else//select 1st automatically
if (varcombo.Items.Count>0) and (oldidx=-1) then
varcombo.Itemindex:=0;}
if (varcombo.ItemIndex=-1) and (varcombo.text<>'') then
varcombo.text:='';
end;
function TDspCommandVarBlock.getParam2: Integer;
var pname:string;
begin
result:=-1;
if Assigned(Ctrl2) and not (csloading in componentstate) then
Begin
pname:=TCombobox(Ctrl2).items[TCombobox(Ctrl2).ItemIndex];
if arduvars.GetVarByName(pname)<>nil then
Result:=arduvars.GetVarByName(pname).VarID;
End;
end;
function TDspCommandVarBlock.getParam2Control(x, y: integer): integer;
var sv:TColor;
begin
if prototype then
Begin
sv:=canvas.Font.Color;
canvas.Font.Color:=clAqua;
if assigned(ctrl2) then
canvas.TextOut(x,y ,tCombobox(ctrl2).Text);
canvas.Font.Color:=sv;
result:=x+canvas.TextWidth(tCombobox(ctrl2).Text);
End
else
Begin
ctrl2.Visible:=true;
tCombobox(ctrl2).Color:=HTMLtoColor('#084B8A');;
tCombobox(ctrl2).font.Color:= clWhite;
ctrl2.Left:=x;
ctrl2.Top:=y-3;
result:=ctrl2.Left+ctrl2.Width;
End;
end;
initialization
System.Classes.RegisterClass(TDspVarBlock);
System.Classes.RegisterClass(TDspCommandVarBlock);
end.