Skip to content

Commit

Permalink
Made options menu lazily load languages list and some other options (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyyrylainen authored May 30, 2023
1 parent 0b359cc commit da63cdc
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/general/OptionsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ public class OptionsMenu : ControlWithInput
[Export]
public NodePath PatchNotesDisplayerPath = null!;

private static readonly List<string> LanguagesCache = TranslationServer.GetLoadedLocales().Cast<string>()
.OrderBy(i => i, StringComparer.InvariantCulture)
.ToList();
private static readonly Lazy<List<string>> LanguagesCache = new(() => TranslationServer.GetLoadedLocales()
.Cast<string>().OrderBy(i => i, StringComparer.InvariantCulture).ToList());

// TODO: this should be refreshed periodically to support user plugging in new devices
private static readonly List<string> AudioOutputDevicesCache = AudioServer
.GetDeviceList().OfType<string>().Where(d => d != Constants.DEFAULT_AUDIO_OUTPUT_DEVICE_NAME)
.Prepend(Constants.DEFAULT_AUDIO_OUTPUT_DEVICE_NAME).ToList();
Expand Down Expand Up @@ -464,6 +464,8 @@ public class OptionsMenu : ControlWithInput

private bool nodeReferencesResolved;

private bool elementItemSelectionsInitialized;

// Signals

[Signal]
Expand All @@ -484,16 +486,18 @@ public enum OptionsTab
Miscellaneous,
}

private static List<string> Languages => LanguagesCache;
private static List<string> Languages => LanguagesCache.Value;
private static List<string> AudioOutputDevices => AudioOutputDevicesCache;

public override void _Ready()
{
ResolveNodeReferences(true);

LoadLanguages();
LoadAudioOutputDevices();
LoadScreenEffects();
if (IsVisibleInTree())
{
GD.Print("Immediately loading options menu items as it is visible in _Ready");
InitializeOptionsSelections();
}

inputGroupList.OnControlsChanged += OnControlsChanged;

Expand Down Expand Up @@ -687,6 +691,8 @@ public void OpenFromMainMenu()
if (Visible)
return;

InitializeOptionsSelections();

// Copy the live game settings so we can check against them for changes.
savedSettings = Settings.Instance.Clone();

Expand All @@ -709,6 +715,8 @@ public void OpenFromInGame(GameProperties gameProperties)
if (Visible)
return;

InitializeOptionsSelections();

// Copy the live game settings so we can check against them for changes.
savedSettings = Settings.Instance.Clone();
savedTutorialsEnabled = gameProperties.TutorialState.Enabled;
Expand Down Expand Up @@ -981,6 +989,18 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

private void InitializeOptionsSelections()
{
if (elementItemSelectionsInitialized)
return;

elementItemSelectionsInitialized = true;

LoadLanguages();
LoadAudioOutputDevices();
LoadScreenEffects();
}

private void SwitchMode(OptionsMode mode)
{
switch (mode)
Expand Down

0 comments on commit da63cdc

Please sign in to comment.