-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainUnit.pas
693 lines (610 loc) · 24.5 KB
/
mainUnit.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
{$I COMPILER_DIRECTIVES.INC}
unit mainUnit;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Objects, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Platform;
type
TMainForm = class(TForm)
mathLineLabel: TLabel;
mathLineBG: TRectangle;
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
procedure FunctionMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
procedure FunctionMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
private
{ Private declarations }
public
{ Public declarations }
procedure OutputMathResult;
procedure AdjustResultFontSize;
{$IFDEF ANDROID}
function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
{$ENDIF}
end;
TCalcKeyRecord =
Record
keyFunction : Integer;
keyText : String;
keyColumn : Integer;
keyRow : Integer;
keyShade : Integer;
End;
const
calcKeyS1L1Grad1 : TAlphaColor = $FFF0F0F0;
calcKeyS1L1Grad2 : TAlphaColor = $FFC0C0C0;
calcKeyS1L2Grad1 : TAlphaColor = $FFE0E0E0;
calcKeyS1L2Grad2 : TAlphaColor = $FFB0B0B0;
calcKeyS2L1Grad1 : TAlphaColor = $FFB0B0B0;
calcKeyS2L1Grad2 : TAlphaColor = $FF909090;
calcKeyS2L2Grad1 : TAlphaColor = $FFA0A0A0;
calcKeyS2L2Grad2 : TAlphaColor = $FF707070;
calcKeyS3L1Grad1 : TAlphaColor = $FFEFB000;
calcKeyS3L1Grad2 : TAlphaColor = $FFAF6000;
calcKeyS3L2Grad1 : TAlphaColor = $FFEDB000;
calcKeyS3L2Grad2 : TAlphaColor = $FFAF6000;
multiplySymbol = #$00D7;
divisionSymbol = #$00F7;
backspaceSymbol = #$2190;//#$232B;
keyShade1 = 0;
keyShade2 = 1;
keyShade3 = 2;
funcClear = 0;
funcPlus = 1;
funcMinus = 2;
funcDivide = 3;
funcMultiply = 4;
funcBackSpace = 5;
func0 = 6;
func1 = 7;
func2 = 8;
func3 = 9;
func4 = 10;
func5 = 11;
func6 = 12;
func7 = 13;
func8 = 14;
func9 = 15;
funcDot = 16;
funcMemory = 17;
funcRecall = 18;
funcEqual = 19;
calcKeyCount = 20;
calcKeyMap : Array[0..calcKeyCount-1] of TCalcKeyRecord = (
(keyFunction : funcClear ; keyText : 'C'; keyColumn : 0; keyRow : 0; KeyShade : keyShade2),
(keyFunction : funcDivide ; keyText : divisionSymbol; keyColumn : 1; keyRow : 0; KeyShade : keyShade2),
(keyFunction : funcMultiply ; keyText : multiplySymbol; keyColumn : 2; keyRow : 0; KeyShade : keyShade2),
(keyFunction : funcBackSpace; keyText : backspaceSymbol; keyColumn : 3; keyRow : 0; KeyShade : keyShade2),
(keyFunction : func7 ; keyText : '7'; keyColumn : 0; keyRow : 1; KeyShade : keyShade1),
(keyFunction : func8 ; keyText : '8'; keyColumn : 1; keyRow : 1; KeyShade : keyShade1),
(keyFunction : func9 ; keyText : '9'; keyColumn : 2; keyRow : 1; KeyShade : keyShade1),
(keyFunction : funcMinus ; keyText : '-'; keyColumn : 3; keyRow : 1; KeyShade : keyShade2),
(keyFunction : func4 ; keyText : '4'; keyColumn : 0; keyRow : 2; KeyShade : keyShade1),
(keyFunction : func5 ; keyText : '5'; keyColumn : 1; keyRow : 2; KeyShade : keyShade1),
(keyFunction : func6 ; keyText : '6'; keyColumn : 2; keyRow : 2; KeyShade : keyShade1),
(keyFunction : funcPlus ; keyText : '+'; keyColumn : 3; keyRow : 2; KeyShade : keyShade2),
(keyFunction : func1 ; keyText : '1'; keyColumn : 0; keyRow : 3; KeyShade : keyShade1),
(keyFunction : func2 ; keyText : '2'; keyColumn : 1; keyRow : 3; KeyShade : keyShade1),
(keyFunction : func3 ; keyText : '3'; keyColumn : 2; keyRow : 3; KeyShade : keyShade1),
(keyFunction : funcDot ; keyText : ''; keyColumn : 3; keyRow : 3; KeyShade : keyShade2),
(keyFunction : funcMemory ; keyText : 'M'; keyColumn : 0; keyRow : 4; KeyShade : keyShade3),
(keyFunction : func0 ; keyText : '0'; keyColumn : 1; keyRow : 4; KeyShade : keyShade1),
(keyFunction : funcRecall ; keyText : 'MR'; keyColumn : 2; keyRow : 4; KeyShade : keyShade3),
(keyFunction : funcEqual ; keyText : '='; keyColumn : 3; keyRow : 4; KeyShade : keyShade2)
);
stAdd = 0;
stSub = 1;
stMul = 2;
stDiv = 3;
var
MainForm : TMainForm;
clientMemoryValue : String = '';
clientScreenService : IFMXScreenService;
implementation
{$R *.fmx}
uses System.Character, FMX.Utils, System.IOUtils, System.Diagnostics, misc_utils;
const
clientGameName : String = 'Elegant Calculator';
var
UserDataPath : String;
clientStopWatch : TStopWatch;
clientScreenSize : TSize;
clientScreenScale : Single = 1;
clientScreenScaleSrc : Single = 1;
clientFunctionPressed : Integer = -1;
clientDotEnabled : Boolean = False;
clientResultShown : Boolean = False;
lastResizeWidth : Integer = -1;
lastResizeHeight : Integer = -1;
calcKeyImages : Array[0..calcKeyCount-1] of TImage;
procedure TMainForm.FormCreate(Sender: TObject);
{$IFDEF ANDROID}
var
FMXApplicationEventService : IFMXApplicationEventService;
{$ENDIF}
begin
// Initialize the stopwatch used for timestamping
clientStopWatch := TStopWatch.Create;
clientStopWatch.Start;
{$IFDEF ANDROID}
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(FMXApplicationEventService)) then
FMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent);
{$ENDIF}
UserDataPath := System.IOUtils.TPath.GetHomePath+System.IOUtils.TPath.DirectorySeparatorChar+clientGameName+System.IOUtils.TPath.DirectorySeparatorChar;
{$IFDEF ANDROID}
if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(clientScreenService)) then
begin
clientScreenScale := clientScreenService.GetScreenScale;
clientScreenScaleSrc := clientScreenScale;
end;
clientScreenSize.CX := Round(clientScreenService.GetScreenSize.X*clientScreenScale);
clientScreenSize.CY := Round(clientScreenService.GetScreenSize.Y*clientScreenScale);
{$IFDEF LIMITRES}
If (clientScreenSize.CX >= 1280) or (clientScreenSize.CY >= 1280) then
begin
clientScreenScale := clientScreenScale / 2;
clientScreenSize.CX := Round(clientScreenService.GetScreenSize.X*clientScreenScale);
clientScreenSize.CY := Round(clientScreenService.GetScreenSize.Y*clientScreenScale);
end;
{$ENDIF}
MainForm.ClientWidth := clientScreenSize.CX;
MainForm.ClientHeight := clientScreenSize.CY;
{$ELSE}
{$IFDEF TESTSCALE}
clientScreenScale := 2;
clientScreenScaleSrc := 4;
{$ENDIF}
{$IFDEF FAKEMOBILE}
{$IFDEF REZ_360x780} ClientWidth := 360; ClientHeight := 780;{$ENDIF}
{$IFDEF REZ_360x740} ClientWidth := 360; ClientHeight := 740;{$ENDIF}
{$IFDEF REZ_360x640} ClientWidth := 360; ClientHeight := 640;{$ENDIF}
{$IFDEF REZ_480x800} ClientWidth := 480; ClientHeight := 800;{$ENDIF}
{$IFDEF REZ_540x960} ClientWidth := 540; ClientHeight := 960;{$ENDIF}
{$IFDEF REZ_576x1024} ClientWidth := 576; ClientHeight := 1024;{$ENDIF}
{$IFDEF REZ_720x1280} ClientWidth := 720; ClientHeight := 1280;{$ENDIF}
{$IFDEF REZ_1080x1920}ClientWidth := 1080; ClientHeight := 1920;{$ENDIF}
{$IFDEF REZ_1440x2560}ClientWidth := 1440; ClientHeight := 2560;{$ENDIF}
{$IFDEF REZ_2160x3840}ClientWidth := 2160; ClientHeight := 3840;{$ENDIF}
//ClientWidth := 240; ClientHeight := 400;
//ClientWidth := 484; ClientHeight := 800; // test partial pixel magnification rendering
{$ELSE}
//ClientWidth := 1056; ClientHeight := 594;
ClientWidth := Screen.DisplayFromForm(Self).BoundsRect.Width;
ClientHeight := Screen.DisplayFromForm(Self).BoundsRect.Height;
//ClientWidth := 1600; ClientHeight := 900;
//ClientWidth := 1200; ClientHeight := 980;
{$ENDIF}
clientScreenSize.CX := Trunc(ClientWidth*clientScreenScale);
clientScreenSize.CY := Trunc(ClientHeight*clientScreenScale);
{$IFDEF DISPLAYON4K}
SetBounds(4600,1240,ClientWidth,ClientHeight);
{$ELSE}
SetBounds((Screen.Width-ClientWidth) div 2,(Screen.Height-ClientHeight) div 2,ClientWidth,ClientHeight);
{$ENDIF}
{$ENDIF}
end;
procedure TMainForm.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
case Key of
vkHardwareBack,vkEscape :
begin
Key := 0;
Close;
end;
end;
end;
procedure TMainForm.FormResize(Sender: TObject);
const
keyboardHeight : Single = 0.75;
keyColumns : Integer = 4;
keyRows : Integer = 5;
keyTextHeight : Single = 0.40;
var
sKey : String;
I : Integer;
fullKeyboardHeight : Integer;
fullKeyboardWidth : Integer;
fullKeyboardYOfs : Integer;
tmpBM : TBitmap;
aRect : TRectF;
bShadeL1Grad1 : TAlphaColor;
bShadeL1Grad2 : TAlphaColor;
bShadeL2Grad1 : TAlphaColor;
bShadeL2Grad2 : TAlphaColor;
CBase : Integer;
C1Width : Integer;
C2Width : Integer;
C3Width : Integer;
C4Width : Integer;
R1Height : Integer;
R2Height : Integer;
R3Height : Integer;
R4Height : Integer;
R5Height : Integer;
RBase : Integer;
keyWidth : Integer;
keyHeight : Integer;
keyXOfs : Integer;
keyYOfs : Integer;
begin
If (clientWidth = 0) or (clientHeight = 0) then
Begin
{$IFDEF TRACEDEBUG}AddDebugEntry('FormResize exit on zero width or height');{$ENDIF}
Exit;
End;
If (lastResizeWidth = clientWidth) and (lastResizeHeight = clientHeight) then
Begin
{$IFDEF TRACEDEBUG}AddDebugEntry('FormResize exit on same resolution');{$ENDIF}
Exit;
End;
{$IFDEF TRACEDEBUG}AddDebugEntry('FormResize (before)');{$ENDIF}
lastResizeWidth := Trunc(clientWidth);
lastResizeHeight := Trunc(clientHeight);
// Make sure there's no extra spaces
fullKeyboardHeight := Trunc(clientHeight*keyboardHeight);
RBase := Trunc(fullKeyboardHeight/5);
R1Height := RBase;
R2Height := RBase;
R3Height := RBase;
R4Height := RBase;
R5Height := RBase;
While R1Height+R2Height+R3Height+R4Height+R5Height < fullKeyboardHeight do
Begin
If R1Height = RBase then Inc(R1Height) else
If R2Height = RBase then Inc(R2Height) else
If R3Height = RBase then Inc(R3Height) else
If R4Height = RBase then Inc(R4Height) else
If R5Height = RBase then Inc(R5Height) else Break;
End;
fullKeyboardWidth := Trunc(clientWidth);
CBase := Trunc(fullKeyboardWidth/4);
C1Width := CBase;
C2Width := CBase;
C3Width := CBase;
C4Width := CBase;
While C1Width+C2Width+C3Width+C4Width < fullKeyboardWidth do
Begin
If C1Width = CBase then Inc(C1Width) else
If C2Width = CBase then Inc(C2Width) else
If C3Width = CBase then Inc(C3Width) else
If C4Width = CBase then Inc(C4Width) else Break;
End;
fullKeyboardYOfs := Trunc(clientHeight-(R1Height+R2Height+R3Height+R4Height+R5Height));
tmpBM := TBitmap.Create;
For I := 0 to calcKeyCount-1 do
Begin
Case calcKeyMap[I].keyRow of
0 : Begin keyHeight := R1Height; keyYOfs := 0; End;
1 : Begin keyHeight := R2Height; keyYOfs := R1Height; End;
2 : Begin keyHeight := R3Height; keyYOfs := R1Height+R2Height; End;
3 : Begin keyHeight := R4Height; keyYOfs := R1Height+R2Height+R3Height; End;
else Begin keyHeight := R5Height; keyYOfs := R1Height+R2Height+R3Height+R4Height; End;
End;
Case calcKeyMap[I].keyColumn of
0 : Begin keyWidth := C1Width; keyXOfs := 0; End;
1 : Begin keyWidth := C2Width; keyXOfs := C1Width; End;
2 : Begin keyWidth := C3Width; keyXOfs := C1Width+C2Width; End;
else Begin keyWidth := C4Width; keyXOfs := C1Width+C2Width+C3Width; End;
End;
calcKeyImages[I] := TImage.Create(MainForm);
calcKeyImages[I].Parent := MainForm;
calcKeyImages[I].Tag := calcKeyMap[I].keyFunction;
calcKeyImages[I].OnMouseDown := FunctionMouseDown;
calcKeyImages[I].OnMouseUp := FunctionMouseUp;
calcKeyImages[I].SetBounds(
KeyXOfs,
fullKeyboardYOfs+KeyYOfs,
keyWidth,
keyHeight);
calcKeyImages[I].Bitmap.SetSize(Trunc(calcKeyImages[I].Width*clientScreenScaleSrc),Trunc(calcKeyImages[I].Height*clientScreenScaleSrc));
Case calcKeyMap[I].keyShade of
KeyShade1 :
Begin
bShadeL1Grad1 := calcKeyS1L1Grad1;
bShadeL1Grad2 := calcKeyS1L1Grad2;
bShadeL2Grad1 := calcKeyS1L2Grad1;
bShadeL2Grad2 := calcKeyS1L2Grad2;
End;
KeyShade2 :
Begin
bShadeL1Grad1 := calcKeyS2L1Grad1;
bShadeL1Grad2 := calcKeyS2L1Grad2;
bShadeL2Grad1 := calcKeyS2L2Grad1;
bShadeL2Grad2 := calcKeyS2L2Grad2;
End;
else
Begin
bShadeL1Grad1 := calcKeyS3L1Grad1;
bShadeL1Grad2 := calcKeyS3L1Grad2;
bShadeL2Grad1 := calcKeyS3L2Grad1;
bShadeL2Grad2 := calcKeyS3L2Grad2;
End;
End;
GradientRectH(calcKeyImages[I].Bitmap,0,0,calcKeyImages[I].Bitmap.Width,calcKeyImages[I].Bitmap.Height,bShadeL1Grad1,bShadeL1Grad2);
tmpBM.SetSize(calcKeyImages[I].Bitmap.Width,calcKeyImages[I].Bitmap.Height);
GradientRectV(tmpBM,0,0,tmpBM.Width,tmpBM.Height,bShadeL2Grad1,bShadeL2Grad2);
{f calcKeyMap[I].keyShade = KeyShade3 then
Begin
calcKeyImages[I].Bitmap.SaveToFile('d:\test1.png');
tmpBM.SaveToFile('d:\test2.png');
End;}
MergeBitmap(tmpBM,calcKeyImages[I].Bitmap,0,0);
aRect := TRectF.Create(0,0,calcKeyImages[I].Width,calcKeyImages[I].Height);
If calcKeyMap[I].keyFunction = funcDot then
sKey := FormatSettings.DecimalSeparator else
sKey := calcKeyMap[I].KeyText;
calcKeyImages[I].Bitmap.Canvas.BeginScene;
calcKeyImages[I].Bitmap.Canvas.Fill.Color := $FF000000;
calcKeyImages[I].Bitmap.Canvas.Stroke.Kind := TBrushKind.Solid;
calcKeyImages[I].Bitmap.Canvas.Font.Size := Trunc(calcKeyImages[I].Height*keyTextHeight);
//calcKeyImages[I].Bitmap.Canvas.Font.Family := 'Tahoma';
calcKeyImages[I].Bitmap.Canvas.FillText(
aRect,
sKey,
False,
0.6,
[],
TTextAlign.Center,
TTextAlign.Center);
calcKeyImages[I].Bitmap.Canvas.EndScene;
End;
tmpBM.Free;
mathLineBG.SetBounds(0,0,clientWidth,fullKeyboardYOfs);
mathLineLabel.SetBounds(0,0,clientWidth,mathLineBG.Height);
end;
procedure TMainForm.FunctionMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
clientFunctionPressed := TImage(Sender).Tag;
end;
procedure TMainForm.FunctionMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
I : Integer;
begin
If clientResultShown = True then
Begin
Case TImage(Sender).Tag of
func0..func9 :
Begin
clientResultShown := False;
mathLineLabel.Text := '';
clientDotEnabled := False;
End;
else clientResultShown := False;
End;
End;
If clientFunctionPressed = TImage(Sender).Tag then
Case TImage(Sender).Tag of
funcClear :
Begin
mathLineLabel.Text := '';
clientDotEnabled := False;
End;
funcPlus :
begin
If mathLineLabel.Text <> '' then If mathLineLabel.Text[High(mathLineLabel.Text)].IsInArray([divisionSymbol,multiplySymbol,'+','-']) = False then
Begin
If mathLineLabel.Text[High(mathLineLabel.Text)] = FormatSettings.DecimalSeparator then mathLineLabel.Text := mathLineLabel.Text+'0';
mathLineLabel.Text := mathLineLabel.Text+'+';
clientDotEnabled := False;
End;
end;
funcMinus :
Begin
If mathLineLabel.Text <> '' then If mathLineLabel.Text[High(mathLineLabel.Text)].IsInArray([divisionSymbol,multiplySymbol,'+','-']) = False then
Begin
If mathLineLabel.Text[High(mathLineLabel.Text)] = FormatSettings.DecimalSeparator then mathLineLabel.Text := mathLineLabel.Text+'0';
mathLineLabel.Text := mathLineLabel.Text+'-';
clientDotEnabled := False;
End;
End;
funcDivide :
Begin
If mathLineLabel.Text <> '' then If mathLineLabel.Text[High(mathLineLabel.Text)].IsInArray([divisionSymbol,multiplySymbol,'+','-']) = False then
Begin
If mathLineLabel.Text[High(mathLineLabel.Text)] = FormatSettings.DecimalSeparator then mathLineLabel.Text := mathLineLabel.Text+'0';
mathLineLabel.Text := mathLineLabel.Text+divisionSymbol;
clientDotEnabled := False;
End;
End;
funcMultiply :
Begin
If mathLineLabel.Text <> '' then If mathLineLabel.Text[High(mathLineLabel.Text)].IsInArray([divisionSymbol,multiplySymbol,'+','-']) = False then
Begin
If mathLineLabel.Text[High(mathLineLabel.Text)] = FormatSettings.DecimalSeparator then mathLineLabel.Text := mathLineLabel.Text+'0';
mathLineLabel.Text := mathLineLabel.Text+multiplySymbol;
clientDotEnabled := False;
End;
End;
funcBackSpace :
Begin
If mathLineLabel.Text <> '' then
Begin
If mathLineLabel.Text[High(mathLineLabel.Text)] = FormatSettings.DecimalSeparator then clientDotEnabled := False
else
Case mathLineLabel.Text[High(mathLineLabel.Text)] of
'+',
'-',
divisionSymbol,
multiplySymbol :
Begin
For I := High(mathLineLabel.Text)-1 downto Low(mathLineLabel.Text) do
Begin
If mathLineLabel.Text[I] = FormatSettings.DecimalSeparator then
Begin
clientDotEnabled := True;
Break;
End
else
Case mathLineLabel.Text[I] of
'+',
'-',
divisionSymbol,
multiplySymbol :
Begin
clientDotEnabled := False;
Break;
End;
End;
End;
End;
End;
mathLineLabel.Text := Copy(mathLineLabel.Text,Low(mathLineLabel.Text),High(mathLineLabel.Text)-Low(mathLineLabel.Text));
End;
End;
func0 : mathLineLabel.Text := mathLineLabel.Text+'0';
func1 : mathLineLabel.Text := mathLineLabel.Text+'1';
func2 : mathLineLabel.Text := mathLineLabel.Text+'2';
func3 : mathLineLabel.Text := mathLineLabel.Text+'3';
func4 : mathLineLabel.Text := mathLineLabel.Text+'4';
func5 : mathLineLabel.Text := mathLineLabel.Text+'5';
func6 : mathLineLabel.Text := mathLineLabel.Text+'6';
func7 : mathLineLabel.Text := mathLineLabel.Text+'7';
func8 : mathLineLabel.Text := mathLineLabel.Text+'8';
func9 : mathLineLabel.Text := mathLineLabel.Text+'9';
funcDot :
Begin
If mathLineLabel.Text = '' then
Begin
mathLineLabel.Text := '0'+FormatSettings.DecimalSeparator;
clientDotEnabled := True;
End
else
If (clientDotEnabled = False) and (mathLineLabel.Text[High(mathLineLabel.Text)].IsInArray([divisionSymbol,multiplySymbol,'+','-',FormatSettings.DecimalSeparator]) = False) then
Begin
mathLineLabel.Text := mathLineLabel.Text+FormatSettings.DecimalSeparator;
clientDotEnabled := True;
End;
End;
funcMemory :
Begin
If (Pos(multiplySymbol,mathLineLabel.Text) > 0) or (Pos(divisionSymbol,mathLineLabel.Text) > 0) or
(Pos('+',mathLineLabel.Text) > 0) or (Pos('-',mathLineLabel.Text) > 0) then OutputMathResult;
clientMemoryValue := mathLineLabel.Text;
End;
funcRecall :
If clientMemoryValue <> '' then
Begin
If mathLineLabel.Text <> '' then
Begin
//If CharInSet(mathLineLabel.Text[High(mathLineLabel.Text)],[divisionSymbol,multiplySymbol,'+','-']) = True then
If mathLineLabel.Text[High(mathLineLabel.Text)].IsInArray([divisionSymbol,multiplySymbol,'+','-']) = True then
Begin
mathLineLabel.Text := mathLineLabel.Text+clientMemoryValue;
If Pos(FormatSettings.DecimalSeparator,clientMemoryValue) > 0 then clientDotEnabled := True;
End;
End
Else mathLineLabel.Text := clientMemoryValue;
End;
funcEqual : OutputMathResult;
End;
AdjustResultFontSize;
end;
function PerformMathAction(wString : String; var wValue : Extended; wState : Integer{; var firstValue : Boolean}) : Integer;
var
nValue : Extended;
begin
Result := S_OK;
Try
nValue := StrToFloat(wString);
Case wState of
stAdd : wValue := wValue+nValue;
stSub : wValue := wValue-nValue;
stMul : wValue := wValue*nValue;
stDiv : If nValue > 0 then wValue := wValue/nValue else Result := S_FALSE;
End;
Finally
End;
end;
function ParseMathForumla(S : String) : String;
var
I : Integer;
cValue : Extended;
cString : String;
cState : Integer;
ErrCode : Integer;
begin
ErrCode := S_OK;
cString := '';
cValue := 0;
cState := stAdd;
For I := Low(S) to High(S) do
Begin
If S[I].IsInArray([divisionSymbol,multiplySymbol,'-','+']) = False then
Begin
cString := cString+S[I];
End
else
Begin
If Length(cString) > 0 then
Begin
ErrCode := PerformMathAction(cString,cValue,cState);
cString := '';
End;
Case S[I] of
divisionSymbol : cState := stDiv;
multiplySymbol : cState := stMul;
'-' : cState := stSub;
'+' : cState := stAdd;
End;
End;
End;
If Length(cString) > 0 then ErrCode := PerformMathAction(cString,cValue,cState);
If ErrCode = S_OK then Result := FloatToStr(cValue) else Result := S;
end;
procedure TMainForm.OutputMathResult;
begin
mathLineLabel.Text := ParseMathForumla(mathLineLabel.Text);
If Pos(FormatSettings.DecimalSeparator,mathLineLabel.Text) > 0 then clientDotEnabled := True;
clientResultShown := True;
end;
{$IFDEF ANDROID}
function TMainForm.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
Result := False;
case AAppEvent of
TApplicationEvent.FinishedLaunching:
Begin
End;
TApplicationEvent.BecameActive:
Begin
Result := True;
End;
TApplicationEvent.WillBecomeInactive:
Begin
End;
TApplicationEvent.EnteredBackground:
Begin
Result := True;
End;
TApplicationEvent.WillBecomeForeground:
Begin
Result := True;
End;
TApplicationEvent.WillTerminate:
Begin
End;
TApplicationEvent.LowMemory:
Begin
End;
end;
end;
{$ENDIF}
procedure TMainForm.AdjustResultFontSize;
const
maxTextHeight : Single = 0.5;
maxTextWidth : Single = 0.9;
var
S : String;
begin
S := mathLineLabel.Text;
mathLineLabel.Font.Size := Trunc(mathLineLabel.Height)*maxTextHeight;
mathLineLabel.Canvas.Font.Assign(mathLineLabel.Font);
While (mathLineLabel.Canvas.TextWidth(S) > mathLineLabel.Width*maxTextWidth) or (mathLineLabel.Canvas.TextHeight(S) > mathLineLabel.Height*maxTextHeight) do
Begin
mathLineLabel.Font.Size := mathLineLabel.Font.Size-1;
mathLineLabel.Canvas.Font.Size := mathLineLabel.Font.Size;
End;
end;
end.