| 1 |
{ |
| 2 |
Copyright 2022-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 |
{ Game options, just volume for now. } |
| 17 |
unit GameViewOptions; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleUIControls, CastleControls, CastleKeysMouse, |
| 23 |
CastleViewport, CastleSoundEngine; |
| 24 |
|
| 25 |
type |
| 26 |
TViewOptions = class(TCastleView) |
| 27 |
published |
| 28 |
{ Components designed using CGE editor. |
| 29 |
These fields will be automatically initialized at Start. } |
| 30 |
ButtonBackMenu, ButtonBackGame: TCastleButton; |
| 31 |
Fade: TCastleRectangleControl; |
| 32 |
SliderVolume: TCastleIntegerSlider; |
| 33 |
private |
| 34 |
procedure ClickBackMenu(Sender: TObject); |
| 35 |
procedure ClickBackGame(Sender: TObject); |
| 36 |
procedure ChangeSliderVolume(Sender: TObject); |
| 37 |
public |
| 38 |
{ Whether this is displayed on top of ViewPlay |
| 39 |
or not (in which case this goes back to ViewMenu). } |
| 40 |
OverGame: Boolean; |
| 41 |
constructor Create(AOwner: TComponent); override; |
| 42 |
procedure Start; override; |
| 43 |
procedure Stop; override; |
| 44 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 45 |
end; |
| 46 |
|
| 47 |
var |
| 48 |
ViewOptions: TViewOptions; |
| 49 |
|
| 50 |
implementation |
| 51 |
|
| 52 |
uses SysUtils, |
| 53 |
CastleLog, CastleConfig, |
| 54 |
GameViewMenu, GameViewportUnderUi; |
| 55 |
|
| 56 |
constructor TViewOptions.Create(AOwner: TComponent); |
| 57 |
begin |
| 58 |
inherited; |
| 59 |
DesignUrl := 'castle-data:/gameviewoptions.castle-user-interface'; |
| 60 |
DesignPreload := true; // make it fast to transition to this view |
| 61 |
end; |
| 62 |
|
| 63 |
procedure TViewOptions.Start; |
| 64 |
begin |
| 65 |
inherited; |
| 66 |
ButtonBackMenu.OnClick := {$ifdef FPC}@{$endif} ClickBackMenu; |
| 67 |
ButtonBackGame.OnClick := {$ifdef FPC}@{$endif} ClickBackGame; |
| 68 |
|
| 69 |
SliderVolume.Value := Round(SoundEngine.Volume * SliderVolume.Max); |
| 70 |
SliderVolume.OnChange := {$ifdef FPC}@{$endif} ChangeSliderVolume; |
| 71 |
|
| 72 |
if not OverGame then |
| 73 |
InsertBack(ViewportUnderUi); |
| 74 |
|
| 75 |
Fade.Exists := true; // looks bette also when from main menu |
| 76 |
ButtonBackGame.Exists := OverGame; |
| 77 |
|
| 78 |
if OverGame then |
| 79 |
ButtonBackMenu.Caption := 'BACK TO MENU (ABORT THE GAME)' |
| 80 |
else |
| 81 |
ButtonBackMenu.Caption := 'BACK TO MENU'; |
| 82 |
end; |
| 83 |
|
| 84 |
procedure TViewOptions.Stop; |
| 85 |
begin |
| 86 |
RemoveControl(ViewportUnderUi); |
| 87 |
inherited; |
| 88 |
end; |
| 89 |
|
| 90 |
procedure TViewOptions.ClickBackMenu(Sender: TObject); |
| 91 |
begin |
| 92 |
Container.View := ViewMenu; |
| 93 |
end; |
| 94 |
|
| 95 |
procedure TViewOptions.ClickBackGame(Sender: TObject); |
| 96 |
begin |
| 97 |
Container.PopView(Self); |
| 98 |
end; |
| 99 |
|
| 100 |
procedure TViewOptions.ChangeSliderVolume(Sender: TObject); |
| 101 |
begin |
| 102 |
SoundEngine.Volume := SliderVolume.Value / SliderVolume.Max; |
| 103 |
|
| 104 |
{ save volume to UserConfig, to make it saved for next game run } |
| 105 |
UserConfig.SetDeleteFloat('sound_volume', SoundEngine.Volume, 1); |
| 106 |
UserConfig.Save; |
| 107 |
end; |
| 108 |
|
| 109 |
function TViewOptions.Press(const Event: TInputPressRelease): Boolean; |
| 110 |
begin |
| 111 |
Result := inherited; |
| 112 |
if Result then Exit; // allow the ancestor to handle keys |
| 113 |
|
| 114 |
if Event.IsKey(keyEscape) then |
| 115 |
begin |
| 116 |
{ on Escape, go back to the most natural place, depending on OverGame } |
| 117 |
if OverGame then |
| 118 |
ClickBackGame(nil) |
| 119 |
else |
| 120 |
ClickBackMenu(nil); |
| 121 |
Exit(true); |
| 122 |
end; |
| 123 |
end; |
| 124 |
|
| 125 |
end. |