-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
atshapelinebgra.pas
399 lines (317 loc) · 10.4 KB
/
atshapelinebgra.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
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{ATShapeLine is a component which paints line (directions: left-right, up-down, diagonals), with or without arrows at both sides. Line width is option. Line color and arrow color are options. It is Lazarus port of Delphi component TLine (renamed since TLine id is busy with TAChart).
Original author: Gon Perez-Jimenez (Spain, 2002)
Ported to Lazarus by: Alexey Torgashin (Russia)
- I redone get/set of canvas.pen and canvas.brush: do it only inside Paint, before it was all accross the code, in getters, setters, etc. This gives crashes of IDE on changing props in Linux.
- I added any linewidth for any direction with arrow1=true and arrow2=true.
- I converted demo to Laz using ide converter.
- Icon added to component-pallette to 'Misc'.
For BGRAControls by: Lainz
- Using BGRABitmap antialiased drawing (2020-09-09)
Lazarus: 1.6+}
unit atshapelinebgra;
interface
{$mode delphi}
uses
Graphics, SysUtils, Classes, Controls;
type
TShapeLineDirection = (drLeftRight, drUpDown, drTopLeftBottomRight, drTopRightBottomLeft);
{ TShapeLineBGRA }
TShapeLineBGRA = class(TGraphicControl)
private
{ Private declarations }
FLineDir: TShapeLineDirection;
FArrow1: Boolean;
FArrow2: Boolean;
FArrowFactor: Integer;
FLineWidth: integer;
FLineColor: TColor;
FArrowColor: TColor;
procedure SetArrowColor(AValue: TColor);
procedure SetLineColor(AValue: TColor);
procedure SetLineDir(AValue: TShapeLineDirection);
procedure SetArrow1(Value: Boolean);
procedure SetArrow2(Value: Boolean);
procedure SetArrowFactor(Value: integer);
procedure SetLineWidth(AValue: Integer);
protected
{ Protected declarations }
procedure Paint; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property DragCursor;
property DragKind;
property DragMode;
property Align;
property Anchors;
property BorderSpacing;
property ParentShowHint;
property Hint;
property ShowHint;
property Visible;
property PopupMenu;
property Direction: TShapeLineDirection read FLineDir write SetLineDir default drLeftRight;
property LineColor: TColor read FLineColor write SetLineColor;
property ArrowColor: TColor read FArrowColor write SetArrowColor;
property LineWidth: Integer read FLineWidth write SetLineWidth;
property Arrow1: Boolean read FArrow1 write SetArrow1 default False;
property Arrow2: Boolean read FArrow2 write SetArrow2 default False;
property ArrowFactor: Integer read FArrowFactor write SetArrowFactor default 8;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEndDock;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnClick;
property OnDblClick;
end;
procedure Register;
implementation
uses Math, BGRABitmap, BGRABitmapTypes;
procedure Register;
begin
RegisterComponents('BGRA Controls', [TShapeLineBGRA]);
end;
{ TShapeLineBGRA }
constructor TShapeLineBGRA.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
Width:=110;
Height:=30;
FArrow1:=false;
FArrow2:=false;
FArrowFactor:=8;
FArrowColor:=clBlack;
FLineColor:=clBlack;
FLineWidth:=1;
FLineDir:=drLeftRight;
end;
destructor TShapeLineBGRA.Destroy;
begin
inherited Destroy;
end;
procedure TShapeLineBGRA.SetArrowFactor(Value: integer);
begin
if Value <> FArrowFactor then begin
FArrowFactor := Value;
Invalidate;
end;
end;
procedure TShapeLineBGRA.SetArrow1(Value: Boolean);
begin
if Value <> FArrow1 then begin
FArrow1 := Value;
Invalidate;
end;
end;
procedure TShapeLineBGRA.SetArrow2(Value: Boolean);
begin
if Value <> FArrow2 then begin
FArrow2 := Value;
Invalidate;
end;
end;
procedure TShapeLineBGRA.SetLineWidth(AValue: Integer);
begin
if AValue <> FLineWidth then
begin
FLineWidth := AValue;
Invalidate;
end;
end;
procedure TShapeLineBGRA.SetLineColor(AValue: TColor);
begin
if AValue <> FLineColor then
begin
FLineColor := AValue;
Invalidate;
end;
end;
procedure TShapeLineBGRA.SetArrowColor(AValue: TColor);
begin
if AValue <> FArrowColor then
begin
FArrowColor := AValue;
Invalidate;
end;
end;
procedure TShapeLineBGRA.SetLineDir(AValue: TShapeLineDirection);
begin
if AValue <> FLineDir then
begin
FLineDir := AValue;
Invalidate;
end;
end;
procedure TShapeLineBGRA.Paint;
var
start: Integer;
p1,p2,p3: TPoint;
H0,W0,H,W: Integer;
Alfa: double;
bgra: TBGRABitmap;
begin
inherited;
bgra := TBGRABitmap.Create(Canvas.Width, Canvas.Height, BGRAPixelTransparent);
bgra.CanvasBGRA.Pen.Color:= FLineColor;
bgra.CanvasBGRA.Brush.Color:=FArrowColor;
bgra.CanvasBGRA.Pen.Width:=FLineWidth;
case FLineDir of
drLeftRight:
begin
start := (Height - FLineWidth) div 2;
bgra.CanvasBGRA.Pen.Width:= FLineWidth;
bgra.CanvasBGRA.MoveTo(IfThen(FArrow1, FArrowFactor), start);
bgra.CanvasBGRA.LineTo(Width-IfThen(FArrow2, FArrowFactor), Start);
bgra.CanvasBGRA.Pen.Width:= 1;
if FArrow1 then begin
//Flecha hacia izquierda
p1:=Point(0,start);
p2:=Point(FArrowFactor,Start-FArrowFactor);
p3:=Point(FArrowFactor,Start+FArrowFactor);
bgra.CanvasBGRA.Polygon([p1,p2,p3]);
end;
if FArrow2 then begin
//Flecha hacia derecha
p1:=Point(Width-1, Start);
p2:=Point(Width-(FArrowFactor+1),Start-FArrowFactor);
p3:=Point(Width-(FArrowFactor+1),Start+FArrowFactor);
bgra.CanvasBGRA.Polygon([p1,p2,p3]);
end;
end;
drUpDown:
begin
start := (Width - FLineWidth) div 2;
bgra.CanvasBGRA.Pen.Width:= FLineWidth;
bgra.CanvasBGRA.MoveTo(start, IfThen(FArrow1, FArrowFactor));
bgra.CanvasBGRA.LineTo(start, Height-IfThen(FArrow2, FArrowFactor));
bgra.CanvasBGRA.Pen.Width:= 1;
if FArrow1 then begin
//Flecha hacia arriba
p1:=Point(start,0);
p2:=Point(Start-FArrowFactor,FArrowFactor);
p3:=Point(Start+FArrowFactor,FArrowFactor);
bgra.CanvasBGRA.Polygon([p1,p2,p3]);
end;
if FArrow2 then begin
//Flecha hacia abajo
p1:=Point(start,Height-1);
p2:=Point(Start-FArrowFactor,Height-(FArrowFactor+1));
p3:=Point(Start+FArrowFactor,Height-(FArrowFactor+1));
bgra.CanvasBGRA.Polygon([p1,p2,p3]);
end;
end;
drTopLeftBottomRight:
begin
Alfa:= arctan2(Height, Width);
bgra.CanvasBGRA.Pen.Width:= FLineWidth;
bgra.CanvasBGRA.MoveTo(
IfThen(FArrow1, Trunc(FArrowFactor*cos(Alfa))),
IfThen(FArrow1, Trunc(FArrowFactor*sin(Alfa)))
);
bgra.CanvasBGRA.LineTo(
Width-IfThen(FArrow2, Trunc(FArrowFactor*cos(Alfa))),
Height-IfThen(FArrow2, Trunc(FArrowFactor*sin(Alfa)))
);
bgra.CanvasBGRA.Pen.Width:= 1;
if FArrow1 and(Width>0)then begin
//Flecha hacia arriba
H0:=Round((FArrowFactor+1)*Sin(Alfa));
W0:=Round((FArrowFactor+1)*Cos(Alfa));
p1:=Point(0,0);
W:=Round(W0+(FArrowFactor*Cos((Pi/2)-Alfa)));
H:=Round(H0-(FArrowFactor*Sin((Pi/2)-Alfa)));
if H<0 then H:=0;
if W<0 then W:=0;
p2:=Point(W,H);
W:=Round(W0-(FArrowFactor*Cos((Pi/2)-Alfa)));
H:=Round(H0+(FArrowFactor*Sin((Pi/2)-Alfa)));
if H<0 then H:=0;
if W<0 then W:=0;
p3:=Point(W,H);
bgra.CanvasBGRA.Polygon([p1,p2,p3]);
end;
if FArrow2 and(Width>0)then begin
//Flecha hacia abajo
H0:=Round((FArrowFactor+1)*Sin(Alfa));
W0:=Round((FArrowFactor+1)*Cos(Alfa));
p1:=Point(Width-1, Height-1);
W:=Round(W0-(FArrowFactor*Cos((Pi/2)-Alfa)));
H:=Round(H0+(FArrowFactor*Sin((Pi/2)-Alfa)));
W:=Width-W-1;
H:=Height-H-1;
if H>=Height then H:=Height-1;
if W>=Width then W:=Width-1;
p2:=Point(W,H);
W:=Round(W0+(FArrowFactor*Cos((Pi/2)-Alfa)));
H:=Round(H0-(FArrowFactor*Sin((Pi/2)-Alfa)));
W:=Width-W-1;
H:=Height-H-1;
if H>=Height then H:=Height-1;
if W>=Width then W:=Width-1;
p3:=Point(W,H);
bgra.CanvasBGRA.Polygon([p1,p2,p3]);
end;
end;
drTopRightBottomLeft:
begin
Alfa:= arctan2(Height, Width);
bgra.CanvasBGRA.Pen.Width:= FLineWidth;
bgra.CanvasBGRA.MoveTo(
Width-IfThen(FArrow1, Trunc(FArrowFactor*cos(Alfa))),
IfThen(FArrow1, Trunc(FArrowFactor*sin(Alfa)))
);
bgra.CanvasBGRA.LineTo(
IfThen(FArrow2, Trunc(FArrowFactor*cos(Alfa))),
Height-IfThen(FArrow2, Trunc(FArrowFactor*sin(Alfa)))
);
bgra.CanvasBGRA.Pen.Width:= 1;
if FArrow1 and(Width>0)then begin
H0:=Round((FArrowFactor+1)*Sin(Alfa));
W0:=Round((FArrowFactor+1)*Cos(Alfa));
p1:=Point(Width-1,0);
W:=Round(W0+(FArrowFactor*Cos((Pi/2)-Alfa)));
H:=Round(H0-(FArrowFactor*Sin((Pi/2)-Alfa)));
W:=Width-W-1;
if H<0 then H:=0;
if W>=Width then W:=Width-1;
p2:=Point(W,H);
W:=Round(W0-(FArrowFactor*Cos((Pi/2)-Alfa)));
H:=Round(H0+(FArrowFactor*Sin((Pi/2)-Alfa)));
W:=Width-W-1;
if H<0 then H:=0;
if W>=Width then W:=Width-1;
p3:=Point(W,H);
bgra.CanvasBGRA.Polygon([p1,p2,p3]);
end;
if FArrow2 and(Width>0)then begin
H0:=Round((FArrowFactor+1)*Sin(Alfa));
W0:=Round((FArrowFactor+1)*Cos(Alfa));
p1:=Point(0, Height-1);
W:=Round(W0-(FArrowFactor*Cos((Pi/2)-Alfa)));
H:=Round(H0+(FArrowFactor*Sin((Pi/2)-Alfa)));
H:=Height-H-1;
if H>=Height then H:=Height-1;
if W<0 then W:=0;
p2:=Point(W,H);
W:=Round(W0+(FArrowFactor*Cos((Pi/2)-Alfa)));
H:=Round(H0-(FArrowFactor*Sin((Pi/2)-Alfa)));
H:=Height-H-1;
if H>=Height then H:=Height-1;
if W<0 then W:=0;
p3:=Point(W,H);
bgra.CanvasBGRA.Polygon([p1,p2,p3]);
end;
end;
end;
bgra.Draw(Canvas, 0, 0, False);
bgra.Free;
end;
end.