| 1 |
{ |
| 2 |
Copyright 2007-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 |
{ View displaying main on-screen menu (TViewMainMenu). } |
| 17 |
unit GameViewMainMenu; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleUIControls, CastleOnScreenMenu, CastleControls, CastleKeysMouse; |
| 23 |
|
| 24 |
type |
| 25 |
{ View displaying main on-screen menu. } |
| 26 |
TViewMainMenu = class(TCastleView) |
| 27 |
private |
| 28 |
type |
| 29 |
TAbstractMenu = class(TCastleOnScreenMenu) |
| 30 |
public |
| 31 |
constructor Create(AOwner: TComponent); override; |
| 32 |
end; |
| 33 |
|
| 34 |
TRiftMainMenu = class(TAbstractMenu) |
| 35 |
strict private |
| 36 |
procedure ClickIntro(Sender: TObject); |
| 37 |
procedure ClickNewGame(Sender: TObject); |
| 38 |
procedure ClickSoundOptions(Sender: TObject); |
| 39 |
procedure ClickQuit(Sender: TObject); |
| 40 |
public |
| 41 |
constructor Create(AOwner: TComponent); override; |
| 42 |
end; |
| 43 |
|
| 44 |
TAbstractSubMenu = class(TAbstractMenu) |
| 45 |
public |
| 46 |
constructor Create(AOwner: TComponent); override; |
| 47 |
end; |
| 48 |
|
| 49 |
TRiftSoundMenu = class(TAbstractSubMenu) |
| 50 |
strict private |
| 51 |
procedure ClickChangeDevice(Sender: TObject); |
| 52 |
procedure ClickBack(Sender: TObject); |
| 53 |
public |
| 54 |
SoundDeviceArgument: TCastleOnScreenMenuItem; |
| 55 |
constructor Create(AOwner: TComponent); override; |
| 56 |
end; |
| 57 |
|
| 58 |
TSoundDeviceMenu = class(TAbstractSubMenu) |
| 59 |
strict private |
| 60 |
procedure ClickBack(Sender: TObject); |
| 61 |
public |
| 62 |
constructor Create(AOwner: TComponent); override; |
| 63 |
end; |
| 64 |
|
| 65 |
var |
| 66 |
MainMenu: TRiftMainMenu; |
| 67 |
SoundMenu: TRiftSoundMenu; |
| 68 |
SoundDeviceMenu: TSoundDeviceMenu; |
| 69 |
CurrentMenu: TAbstractMenu; |
| 70 |
MenuBg: TCastleImageControl; |
| 71 |
procedure SetCurrentMenu(const NewValue: TAbstractMenu); |
| 72 |
public |
| 73 |
procedure Start; override; |
| 74 |
procedure Stop; override; |
| 75 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 76 |
end; |
| 77 |
|
| 78 |
var |
| 79 |
ViewMainMenu: TViewMainMenu; |
| 80 |
|
| 81 |
implementation |
| 82 |
|
| 83 |
uses SysUtils, |
| 84 |
CastleFilesUtils, CastleMessages, CastleWindow, CastleUtils, |
| 85 |
CastleVectors, CastleSoundMenu, CastleStringUtils, |
| 86 |
CastleGLImages, CastleColors, CastleSoundEngine, |
| 87 |
CastleApplicationProperties, CastleRectangles, |
| 88 |
GameConfiguration, GameSound, GameViewPlay, GameLocations, GameViewIntro; |
| 89 |
|
| 90 |
{ TViewMainMenu.TAbstractMenu ------------------------------------------------------------------ } |
| 91 |
|
| 92 |
constructor TViewMainMenu.TAbstractMenu.Create(AOwner: TComponent); |
| 93 |
begin |
| 94 |
inherited; |
| 95 |
CurrentItemBorderColor1 := Black; |
| 96 |
CurrentItemBorderColor2 := Vector4(186/255, 134/255, 88/255, 1.0); |
| 97 |
CurrentItemColor := Vector4(252/255, 253/255, 200/255, 1.0); |
| 98 |
NonCurrentItemColor := CurrentItemBorderColor2; |
| 99 |
|
| 100 |
Anchor(hpRight, -100); |
| 101 |
Anchor(vpTop, -100); |
| 102 |
|
| 103 |
DrawBackgroundRectangle := false; |
| 104 |
CaptureAllEvents := true; |
| 105 |
DrawFocusedBorder := false; |
| 106 |
|
| 107 |
{ Assign TCastleSound instances loaded in GameSound unit. } |
| 108 |
SoundClick := AllSounds.SoundMenuClick; |
| 109 |
SoundCurrentItemChanged := AllSounds.SoundMenuCurrentItemChanged; |
| 110 |
end; |
| 111 |
|
| 112 |
{ TViewMainMenu.TRiftMainMenu -------------------------------------------------------------- } |
| 113 |
|
| 114 |
constructor TViewMainMenu.TRiftMainMenu.Create(AOwner: TComponent); |
| 115 |
begin |
| 116 |
inherited; |
| 117 |
Add('New Game', {$ifdef FPC}@{$endif} ClickNewGame); |
| 118 |
Add('Replay Intro', {$ifdef FPC}@{$endif} ClickIntro); |
| 119 |
Add('Sound Options', {$ifdef FPC}@{$endif} ClickSoundOptions); |
| 120 |
{ on mobile, do not show quit -- users don't expect it, |
| 121 |
and also Application.Terminate cannot be used on iOS and Android. } |
| 122 |
if not ApplicationProperties.TouchDevice then |
| 123 |
Add('Quit', {$ifdef FPC}@{$endif} ClickQuit); |
| 124 |
end; |
| 125 |
|
| 126 |
procedure TViewMainMenu.TRiftMainMenu.ClickIntro(Sender: TObject); |
| 127 |
begin |
| 128 |
Container.View := ViewIntro; |
| 129 |
end; |
| 130 |
|
| 131 |
procedure TViewMainMenu.TRiftMainMenu.ClickNewGame(Sender: TObject); |
| 132 |
begin |
| 133 |
Container.View := ViewPlay; |
| 134 |
end; |
| 135 |
|
| 136 |
procedure TViewMainMenu.TRiftMainMenu.ClickSoundOptions(Sender: TObject); |
| 137 |
begin |
| 138 |
ViewMainMenu.SetCurrentMenu(ViewMainMenu.SoundMenu); |
| 139 |
end; |
| 140 |
|
| 141 |
procedure TViewMainMenu.TRiftMainMenu.ClickQuit(Sender: TObject); |
| 142 |
begin |
| 143 |
Application.Terminate; |
| 144 |
end; |
| 145 |
|
| 146 |
{ TViewMainMenu.TAbstractSubMenu --------------------------------------------------------------- } |
| 147 |
|
| 148 |
constructor TViewMainMenu.TAbstractSubMenu.Create(AOwner: TComponent); |
| 149 |
begin |
| 150 |
inherited; |
| 151 |
end; |
| 152 |
|
| 153 |
{ TViewMainMenu.TRiftSoundMenu ------------------------------------------------------------- } |
| 154 |
|
| 155 |
constructor TViewMainMenu.TRiftSoundMenu.Create(AOwner: TComponent); |
| 156 |
begin |
| 157 |
inherited; |
| 158 |
|
| 159 |
SoundDeviceArgument := TCastleOnScreenMenuItem.Create(Self); |
| 160 |
SoundDeviceArgument.Caption := 'Sound output device'; |
| 161 |
SoundDeviceArgument.RightCaption := SoundEngine.DeviceCaption; |
| 162 |
SoundDeviceArgument.OnClick := {$ifdef FPC}@{$endif} ClickChangeDevice; |
| 163 |
|
| 164 |
Add('Sound options:'); |
| 165 |
Add(TSoundInfoMenuItem.Create(Self)); |
| 166 |
Add(TSoundVolumeMenuItem.Create(Self)); |
| 167 |
Add(TMusicVolumeMenuItem.Create(Self)); |
| 168 |
Add(SoundDeviceArgument); |
| 169 |
Add('Back to main menu', {$ifdef FPC}@{$endif} ClickBack); |
| 170 |
|
| 171 |
// select item 1 as default, because item 0 is the label |
| 172 |
CurrentItem := 1; |
| 173 |
end; |
| 174 |
|
| 175 |
procedure TViewMainMenu.TRiftSoundMenu.ClickChangeDevice(Sender: TObject); |
| 176 |
begin |
| 177 |
ViewMainMenu.SetCurrentMenu(ViewMainMenu.SoundDeviceMenu); |
| 178 |
end; |
| 179 |
|
| 180 |
procedure TViewMainMenu.TRiftSoundMenu.ClickBack(Sender: TObject); |
| 181 |
begin |
| 182 |
ViewMainMenu.SetCurrentMenu(ViewMainMenu.MainMenu); |
| 183 |
end; |
| 184 |
|
| 185 |
{ TSoundDeviceMenuButton ---------------------------------------------------- } |
| 186 |
|
| 187 |
type |
| 188 |
TSoundDeviceMenuButton = class(TCastleOnScreenMenuItem) |
| 189 |
public |
| 190 |
Device: TSoundDevice; |
| 191 |
procedure DoClick; override; |
| 192 |
end; |
| 193 |
|
| 194 |
procedure TSoundDeviceMenuButton.DoClick; |
| 195 |
begin |
| 196 |
inherited; |
| 197 |
|
| 198 |
SoundEngine.Device := Device.Name; |
| 199 |
ViewMainMenu.SoundMenu.SoundDeviceArgument.RightCaption := SoundEngine.DeviceCaption; |
| 200 |
if not SoundEngine.IsContextOpenSuccess then |
| 201 |
MessageOK(Application.MainWindow, SoundEngine.Information); |
| 202 |
|
| 203 |
ViewMainMenu.SetCurrentMenu(ViewMainMenu.SoundMenu); |
| 204 |
end; |
| 205 |
|
| 206 |
{ TViewMainMenu.TSoundDeviceMenu ---------------------------------------------------- } |
| 207 |
|
| 208 |
constructor TViewMainMenu.TSoundDeviceMenu.Create(AOwner: TComponent); |
| 209 |
var |
| 210 |
I: Integer; |
| 211 |
D: TSoundDeviceMenuButton; |
| 212 |
begin |
| 213 |
inherited; |
| 214 |
|
| 215 |
Add('Change sound output device:'); |
| 216 |
for I := 0 to SoundEngine.Devices.Count - 1 do |
| 217 |
begin |
| 218 |
D := TSoundDeviceMenuButton.Create(Self); |
| 219 |
D.Device := SoundEngine.Devices[I]; |
| 220 |
D.Caption := D.Device.Caption; |
| 221 |
Add(D); |
| 222 |
end; |
| 223 |
Add('Cancel', {$ifdef FPC}@{$endif} ClickBack); |
| 224 |
|
| 225 |
// select item 1 as default, because item 0 is the label |
| 226 |
CurrentItem := 1; |
| 227 |
end; |
| 228 |
|
| 229 |
procedure TViewMainMenu.TSoundDeviceMenu.ClickBack(Sender: TObject); |
| 230 |
begin |
| 231 |
ViewMainMenu.SetCurrentMenu(ViewMainMenu.SoundMenu); |
| 232 |
end; |
| 233 |
|
| 234 |
{ TViewMainMenu ------------------------------------------------------------- } |
| 235 |
|
| 236 |
procedure TViewMainMenu.SetCurrentMenu(const NewValue: TAbstractMenu); |
| 237 |
begin |
| 238 |
if CurrentMenu <> nil then |
| 239 |
RemoveControl(CurrentMenu); |
| 240 |
CurrentMenu := NewValue; |
| 241 |
InsertFront(CurrentMenu); |
| 242 |
end; |
| 243 |
|
| 244 |
function TViewMainMenu.Press(const Event: TInputPressRelease): boolean; |
| 245 |
begin |
| 246 |
Result := inherited; |
| 247 |
|
| 248 |
if Event.IsKey(CharEscape) then |
| 249 |
begin |
| 250 |
SetCurrentMenu(MainMenu); |
| 251 |
Result := true; |
| 252 |
end; |
| 253 |
end; |
| 254 |
|
| 255 |
procedure TViewMainMenu.Start; |
| 256 |
begin |
| 257 |
inherited; |
| 258 |
|
| 259 |
SoundEngine.LoopingChannel[0].Sound := AllSounds.SoundMainMenuMusic; |
| 260 |
|
| 261 |
MenuBg := TCastleImageControl.Create(FreeAtStop); |
| 262 |
MenuBg.URL := GameConfig.GetURL('main_menu/image'); |
| 263 |
MenuBg.FullSize := true; |
| 264 |
MenuBg.Stretch := true; |
| 265 |
InsertBack(MenuBg); |
| 266 |
|
| 267 |
MainMenu := TRiftMainMenu.Create(FreeAtStop); |
| 268 |
SoundMenu := TRiftSoundMenu.Create(FreeAtStop); |
| 269 |
SoundDeviceMenu := TSoundDeviceMenu.Create(FreeAtStop); |
| 270 |
|
| 271 |
SetCurrentMenu(MainMenu); |
| 272 |
end; |
| 273 |
|
| 274 |
procedure TViewMainMenu.Stop; |
| 275 |
begin |
| 276 |
{ The menu instance will be freed because it's owned by FreeAtStop. |
| 277 |
|
| 278 |
We should still set CurrentMenu back to nil, |
| 279 |
otherwise starting view again would have CurrentMenu <> nil. |
| 280 |
It may lead to subtle bugs: in case CurrentMenu is (by accident) |
| 281 |
equal to MenuBg in Start, then "SetCurrentMenu(MainMenu)" |
| 282 |
will free the MenuBg. } |
| 283 |
CurrentMenu := nil; |
| 284 |
inherited; |
| 285 |
end; |
| 286 |
|
| 287 |
end. |