| 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, |
| 22 |
CastleControls, CastleUIControls; |
| 23 |
|
| 24 |
type |
| 25 |
{ Loading view. |
| 26 |
This is an example how to show a loading progress using TViewLoading. |
| 27 |
|
| 28 |
As an example, it assumes that your "loading" consists of |
| 29 |
- one call to DoLoadSomething1 |
| 30 |
- one call to DoLoadSomething2 |
| 31 |
- 17 calls to DoLoadSomethingSmall |
| 32 |
} |
| 33 |
TViewLoading = class(TCastleView) |
| 34 |
published |
| 35 |
{ Components designed using CGE editor. |
| 36 |
These fields will be automatically initialized at Start. } |
| 37 |
LabelPercent: TCastleLabel; |
| 38 |
strict private |
| 39 |
const |
| 40 |
FakeLoadingAdditionalStepsCount = 17; |
| 41 |
var |
| 42 |
{ Variable that simulates loading progress, |
| 43 |
we will grow it from 0 to FakeLoadingAdditionalStepsCount during loading. } |
| 44 |
FakeLoadingAdditionalSteps: Cardinal; |
| 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 |
end; |
| 54 |
|
| 55 |
var |
| 56 |
ViewLoading: TViewLoading; |
| 57 |
|
| 58 |
implementation |
| 59 |
|
| 60 |
uses SysUtils, |
| 61 |
CastleColors, CastleWindow, CastleFilesUtils, CastleApplicationProperties, |
| 62 |
CastleUtils, CastleComponentSerialize, |
| 63 |
GameViewPlay; |
| 64 |
|
| 65 |
{ TViewLoading ------------------------------------------------------------- } |
| 66 |
|
| 67 |
constructor TViewLoading.Create(AOwner: TComponent); |
| 68 |
begin |
| 69 |
inherited; |
| 70 |
DesignUrl := 'castle-data:/gameviewloading.castle-user-interface'; |
| 71 |
end; |
| 72 |
|
| 73 |
procedure TViewLoading.Start; |
| 74 |
begin |
| 75 |
inherited; |
| 76 |
|
| 77 |
FakeLoadingAdditionalSteps := 0; |
| 78 |
UpdateProgress(0); |
| 79 |
WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadSomething1); |
| 80 |
end; |
| 81 |
|
| 82 |
procedure TViewLoading.UpdateProgress(const Progress: Single); |
| 83 |
begin |
| 84 |
LabelPercent.Caption := IntToStr(Round(100 * Progress)) + '%'; |
| 85 |
end; |
| 86 |
|
| 87 |
procedure TViewLoading.DoLoadSomething1(Sender: TObject); |
| 88 |
begin |
| 89 |
{ Fake loading something big, one time. Just do something time-consuming there. } |
| 90 |
Sleep(100); |
| 91 |
|
| 92 |
UpdateProgress(0.25); |
| 93 |
WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadSomething2); |
| 94 |
end; |
| 95 |
|
| 96 |
procedure TViewLoading.DoLoadSomething2(Sender: TObject); |
| 97 |
begin |
| 98 |
{ Fake loading something big, one time. Just do something time-consuming there. } |
| 99 |
Sleep(100); |
| 100 |
|
| 101 |
UpdateProgress(0.5); |
| 102 |
WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadSomethingSmall); |
| 103 |
end; |
| 104 |
|
| 105 |
procedure TViewLoading.DoLoadSomethingSmall(Sender: TObject); |
| 106 |
begin |
| 107 |
{ Fake loading something small, 17 times (FakeLoadingAdditionalStepsCount). |
| 108 |
Just do something time-consuming there. } |
| 109 |
Sleep(5); |
| 110 |
|
| 111 |
Inc(FakeLoadingAdditionalSteps); |
| 112 |
UpdateProgress(0.5 + 0.5 * FakeLoadingAdditionalSteps / FakeLoadingAdditionalStepsCount); |
| 113 |
if FakeLoadingAdditionalSteps = FakeLoadingAdditionalStepsCount then |
| 114 |
{ Finished loading. Using WaitForRenderAndCall(@DoFinish) |
| 115 |
means that user can see the value "100%", otherwise it would never get drawn, |
| 116 |
and the last loading frame would always show "97%". } |
| 117 |
WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadingFinish) |
| 118 |
else |
| 119 |
WaitForRenderAndCall({$ifdef FPC}@{$endif}DoLoadSomethingSmall); // call this again, to load next step |
| 120 |
end; |
| 121 |
|
| 122 |
procedure TViewLoading.DoLoadingFinish(Sender: TObject); |
| 123 |
begin |
| 124 |
{ Finished loading, go to ViewPlay } |
| 125 |
Container.View := ViewPlay; |
| 126 |
end; |
| 127 |
|
| 128 |
end. |