forked from Revolutionary-Games/Thrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainMenu.cs
537 lines (426 loc) · 14.8 KB
/
MainMenu.cs
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Godot;
using Array = Godot.Collections.Array;
/// <summary>
/// Class managing the main menu and everything in it
/// </summary>
public class MainMenu : NodeWithInput
{
/// <summary>
/// Index of the current menu.
/// </summary>
[Export]
public uint CurrentMenuIndex;
[Export]
public NodePath ThriveLogoPath = null!;
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Global", Justification = "Set from editor")]
[Export]
public List<Texture> MenuBackgrounds = null!;
[Export]
public NodePath FreebuildButtonPath = null!;
[Export]
public NodePath CreditsContainerPath = null!;
[Export]
public NodePath CreditsScrollPath = null!;
[Export]
public NodePath LicensesDisplayPath = null!;
[Export]
public NodePath GLES2PopupPath = null!;
[Export]
public NodePath ModLoadFailuresPath = null!;
[Export]
public NodePath StoreLoggedInDisplayPath = null!;
[Export]
public NodePath ModManagerPath = null!;
[Export]
public NodePath GalleryViewerPath = null!;
public Array? MenuArray;
public TextureRect Background = null!;
public bool IsReturningToMenu;
private TextureRect thriveLogo = null!;
private OptionsMenu options = null!;
private NewGameSettings newGameSettings = null!;
private AnimationPlayer guiAnimations = null!;
private SaveManagerGUI saves = null!;
private ModManager modManager = null!;
private GalleryViewer galleryViewer = null!;
private Control creditsContainer = null!;
private CreditsScroll credits = null!;
private LicensesDisplay licensesDisplay = null!;
private Button freebuildButton = null!;
private Label storeLoggedInDisplay = null!;
private CustomConfirmationDialog gles2Popup = null!;
private ErrorDialog modLoadFailures = null!;
public override void _Ready()
{
// Unpause the game as the MainMenu should never be paused.
PauseManager.Instance.ForceClear();
RunMenuSetup();
// Start intro video
if (Settings.Instance.PlayIntroVideo && LaunchOptions.VideosEnabled && !IsReturningToMenu)
{
TransitionManager.Instance.AddSequence(
TransitionManager.Instance.CreateCutscene("res://assets/videos/intro.ogv"), OnIntroEnded);
}
else
{
OnIntroEnded();
}
// Let all suppressed deletions happen (if we came back directly from the editor that was loaded from a save)
TemporaryLoadedNodeDeleter.Instance.ReleaseAllHolds();
CheckModFailures();
}
public void StartMusic()
{
Jukebox.Instance.PlayCategory("Menu");
}
/// <summary>
/// Sets the current menu index and then switches the menu
/// </summary>
/// <param name="index">Index of the menu. Set to <see cref="uint.MaxValue"/> to hide all menus</param>
/// <param name="slide">If false then the menu slide animation will not be played</param>
public void SetCurrentMenu(uint index, bool slide = true)
{
if (MenuArray == null)
throw new InvalidOperationException("Main menu has not been initialized");
// Allow disabling all the menus for going to the options menu
if (index > MenuArray.Count - 1 && index != uint.MaxValue)
{
GD.PrintErr("Selected menu index is out of range!");
return;
}
CurrentMenuIndex = index;
if (slide)
{
PlayGUIAnimation("MenuSlide");
}
else
{
// Just switch the menu
SwitchMenu();
}
}
/// <summary>
/// This is when ESC is pressed. Main menu priority is lower than Options Menu
/// to avoid capturing ESC presses in the Options Menu.
/// </summary>
[RunOnKeyDown("ui_cancel", Priority = Constants.MAIN_MENU_CANCEL_PRIORITY)]
public bool OnEscapePressed()
{
// In a sub menu (that doesn't have its own class)
if (CurrentMenuIndex != 0 && CurrentMenuIndex < uint.MaxValue)
{
SetCurrentMenu(0);
// Handled, stop here.
return true;
}
if (CurrentMenuIndex == uint.MaxValue && saves.Visible)
{
OnReturnFromLoadGame();
return true;
}
// Not handled, pass through.
return false;
}
public void NewGameSetupDone(WorldGenerationSettings settings)
{
GUICommon.Instance.PlayButtonPressSound();
if (Settings.Instance.PlayMicrobeIntroVideo)
{
// Stop music for the video (stop is used instead of pause to stop the menu music playing a bit after the video
// before the stage music starts)
Jukebox.Instance.Stop(true);
var transitions = new List<ITransition>();
transitions.Add(TransitionManager.Instance.CreateScreenFade(ScreenFade.FadeType.FadeOut, 1.0f));
if (settings.Origin == WorldGenerationSettings.LifeOrigin.Panspermia)
{
transitions.Add(TransitionManager.Instance.CreateScreenFade(ScreenFade.FadeType.StayBlack, 5.0f, "INTRO_MESSAGE_PANSPERMIA"));
Settings.Instance.PlayMicrobeIntroVideo = new(false);
}
else
{
transitions.Add(TransitionManager.Instance.CreateScreenFade(ScreenFade.FadeType.StayBlack, 5.0f, "INTRO_MESSAGE_1"));
}
TransitionManager.Instance.PlaySequencesSequentially(transitions, () => StartNewGame(settings));
}
else
{
StartNewGame(settings);
}
}
private void StartNewGame(WorldGenerationSettings settings)
{
OnEnteringGame();
// TODO: Add loading screen while changing between scenes
var microbeStage = (MicrobeStage)SceneManager.Instance.LoadScene(MainGameState.MicrobeStage).Instance();
microbeStage.WorldSettings = settings;
SceneManager.Instance.SwitchToScene(microbeStage);
}
/// <summary>
/// Setup the main menu.
/// </summary>
private void RunMenuSetup()
{
Background = GetNode<TextureRect>("Background");
guiAnimations = GetNode<AnimationPlayer>("GUIAnimations");
thriveLogo = GetNode<TextureRect>(ThriveLogoPath);
freebuildButton = GetNode<Button>(FreebuildButtonPath);
creditsContainer = GetNode<Control>(CreditsContainerPath);
credits = GetNode<CreditsScroll>(CreditsScrollPath);
licensesDisplay = GetNode<LicensesDisplay>(LicensesDisplayPath);
storeLoggedInDisplay = GetNode<Label>(StoreLoggedInDisplayPath);
modManager = GetNode<ModManager>(ModManagerPath);
galleryViewer = GetNode<GalleryViewer>(GalleryViewerPath);
MenuArray?.Clear();
// Get all of menu items
MenuArray = GetTree().GetNodesInGroup("MenuItem");
if (MenuArray == null)
{
GD.PrintErr("Failed to find all the menu items!");
return;
}
RandomizeBackground();
options = GetNode<OptionsMenu>("OptionsMenu");
newGameSettings = GetNode<NewGameSettings>("NewGameSettings");
saves = GetNode<SaveManagerGUI>("SaveManagerGUI");
gles2Popup = GetNode<CustomConfirmationDialog>(GLES2PopupPath);
modLoadFailures = GetNode<ErrorDialog>(ModLoadFailuresPath);
// Set initial menu
SwitchMenu();
// Easter egg message
thriveLogo.RegisterToolTipForControl("thriveLogoEasterEgg", "mainMenu");
if (OS.GetCurrentVideoDriver() == OS.VideoDriver.Gles2 && !IsReturningToMenu)
gles2Popup.PopupCenteredShrink();
UpdateStoreNameLabel();
}
/// <summary>
/// Randomizes background images.
/// </summary>
private void RandomizeBackground()
{
Random rand = new Random();
var chosenBackground = MenuBackgrounds.Random(rand);
SetBackground(chosenBackground);
}
private void SetBackground(Texture backgroundImage)
{
Background.Texture = backgroundImage;
}
private void UpdateStoreNameLabel()
{
if (!SteamHandler.Instance.IsLoaded)
{
storeLoggedInDisplay.Visible = false;
}
else
{
storeLoggedInDisplay.Visible = true;
storeLoggedInDisplay.Text = string.Format(CultureInfo.CurrentCulture,
TranslationServer.Translate("STORE_LOGGED_IN_AS"), SteamHandler.Instance.DisplayName);
}
}
/// <summary>
/// Stops any currently playing animation and plays
/// the given one instead
/// </summary>
private void PlayGUIAnimation(string animation)
{
if (guiAnimations.IsPlaying())
guiAnimations.Stop();
guiAnimations.Play(animation);
}
/// <summary>
/// Switches the displayed menu
/// </summary>
private void SwitchMenu()
{
thriveLogo.Hide();
// Hide other menus and only show the one of the current index
foreach (Control menu in MenuArray!)
{
menu.Hide();
if (menu.GetIndex() == CurrentMenuIndex)
{
menu.Show();
thriveLogo.Show();
}
}
}
private void CheckModFailures()
{
var errors = ModLoader.Instance.GetAndClearModErrors();
if (errors.Count > 0)
{
modLoadFailures.ExceptionInfo = string.Join("\n", errors);
modLoadFailures.PopupCenteredShrink();
}
}
private void OnIntroEnded()
{
TransitionManager.Instance.AddSequence(
ScreenFade.FadeType.FadeIn, IsReturningToMenu ? 0.5f : 1.0f, null, false);
// Start music after the video
StartMusic();
}
public void OnMicrobeIntroEnded()
{
OnEnteringGame();
// TODO: Add loading screen while changing between scenes
SceneManager.Instance.SwitchToScene(MainGameState.MicrobeStage);
}
private void OnFreebuildFadeInEnded()
{
OnEnteringGame();
// Instantiate a new editor scene
var editor = (MicrobeEditor)SceneManager.Instance.LoadScene(MainGameState.MicrobeEditor).Instance();
// Start freebuild game
editor.CurrentGame = GameProperties.StartNewMicrobeGame(new WorldGenerationSettings());
// Stop music for the video (stop is used instead of pause to stop the menu music playing a bit after the video
// before the stage music starts)
Jukebox.Instance.Stop(true);
// Switch to the editor scene
SceneManager.Instance.SwitchToScene(editor);
}
private void NewGamePressed()
{
GUICommon.Instance.PlayButtonPressSound();
// Hide all the other menus
SetCurrentMenu(uint.MaxValue, false);
// Show the options
newGameSettings.OpenFromMainMenu();
thriveLogo.Hide();
}
private void ToolsPressed()
{
GUICommon.Instance.PlayButtonPressSound();
SetCurrentMenu(1);
}
private void ExtrasPressed()
{
GUICommon.Instance.PlayButtonPressSound();
SetCurrentMenu(2);
}
private void FreebuildEditorPressed()
{
GUICommon.Instance.PlayButtonPressSound();
// Disable the button to prevent it being executed again.
freebuildButton.Disabled = true;
TransitionManager.Instance.AddSequence(ScreenFade.FadeType.FadeOut, 0.1f, () =>
{
OnEnteringGame();
// Instantiate a new editor scene
var editor = (MicrobeEditor)SceneManager.Instance.LoadScene(MainGameState.MicrobeEditor).Instance();
// Start freebuild game
editor.CurrentGame = GameProperties.StartNewMicrobeGame(new WorldGenerationSettings(), true);
// Switch to the editor scene
SceneManager.Instance.SwitchToScene(editor);
}, false);
}
// TODO: this is now used by another sub menu as well so renaming this to be more generic would be good
private void BackFromToolsPressed()
{
GUICommon.Instance.PlayButtonPressSound();
SetCurrentMenu(0);
}
private void ViewSourceCodePressed()
{
GUICommon.Instance.PlayButtonPressSound();
OS.ShellOpen("https://github.com/adQuid/Thrive");
}
private void QuitPressed()
{
GetTree().Quit();
}
private void OptionsPressed()
{
GUICommon.Instance.PlayButtonPressSound();
// Hide all the other menus
SetCurrentMenu(uint.MaxValue, false);
// Show the options
options.OpenFromMainMenu();
}
private void OnReturnFromOptions()
{
options.Visible = false;
SetCurrentMenu(0, false);
}
private void OnReturnFromNewGameSettings()
{
newGameSettings.Visible = false;
SetCurrentMenu(0, false);
thriveLogo.Show();
}
private void LoadGamePressed()
{
GUICommon.Instance.PlayButtonPressSound();
// Hide all the other menus
SetCurrentMenu(uint.MaxValue, false);
// Show the options
saves.Visible = true;
}
private void OnReturnFromLoadGame()
{
saves.Visible = false;
SetCurrentMenu(0, false);
}
private void CreditsPressed()
{
GUICommon.Instance.PlayButtonPressSound();
// Hide all the other menus
SetCurrentMenu(uint.MaxValue, false);
// Show the credits view
credits.Restart();
creditsContainer.Visible = true;
}
private void OnReturnFromCredits()
{
creditsContainer.Visible = false;
credits.Pause();
SetCurrentMenu(0, false);
}
private void LicensesPressed()
{
GUICommon.Instance.PlayButtonPressSound();
// Hide all the other menus
SetCurrentMenu(uint.MaxValue, false);
// Show the licenses view
licensesDisplay.PopupCenteredShrink();
}
private void OnReturnFromLicenses()
{
SetCurrentMenu(2, false);
}
private void ModsPressed()
{
GUICommon.Instance.PlayButtonPressSound();
// Hide all the other menus
SetCurrentMenu(uint.MaxValue, false);
// Show the mods view
modManager.Visible = true;
}
private void OnReturnFromMods()
{
modManager.Visible = false;
SetCurrentMenu(0, false);
}
private void ArtGalleryPressed()
{
GUICommon.Instance.PlayButtonPressSound();
SetCurrentMenu(uint.MaxValue, false);
galleryViewer.PopupFullRect();
Jukebox.Instance.PlayCategory("ArtGallery");
}
private void OnReturnFromArtGallery()
{
SetCurrentMenu(2, false);
Jukebox.Instance.PlayCategory("Menu");
}
private void OnEnteringGame()
{
CheatManager.OnCheatsDisabled();
SaveHelper.ClearLastSaveTime();
}
}