-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScratchCard.pas
More file actions
133 lines (104 loc) · 3.15 KB
/
ScratchCard.pas
File metadata and controls
133 lines (104 loc) · 3.15 KB
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
unit ScratchCard;
interface
uses
System.SysUtils, System.Classes, FMX.Types, FMX.Controls, FMX.Surfaces,
FMX.Graphics, FMX.Objects, Skia, Skia.FMX, System.Types, System.UITypes;
type
TScratchCard = class( TSkPaintBox )
private
FPath: ISkPath;
FPathBuilder: ISkPathBuilder;
FBackgroundFilter: ISkImageFilter;
img: ISkImage;
FCodedStream: TMemoryStream;
FBackground: TBitmap;
FBlendMode: TSkBlendMode;
FBrushSize: Integer;
procedure SetBackground( const Value: TBitmap );
protected
procedure Draw( const ACanvas: ISkCanvas; const ADest: TRectF; const AOpacity: Single ); override;
procedure MouseMove( Shift: TShiftState; X: Single; Y: Single ); override;
procedure MouseDown( Button: TMouseButton; Shift: TShiftState; X: Single; Y: Single ); override;
public
constructor Create( AOwner: TComponent ); override;
destructor Destroy; override;
published
property Background: TBitmap read FBackground write SetBackground;
property BrushSize: Integer read FBrushSize write FBrushSize default 10;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents( 'Tulip', [TScratchCard] );
end;
{ TScratchCard }
constructor TScratchCard.Create( AOwner: TComponent );
begin
inherited;
FBackground := TBitmap.Create( trunc(Self.LocalRect.Width), trunc(Self.LocalRect.Height) );
FCodedStream := TMemoryStream.Create;
HitTest := true;
BrushSize := 10;
end;
destructor TScratchCard.Destroy;
begin
FBackground.Free;
FCodedStream.Free;
inherited;
end;
procedure TScratchCard.Draw( const ACanvas: ISkCanvas; const ADest: TRectF; const AOpacity: Single );
var
LPaint: ISkPaint;
begin
inherited;
if Assigned( FPathBuilder ) then
begin
if not Assigned( FPath ) then
FPath := FPathBuilder.Snapshot;
LPaint := TSkPaint.Create( TSkPaintStyle.Stroke );
LPaint.AntiAlias := true;
LPaint.Color := TAlphaColorRec.black;
LPaint.ImageFilter := TSkImageFilter.MakeBlend(
TSkBlendMode.DestOut,
FBackgroundFilter, Self.ClipRect
);
LPaint.MaskFilter := TSkMaskFilter.MakeBlur( TSkBlurStyle.Inner, BrushSize );
LPaint.StrokeCap := TSkStrokeCap.Round;
LPaint.StrokeWidth := 40;
LPaint.StrokeMiter := 10;
LPaint.setPathEffect( TSkPathEffect.MakeCorner( 50 ) );
ACanvas.DrawPath( FPath, LPaint );
end else
begin
img := FBackground.ToSkImage;
ACanvas.DrawImageRect(img, self.LocalRect, self.LocalRect);
end;
end;
procedure TScratchCard.MouseDown( Button: TMouseButton; Shift: TShiftState; X, Y: Single );
begin
inherited;
if not Assigned( FPathBuilder ) then
FPathBuilder := TSkPathBuilder.Create;
FPathBuilder.MoveTo( X, Y );
FPath := nil;
if not Assigned( FBackground ) then
exit;
img := FBackground.ToSkImage;
FBackgroundFilter := TSkImageFilter.MakeImage(img);
end;
procedure TScratchCard.MouseMove( Shift: TShiftState; X, Y: Single );
begin
inherited;
if Pressed then
begin
FPath := nil;
FPathBuilder.LineTo( X, Y );
Redraw;
end;
end;
procedure TScratchCard.SetBackground( const Value: TBitmap );
begin
FBackground.Assign( Value );
end;
end.