| 1 |
{ |
| 2 |
Copyright 2008-2022 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 form. } |
| 17 |
unit mainf; |
| 18 |
|
| 19 |
{$mode objfpc}{$H+} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, |
| 24 |
OpenGLContext, Menus, CastleScene, CastleCameras, CastleControl, CastleLog, |
| 25 |
CastleLCLRecentFiles, CastleConfig, Buttons, ExtCtrls, StdCtrls, CastleRecentFiles, |
| 26 |
CastleViewport, CastleDialogs, CastleControls; |
| 27 |
|
| 28 |
type |
| 29 |
TMain = class(TForm) |
| 30 |
ApplicationProperties1: TApplicationProperties; |
| 31 |
ButtonExamine: TSpeedButton; |
| 32 |
ButtonFly: TSpeedButton; |
| 33 |
ButtonNone: TSpeedButton; |
| 34 |
ButtonScreenshot: TBitBtn; |
| 35 |
ButtonChangeCamera: TButton; |
| 36 |
ButtonWalk: TSpeedButton; |
| 37 |
EditPositionX: TEdit; |
| 38 |
EditPositionY: TEdit; |
| 39 |
EditPositionZ: TEdit; |
| 40 |
EditDirectionZ: TEdit; |
| 41 |
EditDirectionY: TEdit; |
| 42 |
EditDirectionX: TEdit; |
| 43 |
EditUpZ: TEdit; |
| 44 |
EditUpY: TEdit; |
| 45 |
EditUpX: TEdit; |
| 46 |
GroupNavigationType: TGroupBox; |
| 47 |
GroupBoxCamera: TGroupBox; |
| 48 |
Browser: TCastleControl; |
| 49 |
ImagePoweredBy: TImage; |
| 50 |
LabelFps: TLabel; |
| 51 |
LabelPosition: TLabel; |
| 52 |
LabelDirection: TLabel; |
| 53 |
LabelUp: TLabel; |
| 54 |
MainMenu1: TMainMenu; |
| 55 |
MenuFile: TMenuItem; |
| 56 |
MenuFocusGLControl: TMenuItem; |
| 57 |
MenuHelp: TMenuItem; |
| 58 |
MenuAboutOpenGL: TMenuItem; |
| 59 |
MenuWebsite: TMenuItem; |
| 60 |
MenuSep2: TMenuItem; |
| 61 |
MenuShowConsole: TMenuItem; |
| 62 |
MenuItemView: TMenuItem; |
| 63 |
MenuQuit: TMenuItem; |
| 64 |
MenuSep1: TMenuItem; |
| 65 |
MenuOpen: TMenuItem; |
| 66 |
OpenDialog1: TCastleOpenSceneDialog; |
| 67 |
PanelBottom: TPanel; |
| 68 |
SaveScreenshotDialog: TCastleSaveImageDialog; |
| 69 |
Timer1: TTimer; |
| 70 |
MenuMouseLookToggle: TMenuItem; |
| 71 |
procedure ButtonNavigationTypeClick(Sender: TObject); |
| 72 |
procedure ButtonScreenshotClick(Sender: TObject); |
| 73 |
procedure BrowserCameraChanged(Camera: TObject); |
| 74 |
procedure ButtonChangeCameraClick(Sender: TObject); |
| 75 |
procedure FormCreate(Sender: TObject); |
| 76 |
procedure FormDeactivate(Sender: TObject); |
| 77 |
procedure MenuAboutOpenGLClick(Sender: TObject); |
| 78 |
procedure MenuFocusGLControlClick(Sender: TObject); |
| 79 |
procedure MenuOpenClick(Sender: TObject); |
| 80 |
procedure MenuQuitClick(Sender: TObject); |
| 81 |
procedure MenuShowConsoleClick(Sender: TObject); |
| 82 |
procedure MenuWebsiteClick(Sender: TObject); |
| 83 |
procedure Timer1Timer(Sender: TObject); |
| 84 |
procedure MenuMouseLookToggleClick(Sender: TObject); |
| 85 |
procedure FormDestroy(Sender: TObject); |
| 86 |
private |
| 87 |
SceneUrl: String; |
| 88 |
CameraChanged: boolean; |
| 89 |
ButtonsNavigationType: array [TNavigationType] of TSpeedButton; |
| 90 |
CrosshairCtl: TCastleCrosshair; |
| 91 |
CrosshairActive: Boolean; //< there is something to touch under the crosshair |
| 92 |
RecentFiles: TCastleRecentFiles; |
| 93 |
Viewport: TCastleViewport; |
| 94 |
MainScene: TCastleScene; //< main loaded scene |
| 95 |
NavigationType: TNavigationType; |
| 96 |
Navigation: TCastleNavigation; |
| 97 |
|
| 98 |
procedure OpenScene(const Url: String); |
| 99 |
procedure UpdateCaption; |
| 100 |
procedure UpdateCrosshairImage; |
| 101 |
procedure OnPointingDeviceSensorsChange(Sender: TObject); |
| 102 |
procedure RecentFilesOpenRecent(const Url: String); |
| 103 |
procedure ChangeNavigationType(const NewNavigationType: TNavigationType); |
| 104 |
public |
| 105 |
{ public declarations } |
| 106 |
end; |
| 107 |
|
| 108 |
var |
| 109 |
Main: TMain; |
| 110 |
|
| 111 |
implementation |
| 112 |
|
| 113 |
uses LCLType, LCLIntf, CastleVectors, CastleBoxes, X3DNodes, |
| 114 |
CastleClassUtils, CastleUtils, X3DLoad, CastleUriUtils, |
| 115 |
CastleGLUtils, CastleSceneCore, CastleFilesUtils, CastleParameters, |
| 116 |
CastleApplicationProperties, |
| 117 |
OpenGLInformation, CastleLCLUtils, ConsoleF, CastleImages, CastleSoundEngine; |
| 118 |
|
| 119 |
{$R *.lfm} |
| 120 |
|
| 121 |
procedure TMain.OpenScene(const Url: String); |
| 122 |
|
| 123 |
procedure LoadScene(const Url: String); |
| 124 |
begin |
| 125 |
MainScene.Free; // free previous MainScene |
| 126 |
|
| 127 |
MainScene := TCastleScene.Create(Self); |
| 128 |
MainScene.Load(Url); |
| 129 |
MainScene.PreciseCollisions := true; |
| 130 |
MainScene.ProcessEvents := true; |
| 131 |
|
| 132 |
Viewport.Items.Add(MainScene); |
| 133 |
|
| 134 |
Viewport.AssignDefaultCamera; |
| 135 |
end; |
| 136 |
|
| 137 |
begin |
| 138 |
Console.WasWarnings := false; |
| 139 |
Console.Memo1.Lines.Append('--- Loading ' + Url); |
| 140 |
|
| 141 |
LoadScene(Url); |
| 142 |
|
| 143 |
SceneUrl := Url; |
| 144 |
UpdateCaption; |
| 145 |
|
| 146 |
if Console.WasWarnings then |
| 147 |
begin |
| 148 |
MenuShowConsole.Checked := true; |
| 149 |
Console.Visible := MenuShowConsole.Checked; |
| 150 |
end; |
| 151 |
|
| 152 |
RecentFiles.Add(Url); |
| 153 |
|
| 154 |
{ for changing the crosshair shape } |
| 155 |
MainScene.OnPointingDeviceSensorsChange := @OnPointingDeviceSensorsChange; |
| 156 |
|
| 157 |
{ simple Browser.Load always recreates the Navigation each time, which means |
| 158 |
that we have to restore all camera properties that should be |
| 159 |
"persistent" when loading scenes. |
| 160 |
For now, this means mouse look stuff. |
| 161 |
|
| 162 |
TODO: |
| 163 |
- is above still true? |
| 164 |
Load() recreates camera vectors? probably no more. fix |
| 165 |
- And fix this example and this comment, to not use deprecated Load(). |
| 166 |
|
| 167 |
Note that you can instead load your scene manually (see *trivial* |
| 168 |
TCastleControl.Load implementation), and this way avoid recreating |
| 169 |
the camera. But then, you will have the opposite problem, you will |
| 170 |
have to explicitly update camera properties that *should* change when |
| 171 |
new scene is loaded, like the default viewpoint and navigation mode. |
| 172 |
Which isn't really a serious problem (you have comfortable |
| 173 |
TCastleSceneCore.InternalUpdateNavigation and TCastleSceneCore.InternalUpdateCamera |
| 174 |
to deal with it). It's your choice, anyway. } |
| 175 |
MenuMouseLookToggleClick(MenuMouseLookToggle); |
| 176 |
end; |
| 177 |
|
| 178 |
procedure TMain.MenuOpenClick(Sender: TObject); |
| 179 |
begin |
| 180 |
if SceneUrl <> '' then |
| 181 |
OpenDialog1.Url := SceneUrl; |
| 182 |
if OpenDialog1.Execute then |
| 183 |
OpenScene(OpenDialog1.Url); |
| 184 |
end; |
| 185 |
|
| 186 |
procedure TMain.UpdateCaption; |
| 187 |
var |
| 188 |
S: string; |
| 189 |
begin |
| 190 |
if SceneUrl <> '' then |
| 191 |
S := UriCaption(SceneUrl) |
| 192 |
else |
| 193 |
S := 'No Scene'; |
| 194 |
S := S + ' | Castle Game Engine - Lazarus Model Viewer'; |
| 195 |
Caption := S; |
| 196 |
end; |
| 197 |
|
| 198 |
procedure TMain.MenuQuitClick(Sender: TObject); |
| 199 |
begin |
| 200 |
Close; |
| 201 |
end; |
| 202 |
|
| 203 |
procedure TMain.MenuShowConsoleClick(Sender: TObject); |
| 204 |
begin |
| 205 |
Console.Visible := MenuShowConsole.Checked; |
| 206 |
end; |
| 207 |
|
| 208 |
procedure TMain.MenuWebsiteClick(Sender: TObject); |
| 209 |
begin |
| 210 |
if not OpenUrl('https://castle-engine.io/') then |
| 211 |
MessageDlg('WWW browser not found on your system.', mtError, [mbClose], 0); |
| 212 |
end; |
| 213 |
|
| 214 |
procedure TMain.Timer1Timer(Sender: TObject); |
| 215 |
var |
| 216 |
Pos, Dir, Up: TVector3; |
| 217 |
begin |
| 218 |
LabelFps.Caption := 'FPS: ' + Browser.Fps.ToString; |
| 219 |
|
| 220 |
{ Update edit boxes about camera only from time to time. |
| 221 |
Otherwise, updating edit controls |
| 222 |
on every frame would lower performance of OpenGL |
| 223 |
(e.g. when rotating object in Examine mode) } |
| 224 |
if CameraChanged then |
| 225 |
begin |
| 226 |
CameraChanged := false; |
| 227 |
|
| 228 |
Viewport.Camera.GetView(Pos, Dir, Up); |
| 229 |
{ Note that Dir, Up returned here are always normalized } |
| 230 |
|
| 231 |
EditPositionX.Text := Format('%f', [Pos[0]]); |
| 232 |
EditPositionY.Text := Format('%f', [Pos[1]]); |
| 233 |
EditPositionZ.Text := Format('%f', [Pos[2]]); |
| 234 |
|
| 235 |
EditDirectionX.Text := Format('%f', [Dir[0]]); |
| 236 |
EditDirectionY.Text := Format('%f', [Dir[1]]); |
| 237 |
EditDirectionZ.Text := Format('%f', [Dir[2]]); |
| 238 |
|
| 239 |
EditUpX.Text := Format('%f', [Up[0]]); |
| 240 |
EditUpY.Text := Format('%f', [Up[1]]); |
| 241 |
EditUpZ.Text := Format('%f', [Up[2]]); |
| 242 |
end; |
| 243 |
end; |
| 244 |
|
| 245 |
procedure TMain.MenuMouseLookToggleClick(Sender: TObject); |
| 246 |
var |
| 247 |
WalkNav: TCastleWalkNavigation; |
| 248 |
begin |
| 249 |
if Navigation is TCastleWalkNavigation then |
| 250 |
begin |
| 251 |
WalkNav := Navigation as TCastleWalkNavigation; |
| 252 |
WalkNav.MouseLook := (Sender as TMenuItem).Checked; |
| 253 |
UpdateCrosshairImage; |
| 254 |
Repaint; |
| 255 |
end; |
| 256 |
end; |
| 257 |
|
| 258 |
procedure TMain.OnPointingDeviceSensorsChange(Sender: TObject); |
| 259 |
var |
| 260 |
OverSensor: Boolean; |
| 261 |
SensorList: TPointingDeviceSensorList; |
| 262 |
begin |
| 263 |
{ check if the crosshair (mouse) is over any sensor } |
| 264 |
OverSensor := false; |
| 265 |
SensorList := MainScene.PointingDeviceSensors; |
| 266 |
if (SensorList <> nil) then |
| 267 |
OverSensor := (SensorList.EnabledCount>0); |
| 268 |
|
| 269 |
if CrosshairActive <> OverSensor then |
| 270 |
begin |
| 271 |
CrosshairActive := OverSensor; |
| 272 |
UpdateCrosshairImage; |
| 273 |
end; |
| 274 |
end; |
| 275 |
|
| 276 |
procedure TMain.UpdateCrosshairImage; |
| 277 |
begin |
| 278 |
CrosshairCtl.Exists := ( |
| 279 |
(Navigation is TCastleWalkNavigation) and |
| 280 |
(Navigation as TCastleWalkNavigation).MouseLook |
| 281 |
); |
| 282 |
if CrosshairActive then |
| 283 |
CrosshairCtl.Shape := csCrossRect |
| 284 |
else |
| 285 |
CrosshairCtl.Shape := csCross; |
| 286 |
end; |
| 287 |
|
| 288 |
procedure TMain.MenuAboutOpenGLClick(Sender: TObject); |
| 289 |
begin |
| 290 |
TOpenGLInformation.Execute; |
| 291 |
end; |
| 292 |
|
| 293 |
procedure TMain.MenuFocusGLControlClick(Sender: TObject); |
| 294 |
begin |
| 295 |
Browser.SetFocus; |
| 296 |
end; |
| 297 |
|
| 298 |
procedure TMain.FormCreate(Sender: TObject); |
| 299 |
begin |
| 300 |
{ load config settings } |
| 301 |
UserConfig.Load; |
| 302 |
|
| 303 |
Viewport := TCastleViewport.Create(Application); |
| 304 |
Viewport.FullSize := true; |
| 305 |
Viewport.OnCameraChanged := @BrowserCameraChanged; |
| 306 |
Viewport.Camera.Add(TCastleDirectionalLight.Create(Self)); // headlight |
| 307 |
Browser.Controls.InsertFront(Viewport); |
| 308 |
|
| 309 |
RecentFiles := TCastleRecentFiles.Create(Self); |
| 310 |
RecentFiles.OnOpenRecent := @RecentFilesOpenRecent; |
| 311 |
RecentFiles.LoadFromConfig(UserConfig); |
| 312 |
RecentFiles.NextMenuItem := MenuSep1; |
| 313 |
|
| 314 |
UpdateCaption; |
| 315 |
|
| 316 |
Console := TConsole.Create(Application); |
| 317 |
|
| 318 |
ButtonsNavigationType[ntExamine] := ButtonExamine; |
| 319 |
ButtonsNavigationType[ntWalk] := ButtonWalk; |
| 320 |
ButtonsNavigationType[ntFly] := ButtonFly; |
| 321 |
ButtonsNavigationType[ntNone] := ButtonNone; |
| 322 |
|
| 323 |
// synchronize NavigationType value with actual state |
| 324 |
NavigationType := ntNone; |
| 325 |
|
| 326 |
// create ntExamine navigation (this changes NavigationType and Navigation) |
| 327 |
ChangeNavigationType(ntExamine); |
| 328 |
Assert(NavigationType = ntExamine); |
| 329 |
ButtonsNavigationType[NavigationType].Down := true; |
| 330 |
|
| 331 |
CrosshairCtl := TCastleCrosshair.Create(Browser); |
| 332 |
CrosshairCtl.Exists := false; // start as invisible |
| 333 |
Browser.Controls.InsertFront(CrosshairCtl); |
| 334 |
|
| 335 |
if Parameters.High >= 1 then |
| 336 |
OpenScene(Parameters[1]); |
| 337 |
end; |
| 338 |
|
| 339 |
procedure TMain.FormDestroy(Sender: TObject); |
| 340 |
begin |
| 341 |
{ save config settings } |
| 342 |
RecentFiles.SaveToConfig(UserConfig); |
| 343 |
UserConfig.Save; |
| 344 |
end; |
| 345 |
|
| 346 |
procedure TMain.RecentFilesOpenRecent(const Url: String); |
| 347 |
begin |
| 348 |
OpenScene(Url); |
| 349 |
end; |
| 350 |
|
| 351 |
procedure TMain.FormDeactivate(Sender: TObject); |
| 352 |
begin |
| 353 |
Browser.ReleaseAllKeysAndMouse; |
| 354 |
end; |
| 355 |
|
| 356 |
procedure TMain.ButtonChangeCameraClick(Sender: TObject); |
| 357 |
begin |
| 358 |
Viewport.Camera.SetView( |
| 359 |
Vector3( |
| 360 |
StrToFloatDot(EditPositionX.Text), |
| 361 |
StrToFloatDot(EditPositionY.Text), |
| 362 |
StrToFloatDot(EditPositionZ.Text)), |
| 363 |
Vector3( |
| 364 |
StrToFloatDot(EditDirectionX.Text), |
| 365 |
StrToFloatDot(EditDirectionY.Text), |
| 366 |
StrToFloatDot(EditDirectionZ.Text)), |
| 367 |
Vector3( |
| 368 |
StrToFloatDot(EditUpX.Text), |
| 369 |
StrToFloatDot(EditUpY.Text), |
| 370 |
StrToFloatDot(EditUpZ.Text))); |
| 371 |
end; |
| 372 |
|
| 373 |
procedure TMain.BrowserCameraChanged(Camera: TObject); |
| 374 |
begin |
| 375 |
CameraChanged := true; |
| 376 |
end; |
| 377 |
|
| 378 |
procedure TMain.ButtonNavigationTypeClick(Sender: TObject); |
| 379 |
var |
| 380 |
NewNavigationType, NT: TNavigationType; |
| 381 |
begin |
| 382 |
{ This is a handler for all four navigation type buttons. |
| 383 |
Scan ButtonsNavigationType to detect which is Sender. } |
| 384 |
for NT := Low(NT) to High(NT) do |
| 385 |
if ButtonsNavigationType[NT] = Sender then |
| 386 |
NewNavigationType := NT; |
| 387 |
ChangeNavigationType(NewNavigationType); |
| 388 |
end; |
| 389 |
|
| 390 |
procedure TMain.ChangeNavigationType(const NewNavigationType: TNavigationType); |
| 391 |
begin |
| 392 |
if NavigationType <> NewNavigationType then |
| 393 |
begin |
| 394 |
FreeAndNil(Navigation); // this will also remove Navigation from Viewport children |
| 395 |
case NewNavigationType of |
| 396 |
ntExamine: |
| 397 |
begin |
| 398 |
Navigation := TCastleExamineNavigation.Create(Self); |
| 399 |
end; |
| 400 |
ntWalk: |
| 401 |
begin |
| 402 |
Navigation := TCastleWalkNavigation.Create(Self); |
| 403 |
(Navigation as TCastleWalkNavigation).Gravity := true; |
| 404 |
end; |
| 405 |
ntFly: |
| 406 |
begin |
| 407 |
Navigation := TCastleWalkNavigation.Create(Self); |
| 408 |
(Navigation as TCastleWalkNavigation).Gravity := false; |
| 409 |
end; |
| 410 |
end; |
| 411 |
NavigationType := NewNavigationType; |
| 412 |
if Navigation <> nil then // we leave Navigation=nil in case of NavigationType=ntNone |
| 413 |
Viewport.InsertFront(Navigation); |
| 414 |
end; |
| 415 |
end; |
| 416 |
|
| 417 |
procedure TMain.ButtonScreenshotClick(Sender: TObject); |
| 418 |
var |
| 419 |
Image: TRGBImage; |
| 420 |
begin |
| 421 |
if SaveScreenshotDialog.Execute then |
| 422 |
begin |
| 423 |
Image := Browser.SaveScreen; |
| 424 |
try |
| 425 |
SaveImage(Image, SaveScreenshotDialog.Url); |
| 426 |
finally FreeAndNil(Image) end; |
| 427 |
end; |
| 428 |
end; |
| 429 |
|
| 430 |
end. |