-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
DW.NativeShape.pas
617 lines (540 loc) · 17.2 KB
/
DW.NativeShape.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
unit DW.NativeShape;
{*******************************************************}
{ }
{ Kastri }
{ }
{ Delphi Worlds Cross-Platform Library }
{ }
{ Copyright 2020-2024 Dave Nottage under MIT license }
{ which is located in the root folder of this library }
{ }
{*******************************************************}
interface
uses
// RTL
System.Classes, System.Types,
// FMX
FMX.Controls.Presentation, FMX.Controls.Model, FMX.Graphics, FMX.Types,
// DW
DW.NativeControlModel;
const
MM_NATIVESHAPE_FILL_CHANGED = MM_USER + 1;
MM_NATIVESHAPE_STROKE_CHANGED = MM_USER + 2;
MM_NATIVESHAPE_SIDES_CHANGED = MM_USER + 3;
MM_NATIVESHAPE_CORNERS_CHANGED = MM_USER + 4;
MM_NATIVESHAPE_RADIUS_CHANGED = MM_USER + 5;
MM_NATIVESHAPE_CORNERTYPE_CHANGED = MM_USER + 6;
MM_NATIVESHAPE_OPACITY_CHANGED = MM_USER + 7;
type
TCustomNativeShapeModel = class(TNativeControlModel)
private
FFill: TBrush;
FOpacity: Single;
FStroke: TStrokeBrush;
FOnChange: TNotifyEvent;
procedure DoChange;
procedure FillChanged(Sender: TObject); virtual;
procedure StrokeChanged(Sender: TObject); virtual;
procedure SetFill(const Value: TBrush);
procedure SetOpacity(const Value: Single);
procedure SetStroke(const Value: TStrokeBrush);
protected
property OnChange: TNotifyEvent read FOnChange write FOnChange;
public
constructor Create(const AOwner: TComponent); override;
destructor Destroy; override;
property Fill: TBrush read FFill write SetFill;
property Opacity: Single read FOpacity;
property Stroke: TStrokeBrush read FStroke write SetStroke;
end;
TCustomNativeShape = class(TPresentedControl)
private
function GetFill: TBrush;
function GetModel: TCustomNativeShapeModel; overload;
function GetOnLongPress: TNotifyEvent;
function GetStroke: TStrokeBrush;
procedure ModelChangeHandler(Sender: TObject);
procedure SetFill(const Value: TBrush);
procedure SetOnLongPress(const Value: TNotifyEvent);
procedure SetStroke(const Value: TStrokeBrush);
protected
function CanPaint: Boolean; virtual;
procedure AfterPaint; override;
function DefineModelClass: TDataModelClass; override;
function GetShapeRect: TRectF;
function RecommendSize(const AWishedSize: TSizeF): TSizeF; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure RecalcOpacity; override;
property Fill: TBrush read GetFill write SetFill;
property Model: TCustomNativeShapeModel read GetModel;
property Stroke: TStrokeBrush read GetStroke write SetStroke;
property OnLongPress: TNotifyEvent read GetOnLongPress write SetOnLongPress;
end;
TCustomNativeEllipseModel = class(TCustomNativeShapeModel);
TCustomNativeEllipse = class(TCustomNativeShape)
private
function GetModel: TCustomNativeEllipseModel; overload;
protected
function DefineModelClass: TDataModelClass; override;
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
property Model: TCustomNativeEllipseModel read GetModel;
end;
{$IF CompilerVersion < 35}
[ComponentPlatformsAttribute(pfidiOS or pidAndroid32Arm or pidAndroid64Arm or pidWin32 or pidWin64)]
{$ELSE}
[ComponentPlatformsAttribute(pfidiOS or pidAndroidArm32 or pidAndroidArm64 or pidWin32 or pidWin64)]
{$ENDIF}
TNativeEllipse = class(TCustomNativeEllipse)
published
property Align;
property Anchors;
property Fill;
property Height;
property HitTest;
property Margins;
property Opacity;
property Position;
property Size;
property Stroke;
property Visible default True;
property Width;
property OnClick;
property OnDblClick;
property OnLongPress;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnResized;
end;
TCustomNativeRectangleModel = class(TCustomNativeShapeModel)
private
FYRadius: Single;
FXRadius: Single;
FCorners: TCorners;
FCornerType: TCornerType;
FSides: TSides;
procedure SetCorners(const Value: TCorners); virtual;
procedure SetCornerType(const Value: TCornerType); virtual;
procedure SetSides(const Value: TSides); virtual;
procedure SetXRadius(const Value: Single); virtual;
procedure SetYRadius(const Value: Single); virtual;
public
constructor Create(const AOwner: TComponent); override;
property Corners: TCorners read FCorners write SetCorners;
property CornerType: TCornerType read FCornerType write SetCornerType;
property Sides: TSides read FSides write SetSides;
property XRadius: Single read FXRadius write SetXRadius;
property YRadius: Single read FYRadius write SetYRadius;
end;
TCustomNativeRectangle = class(TCustomNativeShape)
private
function GetCorners: TCorners;
function GetCornerType: TCornerType;
function GetSides: TSides;
function GetXRadius: Single;
function GetYRadius: Single;
procedure SetCorners(const Value: TCorners);
procedure SetCornerType(const Value: TCornerType);
procedure SetSides(const Value: TSides);
procedure SetXRadius(const Value: Single);
procedure SetYRadius(const Value: Single);
protected
function DefineModelClass: TDataModelClass; override;
function GetModel: TCustomNativeRectangleModel; overload;
function IsCornersStored: Boolean;
function IsSidesStored: Boolean;
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
property Corners: TCorners read GetCorners write SetCorners;
property CornerType: TCornerType read GetCornerType write SetCornerType;
property Model: TCustomNativeRectangleModel read GetModel;
property Sides: TSides read GetSides write SetSides;
property XRadius: Single read GetXRadius write SetXRadius;
property YRadius: Single read GetYRadius write SetYRadius;
end;
{$IF CompilerVersion < 35}
[ComponentPlatformsAttribute(pfidiOS or pidAndroid32Arm or pidAndroid64Arm or pidWin32 or pidWin64)]
{$ELSE}
[ComponentPlatformsAttribute(pfidiOS or pidAndroidArm32 or pidAndroidArm64 or pidWin32 or pidWin64)]
{$ENDIF}
TNativeRectangle = class(TCustomNativeRectangle)
published
property Align;
property Anchors;
property Corners stored IsCornersStored;
property CornerType default TCornerType.Round;
property Fill;
property Height;
property HitTest;
property Margins;
property Opacity;
property Position;
property Sides stored IsSidesStored;
property Size;
property Stroke;
property Visible default True;
property Width;
property XRadius;
property YRadius;
property OnClick;
property OnDblClick;
property OnLongPress;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnResized;
end;
procedure Register;
implementation
uses
// FMX
FMX.Controls,
{$IF Defined(IOS)}
DW.NativeShape.iOS,
{$ENDIF}
{$IF Defined(ANDROID)}
DW.NativeShape.Android,
{$ENDIF}
// RTL
System.SysUtils, System.IOUtils, System.UITypes, System.Math,
// FMX
FMX.Objects;
procedure Register;
begin
RegisterComponents('Kastri FMX', [TNativeEllipse, TNativeRectangle]);
end;
function GetDrawingShapeRectAndSetThickness(const AShape: TCustomNativeShape; const Fit: Boolean; var FillShape, DrawShape: Boolean;
var StrokeThicknessRestoreValue: Single): TRectF;
const
MinRectAreaSize = 0.01;
begin
FillShape := (AShape.Fill <> nil) and (AShape.Fill.Kind <> TBrushKind.None);
DrawShape := (AShape.Stroke <> nil) and (AShape.Stroke.Kind <> TBrushKind.None);
if Fit then
Result := TRectF.Create(0, 0, 1, 1).FitInto(AShape.LocalRect)
else
Result := AShape.LocalRect;
if DrawShape then
begin
if Result.Width < AShape.Stroke.Thickness then
begin
StrokeThicknessRestoreValue := AShape.Stroke.Thickness;
FillShape := False;
AShape.Stroke.Thickness := Min(Result.Width, Result.Height);
Result.Left := (Result.Right + Result.Left) * 0.5;
Result.Right := Result.Left + MinRectAreaSize;
end
else
Result.Inflate(-AShape.Stroke.Thickness * 0.5, 0);
if Result.Height < AShape.Stroke.Thickness then
begin
if StrokeThicknessRestoreValue < 0.0 then
StrokeThicknessRestoreValue := AShape.Stroke.Thickness;
FillShape := False;
AShape.Stroke.Thickness := Min(Result.Width, Result.Height);
Result.Top := (Result.Bottom + Result.Top) * 0.5;
Result.Bottom := Result.Top + MinRectAreaSize;
end
else
Result.Inflate(0, -AShape.Stroke.Thickness * 0.5);
end;
end;
{ TCustomNativeShapeModel }
constructor TCustomNativeShapeModel.Create(const AOwner: TComponent);
begin
inherited;
FFill := TBrush.Create(TBrushKind.Solid, TAlphaColors.Null);
FFill.OnChanged := FillChanged;
FStroke := TStrokeBrush.Create(TBrushKind.Solid, TAlphaColors.Black);
FStroke.OnChanged := StrokeChanged;
end;
destructor TCustomNativeShapeModel.Destroy;
begin
FStroke.Free;
FFill.Free;
inherited;
end;
procedure TCustomNativeShapeModel.DoChange;
begin
if Assigned(FOnChange) then
FOnChange(Self);
end;
procedure TCustomNativeShapeModel.FillChanged(Sender: TObject);
begin
DoChange;
SendMessage(MM_NATIVESHAPE_FILL_CHANGED);
end;
procedure TCustomNativeShapeModel.SetFill(const Value: TBrush);
begin
FFill.Assign(Value);
end;
procedure TCustomNativeShapeModel.SetOpacity(const Value: Single);
begin
FOpacity := Value;
DoChange;
SendMessage(MM_NATIVESHAPE_OPACITY_CHANGED);
end;
procedure TCustomNativeShapeModel.SetStroke(const Value: TStrokeBrush);
begin
FStroke.Assign(Value);
end;
procedure TCustomNativeShapeModel.StrokeChanged(Sender: TObject);
begin
DoChange;
SendMessage(MM_NATIVESHAPE_STROKE_CHANGED);
end;
{ TCustomNativeShape }
constructor TCustomNativeShape.Create(AOwner: TComponent);
begin
inherited;
Model.OnChange := ModelChangeHandler;
end;
destructor TCustomNativeShape.Destroy;
begin
//
inherited;
end;
procedure TCustomNativeShape.AfterPaint;
begin
//
end;
function TCustomNativeShape.DefineModelClass: TDataModelClass;
begin
Result := TCustomNativeShapeModel;
end;
procedure TCustomNativeShape.ModelChangeHandler(Sender: TObject);
begin
Repaint;
end;
function TCustomNativeShape.GetFill: TBrush;
begin
Result := Model.Fill;
end;
function TCustomNativeShape.GetModel: TCustomNativeShapeModel;
begin
Result := inherited GetModel<TCustomNativeShapeModel>;
end;
function TCustomNativeShape.GetOnLongPress: TNotifyEvent;
begin
Result := Model.OnLongPress;
end;
function TCustomNativeShape.GetStroke: TStrokeBrush;
begin
Result := Model.Stroke;
end;
procedure TCustomNativeShape.SetFill(const Value: TBrush);
begin
Model.Fill := Value;
end;
procedure TCustomNativeShape.SetOnLongPress(const Value: TNotifyEvent);
begin
Model.OnLongPress := Value;
end;
procedure TCustomNativeShape.SetStroke(const Value: TStrokeBrush);
begin
Model.Stroke := Value;
end;
procedure TCustomNativeShape.RecalcOpacity;
begin
Model.SetOpacity(Opacity);
end;
function TCustomNativeShape.RecommendSize(const AWishedSize: TSizeF): TSizeF;
begin
Result := AWishedSize;
end;
function TCustomNativeShape.CanPaint: Boolean;
begin
Result := (csDesigning in ComponentState) and not Locked and not FInPaintTo;
end;
function TCustomNativeShape.GetShapeRect: TRectF;
begin
Result := LocalRect;
if Stroke.Kind <> TBrushKind.None then
InflateRect(Result, -(Stroke.Thickness / 2), -(Stroke.Thickness / 2));
end;
{ TCustomNativeEllipse }
constructor TCustomNativeEllipse.Create(AOwner: TComponent);
begin
inherited;
ControlType := TControlType.Platform;
end;
function TCustomNativeEllipse.DefineModelClass: TDataModelClass;
begin
Result := TCustomNativeEllipseModel;
end;
function TCustomNativeEllipse.GetModel: TCustomNativeEllipseModel;
begin
Result := inherited GetModel<TCustomNativeEllipseModel>;
end;
procedure TCustomNativeEllipse.Paint;
begin
if CanPaint then
begin
Canvas.Fill.Assign(Model.Fill);
Canvas.Stroke.Assign(Model.Stroke);
Canvas.FillEllipse(TRectF.Create(0, 0, Width, Height), Opacity);
Canvas.DrawEllipse(TRectF.Create(0, 0, Width, Height), Opacity);
end;
end;
{ TCustomNativeRectangleModel }
constructor TCustomNativeRectangleModel.Create(const AOwner: TComponent);
begin
inherited;
FSides := AllSides;
end;
procedure TCustomNativeRectangleModel.SetCorners(const Value: TCorners);
begin
if Value <> FCorners then
begin
FCorners := Value;
DoChange;
SendMessage(MM_NATIVESHAPE_CORNERS_CHANGED);
end;
end;
procedure TCustomNativeRectangleModel.SetCornerType(const Value: TCornerType);
begin
if Value <> FCornerType then
begin
FCornerType := Value;
SendMessage(MM_NATIVESHAPE_CORNERTYPE_CHANGED);
DoChange;
end;
end;
procedure TCustomNativeRectangleModel.SetSides(const Value: TSides);
begin
if Value <> FSides then
begin
FSides := Value;
SendMessage(MM_NATIVESHAPE_SIDES_CHANGED);
DoChange;
end;
end;
procedure TCustomNativeRectangleModel.SetXRadius(const Value: Single);
begin
if Value <> FXRadius then
begin
FXRadius := Value;
SendMessage(MM_NATIVESHAPE_RADIUS_CHANGED);
DoChange;
end;
end;
procedure TCustomNativeRectangleModel.SetYRadius(const Value: Single);
begin
if Value <> FYRadius then
begin
FYRadius := Value;
SendMessage(MM_NATIVESHAPE_RADIUS_CHANGED);
DoChange;
end;
end;
{ TCustomNativeRectangle }
constructor TCustomNativeRectangle.Create(AOwner: TComponent);
begin
inherited;
ControlType := TControlType.Platform;
Model.OnChange := ModelChangeHandler;
end;
function TCustomNativeRectangle.DefineModelClass: TDataModelClass;
begin
Result := TCustomNativeRectangleModel;
end;
function TCustomNativeRectangle.GetModel: TCustomNativeRectangleModel;
begin
Result := inherited GetModel<TCustomNativeRectangleModel>;
end;
function TCustomNativeRectangle.GetCorners: TCorners;
begin
Result := Model.Corners;
end;
function TCustomNativeRectangle.GetCornerType: TCornerType;
begin
Result := Model.CornerType;
end;
function TCustomNativeRectangle.GetSides: TSides;
begin
Result := Model.Sides;
end;
function TCustomNativeRectangle.GetXRadius: Single;
begin
Result := Model.XRadius;
end;
function TCustomNativeRectangle.GetYRadius: Single;
begin
Result := Model.YRadius;
end;
function TCustomNativeRectangle.IsCornersStored: Boolean;
begin
Result := Model.Corners <> AllCorners;
end;
function TCustomNativeRectangle.IsSidesStored: Boolean;
begin
Result := Model.Sides * AllSides <> AllSides
end;
procedure TCustomNativeRectangle.Paint;
var
LShapeRect: TRectF;
LOffset: Single;
LThickness: Single;
LNeedsFillRect, LNeedsDrawRect: Boolean;
begin
if CanPaint then
begin
LThickness := Stroke.Thickness;
try
LShapeRect := GetDrawingShapeRectAndSetThickness(Self, False, LNeedsFillRect, LNeedsDrawRect, LThickness);
if Sides <> AllSides then
begin
LOffset := LShapeRect.Left;
if not (TSide.Top in Sides) then
LShapeRect.Top := LShapeRect.Top - LOffset;
if not (TSide.Left in Sides) then
LShapeRect.Left := LShapeRect.Left - LOffset;
if not (TSide.Bottom in Sides) then
LShapeRect.Bottom := LShapeRect.Bottom + LOffset;
if not (TSide.Right in Sides) then
LShapeRect.Right := LShapeRect.Right + LOffset;
if LNeedsFillRect then
Canvas.FillRect(LShapeRect, XRadius, YRadius, Corners, Opacity, Fill, CornerType);
if LNeedsDrawRect then
Canvas.DrawRectSides(GetShapeRect, XRadius, YRadius, Corners, Opacity, Sides, Stroke, CornerType);
end
else
begin
if LNeedsFillRect then
Canvas.FillRect(LShapeRect, XRadius, YRadius, Corners, Opacity, Fill, CornerType);
if LNeedsDrawRect then
Canvas.DrawRect(LShapeRect, XRadius, YRadius, Corners, Opacity, Stroke, CornerType);
end;
finally
Stroke.Thickness := LThickness;
end;
end;
end;
procedure TCustomNativeRectangle.SetCorners(const Value: TCorners);
begin
Model.Corners := Value;
end;
procedure TCustomNativeRectangle.SetCornerType(const Value: TCornerType);
begin
Model.CornerType := Value;
end;
procedure TCustomNativeRectangle.SetSides(const Value: TSides);
begin
Model.Sides := Value;
end;
procedure TCustomNativeRectangle.SetXRadius(const Value: Single);
begin
Model.XRadius := Value;
end;
procedure TCustomNativeRectangle.SetYRadius(const Value: Single);
begin
Model.YRadius := Value;
end;
end.