Debian Codegraph @

tools/castle-editor/data/project_templates/3d_model_viewer/files/code/gameviewmain.pas

raw
1 { Main view, where most of the application logic takes place.
2
3 Feel free to use this code as a starting point for your own projects.
4 This template code is in public domain, unlike most other CGE code which
5 is covered by BSD or LGPL (see https://castle-engine.io/license). }
6 unit GameView${MAIN_VIEW};
7
8 interface
9
10 uses Classes,
11 CastleVectors, CastleComponentSerialize, CastleUIControls,
12 CastleControls, CastleKeysMouse, CastleViewport, CastleScene;
13
14 type
15 { Main view, where most of the application logic takes place. }
16 TView${MAIN_VIEW} = class(TCastleView)
17 published
18 { Components designed using CGE editor.
19 These fields will be automatically initialized at Start. }
20 Viewport: TCastleViewport;
21 SceneMain: TCastleScene;
22 ButtonLoadKnight: TCastleButton;
23 ButtonLoadCar: TCastleButton;
24 ButtonLoadCustom: TCastleButton;
25 ButtonPlayAnimation: TCastleButton;
26 ButtonStopAnimation: TCastleButton;
27 LabelLoadedUrl, LabelFps: TCastleLabel;
28 private
29 procedure Load(const Url: String);
30 { Methods assigned to handle buttons' OnClick events. }
31 procedure ClickLoadKnight(Sender: TObject);
32 procedure ClickLoadCar(Sender: TObject);
33 procedure ClickLoadCustom(Sender: TObject);
34 procedure ClickPlayAnimation(Sender: TObject);
35 procedure ClickStopAnimation(Sender: TObject);
36 public
37 constructor Create(AOwner: TComponent); override;
38 procedure Start; override;
39 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
40 function Press(const Event: TInputPressRelease): Boolean; override;
41 end;
42
43 var
44 View${MAIN_VIEW}: TView${MAIN_VIEW};
45
46 implementation
47
48 uses SysUtils,
49 CastleWindow, X3DLoad;
50
51 { TView${MAIN_VIEW} ----------------------------------------------------------------- }
52
53 constructor TView${MAIN_VIEW}.Create(AOwner: TComponent);
54 begin
55 inherited;
56 DesignUrl := 'castle-data:/gameview${MAIN_VIEW_LOWERCASE}.castle-user-interface';
57 end;
58
59 procedure TView${MAIN_VIEW}.Start;
60 begin
61 inherited;
62
63 { Assign OnClick handler to buttons }
64 ButtonLoadKnight.OnClick := {$ifdef FPC}@{$endif} ClickLoadKnight;
65 ButtonLoadCar.OnClick := {$ifdef FPC}@{$endif} ClickLoadCar;
66 ButtonLoadCustom.OnClick := {$ifdef FPC}@{$endif} ClickLoadCustom;
67 ButtonPlayAnimation.OnClick := {$ifdef FPC}@{$endif} ClickPlayAnimation;
68 ButtonStopAnimation.OnClick := {$ifdef FPC}@{$endif} ClickStopAnimation;
69
70 { Although knight.gltf is already loaded at design-time,
71 load it again, to always initialize all UI (e.g. LabelLoadedUrl.Caption)
72 using our Load method. }
73 Load('castle-data:/knight/knight.gltf');
74 end;
75
76 procedure TView${MAIN_VIEW}.Load(const Url: String);
77 begin
78 SceneMain.Load(Url);
79 LabelLoadedUrl.Caption := 'Loaded: ' + Url;
80 Viewport.AssignDefaultCamera;
81 end;
82
83 procedure TView${MAIN_VIEW}.ClickLoadKnight(Sender: TObject);
84 begin
85 { Note that you load here any filename or URL (file://, http:// etc.).
86 - See https://castle-engine.io/manual_network.php about CGE supported URLs.
87 - See https://castle-engine.io/data about the special
88 URL protocol "castle-data:/" }
89 Load('castle-data:/knight/knight.gltf');
90 end;
91
92 procedure TView${MAIN_VIEW}.ClickLoadCar(Sender: TObject);
93 begin
94 Load('castle-data:/car/car.x3d');
95 end;
96
97 procedure TView${MAIN_VIEW}.ClickLoadCustom(Sender: TObject);
98 var
99 Url: String;
100 begin
101 Url := SceneMain.Url;
102 if Application.MainWindow.FileDialog('Open Model', Url, true, LoadScene_FileFilters) then
103 Load(Url);
104 end;
105
106 procedure TView${MAIN_VIEW}.ClickPlayAnimation(Sender: TObject);
107 begin
108 { Play the 1st animation in the scene.
109 In an actual game, you usually hardcode the animation name to play.
110 In a full 3D model viewer, you can display all known animations,
111 and allow user to start the chosen one. }
112 if SceneMain.AnimationsList.Count > 0 then
113 SceneMain.PlayAnimation(SceneMain.AnimationsList[0], true);
114 end;
115
116 procedure TView${MAIN_VIEW}.ClickStopAnimation(Sender: TObject);
117 begin
118 SceneMain.StopAnimation;
119 end;
120
121 procedure TView${MAIN_VIEW}.Update(const SecondsPassed: Single; var HandleInput: Boolean);
122 begin
123 inherited;
124 { This virtual method is executed every frame (many times per second). }
125 Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
126 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
127 end;
128
129 function TView${MAIN_VIEW}.Press(const Event: TInputPressRelease): Boolean;
130 begin
131 Result := inherited;
132 if Result then Exit; // allow the ancestor to handle keys
133
134 { This virtual method is executed when user presses
135 a key, a mouse button, or touches a touch-screen.
136
137 Note that each UI control has also events like OnPress and OnClick.
138 These events can be used to handle the "press", if it should do something
139 specific when used in that UI control.
140 The TView${MAIN_VIEW}.Press method should be used to handle keys
141 not handled in children controls.
142 }
143
144 // when pressing Home, reset the camera, to view complete model
145 if Event.IsKey(keyHome) then
146 begin
147 Viewport.AssignDefaultCamera;
148 Exit(true); // key was handled
149 end;
150 end;
151
152 end.