| 1 |
{ |
| 2 |
Copyright 2020-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 |
{ Main view, where most of the application logic takes place. } |
| 17 |
unit GameViewMain; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleScene, CastleControls, |
| 23 |
CastleKeysMouse, CastleColors, CastleViewport, CastleUIControls, |
| 24 |
CastleDownload; |
| 25 |
|
| 26 |
type |
| 27 |
{ Main view, where most of the application logic takes place. } |
| 28 |
TViewMain = class(TCastleView) |
| 29 |
published |
| 30 |
{ Components designed using CGE editor. |
| 31 |
These fields will be automatically initialized at Start. } |
| 32 |
ButtonStartDownloads, ButtonAbortDownloads: TCastleButton; |
| 33 |
LabelStatus: TCastleLabel; |
| 34 |
strict private |
| 35 |
const |
| 36 |
DownloadsCount = 3; |
| 37 |
var |
| 38 |
LabelDownload: array [1..DownloadsCount] of TCastleLabel; |
| 39 |
ProgressDownload: array [1..DownloadsCount] of TCastleRectangleControl; |
| 40 |
Download: array [1..DownloadsCount] of TCastleDownload; |
| 41 |
procedure ClickStartDownloads(Sender: TObject); |
| 42 |
procedure ClickAbortDownloads(Sender: TObject); |
| 43 |
procedure DownloadFinish(const Sender: TCastleDownload; var FreeSender: Boolean); |
| 44 |
procedure UpdateDownloadState; |
| 45 |
public |
| 46 |
constructor Create(AOwner: TComponent); override; |
| 47 |
procedure Start; override; |
| 48 |
procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override; |
| 49 |
end; |
| 50 |
|
| 51 |
var |
| 52 |
ViewMain: TViewMain; |
| 53 |
|
| 54 |
implementation |
| 55 |
|
| 56 |
uses SysUtils, Math, |
| 57 |
{$ifdef FPC} OpenSSLSockets, {$endif} // https support |
| 58 |
CastleComponentSerialize, CastleUtils, CastleStringUtils, CastleLog, |
| 59 |
CastleUriUtils; |
| 60 |
|
| 61 |
{ TViewMain ----------------------------------------------------------------- } |
| 62 |
|
| 63 |
constructor TViewMain.Create(AOwner: TComponent); |
| 64 |
begin |
| 65 |
inherited; |
| 66 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 67 |
end; |
| 68 |
|
| 69 |
procedure TViewMain.Start; |
| 70 |
begin |
| 71 |
inherited; |
| 72 |
|
| 73 |
{ Find components, by name, that we need to access from code } |
| 74 |
LabelDownload[1] := DesignedComponent('LabelDownload1') as TCastleLabel; |
| 75 |
LabelDownload[2] := DesignedComponent('LabelDownload2') as TCastleLabel; |
| 76 |
LabelDownload[3] := DesignedComponent('LabelDownload3') as TCastleLabel; |
| 77 |
ProgressDownload[1] := DesignedComponent('ProgressDownload1') as TCastleRectangleControl; |
| 78 |
ProgressDownload[2] := DesignedComponent('ProgressDownload2') as TCastleRectangleControl; |
| 79 |
ProgressDownload[3] := DesignedComponent('ProgressDownload3') as TCastleRectangleControl; |
| 80 |
|
| 81 |
ButtonStartDownloads.OnClick := {$ifdef FPC}@{$endif} ClickStartDownloads; |
| 82 |
ButtonAbortDownloads.OnClick := {$ifdef FPC}@{$endif} ClickAbortDownloads; |
| 83 |
|
| 84 |
UpdateDownloadState; |
| 85 |
end; |
| 86 |
|
| 87 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: boolean); |
| 88 |
begin |
| 89 |
inherited; |
| 90 |
LabelStatus.Caption := 'FPS: ' + Container.Fps.ToString; |
| 91 |
UpdateDownloadState; |
| 92 |
end; |
| 93 |
|
| 94 |
procedure TViewMain.ClickStartDownloads(Sender: TObject); |
| 95 |
const |
| 96 |
Urls: array [1..DownloadsCount] of String = ( |
| 97 |
'https://castle-engine.io/latest.zip', |
| 98 |
'https://castle-engine.io/modern_pascal_introduction.html', |
| 99 |
'https://en.wikipedia.org/wiki/Main_Page' |
| 100 |
// 'file:///home/michalis/sources/castle-engine/castle-engine/examples/network/asynchronous_download/data/gears.blend', |
| 101 |
// 'castle-data:/gears.gltf', |
| 102 |
// 'https://deliberately-invalid-server.org/deliberately-invalid-url' |
| 103 |
// 'https://castle-engine.io/deliberately-invalid-url', |
| 104 |
// 'http://example.org/', |
| 105 |
// 'https://github.com/castle-engine/castle-engine/' |
| 106 |
); |
| 107 |
var |
| 108 |
I: Integer; |
| 109 |
begin |
| 110 |
for I := 1 to DownloadsCount do |
| 111 |
begin |
| 112 |
FreeAndNil(Download[I]); |
| 113 |
Download[I] := TCastleDownload.Create(Self); |
| 114 |
Download[I].Url := Urls[I]; |
| 115 |
Download[I].OnFinish := {$ifdef FPC}@{$endif} DownloadFinish; |
| 116 |
|
| 117 |
{ Without soForceMemoryStream, returns as soon as possible with |
| 118 |
any stream class. This may give you e.g.: |
| 119 |
|
| 120 |
- TFileStream for normal files. Which is usually fine for normal reading, |
| 121 |
but note that underlying TFileStream may prevent from keeping |
| 122 |
the same file open multiple times in multiple TFileStream instances. |
| 123 |
|
| 124 |
- If you would use soGzip, then it may give you TGZFileStream |
| 125 |
which is not "seekable", i.e. you cannot freely move within the stream. |
| 126 |
|
| 127 |
Using soForceMemoryStream guarantees you get TMemoryStream which is easy |
| 128 |
to handle, always seekable etc. |
| 129 |
} |
| 130 |
//Download[I].Options := [soForceMemoryStream]; |
| 131 |
|
| 132 |
Download[I].Start; |
| 133 |
end; |
| 134 |
end; |
| 135 |
|
| 136 |
procedure TViewMain.DownloadFinish(const Sender: TCastleDownload; var FreeSender: Boolean); |
| 137 |
var |
| 138 |
HttpResponseHeaders: String; |
| 139 |
begin |
| 140 |
{ Gracefully handle the case when Sender.HttpResponseHeaders = nil, |
| 141 |
which will happen if you try to download non-HTTP/HTTPS URL, |
| 142 |
like 'castle-data:/gears.gltf' . } |
| 143 |
if Sender.HttpResponseHeaders <> nil then |
| 144 |
HttpResponseHeaders := Sender.HttpResponseHeaders.Text |
| 145 |
else |
| 146 |
HttpResponseHeaders := ''; |
| 147 |
|
| 148 |
if Sender.Status = dsError then |
| 149 |
WritelnLog('Downloading "%s" failed: %s.' + NL + |
| 150 |
'HTTP response code: %d' + NL + |
| 151 |
'HTTP response headers: %s' + NL + |
| 152 |
'Final URL: %s', [ |
| 153 |
UriDisplay(Sender.Url), |
| 154 |
Sender.ErrorMessage, |
| 155 |
Sender.HttpResponseCode, |
| 156 |
HttpResponseHeaders, |
| 157 |
UriDisplay(Sender.FinalUrl) |
| 158 |
]) |
| 159 |
else |
| 160 |
WritelnLog('Downloading "%s" successful.' + NL + |
| 161 |
'HTTP response code: %d' + NL + |
| 162 |
'HTTP response headers: %s' + NL + |
| 163 |
'Final URL: %s', [ |
| 164 |
UriDisplay(Sender.Url), |
| 165 |
Sender.HttpResponseCode, |
| 166 |
HttpResponseHeaders, |
| 167 |
UriDisplay(Sender.FinalUrl) |
| 168 |
]); |
| 169 |
end; |
| 170 |
|
| 171 |
procedure TViewMain.ClickAbortDownloads(Sender: TObject); |
| 172 |
var |
| 173 |
I: Integer; |
| 174 |
begin |
| 175 |
for I := 1 to DownloadsCount do |
| 176 |
FreeAndNil(Download[I]); |
| 177 |
end; |
| 178 |
|
| 179 |
procedure TViewMain.UpdateDownloadState; |
| 180 |
const |
| 181 |
StatusToStr: array [TDownloadStatus] of String = ( |
| 182 |
{ The "Not Started" from here will never be visible, as we call TCastleDownload.Start |
| 183 |
right after creating it. } |
| 184 |
'Not Started', |
| 185 |
'Downloading', |
| 186 |
'Error', |
| 187 |
'Success' |
| 188 |
); |
| 189 |
var |
| 190 |
I: Integer; |
| 191 |
begin |
| 192 |
for I := 1 to DownloadsCount do |
| 193 |
begin |
| 194 |
if Download[I] = nil then |
| 195 |
begin |
| 196 |
LabelDownload[I].Caption := 'Not started (or aborted)'; |
| 197 |
ProgressDownload[I].Exists := false; |
| 198 |
end else |
| 199 |
begin |
| 200 |
LabelDownload[I].Text.Clear; |
| 201 |
LabelDownload[I].Text.Add('Downloading: ' + Download[I].Url); |
| 202 |
LabelDownload[I].Text.Add('Status: ' + StatusToStr[Download[I].Status]); |
| 203 |
if Download[I].Status = dsError then |
| 204 |
LabelDownload[I].Text.AddMultiLine('Error message: ' + Download[I].ErrorMessage) |
| 205 |
else |
| 206 |
// ErrorMessage should only be set if Status is dsError |
| 207 |
Assert(Download[I].ErrorMessage = ''); |
| 208 |
LabelDownload[I].Text.Add(Format('Downloaded size: %s / %s (bytes: %d / %d)', [ |
| 209 |
SizeToStr(Download[I].DownloadedBytes), |
| 210 |
SizeToStr(Download[I].TotalBytes), |
| 211 |
Download[I].DownloadedBytes, |
| 212 |
Download[I].TotalBytes |
| 213 |
])); |
| 214 |
LabelDownload[I].Text.Add('MIME type: ' + Download[I].MimeType); |
| 215 |
|
| 216 |
ProgressDownload[I].Exists := true; |
| 217 |
if Download[I].Status in [dsDownloading, dsSuccess] then |
| 218 |
begin |
| 219 |
ProgressDownload[I].Color := HexToColor('399100E6'); |
| 220 |
|
| 221 |
if Download[I].Status = dsSuccess then |
| 222 |
{ Regardless of TotalBytes (which may remain -1 if server never reported them) |
| 223 |
report progress as finished when dsSuccess. } |
| 224 |
ProgressDownload[I].WidthFraction := 1 |
| 225 |
else |
| 226 |
if Download[I].TotalBytes > 0 then |
| 227 |
{ Note that when WidthFraction = 0, then WidthFraction is ignored, |
| 228 |
and the Width property determines size. |
| 229 |
But ProgressDownload[I].Width is always 0 (set in editor). |
| 230 |
So WidthFraction = 0 reliably hides the bar. } |
| 231 |
ProgressDownload[I].WidthFraction := Download[I].DownloadedBytes / Download[I].TotalBytes |
| 232 |
else |
| 233 |
ProgressDownload[I].WidthFraction := 0; |
| 234 |
end else |
| 235 |
begin |
| 236 |
ProgressDownload[I].Color := HexToColor('FF0000E6'); |
| 237 |
ProgressDownload[I].WidthFraction := 1; |
| 238 |
end; |
| 239 |
end; |
| 240 |
end; |
| 241 |
end; |
| 242 |
|
| 243 |
end. |