Debian Codegraph @

examples/user_interface/zombie_fighter_alternative_short_lived_views/code/gameviewloading.pas

raw
1 {
2 Copyright 2016-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { View with sample loading. }
17 unit GameViewLoading;
18
19 interface
20
21 uses Classes, CastleControls, CastleUIControls;
22
23 type
24 { Loading view.
25 This is an example how to show a loading progress using TViewLoading.
26
27 As an example, it assumes that your "loading" consists of
28 - one call to DoLoadSomething1
29 - one call to DoLoadSomething2
30 - 17 calls to DoLoadSomethingSmall
31 }
32 TViewLoading = class(TCastleView)
33 published
34 { Components designed using CGE editor.
35 These fields will be automatically initialized at Start. }
36 LabelPercent: TCastleLabel;
37 strict private
38 const
39 FakeLoadingAdditionalStepsCount = 17;
40 var
41 { Variable that simulates loading progress,
42 we will grow it from 0 to FakeLoadingAdditionalStepsCount during loading. }
43 FakeLoadingAdditionalSteps: Cardinal;
44 LoadingFinished: Boolean;
45 procedure UpdateProgress(const Progress: Single);
46 procedure DoLoadSomething1(Sender: TObject);
47 procedure DoLoadSomething2(Sender: TObject);
48 procedure DoLoadSomethingSmall(Sender: TObject);
49 procedure DoLoadingFinish(Sender: TObject);
50 public
51 constructor Create(AOwner: TComponent); override;
52 procedure Start; override;
53 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
54 end;
55
56 implementation
57
58 uses SysUtils,
59 CastleColors, CastleWindow, CastleFilesUtils, CastleApplicationProperties,
60 CastleUtils, CastleComponentSerialize,
61 GameViewPlay;
62
63 { TViewLoading ------------------------------------------------------------- }
64
65 constructor TViewLoading.Create(AOwner: TComponent);
66 begin
67 inherited;
68 DesignUrl := 'castle-data:/gameviewloading.castle-user-interface';
69 end;
70
71 procedure TViewLoading.Start;
72 begin
73 inherited;
74
75 LoadingFinished := false;
76 FakeLoadingAdditionalSteps := 0;
77 UpdateProgress(0);
78 WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadSomething1);
79 end;
80
81 procedure TViewLoading.UpdateProgress(const Progress: Single);
82 begin
83 LabelPercent.Caption := IntToStr(Round(100 * Progress)) + '%';
84 end;
85
86 procedure TViewLoading.DoLoadSomething1(Sender: TObject);
87 begin
88 { Fake loading something big, one time. Just do something time-consuming there. }
89 Sleep(100);
90
91 UpdateProgress(0.25);
92 WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadSomething2);
93 end;
94
95 procedure TViewLoading.DoLoadSomething2(Sender: TObject);
96 begin
97 { Fake loading something big, one time. Just do something time-consuming there. }
98 Sleep(100);
99
100 UpdateProgress(0.5);
101 WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadSomethingSmall);
102 end;
103
104 procedure TViewLoading.DoLoadSomethingSmall(Sender: TObject);
105 begin
106 { Fake loading something small, 17 times (FakeLoadingAdditionalStepsCount).
107 Just do something time-consuming there. }
108 Sleep(5);
109
110 Inc(FakeLoadingAdditionalSteps);
111 UpdateProgress(0.5 + 0.5 * FakeLoadingAdditionalSteps / FakeLoadingAdditionalStepsCount);
112 if FakeLoadingAdditionalSteps = FakeLoadingAdditionalStepsCount then
113 { Finished loading. Using WaitForRenderAndCall(@DoFinish)
114 means that user can see the value "100%", otherwise it would never get drawn,
115 and the last loading frame would always show "97%". }
116 WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadingFinish)
117 else
118 WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadSomethingSmall); // call this again, to load next step
119 end;
120
121 procedure TViewLoading.DoLoadingFinish(Sender: TObject);
122 begin
123 { Finished loading, go to ViewPlay.
124
125 In case of using CreateUntilStopped, you cannot change view
126 inside a WaitForRenderAndCall callback.
127 So we need additional flag LoadingFinished to mark this view,
128 that is handled in Update.
129 This is more straightforward in version without CreateUntilStopped,
130 in examples/zombie_fighter/ . }
131 LoadingFinished := true;
132 end;
133
134 procedure TViewLoading.Update(const SecondsPassed: Single; var HandleInput: Boolean);
135 begin
136 inherited;
137 if LoadingFinished then
138 Container.View := TViewPlay.CreateUntilStopped;
139 end;
140
141 end.