Skip to content

Commit

Permalink
Change all static state in CS to belong to instance (#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav authored Apr 14, 2024
1 parent 9fcddda commit 67dedb6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
31 changes: 18 additions & 13 deletions Source/RP0/ConfigurableStart/PresetPickerGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ namespace RP0.ConfigurableStart
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class PresetPickerGUI : MonoBehaviour
{
//TODO: find a better starting position
private static Rect _selectionWindowRect = new Rect(267, 104, 400, 200);
private static bool _shouldResetUIHeight = false;
private static bool _showUI = false;
private Rect _selectionWindowRect = new Rect(267, 104, 400, 200);
private bool _shouldResetUIHeight = false;
private bool _showUI = false;

private static string[] _loadedScenarioNames;
private static int _selectedScenarioIndex = 0;
private string[] _loadedScenarioNames;
private int _selectedScenarioIndex = 0;

public static PresetPickerGUI Instance { get; private set; }
public bool Initialized { get; private set; }

public void Awake()
internal void Awake()
{
if (Instance != null)
{
Expand All @@ -27,9 +26,15 @@ public void Awake()
Instance = this;
}

public static void SetVisible(bool visible) => _showUI = visible;
internal void OnDestroy()
{
if (Instance == this)
Instance = null;
}

public void SetVisible(bool visible) => _showUI = visible;

public static void ToggleVisible() => _showUI = !_showUI;
public void ToggleVisible() => _showUI = !_showUI;

public void Setup(string[] names)
{
Expand All @@ -47,11 +52,11 @@ public void OnGUI()
_shouldResetUIHeight = false;
}

_selectionWindowRect = GUILayout.Window(GetInstanceID(), _selectionWindowRect, SelectionWindow, "Scenario Selector", HighLogic.Skin.window);
_selectionWindowRect = GUILayout.Window(GetInstanceID(), _selectionWindowRect, RenderSelectionWindow, "Scenario Selector", HighLogic.Skin.window);
}
}

private static void SelectionWindow(int windowID)
private void RenderSelectionWindow(int windowID)
{
GUILayout.BeginVertical(HighLogic.Skin.box);
{
Expand All @@ -63,11 +68,11 @@ private static void SelectionWindow(int windowID)
{
_shouldResetUIHeight = true;
RP0Debug.Log("Selected Scenario changed, updating values");
ScenarioHandler.SetCurrentScenarioFromName(_loadedScenarioNames[_selectedScenarioIndex]);
ScenarioHandler.Instance.SetCurrentScenarioFromName(_loadedScenarioNames[_selectedScenarioIndex]);
}
}
GUILayout.BeginHorizontal();
GUILayout.Label(ScenarioHandler.CurrentScenario?.Description ?? "");
GUILayout.Label(ScenarioHandler.Instance.CurrentScenario?.Description ?? "");
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
Expand Down
30 changes: 12 additions & 18 deletions Source/RP0/ConfigurableStart/ScenarioHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ public class ScenarioHandler : MonoBehaviour
{
public const string EmptyScenarioName = "None";

private static bool _mainMenuVisited;
private static string _curScenarioName;
private static readonly Dictionary<string, ConfigNode> _contractNodes = new Dictionary<string, ConfigNode>(); // to cache the contract nodes
private static uint _runningContractCoroutines = 0;
private static bool _contractsIterated = false;

private string _curScenarioName;
private readonly Dictionary<string, ConfigNode> _contractNodes = new Dictionary<string, ConfigNode>(); // to cache the contract nodes
private uint _runningContractCoroutines = 0;
private bool _contractsIterated = false;
private TextMeshProUGUI _btnText;

public static ScenarioHandler Instance { get; private set; }
public static Dictionary<string, Scenario> LoadedScenarios { get; private set; }
public static bool ContractsInitialized => _runningContractCoroutines == 0 && _contractsIterated;
public static Scenario CurrentScenario
public Dictionary<string, Scenario> LoadedScenarios { get; private set; }
public bool ContractsInitialized => _runningContractCoroutines == 0 && _contractsIterated;
public Scenario CurrentScenario
{
get
{
Expand All @@ -45,7 +43,7 @@ public static Scenario CurrentScenario
}
}

public static void SetCurrentScenarioFromName(string name)
public void SetCurrentScenarioFromName(string name)
{
if (!string.IsNullOrEmpty(name) && LoadedScenarios.ContainsKey(name))
{
Expand All @@ -69,12 +67,8 @@ internal void Start()

// don't destroy on scene switch
DontDestroyOnLoad(this);

if (!_mainMenuVisited)
{
GameEvents.onGameNewStart.Add(OnGameNewStart);
_mainMenuVisited = true;
}

GameEvents.onGameNewStart.Add(OnGameNewStart);

LoadedScenarios = Database.CustomScenarios;
_curScenarioName = EmptyScenarioName;
Expand All @@ -92,7 +86,7 @@ internal void OnDestroy()

public void OnGameNewStart()
{
PresetPickerGUI.SetVisible(false);
PresetPickerGUI.Instance?.SetVisible(false);
if (CurrentScenario == null || CurrentScenario.ScenarioName == EmptyScenarioName) return;

switch (HighLogic.CurrentGame.Mode)
Expand Down Expand Up @@ -784,7 +778,7 @@ public void ClobberNewGameUI(PopupDialog newGameDlg)
var btn = newBtn.GetComponentInChildren<Button>();
btn.onClick.AddListener(() =>
{
PresetPickerGUI.ToggleVisible();
PresetPickerGUI.Instance.ToggleVisible();
});

hzGroup.transform.SetParent(uiItem.transform);
Expand Down

0 comments on commit 67dedb6

Please sign in to comment.