| 1 |
{ |
| 2 |
Copyright 2018-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 |
{ Quick file viewer for various file types supported by CGE. } |
| 17 |
unit FrameViewFile; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Classes, SysUtils, Forms, Controls, |
| 23 |
LCLVersion, |
| 24 |
CastleControl, CastleControls, CastleViewport, CastleScene, |
| 25 |
CastleUIControls, CastleSoundEngine; |
| 26 |
|
| 27 |
{$if defined(LCLGTK3)} |
| 28 |
{$error Using LCL GTK3 widgetset to build CGE editor is unsupported. The LCL GTK3 widgetset is too unstable (lots of issues on FormChooseProject and FormProject), TOpenGLControl crashes, and TOpenGLControl doesn't support OpenGL context sharing.} |
| 29 |
{$endif} |
| 30 |
|
| 31 |
{ CGE editor critically needs this MR for Qt5: |
| 32 |
https://gitlab.com/freepascal.org/lazarus/lazarus/-/merge_requests/95 |
| 33 |
It is in main, and released with 2.2.4 and 2.2.6 (and, we assume, all future): |
| 34 |
https://gitlab.com/freepascal.org/lazarus/lazarus/-/blob/lazarus_2_2_4/components/opengl/glqtcontext.pas?ref_type=tags |
| 35 |
Without this MR, the editor crashes if you open the same model in preview |
| 36 |
and main design -- as CGE code assumes all OpenGL contexts share data like VBO. } |
| 37 |
{$if defined(LCLQt) or defined(LCLQt5)} |
| 38 |
{$if LCL_FULLVERSION < 2020400} |
| 39 |
{$error Using LCL Qt5 widgetset to build CGE editor requires Lazarus (LCL) version >= 2.2.4.} |
| 40 |
{$endif} |
| 41 |
{$endif} |
| 42 |
|
| 43 |
{$if defined(LCLCocoa)} |
| 44 |
{$if LCL_FULLVERSION < 3050000} |
| 45 |
// To have https://gitlab.com/freepascal.org/lazarus/lazarus/-/merge_requests/291 fixed |
| 46 |
{$error Using LCL Cocoa widgetset to build CGE editor requires Lazarus (LCL) version >= 3.5.} |
| 47 |
{$endif} |
| 48 |
{$endif} |
| 49 |
|
| 50 |
type |
| 51 |
TViewFileFrame = class(TFrame) |
| 52 |
Control: TCastleControl; |
| 53 |
private |
| 54 |
FURL, FSuccessMessage, FErrorMessage: String; |
| 55 |
LabelURL, LabelInformation: TCastleLabel; |
| 56 |
PreviewLayer: TCastleUserInterface; |
| 57 |
{$warnings off} // using TCastleAutoNavigationViewport that should be internal |
| 58 |
Viewport: TCastleAutoNavigationViewport; |
| 59 |
{$warnings on} |
| 60 |
Scene: TCastleScene; |
| 61 |
Image: TCastleImageControl; |
| 62 |
Sound: TCastleSound; |
| 63 |
PlayingSound: TCastlePlayingSound; |
| 64 |
SoundButton: TCastleButton; |
| 65 |
LabelVolume: TCastleLabel; |
| 66 |
procedure ClickSoundButton(Sender: TObject); |
| 67 |
procedure SoundStop(Sender: TObject); |
| 68 |
procedure FinishLoading(const AURL: String); |
| 69 |
procedure UpdateLabelVolume(const Sender: TCastleUserInterface; |
| 70 |
const SecondsPassed: Single; var HandleInput: Boolean); |
| 71 |
protected |
| 72 |
procedure Loaded; override; |
| 73 |
public |
| 74 |
constructor Create(AOwner: TComponent); override; |
| 75 |
destructor Destroy; override; |
| 76 |
|
| 77 |
{ Load preview of various file types. } |
| 78 |
procedure LoadScene(const AURL: String); |
| 79 |
procedure LoadImage(const AURL: String); |
| 80 |
procedure LoadSound(const AURL: String); |
| 81 |
procedure ClearLoaded; |
| 82 |
end; |
| 83 |
|
| 84 |
implementation |
| 85 |
|
| 86 |
uses CastleColors, CastleUtils, CastleSoundBase, CastleVectors, CastleCameras, |
| 87 |
CastleTransform, CastleUriUtils, |
| 88 |
EditorUtils; |
| 89 |
|
| 90 |
{$R *.lfm} |
| 91 |
|
| 92 |
{ TViewFileFrame ----------------------------------------------------------- } |
| 93 |
|
| 94 |
procedure TViewFileFrame.ClearLoaded; |
| 95 |
var |
| 96 |
OldInternalCastleDesignInvalidate: Boolean; |
| 97 |
begin |
| 98 |
OldInternalCastleDesignInvalidate := InternalCastleDesignInvalidate; |
| 99 |
|
| 100 |
FURL := ''; |
| 101 |
FSuccessMessage := ''; |
| 102 |
FErrorMessage := ''; |
| 103 |
|
| 104 |
FreeAndNil(Viewport); |
| 105 |
FreeAndNil(Scene); |
| 106 |
FreeAndNil(Image); |
| 107 |
FreeAndNil(SoundButton); |
| 108 |
FreeAndNil(LabelVolume); |
| 109 |
FreeAndNil(PlayingSound); |
| 110 |
FreeAndNil(Sound); |
| 111 |
|
| 112 |
{ Save and restore InternalCastleDesignInvalidate here, |
| 113 |
for the same reason as in LoadXxx. |
| 114 |
Testcase: without this save+restore, |
| 115 |
selecting in CGE editor a non-scene from scene in "Files" |
| 116 |
would deselect your current selection in "Hierarchy". } |
| 117 |
InternalCastleDesignInvalidate := OldInternalCastleDesignInvalidate; |
| 118 |
end; |
| 119 |
|
| 120 |
procedure TViewFileFrame.ClickSoundButton(Sender: TObject); |
| 121 |
begin |
| 122 |
if PlayingSound <> nil then |
| 123 |
begin |
| 124 |
PlayingSound.Stop;// will call SoundStop which will set PlayingSound := nil |
| 125 |
Assert(PlayingSound = nil); |
| 126 |
SoundButton.Caption := 'PLAY'; |
| 127 |
end else |
| 128 |
begin |
| 129 |
SoundButton.Caption := 'STOP'; |
| 130 |
PlayingSound := TCastlePlayingSound.Create(Self); |
| 131 |
PlayingSound.Sound := Sound; |
| 132 |
PlayingSound.FreeOnStop := true; |
| 133 |
PlayingSound.OnStop := @SoundStop; |
| 134 |
{ Note: In special cases, if we don't have enough audio sources, the Play immediately |
| 135 |
stops the sound (calling OnStop (changing SoundButton.Caption) and applying FreeOnStop). } |
| 136 |
SoundEngine.Play(PlayingSound); |
| 137 |
end; |
| 138 |
end; |
| 139 |
|
| 140 |
procedure TViewFileFrame.SoundStop(Sender: TObject); |
| 141 |
begin |
| 142 |
if SoundButton <> nil then |
| 143 |
SoundButton.Caption := 'PLAY'; |
| 144 |
PlayingSound := nil; |
| 145 |
end; |
| 146 |
|
| 147 |
procedure TViewFileFrame.FinishLoading(const AURL: String); |
| 148 |
begin |
| 149 |
FURL := AURL; |
| 150 |
LabelURL.Caption := UriDisplay(AURL, true); |
| 151 |
if FErrorMessage <> '' then |
| 152 |
begin |
| 153 |
LabelInformation.Caption := FErrorMessage; |
| 154 |
LabelInformation.Color := HexToColor('ff7e50'); // Red |
| 155 |
end else |
| 156 |
begin |
| 157 |
LabelInformation.Caption := FSuccessMessage; |
| 158 |
LabelInformation.Color := HexToColor('58ff43'); // Green |
| 159 |
end; |
| 160 |
end; |
| 161 |
|
| 162 |
constructor TViewFileFrame.Create(AOwner: TComponent); |
| 163 |
begin |
| 164 |
inherited Create(AOwner); |
| 165 |
end; |
| 166 |
|
| 167 |
destructor TViewFileFrame.Destroy; |
| 168 |
begin |
| 169 |
ClearLoaded; // makes sure to stop the sound |
| 170 |
inherited; |
| 171 |
end; |
| 172 |
|
| 173 |
procedure TViewFileFrame.Loaded; |
| 174 |
begin |
| 175 |
inherited; |
| 176 |
|
| 177 |
PreviewLayer := TCastleUserInterface.Create(Self); |
| 178 |
PreviewLayer.FullSize := true; |
| 179 |
Control.Controls.InsertFront(PreviewLayer); |
| 180 |
|
| 181 |
LabelURL := TCastleLabel.Create(Self); |
| 182 |
LabelURL.Color := White; |
| 183 |
LabelURL.Anchor(hpLeft, 5); |
| 184 |
LabelURL.Anchor(vpTop, -5); |
| 185 |
Control.Controls.InsertFront(LabelURL); |
| 186 |
|
| 187 |
LabelInformation := TCastleLabel.Create(Self); |
| 188 |
LabelInformation.Color := White; |
| 189 |
LabelInformation.Anchor(hpLeft, 5); |
| 190 |
LabelInformation.Anchor(vpTop, - (5 + 5 + LabelURL.Font.Height)); |
| 191 |
Control.Controls.InsertFront(LabelInformation); |
| 192 |
end; |
| 193 |
|
| 194 |
procedure TViewFileFrame.LoadScene(const AURL: String); |
| 195 |
var |
| 196 |
Pos, Dir, Up, GravityUp: TVector3; |
| 197 |
OldInternalCastleDesignInvalidate: Boolean; |
| 198 |
begin |
| 199 |
OldInternalCastleDesignInvalidate := InternalCastleDesignInvalidate; |
| 200 |
ClearLoaded; |
| 201 |
|
| 202 |
{$warnings off} // using TCastleAutoNavigationViewport that should be internal |
| 203 |
Viewport := TCastleAutoNavigationViewport.InternalCreateNonDesign(Self, 0); |
| 204 |
{$warnings on} |
| 205 |
Viewport.FullSize := true; |
| 206 |
// using deprecated AutoCamera, for now this is OK to get initial camera. |
| 207 |
// TODO: Should use AssignDefaultCamera instead. |
| 208 |
{$warnings off} |
| 209 |
Viewport.AutoCamera := true; |
| 210 |
{$warnings on} |
| 211 |
Viewport.AutoNavigation := true; |
| 212 |
PreviewLayer.InsertFront(Viewport); |
| 213 |
|
| 214 |
Scene := TCastleScene.Create(Self); |
| 215 |
Viewport.Items.Add(Scene); |
| 216 |
{$warnings off} // using deprecated MainScene, for now this is OK to get AutoCamera working |
| 217 |
Viewport.Items.MainScene := Scene; |
| 218 |
{$warnings on} |
| 219 |
|
| 220 |
try |
| 221 |
Scene.URL := AURL; |
| 222 |
FSuccessMessage := Format( |
| 223 |
'Vertexes: %d' + NL + |
| 224 |
'Triangles: %d' + NL + |
| 225 |
'Bounding Box: %s', [ |
| 226 |
Scene.VerticesCount, |
| 227 |
Scene.TrianglesCount, |
| 228 |
Scene.BoundingBox.ToString |
| 229 |
]); |
| 230 |
|
| 231 |
CameraViewpointForWholeScene(Viewport.Items.BoundingBox, |
| 232 |
2, 1, false, true, Pos, Dir, Up, GravityUp); |
| 233 |
Viewport.NavigationType := ntExamine; |
| 234 |
Viewport.Camera.SetWorldView(Pos, Dir, Up); |
| 235 |
Viewport.Camera.GravityUp := GravityUp; |
| 236 |
except |
| 237 |
on E: Exception do |
| 238 |
begin |
| 239 |
FErrorMessage := ExceptMessage(E); |
| 240 |
// continue the rest, to show URL and error message |
| 241 |
end; |
| 242 |
end; |
| 243 |
|
| 244 |
FinishLoading(AURL); |
| 245 |
{ Save and restore InternalCastleDesignInvalidate in all LoadXxx methods, |
| 246 |
because changes done while initializing the preview frame |
| 247 |
(even creating UI / transform components) don't really matter for the editor's |
| 248 |
displayed hierarchy. |
| 249 |
Testcase: without this save+restore, selecting in CGE editor a scene in "Files" |
| 250 |
would deselect your current selection in "Hierarchy". } |
| 251 |
InternalCastleDesignInvalidate := OldInternalCastleDesignInvalidate; |
| 252 |
end; |
| 253 |
|
| 254 |
procedure TViewFileFrame.LoadImage(const AURL: String); |
| 255 |
var |
| 256 |
OldInternalCastleDesignInvalidate: Boolean; |
| 257 |
begin |
| 258 |
OldInternalCastleDesignInvalidate := InternalCastleDesignInvalidate; |
| 259 |
ClearLoaded; |
| 260 |
|
| 261 |
Image := TCastleImageControl.Create(Self); |
| 262 |
Image.WidthFraction := 1; |
| 263 |
Image.HeightFraction := 1; |
| 264 |
Image.Stretch := true; |
| 265 |
Image.ProportionalScaling := psFit; |
| 266 |
Image.Anchor(hpMiddle); |
| 267 |
Image.Anchor(vpMiddle); |
| 268 |
PreviewLayer.InsertFront(Image); |
| 269 |
|
| 270 |
try |
| 271 |
Image.URL := AURL; |
| 272 |
FSuccessMessage := Format( |
| 273 |
'Width: %d' + NL + |
| 274 |
'Height: %d' + NL + |
| 275 |
'Depth: %d', [ |
| 276 |
Image.Content.Image.Width, |
| 277 |
Image.Content.Image.Height, |
| 278 |
Image.Content.Image.Depth |
| 279 |
]); |
| 280 |
except |
| 281 |
on E: Exception do |
| 282 |
begin |
| 283 |
FErrorMessage := ExceptMessage(E); |
| 284 |
// continue the rest, to show URL and error message |
| 285 |
end; |
| 286 |
end; |
| 287 |
|
| 288 |
FinishLoading(AURL); |
| 289 |
InternalCastleDesignInvalidate := OldInternalCastleDesignInvalidate; |
| 290 |
end; |
| 291 |
|
| 292 |
procedure TViewFileFrame.LoadSound(const AURL: String); |
| 293 |
var |
| 294 |
OldInternalCastleDesignInvalidate: Boolean; |
| 295 |
begin |
| 296 |
OldInternalCastleDesignInvalidate := InternalCastleDesignInvalidate; |
| 297 |
ClearLoaded; |
| 298 |
|
| 299 |
SoundButton := TCastleButton.Create(Self); |
| 300 |
SoundButton.Caption := 'PLAY'; |
| 301 |
SoundButton.OnClick := @ClickSoundButton; |
| 302 |
SoundButton.Anchor(hpMiddle); |
| 303 |
SoundButton.Anchor(vpMiddle); |
| 304 |
SoundButton.Enabled := false; |
| 305 |
SoundButton.MinWidth := 100; |
| 306 |
SoundButton.MinHeight := 100; |
| 307 |
PreviewLayer.InsertFront(SoundButton); |
| 308 |
|
| 309 |
LabelVolume := TCastleLabel.Create(Self); |
| 310 |
LabelVolume.FontSize := 20; |
| 311 |
LabelVolume.Color := Gray; |
| 312 |
LabelVolume.Anchor(hpMiddle); |
| 313 |
LabelVolume.Anchor(vpTop, vpBottom, -5); |
| 314 |
LabelVolume.OnUpdate := @UpdateLabelVolume; |
| 315 |
SoundButton.InsertFront(LabelVolume); |
| 316 |
|
| 317 |
Sound := TCastleSound.Create(Self); |
| 318 |
try |
| 319 |
Sound.URL := AURL; |
| 320 |
|
| 321 |
// without this check, loading fails silently e.g. when OpenAL dll not found |
| 322 |
if not SoundEngine.IsContextOpenSuccess then |
| 323 |
raise Exception.Create('Sound engine backend cannot be initialized.' + NL + |
| 324 |
SoundEngine.Information); |
| 325 |
|
| 326 |
FSuccessMessage := FormatDot( |
| 327 |
'Duration: %f' + NL + |
| 328 |
'Format: %s' + NL + |
| 329 |
'Frequency: %d', [ |
| 330 |
Sound.Duration, |
| 331 |
DataFormatToStr(Sound.DataFormat), |
| 332 |
Sound.Frequency |
| 333 |
]); |
| 334 |
SoundButton.Enabled := true; |
| 335 |
except |
| 336 |
on E: Exception do |
| 337 |
begin |
| 338 |
FErrorMessage := ExceptMessage(E); |
| 339 |
// continue the rest, to show URL and error message |
| 340 |
end; |
| 341 |
end; |
| 342 |
|
| 343 |
FinishLoading(AURL); |
| 344 |
InternalCastleDesignInvalidate := OldInternalCastleDesignInvalidate; |
| 345 |
end; |
| 346 |
|
| 347 |
procedure TViewFileFrame.UpdateLabelVolume(const Sender: TCastleUserInterface; |
| 348 |
const SecondsPassed: Single; var HandleInput: Boolean); |
| 349 |
var |
| 350 |
S: String; |
| 351 |
begin |
| 352 |
if SoundEngine.Volume = 0 then |
| 353 |
begin |
| 354 |
S := 'Volume: mute'; |
| 355 |
if RunningApplication and MuteOnRun then |
| 356 |
S := S + ' (application running)'; |
| 357 |
end else |
| 358 |
S := FormatDot('Volume: %f', [SoundEngine.Volume]); |
| 359 |
LabelVolume.Caption := S; |
| 360 |
end; |
| 361 |
|
| 362 |
end. |