1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
{
Copyright 2014-2022 Michalis Kamburelis.
This file is part of "view3dscene".
"view3dscene" is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"view3dscene" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with "view3dscene"; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
----------------------------------------------------------------------------
}
{ UI to play named animations of the current scene, from TCastleScene.Animations. }
unit V3DSceneNamedAnimations;
interface
uses Classes,
CastleControls, CastleScene, CastleUIControls, CastleWindow;
{ Recreate UI to show animations on the scene.
Scene may be @nil, to not show any animations. }
procedure RefreshNamedAnimationsUi(const Window: TCastleWindow;
const Scene: TCastleScene; const WindowMarginTop: Single);
function GetNamedAnimationsUiExists: Boolean;
procedure SetNamedAnimationsUiExists(const Value: Boolean);
property NamedAnimationsUiExists: Boolean
read GetNamedAnimationsUiExists
write SetNamedAnimationsUiExists;
implementation
uses SysUtils, Math,
CastleLog, CastleSceneCore, CastleColors, CastleUtils, X3DNodes,
V3DSceneCaptions;
const
Margin = 10;
{ State preserved across RefreshNamedAnimationsUi ---------------------------- }
var
Loop: boolean = true;
Forward: boolean = true;
MultipleAnimations: boolean = false;
Transition: Single = 0;
{ TButtonAnimation ----------------------------------------------------------- }
type
TButtonAnimation = class(TCastleButton)
strict private
FTimeSensor: TTimeSensorNode;
procedure SetTimeSensor(const Value: TTimeSensorNode);
procedure TimeSensorDestruction(const Node: TX3DNode);
public
AnimationName: String;
{ Store TTimeSensorNode reference, not only AnimationName,
as time sensor better identifies the animation.
Testcase: Bee_10.x3dv that Inlines and IMPORTs animations
from multiple copies of the same glTF file. }
property TimeSensor: TTimeSensorNode read FTimeSensor write SetTimeSensor;
destructor Destroy; override;
end;
procedure TButtonAnimation.TimeSensorDestruction(const Node: TX3DNode);
begin
{ Free TButtonAnimation when associated TimeSensor is freed.
Testcase:
- open demo-models/x3d/data_uri.x3dv
- open "Animations" panel
- click on "Anchor to a model embedded using data URI"
}
FTimeSensor := nil; // no point doing FTimeSensor.RemoveDestructionNotification
Destroy;
end;
destructor TButtonAnimation.Destroy;
begin
TimeSensor := nil;
inherited;
end;
procedure TButtonAnimation.SetTimeSensor(const Value: TTimeSensorNode);
begin
if FTimeSensor <> Value then
begin
if FTimeSensor <> nil then
FTimeSensor.RemoveDestructionNotification(@TimeSensorDestruction);
FTimeSensor := Value;
if FTimeSensor <> nil then
FTimeSensor.AddDestructionNotification(@TimeSensorDestruction);
end;
end;
{ TNamedAnimationsUi and friend classes -------------------------------------- }
type
TNamedAnimationsUi = class(TCastleVerticalGroup)
public
Scene: TCastleScene;
AnimationsScrollView: TCastleScrollView;
AnimationsScrollGroup: TCastleVerticalGroup;
LabelCurrentAnimation: TCastleLabel;
constructor Create(const AOwner: TComponent; const AScene: TCastleScene); reintroduce;
procedure Resize; override;
procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override;
procedure ChangeCheckboxLoop(Sender: TObject);
procedure ChangeCheckboxForward(Sender: TObject);
procedure ChangeSliderTransition(Sender: TObject);
procedure ChangeSliderSpeed(Sender: TObject);
procedure ChangeCheckboxMultipleAnimations(Sender: TObject);
procedure ClickAnimation(Sender: TObject);
procedure ClickResetAnimationState(Sender: TObject);
procedure ClickStopAnimation(Sender: TObject);
end;
constructor TNamedAnimationsUi.Create(const AOwner: TComponent; const AScene: TCastleScene);
procedure AppendLoop;
var
Ui: TCastleCheckbox;
begin
Ui := TCastleCheckbox.Create(Self);
Ui.Caption := 'Loop';
Ui.Checked := Loop;
Ui.OnChange := @ChangeCheckboxLoop;
Ui.TextColor := White;
Ui.CheckboxColor := White;
InsertFront(Ui);
end;
procedure AppendForward;
var
Ui: TCastleCheckbox;
begin
Ui := TCastleCheckbox.Create(Self);
Ui.Caption := 'Forward';
Ui.Checked := Forward;
Ui.OnChange := @ChangeCheckboxForward;
Ui.TextColor := White;
Ui.CheckboxColor := White;
InsertFront(Ui);
end;
procedure AppendMultipleAnimations;
var
Ui: TCastleCheckbox;
begin
Ui := TCastleCheckbox.Create(Self);
Ui.Caption := 'Enable Multiple Simultaneous Animations';
Ui.Checked := MultipleAnimations;
Ui.OnChange := @ChangeCheckboxMultipleAnimations;
Ui.TextColor := White;
Ui.CheckboxColor := White;
InsertFront(Ui);
end;
procedure AppendAnimationsInfo(const AnimationsCount: Cardinal);
var
Ui: TCastleLabel;
begin
Ui := TCastleLabel.Create(Self);
Ui.Caption := Format('%d Animations', [AnimationsCount]);
Ui.Color := White;
InsertFront(Ui);
end;
procedure AppendAnimation(const AnimationName: String; const TimeSensor: TTimeSensorNode);
var
Ui: TButtonAnimation;
begin
Ui := TButtonAnimation.Create(Self);
Ui.Caption := Format('%s (%f)', [
SForCaption(AnimationName),
Scene.AnimationDuration(AnimationName)
]);
Ui.TimeSensor := TimeSensor;
Ui.AnimationName := AnimationName;
Ui.OnClick := @ClickAnimation;
AnimationsScrollGroup.InsertFront(Ui);
end;
procedure AppendSpacer(const Height: Single);
var
Ui: TCastleUserInterface;
begin
Ui := TCastleUserInterface.Create(Self);
Ui.Height := Height;
InsertFront(Ui);
end;
procedure AppendCurrentAnimation;
begin
LabelCurrentAnimation := TCastleLabel.Create(Self);
LabelCurrentAnimation.Caption := 'Current:';
LabelCurrentAnimation.Color := White;
LabelCurrentAnimation.Exists := false; // will be shown in Update if needed
InsertFront(LabelCurrentAnimation);
end;
procedure AppendStopAnimation;
var
Ui: TCastleButton;
begin
Ui := TCastleButton.Create(Self);
Ui.Caption := 'Stop Animation';
Ui.OnClick := @ClickStopAnimation;
InsertFront(Ui);
end;
procedure AppendResetAnimationState;
var
Ui: TCastleButton;
begin
Ui := TCastleButton.Create(Self);
Ui.Caption := 'Reset Animation State';
Ui.OnClick := @ClickResetAnimationState;
InsertFront(Ui);
end;
procedure AppendTransition;
var
Lab: TCastleLabel;
Slider: TCastleFloatSlider;
LabelAndSlider: TCastleHorizontalGroup;
begin
LabelAndSlider := TCastleHorizontalGroup.Create(Self);
LabelAndSlider.Spacing := Margin;
Lab := TCastleLabel.Create(Self);
Lab.Caption := 'Transition:';
Lab.Color := White;
LabelAndSlider.InsertFront(Lab);
Slider := TCastleFloatSlider.Create(Self);
Slider.Min := 0;
Slider.Max := 5;
Slider.Value := Transition;
Slider.OnChange := @ChangeSliderTransition;
LabelAndSlider.InsertFront(Slider);
InsertFront(LabelAndSlider);
end;
procedure AppendSpeed;
var
Lab: TCastleLabel;
Slider: TCastleFloatSlider;
LabelAndSlider: TCastleHorizontalGroup;
begin
LabelAndSlider := TCastleHorizontalGroup.Create(Self);
LabelAndSlider.Spacing := Margin;
Lab := TCastleLabel.Create(Self);
Lab.Caption := 'Playback Speed:';
Lab.Color := White;
LabelAndSlider.InsertFront(Lab);
Slider := TCastleFloatSlider.Create(Self);
Slider.Min := 0;
Slider.Max := 10;
{ We use Scene.TimePlayingSpeed to preserve playing speed when scene changes.
(view3dscene doesn't create new Scene each time, it only calls Load on one
Scene instance). }
if Scene <> nil then
Slider.Value := Scene.TimePlayingSpeed
else
Slider.Value := 1;
Slider.OnChange := @ChangeSliderSpeed;
LabelAndSlider.InsertFront(Slider);
InsertFront(LabelAndSlider);
end;
procedure CreateScrollView;
begin
{ We place animations in TCastleScrollView,
in case we have too many animations to fit on screen. }
AnimationsScrollView := TCastleScrollView.Create(Self);
AnimationsScrollView.ScrollArea.AutoSizeToChildren := true;
InsertFront(AnimationsScrollView);
AnimationsScrollGroup := TCastleVerticalGroup.Create(Self);
AnimationsScrollGroup.Spacing := Spacing; // copy own Spacing
AnimationsScrollView.ScrollArea.InsertFront(AnimationsScrollGroup);
end;
var
NamedAnimations: TStrings;
I: Integer;
begin
inherited Create(AOwner);
Scene := AScene;
Frame := true;
Padding := Margin;
Spacing := 4;
if Scene <> nil then
begin
AppendLoop;
AppendForward;
AppendMultipleAnimations;
AppendTransition;
AppendSpeed;
AppendSpacer(Margin);
NamedAnimations := Scene.AnimationsList;
AppendAnimationsInfo(NamedAnimations.Count);
if NamedAnimations.Count <> 0 then
begin
CreateScrollView;
for I := 0 to NamedAnimations.Count - 1 do
AppendAnimation(NamedAnimations[I], Scene.AnimationTimeSensor(I));
AppendSpacer(Margin);
AppendStopAnimation;
AppendResetAnimationState;
{ Since it dynamically appears / disappears, it's best to show it last,
so that it doesn't shift the remaining UI when disappearing. }
AppendCurrentAnimation;
end;
end;
{ The state with Scene = nil is never visible by user in practice,
so we don't bother to make it look nice with some label 'open some scene'. }
end;
procedure TNamedAnimationsUi.Resize;
procedure UpdateScrollViewSize;
const
{ It is easier to set it experimentally than to calculate from code, for now }
HeightForRestOfUi = 470;
begin
if AnimationsScrollView <> nil then
begin
Assert(Container <> nil);
AnimationsScrollView.Width := AnimationsScrollGroup.EffectiveWidth
+ AnimationsScrollView.EffectiveScrollBarWidth;
AnimationsScrollView.Height := Max(0, Min(AnimationsScrollGroup.EffectiveHeight,
Container.UnscaledHeight - HeightForRestOfUi));
end;
end;
begin
inherited;
UpdateScrollViewSize;
end;
procedure TNamedAnimationsUi.Update(const SecondsPassed: Single; var HandleInput: boolean);
var
TimeSensor: TTimeSensorNode;
C: TCastleUserInterface;
begin
inherited;
// LabelCurrentAnimation is nil <=> Scene is nil
if LabelCurrentAnimation <> nil then
begin
Assert(Scene <> nil);
LabelCurrentAnimation.Exists := Scene.CurrentAnimation <> nil;
if Scene.CurrentAnimation <> nil then
begin
LabelCurrentAnimation.Caption := Format('Current:' + NL + '%s' + NL + '%f / %f', [
{ We write animation name on a separate line,
this way we know it will fit within AnimationsScrollView width,
without the need to resize it. }
Scene.CurrentAnimation.X3DName,
Scene.CurrentAnimation.ElapsedTimeInCycle,
Scene.CurrentAnimation.CycleInterval
]);
end;
end;
{ AnimationsScrollGroup doesn't exist if no animations.
Testcase:
- open glTF Drone animation,
- play animation by clicking button in Animations panel,
- load glTF DamagedHelmet through recent menu }
if AnimationsScrollGroup <> nil then
for C in AnimationsScrollGroup do
if C is TButtonAnimation then
begin
Assert(Scene <> nil); // no TButtonAnimation should exist if Scene = nil
TimeSensor := TButtonAnimation(C).TimeSensor;
TButtonAnimation(C).Pressed := TimeSensor.IsActive;
end;
end;
procedure TNamedAnimationsUi.ClickAnimation(Sender: TObject);
var
Button: TButtonAnimation;
AnimationName: String;
Params: TPlayAnimationParameters;
TimeSensor: TTimeSensorNode;
begin
Button := Sender as TButtonAnimation;
AnimationName := Button.AnimationName;
if MultipleAnimations then
begin
TimeSensor := Button.TimeSensor;
if TimeSensor.IsActive then
TimeSensor.Stop
else
TimeSensor.Start(Loop, Forward);
end else
begin
Params := TPlayAnimationParameters.Create;
try
Params.Name := AnimationName;
Params.Loop := Loop;
Params.Forward := Forward;
Params.TransitionDuration := Transition;
if not Scene.PlayAnimation(Params) then
WritelnWarning('Named Animations', Format('Animation "%s" no longer exists, it was removed from scene since loading',
[AnimationName]));
finally FreeAndNil(Params) end;
end;
end;
procedure TNamedAnimationsUi.ChangeCheckboxLoop(Sender: TObject);
begin
// change future animations
Loop := (Sender as TCastleCheckbox).Checked;
// also change currently playing animation, if any
if Scene.CurrentAnimation <> nil then
Scene.CurrentAnimation.Loop := Loop;
end;
procedure TNamedAnimationsUi.ChangeCheckboxForward(Sender: TObject);
begin
inherited;
// change future animations
Forward := (Sender as TCastleCheckbox).Checked;
// also change currently playing animation, if any
if Scene.CurrentAnimation <> nil then
Scene.CurrentAnimation.FractionIncreasing := Forward;
end;
procedure TNamedAnimationsUi.ChangeSliderTransition(Sender: TObject);
begin
Transition := (Sender as TCastleFloatSlider).Value;
end;
procedure TNamedAnimationsUi.ChangeSliderSpeed(Sender: TObject);
begin
if Scene <> nil then
Scene.TimePlayingSpeed := (Sender as TCastleFloatSlider).Value;
end;
procedure TNamedAnimationsUi.ChangeCheckboxMultipleAnimations(Sender: TObject);
begin
MultipleAnimations := (Sender as TCastleCheckbox).Checked;
end;
procedure TNamedAnimationsUi.ClickResetAnimationState(Sender: TObject);
begin
Scene.ResetAnimationState;
end;
procedure TNamedAnimationsUi.ClickStopAnimation(Sender: TObject);
begin
Scene.StopAnimation;
end;
{ RefreshNamedAnimations ----------------------------------------------------- }
var
NamedAnimationsUi: TNamedAnimationsUi;
procedure RefreshNamedAnimationsUi(const Window: TCastleWindow;
const Scene: TCastleScene; const WindowMarginTop: Single);
var
OldNamedAnimationsExists: Boolean;
begin
OldNamedAnimationsExists := (NamedAnimationsUi <> nil) and NamedAnimationsUi.Exists;
{ free previous UI owned by NamedAnimationsUi, including previous animations }
FreeAndNil(NamedAnimationsUi);
NamedAnimationsUi := TNamedAnimationsUi.Create(Window, Scene);
NamedAnimationsUi.Exists := OldNamedAnimationsExists;
NamedAnimationsUi.Anchor(hpLeft, Margin);
NamedAnimationsUi.Anchor(vpTop, - WindowMarginTop - Margin);
Window.Controls.InsertFront(NamedAnimationsUi);
end;
function GetNamedAnimationsUiExists: Boolean;
begin
Result := (NamedAnimationsUi <> nil) and NamedAnimationsUi.Exists;
end;
procedure SetNamedAnimationsUiExists(const Value: Boolean);
begin
NamedAnimationsUi.Exists := Value;
end;
end.
|