| 1 |
{ |
| 2 |
Copyright 2019-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 |
{ Main user interface class. |
| 17 |
This implements the majority of this application functionality. } |
| 18 |
unit GameViewMain; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses Classes, Generics.Collections, |
| 23 |
CastleUIControls, CastleComponentSerialize, CastleControls, CastleSoundEngine; |
| 24 |
|
| 25 |
type |
| 26 |
{ Main view, with the main application logic. } |
| 27 |
TViewMain = class(TCastleView) |
| 28 |
published |
| 29 |
{ Components designed using CGE editor. |
| 30 |
These fields will be automatically initialized at Start. } |
| 31 |
LabelPlayingSounds: TCastleLabel; |
| 32 |
GroupSoundBuffers, GroupPlayingSounds: TCastleVerticalGroup; |
| 33 |
ButtonExit: TCastleButton; |
| 34 |
private |
| 35 |
type |
| 36 |
TButtonSound = class(TCastleButton) |
| 37 |
public |
| 38 |
Sound: TCastleSound; |
| 39 |
constructor Create(const AOwner: TComponent; const SoundFileUrl: String); reintroduce; |
| 40 |
end; |
| 41 |
|
| 42 |
TPlayingSoundUiOwner = class(TComponent) |
| 43 |
strict private |
| 44 |
SliderSoundVolume, SliderSoundPitch: TCastleFloatSlider; |
| 45 |
CheckboxLoop: TCastleCheckbox; |
| 46 |
procedure ClickStop(Sender: TObject); |
| 47 |
procedure ChangeSliderSoundVolume(Sender: TObject); |
| 48 |
procedure ChangeSliderSoundPitch(Sender: TObject); |
| 49 |
procedure ChangeCheckboxLoop(Sender: TObject); |
| 50 |
public |
| 51 |
PlayingSound: TCastlePlayingSound; |
| 52 |
constructor Create(const AOwner: TComponent; const APlayingSound: TCastlePlayingSound; |
| 53 |
const UiFactory: TCastleComponentFactory; |
| 54 |
const GroupPlayingSounds: TCastleVerticalGroup); reintroduce; |
| 55 |
end; |
| 56 |
|
| 57 |
TPlayingSoundUiOwnerList = {$ifdef FPC}specialize{$endif} TObjectList<TPlayingSoundUiOwner>; |
| 58 |
|
| 59 |
var |
| 60 |
PlayingSoundUiFactory: TCastleComponentFactory; |
| 61 |
PlayingSoundUiOwners: TPlayingSoundUiOwnerList; |
| 62 |
procedure ClickExit(Sender: TObject); |
| 63 |
procedure ClickPlayBuffer(Sender: TObject); |
| 64 |
procedure PlayingSoundStop(Sender: TObject); |
| 65 |
public |
| 66 |
constructor Create(AOwner: TComponent); override; |
| 67 |
procedure Start; override; |
| 68 |
procedure Stop; override; |
| 69 |
end; |
| 70 |
|
| 71 |
var |
| 72 |
ViewMain: TViewMain; |
| 73 |
|
| 74 |
implementation |
| 75 |
|
| 76 |
uses SysUtils, |
| 77 |
CastleLog, CastleWindow, CastleUriUtils, CastleTimeUtils, |
| 78 |
CastleSoundBase, CastleViewport, CastleUtils; |
| 79 |
|
| 80 |
{ TButtonSound --------------------------------------------------------- } |
| 81 |
|
| 82 |
constructor TViewMain.TButtonSound.Create(const AOwner: TComponent; |
| 83 |
const SoundFileUrl: String); |
| 84 |
begin |
| 85 |
inherited Create(AOwner); |
| 86 |
Sound := TCastleSound.Create(Self); |
| 87 |
{ Uncomment this to allow streaming loading. |
| 88 |
This means that sound file will be loaded partially, on-demand, |
| 89 |
instead of being loaded to memory all at once. |
| 90 |
|
| 91 |
The upside is much faster initialization (loading of a sound file |
| 92 |
with slStreaming is almost instant, even for large files). |
| 93 |
|
| 94 |
The downside is a possible additional work at run-time |
| 95 |
(but it's done in a thread and should not matter in normal use-cases). |
| 96 |
} |
| 97 |
// Sound.Stream := true; |
| 98 |
Sound.Url := SoundFileUrl; |
| 99 |
|
| 100 |
Caption := FormatDot('%s (%f)', [ |
| 101 |
// extract last URL component, i.e. just the filename |
| 102 |
UriDisplay(SoundFileUrl, true), |
| 103 |
Sound.Duration |
| 104 |
]); |
| 105 |
end; |
| 106 |
|
| 107 |
{ TPlayingSoundUiOwner ---------------------------------------------------------- } |
| 108 |
|
| 109 |
constructor TViewMain.TPlayingSoundUiOwner.Create(const AOwner: TComponent; |
| 110 |
const APlayingSound: TCastlePlayingSound; |
| 111 |
const UiFactory: TCastleComponentFactory; |
| 112 |
const GroupPlayingSounds: TCastleVerticalGroup); |
| 113 |
var |
| 114 |
Ui: TCastleUserInterface; |
| 115 |
LabelSoundName: TCastleLabel; |
| 116 |
ButtonStop: TCastleButton; |
| 117 |
begin |
| 118 |
inherited Create(AOwner); |
| 119 |
PlayingSound := APlayingSound; |
| 120 |
|
| 121 |
// use Self as Owner of Ui, so below we just call Self.FindRequiredComponent |
| 122 |
Ui := UiFactory.UserInterfaceLoad(Self); |
| 123 |
GroupPlayingSounds.InsertFront(Ui); |
| 124 |
|
| 125 |
LabelSoundName := FindRequiredComponent('LabelSoundName') as TCastleLabel; |
| 126 |
LabelSoundName.Caption := UriDisplay(PlayingSound.Sound.Url, true); |
| 127 |
|
| 128 |
ButtonStop := FindRequiredComponent('ButtonStop') as TCastleButton; |
| 129 |
ButtonStop.OnClick := {$ifdef FPC}@{$endif}ClickStop; |
| 130 |
|
| 131 |
SliderSoundVolume := FindRequiredComponent('SliderSoundVolume') as TCastleFloatSlider; |
| 132 |
SliderSoundVolume.Value := PlayingSound.Volume; |
| 133 |
SliderSoundVolume.OnChange := {$ifdef FPC}@{$endif}ChangeSliderSoundVolume; |
| 134 |
|
| 135 |
SliderSoundPitch := FindRequiredComponent('SliderSoundPitch') as TCastleFloatSlider; |
| 136 |
SliderSoundPitch.Value := PlayingSound.Pitch; |
| 137 |
SliderSoundPitch.OnChange := {$ifdef FPC}@{$endif}ChangeSliderSoundPitch; |
| 138 |
|
| 139 |
CheckboxLoop := FindRequiredComponent('CheckboxLoop') as TCastleCheckbox; |
| 140 |
CheckboxLoop.Checked := PlayingSound.Loop; |
| 141 |
CheckboxLoop.OnChange := {$ifdef FPC}@{$endif}ChangeCheckboxLoop; |
| 142 |
end; |
| 143 |
|
| 144 |
procedure TViewMain.TPlayingSoundUiOwner.ClickStop(Sender: TObject); |
| 145 |
begin |
| 146 |
PlayingSound.Stop; // this will also call PlayingSoundStop |
| 147 |
end; |
| 148 |
|
| 149 |
procedure TViewMain.TPlayingSoundUiOwner.ChangeSliderSoundVolume(Sender: TObject); |
| 150 |
begin |
| 151 |
PlayingSound.Volume := SliderSoundVolume.Value; |
| 152 |
end; |
| 153 |
|
| 154 |
procedure TViewMain.TPlayingSoundUiOwner.ChangeSliderSoundPitch(Sender: TObject); |
| 155 |
begin |
| 156 |
PlayingSound.Pitch := SliderSoundPitch.Value; |
| 157 |
end; |
| 158 |
|
| 159 |
procedure TViewMain.TPlayingSoundUiOwner.ChangeCheckboxLoop(Sender: TObject); |
| 160 |
begin |
| 161 |
PlayingSound.Loop := CheckboxLoop.Checked; |
| 162 |
end; |
| 163 |
|
| 164 |
{ TViewMain ----------------------------------------------------------------- } |
| 165 |
|
| 166 |
constructor TViewMain.Create(AOwner: TComponent); |
| 167 |
begin |
| 168 |
inherited; |
| 169 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 170 |
end; |
| 171 |
|
| 172 |
procedure TViewMain.Start; |
| 173 |
|
| 174 |
procedure AddSoundBufferButton(const SoundFileUrl: String); |
| 175 |
var |
| 176 |
Button: TButtonSound; |
| 177 |
begin |
| 178 |
try |
| 179 |
Button := TButtonSound.Create(FreeAtStop, SoundFileUrl); |
| 180 |
except |
| 181 |
on E: Exception do |
| 182 |
begin |
| 183 |
WritelnWarning('Loading of sound file "%s" failed: %s', |
| 184 |
[SoundFileUrl, E.Message]); |
| 185 |
Exit; |
| 186 |
end; |
| 187 |
end; |
| 188 |
Button.OnClick := {$ifdef FPC}@{$endif}ClickPlayBuffer; |
| 189 |
GroupSoundBuffers.InsertFront(Button); |
| 190 |
end; |
| 191 |
|
| 192 |
begin |
| 193 |
inherited; |
| 194 |
|
| 195 |
PlayingSoundUiOwners := TPlayingSoundUiOwnerList.Create(false); |
| 196 |
|
| 197 |
LabelPlayingSounds.Caption := Format('Currently playing sounds (max %d):', |
| 198 |
[SoundEngine.MaxAllocatedSources]); |
| 199 |
ButtonExit.OnClick := {$ifdef FPC}@{$endif}ClickExit; |
| 200 |
|
| 201 |
{ List the sound files to load. |
| 202 |
Hint: We could also use FindFiles from CastleFindFiles unit to automatically |
| 203 |
scan the directory for files. } |
| 204 |
AddSoundBufferButton('castle-data:/sounds/beating_that_thing-22000Hz-16bit-stereo.ogg'); |
| 205 |
AddSoundBufferButton('castle-data:/sounds/beating_that_thing-44100Hz-16bit-stereo.ogg'); |
| 206 |
AddSoundBufferButton('castle-data:/sounds/misc_sound-22000Hz-8bit-mono.wav'); |
| 207 |
AddSoundBufferButton('castle-data:/sounds/misc_sound-44100Hz-8bit-mono.wav'); |
| 208 |
AddSoundBufferButton('castle-data:/sounds/negative-44100Hz-8bit-stereo.wav'); |
| 209 |
AddSoundBufferButton('castle-data:/sounds/positive-44100Hz-16bit-mono.wav'); |
| 210 |
AddSoundBufferButton('castle-data:/sounds/save-44100Hz-16bit-stereo.wav'); |
| 211 |
AddSoundBufferButton('castle-data:/sounds/stereo_test.wav'); |
| 212 |
AddSoundBufferButton('castle-data:/sounds/temple_adam_goh-44000Hz-16bit-mono.ogg'); |
| 213 |
|
| 214 |
PlayingSoundUiFactory := TCastleComponentFactory.Create(FreeAtStop); |
| 215 |
PlayingSoundUiFactory.Url := 'castle-data:/part_playing_sound.castle-user-interface'; |
| 216 |
end; |
| 217 |
|
| 218 |
procedure TViewMain.Stop; |
| 219 |
begin |
| 220 |
FreeAndNil(PlayingSoundUiOwners); |
| 221 |
inherited; |
| 222 |
end; |
| 223 |
|
| 224 |
procedure TViewMain.ClickExit(Sender: TObject); |
| 225 |
begin |
| 226 |
Application.Terminate; |
| 227 |
end; |
| 228 |
|
| 229 |
procedure TViewMain.ClickPlayBuffer(Sender: TObject); |
| 230 |
var |
| 231 |
SenderButton: TButtonSound; |
| 232 |
PlayingSound: TCastlePlayingSound; |
| 233 |
PlayingSoundUiOwner: TPlayingSoundUiOwner; |
| 234 |
begin |
| 235 |
inherited; |
| 236 |
SenderButton := Sender as TButtonSound; |
| 237 |
|
| 238 |
{ Note: by freeing TCastlePlayingSound at state stop (using FreeAtStop) |
| 239 |
we make sure sound stops at state Stop too. } |
| 240 |
PlayingSound := TCastlePlayingSound.Create(FreeAtStop); |
| 241 |
PlayingSound.FreeOnStop := true; |
| 242 |
PlayingSound.Sound := SenderButton.Sound; |
| 243 |
{ It's better to make PlayingSoundStop a method of TViewMain, |
| 244 |
not TPlayingSoundUiOwner, because when it occurs the whole instance |
| 245 |
of TPlayingSoundUiOwner (along with the UI) should be destroyed. } |
| 246 |
PlayingSound.OnStop := {$ifdef FPC}@{$endif}PlayingSoundStop; |
| 247 |
SoundEngine.Play(PlayingSound); |
| 248 |
|
| 249 |
PlayingSoundUiOwner := TPlayingSoundUiOwner.Create(FreeAtStop, PlayingSound, |
| 250 |
PlayingSoundUiFactory, GroupPlayingSounds); |
| 251 |
PlayingSoundUiOwners.Add(PlayingSoundUiOwner); |
| 252 |
end; |
| 253 |
|
| 254 |
procedure TViewMain.PlayingSoundStop(Sender: TObject); |
| 255 |
var |
| 256 |
PlayingSoundUiOwner: TPlayingSoundUiOwner; |
| 257 |
begin |
| 258 |
{ This may happen when TCastlePlayingSound is freed by FreeAtStop, |
| 259 |
when our PlayingSoundUiOwners is freed. So secure from it. |
| 260 |
Testcase: start some longer sound, and then just close application by Alt+F4. } |
| 261 |
if PlayingSoundUiOwners = nil then |
| 262 |
Exit; |
| 263 |
|
| 264 |
for PlayingSoundUiOwner in PlayingSoundUiOwners do |
| 265 |
if PlayingSoundUiOwner.PlayingSound = Sender then |
| 266 |
begin |
| 267 |
PlayingSoundUiOwners.Remove(PlayingSoundUiOwner); |
| 268 |
// This frees TPlayingSoundUiOwner, along with UI |
| 269 |
PlayingSoundUiOwner.Free; |
| 270 |
Break; |
| 271 |
end; |
| 272 |
end; |
| 273 |
|
| 274 |
end. |