-
Notifications
You must be signed in to change notification settings - Fork 6
/
fKeyboard.pas
363 lines (323 loc) · 8.11 KB
/
fKeyboard.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
unit fKeyboard;
interface
uses
// Delphi
System.Classes,
// FireMonkey
FMX.Ani,
FMX.Forms,
FMX.Types,
FMX.Objects,
FMX.Controls,
FMX.StdCtrls,
FMX.Controls.Presentation;
type
TfrmKeyboard = class(TForm)
pnlCallout: TCalloutPanel;
aniTop: TFloatAnimation;
aniHeight: TFloatAnimation;
aniOpacity: TFloatAnimation;
rct1: TRoundRect;
btn1: TButton;
rct2: TRoundRect;
btn2: TButton;
rct3: TRoundRect;
btn3: TButton;
rct4: TRoundRect;
btn4: TButton;
rct5: TRoundRect;
btn5: TButton;
rct6: TRoundRect;
btn6: TButton;
rct7: TRoundRect;
btn7: TButton;
rct8: TRoundRect;
btn8: TButton;
rct9: TRoundRect;
btn9: TButton;
rctAC: TRoundRect;
btnAC: TButton;
rct0: TRoundRect;
btn0: TButton;
rctOK: TRoundRect;
btnOK: TButton;
procedure btnNumClick(Sender: TObject);
procedure btnACClick(Sender: TObject);
procedure btnOKClick(Sender: TObject);
private
FButton: TButton;
FDigits: Integer;
FDecimals: Integer;
FAnimations: Boolean;
FOnCalculate: TNotifyEvent;
procedure Clear;
procedure Calculate;
procedure KeyPress(Value: Integer);
procedure SetButton(Value: TButton);
public
constructor Create(AOwner: TComponent); override;
property Digits: Integer read FDigits write FDigits;
property Button: TButton read FButton write SetButton;
property Decimals: Integer read FDecimals write FDecimals;
property Animations: Boolean read FAnimations write FAnimations;
property OnCalculate: TNotifyEvent read FOnCalculate write FOnCalculate;
end;
var
frmKeyboard: TfrmKeyboard;
function Keyboard: TfrmKeyboard;
implementation
{$R *.fmx}
{$R *.iPhone47in.fmx IOS}
{$R *.iPhone55in.fmx IOS}
{$R *.iPhone.fmx IOS}
{$R *.iPhone4in.fmx IOS}
uses
// Delphi
System.SysUtils,
// Project
uSettings;
var
varKeyboard: TfrmKeyboard = nil;
function Keyboard: TfrmKeyboard;
begin
if not Assigned(varKeyboard) then
varKeyboard := TfrmKeyboard.Create(Application);
Result := varKeyboard;
end;
function GetContainer(AComponent: TFMXObject): TControl;
var
P: TFMXObject;
begin
Result := nil;
P := AComponent;
while Assigned(P) do
begin
if P is TContent then
EXIT;
if P is TCommonCustomForm then
EXIT;
if P is TControl then
Result := TControl(P);
P := P.Parent;
end;
end;
function GetParentTab(AComponent: TFMXObject): TContent;
var
P: TFMXObject;
begin
Result := nil;
P := AComponent.Parent;
while Assigned(P) do
begin
if P is TContent then
begin
Result := TContent(P);
EXIT;
end;
P := P.Parent;
end;
end;
function GetParentForm(AComponent: TFMXObject): TCommonCustomForm;
var
P: TFMXObject;
begin
Result := nil;
P := AComponent.Parent;
while Assigned(P) do
begin
if P is TCommonCustomForm then
begin
Result := TCommonCustomForm(P);
EXIT;
end;
P := P.Parent;
end;
end;
function GetAnimation(AControl: TControl; const PropertyName: string): TFloatAnimation;
var
I: Integer;
begin
for I := 0 to Pred(AControl.ChildrenCount) do
if AControl.Children[I] is TFloatAnimation then
if TFloatAnimation(AControl.Children[I]).PropertyName = PropertyName then
begin
Result := TFloatAnimation(AControl.Children[I]);
EXIT;
end;
Result := nil;
end;
function GetAnimationTop(AControl: TControl): TFloatAnimation;
begin
Result := GetAnimation(AControl, 'Position.Y');
end;
function GetAnimationHeight(AControl: TControl): TFloatAnimation;
begin
Result := GetAnimation(AControl, 'Height');
end;
function GetAnimationOpacity(AControl: TControl): TFloatAnimation;
begin
Result := GetAnimation(AControl, 'Opacity');
end;
{ TfrmKeyboard}
constructor TfrmKeyboard.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAnimations := True;
FButton := nil;
end;
procedure TfrmKeyboard.Calculate;
begin
if Assigned(FOnCalculate) then
FOnCalculate(Self);
end;
procedure TfrmKeyboard.SetButton(Value: TButton);
var
aTop, aHeight, aOpacity: TFloatAnimation;
begin
if Value <> FButton then
begin
FButton := Value;
aTop := nil;
aHeight := nil;
aOpacity := nil;
if Assigned(FButton) then
begin
Clear;
pnlCallout.Parent := GetParentTab(FButton);
pnlCallout.Position.Y := GetContainer(FButton).Position.Y + FButton.Height - 6;
pnlCallout.Height := GetParentTab(FButton).Height - pnlCallout.Position.Y - 3;
if FAnimations then
begin
// get TFloatAnimation for Position.Y
aTop := GetAnimationTop(pnlCallout);
if Assigned(aTop) then
begin
aTop.StartValue := GetParentTab(FButton).Height - 3;
aTop.StopValue := GetContainer(FButton).Position.Y + FButton.Height - 6;
end;
// get TFloatAnimation for Height
aHeight := GetAnimationHeight(pnlCallout);
if Assigned(aHeight) then
begin
aHeight.StartValue := 0;
aHeight.StopValue := GetParentTab(FButton).Height - pnlCallout.Position.Y - 3;
end;
// get TFloatAnimation for Opacity
aOpacity := GetAnimationOpacity(pnlCallout);
if Assigned(aOpacity) then
begin
aOpacity.StartValue := 0;
aOpacity.StopValue := 1;
end;
end
else
pnlCallout.Opacity := 1;
pnlCallout.Visible := True;
end
else
if FAnimations then
begin
// get TFloatAnimation for Position.Y
aTop := GetAnimationTop(pnlCallout);
if Assigned(aTop) then
begin
aTop.StartValue := pnlCallout.Position.Y;
aTop.StopValue := GetParentTab(pnlCallout).Height - 3;
end;
// get TFloatAnimation for Height
aHeight := GetAnimationHeight(pnlCallout);
if Assigned(aHeight) then
begin
aHeight.StartValue := pnlCallout.Height;
aHeight.StopValue := 0;
end;
// get TFloatAnimation for Opacity
aOpacity := GetAnimationOpacity(pnlCallout);
if Assigned(aOpacity) then
begin
aOpacity.StartValue := 1;
aOpacity.StopValue := 0;
end;
end
else
pnlCallout.Visible := False;
if Assigned(aTop) then
aTop.Start;
if Assigned(aHeight) then
aHeight.Start;
if Assigned(aOpacity) then
aOpacity.Start;
end;
end;
procedure TfrmKeyboard.Clear;
begin
if not Assigned(FButton) then
EXIT;
FButton.Text := '0';
if FDecimals > 0 then
FButton.Text := FButton.Text + '.0';
end;
procedure TfrmKeyboard.KeyPress(Value: Integer);
var
I: Integer;
D1, D2: string;
begin
if not Assigned(FButton) then
EXIT;
if (FButton.Text <> '') and (StrToFloat(FButton.Text, FormatSettingsFloat) = 0) then
FButton.Text := '';
if FDecimals = 0 then
begin
if Length(FButton.Text) < FDigits then
FButton.Text := FButton.Text + IntToStr(Value);
EXIT;
end;
D1 := '0';
D2 := '0';
if FButton.Text <> '' then
begin
I := Pos('.', FButton.Text);
if I > 0 then
begin
D1 := Copy(FButton.Text, 1, I - 1);
D2 := Copy(FButton.Text, I + 1, MaxInt);
end;
end;
if (StrToIntDef(D1, 0) = 0) and ((StrToIntDef(D2, 0) = 0) or (Length(D2) < FDecimals)) then
begin
FButton.Text := D1 + '.';
if StrToIntDef(D2, 0) <> 0 then
FButton.Text := FButton.Text + D2;
FButton.Text := FButton.Text + IntToStr(Value);
EXIT;
end;
if (StrToIntDef(D1, 0) = 0) or (Length(D1) < FDigits) then
begin
FButton.Text := '';
if StrToIntDef(D1, 0) <> 0 then
FButton.Text := D1;
if D2 <> '' then
FButton.Text := FButton.Text + D2[Low(D2)];
FButton.Text := FButton.Text + '.';
for I := 1 to Pred(Length(D2)) do
FButton.Text := FButton.Text + D2[I];
FButton.Text := FButton.Text + IntToStr(Value);
EXIT;
end;
end;
procedure TfrmKeyboard.btnNumClick(Sender: TObject);
begin
if Sender is TButton then
KeyPress(StrToInt(TButton(Sender).Text));
Calculate;
end;
procedure TfrmKeyboard.btnACClick(Sender: TObject);
begin
Clear;
Calculate;
end;
procedure TfrmKeyboard.btnOKClick(Sender: TObject);
begin
Button := nil;
end;
end.