| 1 |
{ |
| 2 |
Copyright 2018-2023 Michalis Kamburelis, Andrzej Kilijański. |
| 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 SysUtils, Classes, Math, |
| 22 |
CastleScene, CastleSceneCore, CastleControls, CastleLog, |
| 23 |
CastleFilesUtils, CastleColors, CastleUIControls, X3DLoad, CastleUtils, |
| 24 |
CastleApplicationProperties, CastleVectors, CastleCameras, CastleViewport, |
| 25 |
CastleUriUtils, X3DNodes, CastleTextureImages; |
| 26 |
|
| 27 |
type |
| 28 |
{ Main view, where most of the application logic takes place. } |
| 29 |
TViewMain = class(TCastleView) |
| 30 |
published |
| 31 |
{ Components designed using CGE editor. |
| 32 |
These fields will be automatically initialized at Start. } |
| 33 |
LabelFps: TCastleLabel; |
| 34 |
Viewport: TCastleViewport; |
| 35 |
Scene: TCastleScene; |
| 36 |
ButtonOpen3D, ButtonOpen2DSpine, ButtonOpen2DStarling, ButtonOpen2DCocos2d, ButtonOpen2DImage, ButtonOpenDialog: TCastleButton; |
| 37 |
SceneAnimationButtons: TCastleUserInterface; |
| 38 |
CheckboxForward, CheckboxLoop, CheckboxMagFilterNearest, CheckboxMinFilterNearest, CheckboxAnimationNamingLoadOpt: TCastleCheckbox; |
| 39 |
SliderTransition, SliderScale, SliderFPSLoadOpt: TCastleFloatSlider; |
| 40 |
private |
| 41 |
{ Event handlers } |
| 42 |
procedure OpenScene(const Url: String); |
| 43 |
procedure ClickButtonOpen3D(Sender: TObject); |
| 44 |
procedure ClickButtonOpen2DSpine(Sender: TObject); |
| 45 |
procedure ClickButtonOpen2DStarling(Sender: TObject); |
| 46 |
procedure ClickButtonOpen2DCocos2d(Sender: TObject); |
| 47 |
procedure ClickButtonOpen2DImage(Sender: TObject); |
| 48 |
procedure ClickButtonOpenDialog(Sender: TObject); |
| 49 |
procedure ClickButtonPlayAnimation(Sender: TObject); |
| 50 |
procedure ChangedScale(Sender: TObject); |
| 51 |
procedure ChangedTextureMagOptions(Sender: TObject); |
| 52 |
procedure ChangedTextureMinOptions(Sender: TObject); |
| 53 |
procedure ChangedStarlingOptions(Sender: TObject); |
| 54 |
|
| 55 |
{ Simple function to parse current settings in options UI |
| 56 |
see ClickButtonOpen2DStarling for more info. } |
| 57 |
function CurrentUIStarlingSettingsToAnchor: String; |
| 58 |
public |
| 59 |
constructor Create(AOwner: TComponent); override; |
| 60 |
procedure Start; override; |
| 61 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 62 |
end; |
| 63 |
|
| 64 |
var |
| 65 |
ViewMain: TViewMain; |
| 66 |
|
| 67 |
implementation |
| 68 |
|
| 69 |
uses CastleWindow, CastleComponentSerialize, CastleRenderOptions; |
| 70 |
|
| 71 |
{ TViewMain ----------------------------------------------------------------- } |
| 72 |
|
| 73 |
constructor TViewMain.Create(AOwner: TComponent); |
| 74 |
begin |
| 75 |
inherited; |
| 76 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 77 |
end; |
| 78 |
|
| 79 |
procedure TViewMain.Start; |
| 80 |
begin |
| 81 |
inherited; |
| 82 |
|
| 83 |
{ attach events } |
| 84 |
ButtonOpen3D.OnClick := {$ifdef FPC}@{$endif} ClickButtonOpen3D; |
| 85 |
ButtonOpen2DSpine.OnClick := {$ifdef FPC}@{$endif} ClickButtonOpen2DSpine; |
| 86 |
ButtonOpen2DStarling.OnClick := {$ifdef FPC}@{$endif} ClickButtonOpen2DStarling; |
| 87 |
ButtonOpen2DCocos2d.OnClick := {$ifdef FPC}@{$endif} ClickButtonOpen2DCocos2d; |
| 88 |
ButtonOpen2DImage.OnClick := {$ifdef FPC}@{$endif} ClickButtonOpen2DImage; |
| 89 |
ButtonOpenDialog.OnClick := {$ifdef FPC}@{$endif} ClickButtonOpenDialog; |
| 90 |
SliderFPSLoadOpt.OnChange := {$ifdef FPC}@{$endif} ChangedStarlingOptions; |
| 91 |
CheckboxAnimationNamingLoadOpt.OnChange := {$ifdef FPC}@{$endif} ChangedStarlingOptions; |
| 92 |
SliderScale.OnChange := {$ifdef FPC}@{$endif} ChangedScale; |
| 93 |
CheckboxMagFilterNearest.OnChange := {$ifdef FPC}@{$endif} ChangedTextureMagOptions; |
| 94 |
CheckboxMinFilterNearest.OnChange := {$ifdef FPC}@{$endif} ChangedTextureMinOptions; |
| 95 |
|
| 96 |
// pretend ButtonOpen2DSpine was clicked |
| 97 |
ClickButtonOpen2DSpine(nil); |
| 98 |
end; |
| 99 |
|
| 100 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 101 |
begin |
| 102 |
inherited; |
| 103 |
{ This virtual method is executed every frame (many times per second). } |
| 104 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 105 |
end; |
| 106 |
|
| 107 |
procedure TViewMain.OpenScene(const Url: String); |
| 108 |
|
| 109 |
procedure CreateAnimationsButtons; |
| 110 |
var |
| 111 |
AnimationName: String; |
| 112 |
Button: TCastleButton; |
| 113 |
begin |
| 114 |
{ Remove previous animation controls. } |
| 115 |
SceneAnimationButtons.ClearControls; |
| 116 |
|
| 117 |
for AnimationName in Scene.AnimationsList do |
| 118 |
begin |
| 119 |
Button := TCastleButton.Create(Self); |
| 120 |
Button.Caption := AnimationName; |
| 121 |
Button.OnClick := {$ifdef FPC}@{$endif} ClickButtonPlayAnimation; |
| 122 |
SceneAnimationButtons.InsertFront(Button); |
| 123 |
end; |
| 124 |
end; |
| 125 |
|
| 126 |
begin |
| 127 |
{ Scale reset - to make the following Viewport.AssignDefaultCamera adjust |
| 128 |
camera to the scene unscaled size } |
| 129 |
Scene.Scale := Vector3(1.0, 1.0, 1.0); |
| 130 |
Scene.Load(Url); |
| 131 |
|
| 132 |
{ Set blending sort following "NavigationInfo.blendingSort" info from scene. |
| 133 |
This means we use 2D sorting e.g. for Spine models by default. } |
| 134 |
if (Scene.NavigationInfoStack.Top <> nil) and |
| 135 |
(Scene.NavigationInfoStack.Top.BlendingSort <> sortAuto) then |
| 136 |
Viewport.BlendingSort := Scene.NavigationInfoStack.Top.BlendingSort |
| 137 |
else |
| 138 |
Viewport.BlendingSort := sortAuto; |
| 139 |
|
| 140 |
Viewport.AssignDefaultCamera; |
| 141 |
|
| 142 |
CreateAnimationsButtons; |
| 143 |
|
| 144 |
{ Update scale for current SliderScale value } |
| 145 |
ChangedScale(nil); |
| 146 |
end; |
| 147 |
|
| 148 |
procedure TViewMain.ClickButtonOpen3D(Sender: TObject); |
| 149 |
begin |
| 150 |
OpenScene('castle-data:/gltf_knight/knight.gltf'); |
| 151 |
|
| 152 |
{ Note: TransitionDuration is not supported for castle-anim-frames. |
| 153 |
TransitionDuration is supported on other model formats (in particular |
| 154 |
glTF, Spine, X3D support animations too). } |
| 155 |
//OpenScene('../../deprecated_resource_animations/data/deprecated/knight_single_castle_anim_frames/knight.castle-anim-frames'); |
| 156 |
end; |
| 157 |
|
| 158 |
procedure TViewMain.ClickButtonOpen2DSpine(Sender: TObject); |
| 159 |
begin |
| 160 |
OpenScene('castle-data:/spine_dragon/dragon.json'); |
| 161 |
end; |
| 162 |
|
| 163 |
function TViewMain.CurrentUIStarlingSettingsToAnchor: String; |
| 164 |
var |
| 165 |
AnimNaming: String; |
| 166 |
begin |
| 167 |
if CheckboxAnimationNamingLoadOpt.Checked then |
| 168 |
AnimNaming := 'trailing-number' |
| 169 |
else |
| 170 |
AnimNaming := 'strict-underscore'; |
| 171 |
|
| 172 |
Result := '#fps:' + FloatToStrDot(SliderFPSLoadOpt.Value) + ',anim-naming:' + AnimNaming; |
| 173 |
end; |
| 174 |
|
| 175 |
procedure TViewMain.ClickButtonOpen2DStarling(Sender: TObject); |
| 176 |
begin |
| 177 |
{ When using Starling files you can specify some options, as URL anchors. |
| 178 |
See https://castle-engine.io/sprite_sheets . |
| 179 |
|
| 180 |
The sample Starling file below is based on one of many great assets by Kenney, |
| 181 |
check https://kenney.nl/ for more. } |
| 182 |
OpenScene('castle-data:/starling/character_zombie_atlas.starling-xml' + CurrentUIStarlingSettingsToAnchor); |
| 183 |
end; |
| 184 |
|
| 185 |
procedure TViewMain.ClickButtonOpen2DCocos2d(Sender: TObject); |
| 186 |
begin |
| 187 |
OpenScene('castle-data:/cocos2d/wolf.plist'); |
| 188 |
end; |
| 189 |
|
| 190 |
procedure TViewMain.ClickButtonOpen2DImage(Sender: TObject); |
| 191 |
begin |
| 192 |
{ You can open normal images in TCastleScene, optionaly you can set rect |
| 193 |
from image in anchor (left, bottom, width, height). |
| 194 |
See https://castle-engine.io/using_images . } |
| 195 |
|
| 196 |
{ Full image } |
| 197 |
//OpenScene('castle-data:/starling/character_zombie_atlas.png'); |
| 198 |
//OpenScene('castle-data:/starling/character_zombie_atlas.png#left:0,bottom:0,width:1024,height:2048'); |
| 199 |
|
| 200 |
{ Just first pose from our character_zombie_atlas.png } |
| 201 |
OpenScene('castle-data:/starling/character_zombie_atlas.png#left:0,bottom:1756,width:182,height:259'); |
| 202 |
end; |
| 203 |
|
| 204 |
procedure TViewMain.ClickButtonOpenDialog(Sender: TObject); |
| 205 |
var |
| 206 |
Url: String; |
| 207 |
begin |
| 208 |
Url := Scene.Url; |
| 209 |
if Application.MainWindow.FileDialog('Open model', Url, true, LoadScene_FileFilters) then |
| 210 |
begin |
| 211 |
{ In case of Starling add current settings } |
| 212 |
if UriMimeType(Url) = 'application/x-starling-sprite-sheet' then |
| 213 |
OpenScene(Url + CurrentUIStarlingSettingsToAnchor) |
| 214 |
else |
| 215 |
OpenScene(Url); |
| 216 |
end; |
| 217 |
end; |
| 218 |
|
| 219 |
procedure TViewMain.ClickButtonPlayAnimation(Sender: TObject); |
| 220 |
var |
| 221 |
AnimationName: string; |
| 222 |
Params: TPlayAnimationParameters; |
| 223 |
begin |
| 224 |
AnimationName := (Sender as TCastleButton).Caption; |
| 225 |
Params := TPlayAnimationParameters.Create; |
| 226 |
try |
| 227 |
Params.Name := AnimationName; |
| 228 |
Params.Forward := CheckboxForward.Checked; |
| 229 |
Params.Loop := CheckboxLoop.Checked; |
| 230 |
Params.TransitionDuration := SliderTransition.Value; |
| 231 |
Scene.PlayAnimation(Params); |
| 232 |
finally FreeAndNil(Params) end; |
| 233 |
end; |
| 234 |
|
| 235 |
procedure TViewMain.ChangedScale(Sender: TObject); |
| 236 |
begin |
| 237 |
Scene.Scale := Vector3(SliderScale.Value, SliderScale.Value, SliderScale.Value); |
| 238 |
end; |
| 239 |
|
| 240 |
procedure TViewMain.ChangedTextureMagOptions(Sender: TObject); |
| 241 |
begin |
| 242 |
if CheckboxMagFilterNearest.Checked then |
| 243 |
Scene.RenderOptions.MagnificationFilter := magNearest |
| 244 |
else |
| 245 |
Scene.RenderOptions.MagnificationFilter := magDefault; |
| 246 |
end; |
| 247 |
|
| 248 |
procedure TViewMain.ChangedTextureMinOptions(Sender: TObject); |
| 249 |
begin |
| 250 |
{ There are a lot of other options like: |
| 251 |
- minNearestMipmapNearest, |
| 252 |
- minNearestMipmapLinear, |
| 253 |
- minLinearMipmapNearest, |
| 254 |
- minLinearMipmapLinear, |
| 255 |
- minDefault, |
| 256 |
- minFastest - Alias for minNearest, |
| 257 |
- minNicest - minLinearMipmapLinear, |
| 258 |
|
| 259 |
But to keep example UI simple we change only minNearest and minDefault. } |
| 260 |
|
| 261 |
if CheckboxMinFilterNearest.Checked then |
| 262 |
Scene.RenderOptions.MinificationFilter := minNearest |
| 263 |
else |
| 264 |
Scene.RenderOptions.MinificationFilter := minDefault; |
| 265 |
end; |
| 266 |
|
| 267 |
procedure TViewMain.ChangedStarlingOptions(Sender: TObject); |
| 268 |
var |
| 269 |
AnimationName: String; |
| 270 |
Params: TPlayAnimationParameters; |
| 271 |
begin |
| 272 |
{ This function is only to show that Starling loading options work. |
| 273 |
In real application you would just load the scene with proper "fps" at the start, |
| 274 |
and later change the speed at runtime by adjusting TCastleScene.TimePlayingSpeed, |
| 275 |
without reloading the model. } |
| 276 |
|
| 277 |
{ Check last loaded model was Starling } |
| 278 |
if UriMimeType(URIDeleteAnchor(Scene.URL)) <> 'application/x-starling-sprite-sheet' then |
| 279 |
Exit; |
| 280 |
|
| 281 |
{ Get latest animation } |
| 282 |
if Scene.CurrentAnimation <> nil then |
| 283 |
AnimationName := Scene.CurrentAnimation.X3DName |
| 284 |
else |
| 285 |
AnimationName := ''; |
| 286 |
|
| 287 |
{ Reload model with new settings } |
| 288 |
OpenScene(URIDeleteAnchor(Scene.URL) + CurrentUIStarlingSettingsToAnchor); |
| 289 |
|
| 290 |
{ Re-run animation. } |
| 291 |
if AnimationName <> '' then |
| 292 |
begin |
| 293 |
Params := TPlayAnimationParameters.Create; |
| 294 |
try |
| 295 |
Params.Name := AnimationName; |
| 296 |
Params.Forward := CheckboxForward.Checked; |
| 297 |
Params.Loop := CheckboxLoop.Checked; |
| 298 |
Params.TransitionDuration := SliderTransition.Value; |
| 299 |
Scene.PlayAnimation(Params); |
| 300 |
finally FreeAndNil(Params) end; |
| 301 |
end; |
| 302 |
end; |
| 303 |
|
| 304 |
end. |