-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
DW.NativeShape.iOS.pas
322 lines (278 loc) · 9.81 KB
/
DW.NativeShape.iOS.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
unit DW.NativeShape.iOS;
{*******************************************************}
{ }
{ 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
implementation
uses
// RTL
System.TypInfo, System.Classes, System.SysUtils, System.Math, System.Types, System.UITypes,
// macOS
Macapi.Helpers, Macapi.CoreFoundation,
// iOS
iOSapi.UIKit, iOSapi.CoreGraphics, iOSapi.QuartzCore, iOSapi.Foundation, iOSapi.CocoaTypes,
// FMX
FMX.Presentation.Messages, FMX.Presentation.iOS, FMX.Presentation.Factory, FMX.Controls, FMX.Controls.Presentation, FMX.Controls.Model,
FMX.Helpers.iOS, FMX.Types,
// DW
DW.NativeShape, DW.NativeControl.iOS;
type
INativeShape = interface(UIView)
['{BC29FC38-7022-4CDA-8F1C-51CB7B86A979}']
{ Native methods }
function canBecomeFirstResponder: Boolean; cdecl;
procedure touchesBegan(touches: NSSet; withEvent: UIEvent); cdecl;
procedure touchesCancelled(touches: NSSet; withEvent: UIEvent); cdecl;
procedure touchesEnded(touches: NSSet; withEvent: UIEvent); cdecl;
procedure touchesMoved(touches: NSSet; withEvent: UIEvent); cdecl;
{ Handlers }
procedure HandleLongPress(gestureRecognizer: UILongPressGestureRecognizer); cdecl;
end;
TiOSNativeShape = class(TNativeControl)
private
FFillPath: UIBezierPath;
FFillLayer: CAShapeLayer;
FShapeLayer: CAShapeLayer;
FShapePath: UIBezierPath;
function GetCGColorRef(const AColor: TAlphaColor): CGColorRef;
function GetShapeControl: TCustomNativeShape;
function GetView: UIView;
function GetModel: TCustomNativeShapeModel; overload;
procedure MMFillChanged(var AMessage: TDispatchMessage); message MM_NATIVESHAPE_FILL_CHANGED;
procedure MMOpacityChanged(var AMessage: TDispatchMessage); message MM_NATIVESHAPE_OPACITY_CHANGED;
procedure MMStrokeChanged(var AMessage: TDispatchMessage); message MM_NATIVESHAPE_STROKE_CHANGED;
protected
procedure DoLongPress; override;
procedure FillChanged; virtual;
procedure PathChanged;
procedure StrokeChanged; virtual;
function DefineModelClass: TDataModelClass; override;
property FillPath: UIBezierPath read FFillPath write FFillPath;
property ShapePath: UIBezierPath read FShapePath;
property ShapeControl: TCustomNativeShape read GetShapeControl;
public
constructor Create; override;
destructor Destroy; override;
property Model: TCustomNativeShapeModel read GetModel;
property View: UIView read GetView;
end;
TiOSNativeEllipse = class(TiOSNativeShape)
private
function GetModel: TCustomNativeEllipseModel; overload;
protected
function DefineModelClass: TDataModelClass; override;
function GetObjectiveCClass: PTypeInfo; override;
procedure SetSize(const ASize: TSizeF); override;
public
constructor Create; override;
property Model: TCustomNativeEllipseModel read GetModel;
end;
TiOSNativeRectangle = class(TiOSNativeShape)
private
function GetModel: TCustomNativeRectangleModel; overload;
procedure MMCornersChanged(var AMessage: TDispatchMessage); message MM_NATIVESHAPE_CORNERS_CHANGED;
procedure MMCornerTypeChanged(var AMessage: TDispatchMessage); message MM_NATIVESHAPE_CORNERTYPE_CHANGED;
procedure MMRadiusChanged(var AMessage: TDispatchMessage); message MM_NATIVESHAPE_RADIUS_CHANGED;
procedure MMSidesChanged(var AMessage: TDispatchMessage); message MM_NATIVESHAPE_SIDES_CHANGED;
procedure UpdatePath;
protected
function DefineModelClass: TDataModelClass; override;
function GetObjectiveCClass: PTypeInfo; override;
procedure SetSize(const ASize: TSizeF); override;
public
constructor Create; override;
property Model: TCustomNativeRectangleModel read GetModel;
end;
const
cPI = 3.141592654;
cTopRightStartAngle = cPI * 3 / 2;
cTopRightEndAngle = 0;
cBottomRightStartAngle = 0;
cBottomRightEndAngle = cPI / 2;
cBottomLeftStartAngle = cPI / 2;
cBottomLeftEndAngle = cPI;
cTopLeftStartAngle = cPI;
cTopLeftEndAngle = cPI * 3 / 2;
{ TiOSNativeShape }
constructor TiOSNativeShape.Create;
begin
inherited;
FFillPath := TUIBezierPath.Wrap(TUIBezierPath.OCClass.bezierPath);
FFillLayer := TCAShapeLayer.Create;
View.layer.addSublayer(FFillLayer);
FShapePath := TUIBezierPath.Wrap(TUIBezierPath.OCClass.bezierPath);
FShapeLayer := TCAShapeLayer.Create;
FShapeLayer.setFillColor(nil);
View.layer.addSublayer(FShapeLayer);
end;
destructor TiOSNativeShape.Destroy;
begin
//
inherited;
end;
procedure TiOSNativeShape.DoLongPress;
begin
Model.DoLongPress;
end;
function TiOSNativeShape.GetCGColorRef(const AColor: TAlphaColor): CGColorRef;
begin
if AColor = TAlphaColors.Null then
Result := nil
else
Result := AlphaColorToUIColor(AColor).CGColor;
end;
function TiOSNativeShape.DefineModelClass: TDataModelClass;
begin
Result := TCustomNativeShapeModel;
end;
function TiOSNativeShape.GetShapeControl: TCustomNativeShape;
begin
Result := TCustomNativeShape(Control)
end;
function TiOSNativeShape.GetView: UIView;
begin
Result := inherited GetView<UIView>;
end;
procedure TiOSNativeShape.MMFillChanged(var AMessage: TDispatchMessage);
begin
FillChanged;
end;
procedure TiOSNativeShape.MMOpacityChanged(var AMessage: TDispatchMessage);
begin
FFillLayer.setOpacity(Model.Opacity);
FShapeLayer.setOpacity(Model.Opacity);
end;
procedure TiOSNativeShape.MMStrokeChanged(var AMessage: TDispatchMessage);
begin
StrokeChanged;
end;
procedure TiOSNativeShape.FillChanged;
begin
FFillLayer.setFillColor(GetCGColorRef(Model.Fill.Color));
end;
procedure TiOSNativeShape.PathChanged;
begin
FShapeLayer.setPath(FShapePath.CGPath);
FFillLayer.setPath(FFillPath.CGPath);
FillChanged;
StrokeChanged;
end;
procedure TiOSNativeShape.StrokeChanged;
begin
FShapeLayer.setStrokeColor(GetCGColorRef(Model.Stroke.Color));
FShapeLayer.setLineWidth(Model.Stroke.Thickness);
end;
function TiOSNativeShape.GetModel: TCustomNativeShapeModel;
begin
Result := inherited GetModel<TCustomNativeShapeModel>;
end;
{ TiOSNativeEllipse }
constructor TiOSNativeEllipse.Create;
begin
inherited;
//
end;
function TiOSNativeEllipse.GetObjectiveCClass: PTypeInfo;
begin
Result := TypeInfo(INativeShape);
end;
function TiOSNativeEllipse.DefineModelClass: TDataModelClass;
begin
Result := TCustomNativeEllipseModel;
end;
function TiOSNativeEllipse.GetModel: TCustomNativeEllipseModel;
begin
Result := inherited GetModel<TCustomNativeEllipseModel>;
end;
procedure TiOSNativeEllipse.SetSize(const ASize: TSizeF);
var
LPoint: CGPoint;
begin
inherited;
FillPath.removeAllPoints;
ShapePath.removeAllPoints;
LPoint.x := Size.cx / 2;
LPoint.y := Size.cy / 2;
FillPath.addArcWithCenter(LPoint, Min(LPoint.x, LPoint.y), 0, cPI * 2, True);
ShapePath.addArcWithCenter(LPoint, Min(LPoint.x, LPoint.y), 0, cPI * 2, True);
PathChanged;
end;
{ TiOSNativeRectangle }
constructor TiOSNativeRectangle.Create;
begin
inherited;
//
end;
function TiOSNativeRectangle.GetObjectiveCClass: PTypeInfo;
begin
Result := TypeInfo(INativeShape);
end;
function TiOSNativeRectangle.DefineModelClass: TDataModelClass;
begin
Result := TCustomNativeRectangleModel;
end;
function TiOSNativeRectangle.GetModel: TCustomNativeRectangleModel;
begin
Result := inherited GetModel<TCustomNativeRectangleModel>;
end;
procedure TiOSNativeRectangle.MMCornersChanged(var AMessage: TDispatchMessage);
begin
UpdatePath;
end;
procedure TiOSNativeRectangle.MMCornerTypeChanged(var AMessage: TDispatchMessage);
begin
UpdatePath;
end;
procedure TiOSNativeRectangle.MMRadiusChanged(var AMessage: TDispatchMessage);
begin
UpdatePath;
end;
procedure TiOSNativeRectangle.MMSidesChanged(var AMessage: TDispatchMessage);
begin
UpdatePath;
end;
procedure TiOSNativeRectangle.SetSize(const ASize: TSizeF);
begin
inherited;
UpdatePath;
end;
procedure TiOSNativeRectangle.UpdatePath;
begin
ShapePath.removeAllPoints;
FillPath := TUIBezierPath.Wrap(TUIBezierPath.OCClass.bezierPathWithRect(CGRectMake(0, 0, Size.cx, Size.cy)));
if TSide.Top in Model.Sides then
begin
ShapePath.moveToPoint(CGPointMake(0, 0));
ShapePath.addLineToPoint(CGPointMake(Size.cx, 0));
end;
if TSide.Left in Model.Sides then
begin
ShapePath.moveToPoint(CGPointMake(0, 0));
ShapePath.addLineToPoint(CGPointMake(0, Size.cy));
end;
if TSide.Right in Model.Sides then
begin
ShapePath.moveToPoint(CGPointMake(Size.cx, 0));
ShapePath.addLineToPoint(CGPointMake(Size.cx, Size.cy));
end;
if TSide.Bottom in Model.Sides then
begin
ShapePath.moveToPoint(CGPointMake(0, Size.cy));
ShapePath.addLineToPoint(CGPointMake(Size.cx, Size.cy));
end;
PathChanged;
end;
initialization
TPresentationProxyFactory.Current.Register(TNativeEllipse, TControlType.Platform, TiOSPresentationProxy<TiOSNativeEllipse>);
TPresentationProxyFactory.Current.Register(TNativeRectangle, TControlType.Platform, TiOSPresentationProxy<TiOSNativeRectangle>);
finalization
TPresentationProxyFactory.Current.Unregister(TNativeEllipse, TControlType.Platform, TiOSPresentationProxy<TiOSNativeEllipse>);
TPresentationProxyFactory.Current.Unregister(TNativeRectangle, TControlType.Platform, TiOSPresentationProxy<TiOSNativeRectangle>);
end.