Skip to content

Instantly share code, notes, and snippets.

@viniciusfbb
Created November 21, 2022 12:35
Show Gist options
  • Save viniciusfbb/df9c4d57102514faf105d2dddcd8d122 to your computer and use it in GitHub Desktop.
Save viniciusfbb/df9c4d57102514faf105d2dddcd8d122 to your computer and use it in GitHub Desktop.
Check if the mouse is in path with Skia4Delphi
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 480
ClientWidth = 580
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnCreate = FormCreate
DesignerMasterStyle = 0
object SkPaintBox1: TSkPaintBox
Align = Client
HitTest = True
Size.Width = 580.000000000000000000
Size.Height = 480.000000000000000000
Size.PlatformDefault = False
OnMouseLeave = SkPaintBox1MouseLeave
OnMouseMove = SkPaintBox1MouseMove
OnDraw = SkPaintBox1Draw
end
end
unit CheckInPath;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, Skia, Skia.FMX;
type
TForm1 = class(TForm)
SkPaintBox1: TSkPaintBox;
procedure SkPaintBox1Draw(ASender: TObject; const ACanvas: ISkCanvas;
const ADest: TRectF; const AOpacity: Single);
procedure FormCreate(Sender: TObject);
procedure SkPaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
procedure SkPaintBox1MouseLeave(Sender: TObject);
private
{ Private declarations }
FDrawPath: ISkPath;
FInsideDraw: Boolean;
FRegion: ISkRegion;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
function CreatePath: ISkPath;
var
LPathBuilder: ISkPathBuilder;
begin
LPathBuilder := TSkPathBuilder.Create;
LPathBuilder.AddArc(RectF(10, 10, 300, 300), 0, 180);
Result := LPathBuilder.Detach;
end;
function CreatePathPaint: ISkPaint;
begin
Result := TSkPaint.Create(TSkPaintStyle.Stroke);
Result.AntiAlias := True;
Result.StrokeWidth := 10;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
LPaintPath: ISkPath;
begin
FDrawPath := CreatePath;
// Get the final path after apply the paint, that is, with stroke style and stroke width
LPaintPath := CreatePathPaint.GetFillPath(FDrawPath);
// Create a Region responsible for checking if a coordinate is contained in a Path
FRegion := TSkRegion.Create;
FRegion.SetPath(LPaintPath, TSkRegion.Create(LPaintPath.Bounds.Round));
end;
procedure TForm1.SkPaintBox1Draw(ASender: TObject; const ACanvas: ISkCanvas;
const ADest: TRectF; const AOpacity: Single);
var
LPaint: ISkPaint;
begin
LPaint := CreatePathPaint;
if FInsideDraw then
LPaint.Color := TAlphaColors.Red;
ACanvas.DrawPath(FDrawPath, LPaint);
end;
procedure TForm1.SkPaintBox1MouseLeave(Sender: TObject);
begin
if FInsideDraw then
begin
FInsideDraw := False;
SkPaintBox1.Redraw;
end;
end;
procedure TForm1.SkPaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
var
LInsideDraw: Boolean;
begin
LInsideDraw := FRegion.Contains(Round(X), Round(Y));
if LInsideDraw <> FInsideDraw then
begin
FInsideDraw := LInsideDraw;
SkPaintBox1.Redraw;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment