-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFMXAnimation.DemoShell.pas
More file actions
184 lines (162 loc) · 4.95 KB
/
Copy pathFMXAnimation.DemoShell.pas
File metadata and controls
184 lines (162 loc) · 4.95 KB
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
/// <summary>
/// FMXAnimation.DemoShell
/// Small shared helpers for demo form shells (paint box, HUD strip, chrome).
/// </summary>
///
/// <remarks>
/// Keeps Main.Form units thin: scene logic stays in the demos; this unit only
/// generalizes the repeated FMX/Skia surface and dark toolbar chip styling.
/// Intentionally not a base form — demos keep their own FMX layouts and events.
/// </remarks>
///
/// <copyright>
/// Copyright © 2026 Olaf Monien
/// Licensed under MIT
/// </copyright>
unit FMXAnimation.DemoShell;
interface
uses
System.Types,
System.UITypes,
System.Classes,
System.Math,
FMX.Types,
FMX.Controls,
FMX.StdCtrls,
FMX.Objects,
FMX.Skia,
FMXAnimation.GameLoop,
FMXAnimation.Stats.Hud,
FMXAnimation.SystemInfo;
const
/// <summary>Dark chrome — matches the stats HUD bar.</summary>
cDemoBarBg = $FF0B1220;
cDemoBarLine = $FF1E2A3C;
cDemoSegIdleFill = $FF151C2A;
cDemoSegIdleStroke = $FF2A3A50;
cDemoSegIdleText = $FF9AABC4;
cDemoSegSelFill = $FF2563A8;
cDemoSegSelStroke = $FF3B82C4;
cDemoSegSelText = $FFE8EEF8;
cDemoLabelMuted = $FF8B9BB4;
cDemoHintMuted = $FF5C6B82;
/// <summary>
/// Client-aligned TSkPaintBox under AHost. Caller assigns OnDraw / mouse handlers.
/// Host HitTest is set False so the paint box receives pointer input.
/// </summary>
function CreateClientPaintBox(
AOwner: TComponent;
AHost: TFmxObject): TSkPaintBox;
/// <summary>
/// Scene height above the stats HUD strip for a surface of AWidth × AHeight.
/// </summary>
function GetSceneViewportHeight(const AWidth, AHeight: Single): Single;
/// <summary>
/// Local scene rect (0,0)–(Width, viewport height) for hit-tests and viewport setup.
/// </summary>
function GetSceneDrawRect(const AWidth, AHeight: Single): TRectF;
/// <summary>
/// Splits a paint-box OnDraw destination into the scene area (above the HUD strip).
/// </summary>
function GetSceneDestInPaintBox(const ADest: TRectF): TRectF;
/// <summary>
/// Styles a dark toolbar segment chip (rectangle + centered label).
/// </summary>
procedure StyleSegmentChip(
const ARect: TRectangle;
const ALabel: TLabel;
const ASelected: Boolean);
/// <summary>
/// Drops family/style from styled settings and sets a fixed font color
/// so platform styles do not recolor demo chrome.
/// </summary>
procedure StyleDemoLabel(const ALabel: TLabel; const AColor: TAlphaColor);
/// <summary>
/// Sets GlobalPreferredFramesPerSecond and optionally restarts AGameLoop so
/// Mac/iOS CADisplayLink re-applies the preferred range. No-op when already set.
/// Returns True if the preferred value changed.
/// </summary>
function ApplyPreferredFps(
const AFps: Integer;
AGameLoop: TGameLoop;
const ARestartLoop: Boolean;
const AFormVisible: Boolean): Boolean;
implementation
function CreateClientPaintBox(
AOwner: TComponent;
AHost: TFmxObject): TSkPaintBox;
begin
Result := TSkPaintBox.Create(AOwner);
Result.Parent := AHost;
Result.Align := TAlignLayout.Client;
// HitTest must be True so pointer events hit the scene surface.
Result.HitTest := True;
Result.CanFocus := False;
if AHost is TControl then
begin
TControl(AHost).HitTest := False;
end;
end;
function GetSceneViewportHeight(const AWidth, AHeight: Single): Single;
begin
Result := Max(1, AHeight - GetStatsHudHeight(AWidth));
end;
function GetSceneDrawRect(const AWidth, AHeight: Single): TRectF;
begin
Result := TRectF.Create(0, 0, Max(1, AWidth), GetSceneViewportHeight(AWidth, AHeight));
end;
function GetSceneDestInPaintBox(const ADest: TRectF): TRectF;
begin
Result := ADest;
Result.Bottom := Max(ADest.Top + 1, ADest.Bottom - GetStatsHudHeight(ADest.Width));
end;
procedure StyleSegmentChip(
const ARect: TRectangle;
const ALabel: TLabel;
const ASelected: Boolean);
begin
if ASelected then
begin
ARect.Fill.Color := cDemoSegSelFill;
ARect.Stroke.Color := cDemoSegSelStroke;
ALabel.TextSettings.FontColor := cDemoSegSelText;
end
else
begin
ARect.Fill.Color := cDemoSegIdleFill;
ARect.Stroke.Color := cDemoSegIdleStroke;
ALabel.TextSettings.FontColor := cDemoSegIdleText;
end;
end;
procedure StyleDemoLabel(const ALabel: TLabel; const AColor: TAlphaColor);
begin
ALabel.StyledSettings := [TStyledSetting.Family, TStyledSetting.Style];
ALabel.TextSettings.FontColor := AColor;
end;
function ApplyPreferredFps(
const AFps: Integer;
AGameLoop: TGameLoop;
const ARestartLoop: Boolean;
const AFormVisible: Boolean): Boolean;
var
LWasRunning: Boolean;
begin
Result := GetPreferredFramesPerSecond <> AFps;
if not Result then
begin
Exit;
end;
SetPreferredFramesPerSecond(AFps);
if ARestartLoop and Assigned(AGameLoop) then
begin
// Mac/iOS CADisplayLink applies preferred range on Subscribe — restart loop.
// Windows DWM still needs GameLoop Preferred pacing (see FMXAnimation.GameLoop).
LWasRunning := AGameLoop.Running;
AGameLoop.StopLoop;
if LWasRunning or AFormVisible then
begin
AGameLoop.StartLoop;
end;
end;
end;
end.