-
Notifications
You must be signed in to change notification settings - Fork 4
/
MyPaintBox.pas
629 lines (556 loc) · 15.1 KB
/
MyPaintBox.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
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
unit MyPaintBox;
interface
uses
System.SysUtils,FMX.Surfaces,FMX.Colors,System.UITypes,System.Types, System.Classes,FMX.Controls,FMX.Graphics, FMX.Types, FMX.Objects, FMX.Platform;
type
TFunctionDraw=(fdNone,fdPen,fdLine,fdRectangle,fdEllipse,fdFillBgr,fdBitmapStamp,fdPolyLine);
TMyPaintBox = class(TPaintBox)
private
{$IFDEF POSIX}
ffillBrush : TStrokeBrush;
{$ENDIF}
fDrawing:boolean;
fbmpstamp:TBitmap;
ffDraw:TFunctionDraw;
fdrawbmp:TBitmap;
forigbmp:TBitmap;
fdrawrect:TRectF;//Paint box size
pFrom,pTo:TPointF;
fThickness:Single;
ffgColor:TAlphaColor;
fbgColor:TAlphaColor;
fnofill:boolean;
fcbrush:TBrush;//Current drawing Brush
fcstroke:TStrokeBrush;//Current drawing stroke
MouseMoved: Boolean;
MouseDowned: Boolean;
procedure SetForegroundColor(v:TAlphaColor);
procedure SetBackgroundColor(v:TAlphaColor);
procedure SetThickness(v:Single);
procedure SetNoFill(v:boolean);
procedure SetBitmapStamp(v:TBitmap);
private
procedure StartDrawing(startP:TPointF);
procedure EndDrawing(startP:TPointF);
procedure DoDraw(vCanvas: TCanvas;const drawall:boolean=true);
protected
procedure Paint; override;
procedure Resize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
procedure MouseMove(Shift: TShiftState; X, Y: Single); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property ForegroundColor:TAlphaColor read ffgColor write SetForegroundColor;
property BackgroundColor:TAlphaColor read fbgColor write SetBackgroundColor;
property Thickness:Single read fThickness write SetThickness;
property FuncDraw:TFunctionDraw read ffDraw write ffDraw;
property NoFill:Boolean read fnofill write SetNoFill;
property BitmapStamp:TBitmap read fbmpstamp write SetBitmapStamp;
public
procedure MouseLeave;
procedure FillColor(color:TAlphaColor);
procedure SaveToJPEGStream(Stream: TStream);
procedure SaveToBitmap(B: TBitmap); overload;
procedure LoadFromBitmap(B: TBitmap);
procedure SaveToFile(AFileName: String);
procedure SaveToBitmap(B: TBitmap; Width, Height: Integer); overload;
procedure StampBitmap(X,Y: Single; B: TBitmap);
procedure StampText(X,Y: Single; F: TFont; Text: String);
procedure SaveToStream(Stream: TStream);
procedure LoadFromStream(Stream: TStream);
procedure LoadFromFile(AFileName: String);
{$IFDEF POSIX}
procedure FFillerMod;
{$ENDIF}
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyPaintBox', [TMyPaintBox]);
end;
{ TMyPaintBox }
constructor TMyPaintBox.Create(AOwner: TComponent);
begin
inherited;
fbmpstamp:=nil;
Parent:=TFmxObject(AOwner);
Align:= TAlignLayout.Client;
ffDraw:=TFunctionDraw.fdPen;
fnofill:=false;
fDrawing:=false;
fThickness:=1;
pFrom := PointF(-1, -1);
pTo := PointF(-1, -1);
fdrawrect:=RectF(0,0,self.Width, self.Height);
fdrawbmp := TBitmap.Create(Round(Self.Width),Round(Self.Height));
forigbmp := TBitmap.Create(Round(Self.Width),Round(Self.Height));
forigbmp.Clear(TAlphaColors.Black);
SetBackgroundColor(TAlphaColorRec.White);
SetForegroundColor(TAlphaColorRec.Black);
FillColor(fbgColor);
{$IFDEF POSIX}
FFillerMod;
{$ENDIF}
end;
destructor TMyPaintBox.Destroy;
begin
if (assigned(fcbrush)) then
fcbrush.DisposeOf;
if (assigned(fcstroke)) then
fcstroke.DisposeOf;
if (assigned(fdrawbmp)) then
fdrawbmp.DisposeOf;
if (assigned(forigbmp)) then
forigbmp.DisposeOf;
if assigned(fbmpstamp) then
fbmpstamp.DisposeOf;
{$IFDEF POSIX}
if assigned ( ffillBrush ) then
FreeAndNil ( ffillBrush );
{$ENDIF}
inherited;
end;
procedure TMyPaintBox.DoDraw(vCanvas: TCanvas; const drawall: boolean);
var
r,rd: TRectF;
begin
if (drawall) then self.Canvas.DrawBitmap(fdrawbmp,fdrawrect,fdrawrect,1);
if (ffdraw=TFunctionDraw.fdNone) or (not fdrawing) then exit;
r:=TRectF.Create(pFrom,pTo,True);
with vCanvas do
begin
BeginScene();
case ffdraw of
{$IFDEF MSWINDOWS}
TFunctionDraw.fdPen:begin
DrawLine(pFrom,pTo,1,fcstroke);
end;
{$ENDIF}
TFunctionDraw.fdLine:begin
DrawLine(pFrom,pTo,1,fcstroke);
end;
TFunctionDraw.fdRectangle:begin
if not fnofill then
FillRect(r,0,0,[TCorner.TopLeft],1,fcbrush);
DrawRect(r,0,0,[TCorner.TopLeft],1,fcstroke);
end;
TFunctionDraw.fdEllipse:begin
if not fnofill then
FillEllipse(r,1,fcbrush);
DrawEllipse(r,1,fcstroke);
end;
TFunctionDraw.fdFillBgr:begin
Clear(fbgColor);
end;
TFunctionDraw.fdBitmapStamp:
if (assigned(fbmpstamp)) then begin
r:=TRectF.Create(PointF(0,0),fbmpstamp.Width,fbmpstamp.Height);
rd:=TRectF.Create(PointF(pTo.X-(fbmpstamp.Width/2),pTo.Y-(fbmpstamp.Height/2)),fbmpstamp.Width,fbmpstamp.Height);
DrawBitmap(fbmpstamp,r,rd,1);
end;
end;
EndScene;
end;
forigbmp.Assign(fdrawbmp);
end;
procedure TMyPaintBox.StampBitmap(X,Y: Single; B: TBitmap);
var
r,rd:TRectF;
begin
with fdrawbmp.Canvas do
begin
BeginScene();
if (assigned(B)) then
begin
r:=TRectF.Create(PointF(0,0),B.Width,B.Height);
rd:=TRectF.Create(PointF(X-(B.Width/2),Y-(B.Height/2)),B.Width,B.Height);
DrawBitmap(B,r,rd,1);
end;
EndScene;
end;
forigbmp.Assign(fdrawbmp);
{$IFDEF POSIX}
InvalidateRect(fdrawrect);
{$ENDIF}
end;
procedure TMyPaintBox.StampText(X,Y: Single; F: TFont; Text: String);
var
r:TRectF;
begin
with fdrawbmp.Canvas do
begin
BeginScene();
if (Text<>'') then
begin
r:=TRectF.Create(PointF(X-525,Y-25),1000,10);
//rd:=TRectF.Create(PointF(X-(B.Width/2),Y-(B.Height/2)),B.Width,B.Height);
//DrawBitmap(B,r,rd,1);
Font.Assign( F );
// flip foreground and background colors
Fill.Assign( fcstroke );
Stroke.Assign( fcbrush );
FillText(r,Text, False, 1, [], TTextAlign.Center );
end;
EndScene;
end;
forigbmp.Assign(fdrawbmp);
{$IFDEF POSIX}
InvalidateRect(fdrawrect);
{$ENDIF}
end;
procedure TMyPaintBox.EndDrawing(startP: TPointF);
begin
if (not fdrawing) then exit;
pTo := PointF(startP.X,startP.Y);
DoDraw(fdrawbmp.Canvas,false);
fdrawing:=false;
pFrom := PointF(-1, -1);
pTo := PointF(-1, -1);
end;
procedure TMyPaintBox.FillColor(color: TAlphaColor);
begin
with fdrawbmp.Canvas do
begin
BeginScene();
Clear(color);
EndScene;
end;
end;
procedure TMyPaintBox.Resize;
//var
// B: TBitmap;
begin
if Assigned(fdrawbmp) then
begin
// B := TBitmap.Create;
// B.Assign(fdrawbmp);
fdrawrect := RectF(0, 0, self.Width, self.Height);
fdrawbmp.SetSize(Trunc(self.Width),Trunc(self.Height));
fdrawbmp.Clear(TAlphaColors.Black);
fdrawbmp.Canvas.BeginScene();
// switch to scale draw instead of cutting the changed space
fdrawbmp.Canvas.DrawBitmap(forigbmp, forigbmp.BoundsF, fdrawbmp.BoundsF, 1, False);
// fdrawbmp.Canvas.DrawBitmap(B,B.BoundsF,B.BoundsF,1,True);
fdrawbmp.Canvas.EndScene;
// B.DisposeOf;
end;
{$IFDEF POSIX}
InvalidateRect(fdrawrect);
{$ENDIF}
end;
procedure TMyPaintBox.MouseLeave;
begin
if (MouseDowned = False) then
begin
if (not fdrawing) then
begin
//StartDrawing(PointF(X, Y));
end;
end;
if (MouseMoved = False) then
begin
{$IFDEF MSWINDOWS}
//pTo := PointF(X, Y);
InvalidateRect(fdrawrect);
case ffdraw of
TFunctionDraw.fdPen: //if (pFrom<>pTo) then
begin
DoDraw(fdrawbmp.Canvas,false);
pFrom:=pTo;
end;
end;
{$ENDIF}
end;
MouseMoved := False;
MouseDowned := False;
fdrawing := False;
//EndDrawing(PointF(X, Y));
{$IFDEF POSIX}
InvalidateRect(fdrawrect);
{$ENDIF}
end;
procedure TMyPaintBox.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Single);
begin
inherited;
if (not fdrawing) then
begin
StartDrawing(PointF(X, Y));
end;
MouseDowned := True;
end;
procedure TMyPaintBox.MouseMove(Shift: TShiftState; X, Y: Single);
{$IFDEF POSIX}
var
Radius : Single;
xDir, yDir : Single;
Dx, Dy : Single;
Ratio : Single;
MoveX, MoveY : Single;
{$ENDIF}
begin
inherited;
if (not fdrawing) then exit;
case ffdraw of
TFunctionDraw.fdFillBgr: Exit;
end;
{$IFDEF POSIX}
Radius := fThickness / 2;
{$ENDIF}
pTo := PointF(X, Y);
InvalidateRect(fdrawrect);
case ffdraw of
TFunctionDraw.fdPen:
begin
{$IFDEF POSIX}
if ( pFrom.Round <> pTo.Round ) then
begin
{ Direction detection from pFrom to pTo }
{ to adjust start center }
IF pTo.Y >= pFrom.Y THEN yDir := -1 ELSE yDir := 1;
IF pTo.X >= pFrom.X THEN xDir := -1 ELSE xDir := 1;
{ Quantify movement }
Dx := ABS ( pTo.X - pFrom.X );
Dy := ABS ( pTo.Y - pFrom.Y );
IF ABS ( Dy ) > ABS ( Dx ) THEN
Begin
Ratio := ABS ( Radius / Dy * Dx );
MoveY := Radius * yDir;
pFrom.Y := pFrom.Y + MoveY;
MoveX := Ratio * xDir;
pFrom.X := pFrom.X + MoveX;
End
ELSE
Begin
Ratio := ABS ( Radius / Dx * Dy );
MoveX := Radius * xDir;
pFrom.X := pFrom.X + MoveX;
MoveY := Ratio * yDir;
pFrom.Y := pFrom.Y + MoveY;
End;
fdrawbmp.Canvas.BeginScene ();
fdrawbmp.Canvas.DrawLine ( pFrom, pTo, 1, ffillBrush );
fdrawbmp.Canvas.EndScene;
{ Direction detection end of line }
{ to adjust end of line center }
IF pTo.Y >= pFrom.Y THEN yDir := -1 ELSE yDir := 1;
IF pTo.X >= pFrom.X THEN xDir := -1 ELSE xDir := 1;
{ Quantify movement }
Dx := ABS ( pTo.X - pFrom.X );
Dy := ABS ( pTo.Y - pFrom.Y );
IF ABS ( Dy ) > ABS ( Dx ) THEN
Begin
Ratio := ABS ( Radius / Dy * Dx );
MoveY := Radius * yDir;
pFrom.Y := pTo.Y + MoveY;
MoveX := Ratio * xDir;
pFrom.X := pTo.X + MoveX;
End
ELSE
Begin
Ratio := ABS ( Radius / Dx * Dy );
MoveX := Radius * xDir;
pFrom.X := pTo.X + MoveX;
MoveY := Ratio * yDir;
pFrom.Y := pTo.Y + MoveY;
End;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
DoDraw(fdrawbmp.Canvas,false);
pFrom:=pTo;
{$ENDIF}
end;
TFunctionDraw.fdBitmapStamp: //if (pFrom<>pTo) then
begin
DoDraw(fdrawbmp.Canvas,false);
pFrom:=pTo;
end;
end;
MouseMoved := True;
end;
procedure TMyPaintBox.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
inherited;
if (MouseDowned = False) then
begin
if (not fdrawing) then
begin
StartDrawing(PointF(X, Y));
end;
end;
if (MouseMoved = False) then
begin
{$IFDEF MSWINDOWS}
pTo := PointF(X, Y);
InvalidateRect(fdrawrect);
case ffdraw of
TFunctionDraw.fdPen: //if (pFrom<>pTo) then
begin
DoDraw(fdrawbmp.Canvas,false);
pFrom:=pTo;
end;
end;
{$ENDIF}
end;
MouseMoved := False;
MouseDowned := False;
EndDrawing(PointF(X, Y));
{$IFDEF POSIX}
InvalidateRect(fdrawrect);
{$ENDIF}
end;
procedure TMyPaintBox.Paint;
begin
inherited;
if (csDesigning in ComponentState) then exit;
DoDraw(self.Canvas);
end;
procedure TMyPaintBox.SaveToJPEGStream(Stream: TStream);
var
Surf: TBitmapSurface;
saveParams : TBitmapCodecSaveParams;
begin
Surf := TBitmapSurface.Create;
try
Surf.Assign(fdrawbmp);
saveparams.Quality:=93; // <-- always stops here with an AV error
TBitmapCodecManager.SaveToStream(Stream, Surf, '.jpg',@saveParams);
finally
Surf.Free;
end;
end;
procedure TMyPaintBox.SaveToStream(Stream: TStream);
begin
try
fdrawbmp.SaveToStream(Stream);
finally
end;
end;
procedure TMyPaintBox.SaveToFile(AFileName: String);
begin
try
fdrawbmp.SaveToFile(AFileName);
finally
end;
end;
procedure TMyPaintBox.LoadFromFile(AFileName: String);
begin
try
fdrawbmp.LoadFromFile(AFileName);
forigbmp.LoadFromFile(AFileName);
finally
end;
end;
procedure TMyPaintBox.SaveToBitmap(B: TBitmap);
begin
try
B.Assign(fdrawbmp);
finally
end;
end;
procedure TMyPaintBox.SaveToBitmap(B: TBitmap; Width, Height: Integer);
begin
if B.Width = 0 then
Exit;
if fdrawbmp <> nil then
begin
B.Assign(fdrawbmp.CreateThumbnail(Width,Height));
end;
end;
procedure TMyPaintBox.LoadFromBitmap(B: TBitmap);
var
r, rd: TRectF;
begin
try
if assigned(fdrawbmp) then
begin
r:=TRectF.Create(PointF(0,0),B.Width,B.Height);
rd:=TRectF.Create(PointF(0,0),B.Width,B.Height);
fdrawbmp.Canvas.BeginScene();
fdrawbmp.Canvas.DrawBitmap(B,r,rd,1);
fdrawbmp.Canvas.EndScene;
InvalidateRect(fdrawrect);
end;
finally
end;
end;
procedure TMyPaintBox.LoadFromStream(Stream: TStream);
begin
fdrawbmp.LoadFromStream(Stream);
forigbmp.Assign(fdrawbmp);
end;
procedure TMyPaintBox.SetBackgroundColor(v: TAlphaColor);
begin
if (v=fbgColor) then exit;
if (assigned(fcbrush)) then
fcbrush.Free;
fbgColor:=v;
fcbrush:=TBrush.Create(TBrushKind.Solid,fbgColor);
end;
procedure TMyPaintBox.SetBitmapStamp(v: TBitmap);
begin
if not assigned(v) then exit;
if assigned(fbmpstamp) then
fbmpstamp.Free;
fbmpstamp:=TBitmap.Create(0,0);
fbmpstamp.Assign(v);
end;
procedure TMyPaintBox.SetForegroundColor(v: TAlphaColor);
begin
if v=ffgColor then exit;
if assigned(fcstroke) then
fcstroke.Free;
ffgColor:=v;
fcstroke:=TStrokeBrush.Create(TBrushKind.Solid,ffgColor);
fcstroke.DefaultColor:=ffgColor;
fcstroke.Thickness:=fThickness;
fcstroke.Cap := TStrokeCap.Round;
{$IFDEF POSIX}
ffillermod;
{$ENDIF}
end;
procedure TMyPaintBox.SetNoFill(v: boolean);
begin
if fnofill <> v then
fnofill := v;
end;
procedure TMyPaintBox.SetThickness(v: Single);
begin
if v = fThickness then exit;
if assigned(fcstroke) then
fcstroke.Free;
fThickness := v;
fcstroke:=TStrokeBrush.Create(TBrushKind.Solid, ffgColor);
fcstroke.DefaultColor:=ffgColor;
fcstroke.Thickness:=fThickness;
fcstroke.Cap := TStrokeCap.Round;
{$IFDEF POSIX}
ffillermod;
{$ENDIF}
end;
procedure TMyPaintBox.StartDrawing(startP: TPointF);
begin
if (csDesigning in ComponentState) then exit;
if (fDrawing) or (ffDraw = TFunctionDraw.fdNone) then exit;
pFrom := PointF(startP.X, startP.Y);
pTo := PointF(startP.X, startP.Y);
fDrawing := true;
end;
{$IFDEF POSIX}
procedure TMyPaintBox.FFillerMod;
begin
if not Assigned(ffillBrush) then
ffillBrush := TStrokeBrush.Create(TBrushKind.Solid, ffgcolor);
ffillBrush.Thickness := fThickness;
ffillBrush.Cap := TStrokeCap.Round;
ffillBrush.Join := TStrokeJoin.Round;
ffillBrush.Color := ffgcolor;
End;
{$ENDIF}
end.