Skip to content

Commit 036a43e

Browse files
authored
Merge pull request #7 from Falki-git/Layout-persistence
Add layout persistence
2 parents 69e3914 + b7e64b0 commit 036a43e

File tree

2 files changed

+235
-24
lines changed

2 files changed

+235
-24
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using KSP.Rendering;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Reflection;
6+
using System.Text;
7+
using UnityEngine;
8+
9+
namespace MicroMod
10+
{
11+
/// <summary>
12+
/// Manages saving and loading of window positions and states
13+
/// </summary>
14+
public class LayoutState
15+
{
16+
public bool ShowSettings { get; set; }
17+
public bool ShowVes { get; set; }
18+
public bool ShowOrb { get; set; }
19+
public bool ShowSur { get; set; }
20+
public bool ShowFlt { get; set; }
21+
public bool ShowMan { get; set; }
22+
public bool ShowTgt { get; set; }
23+
public bool ShowStg { get; set; }
24+
25+
public bool IsPopoutSettings { get; set; }
26+
public bool IsPopoutVes { get; set; }
27+
public bool IsPopoutOrb { get; set; }
28+
public bool IsPopoutSur { get; set; }
29+
public bool IsPopOutMan { get; set; }
30+
public bool IsPopOutTgt { get; set; }
31+
public bool IsPopOutFlt { get; set; }
32+
public bool IsPopOutStg { get; set; }
33+
34+
public Vector2 MainGuiPosition { get; set; }
35+
public Vector2 SettingsPosition { get; set; }
36+
public Vector2 VesPosition { get; set; }
37+
public Vector2 OrbPosition { get; set; }
38+
public Vector2 SurPosition { get; set; }
39+
public Vector2 FltPosition { get; set; }
40+
public Vector2 ManPosition { get; set; }
41+
public Vector2 TgtPosition { get; set; }
42+
public Vector2 StgPosition { get; set; }
43+
44+
private static string _assemblyFolder;
45+
public static string AssemblyFolder => _assemblyFolder ??= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
46+
47+
private static string _settingsPath;
48+
public static string SettingsPath => _settingsPath ??= Path.Combine(AssemblyFolder, "LayoutState.json");
49+
50+
public LayoutState()
51+
{ }
52+
53+
public LayoutState(MicroEngineerMod layout)
54+
{
55+
ShowSettings = layout.showSettings;
56+
ShowVes = layout.showVes;
57+
ShowOrb = layout.showOrb;
58+
ShowSur = layout.showSur;
59+
ShowFlt = layout.showFlt;
60+
ShowMan = layout.showMan;
61+
ShowTgt = layout.showTgt;
62+
ShowStg = layout.showStg;
63+
IsPopoutSettings = layout.popoutSettings;
64+
IsPopoutVes = layout.popoutVes;
65+
IsPopoutOrb = layout.popoutOrb;
66+
IsPopoutSur = layout.popoutSur;
67+
IsPopOutFlt = layout.popoutFlt;
68+
IsPopOutMan = layout.popoutMan;
69+
IsPopOutTgt = layout.popoutTgt;
70+
IsPopOutStg = layout.popoutStg;
71+
MainGuiPosition = layout.mainGuiRect.position;
72+
SettingsPosition = layout.settingsGuiRect.position;
73+
VesPosition = layout.vesGuiRect.position;
74+
OrbPosition = layout.orbGuiRect.position;
75+
SurPosition = layout.surGuiRect.position;
76+
FltPosition = layout.fltGuiRect.position;
77+
ManPosition = layout.manGuiRect.position;
78+
TgtPosition = layout.tgtGuiRect.position;
79+
StgPosition = layout.stgGuiRect.position;
80+
}
81+
82+
/// <summary>
83+
/// Saves layout to disk
84+
/// </summary>
85+
/// <returns></returns>
86+
public bool Save()
87+
{
88+
try
89+
{
90+
File.WriteAllText(LayoutState.SettingsPath, JsonConvert.SerializeObject(this));
91+
}
92+
catch (Exception ex)
93+
{
94+
// TODO logging
95+
}
96+
97+
return true;
98+
}
99+
100+
/// <summary>
101+
/// Loads layout from disk
102+
/// </summary>
103+
/// <returns>Loaded LayoutState if file exist, otherwise null</returns>
104+
public static LayoutState Load()
105+
{
106+
try
107+
{
108+
return JsonConvert.DeserializeObject<LayoutState>(File.ReadAllText(LayoutState.SettingsPath));
109+
110+
}
111+
catch (FileNotFoundException ex)
112+
{
113+
// TODO logging
114+
return null;
115+
}
116+
catch (Exception ex)
117+
{
118+
// TODO logging
119+
return null;
120+
}
121+
}
122+
}
123+
}

MicroEngineerProject/MicroEngineer/MicroEngineerMod.cs

Lines changed: 112 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class MicroEngineerMod : BaseSpaceWarpPlugin
2626

2727
private readonly int windowWidth = 290;
2828
private readonly int windowHeight = 700;
29-
private Rect mainGuiRect, vesGuiRect, orbGuiRect, surGuiRect, fltGuiRect, manGuiRect, tgtGuiRect, stgGuiRect;
29+
public Rect mainGuiRect, settingsGuiRect, vesGuiRect, orbGuiRect, surGuiRect, fltGuiRect, manGuiRect, tgtGuiRect, stgGuiRect;
3030
private Rect closeBtnRect;
3131

3232
private GUISkin _spaceWarpUISkin;
@@ -35,28 +35,30 @@ public class MicroEngineerMod : BaseSpaceWarpPlugin
3535
private GUIStyle popoutWindowStyle;
3636
private GUIStyle sectionToggleStyle;
3737
private GUIStyle closeBtnStyle;
38+
private GUIStyle saveLoadBtnStyle;
39+
private GUIStyle loadBtnStyle;
3840
private GUIStyle nameLabelStyle;
3941
private GUIStyle valueLabelStyle;
4042
private GUIStyle unitLabelStyle;
4143
private GUIStyle tableHeaderLabelStyle;
4244

4345
private string unitColorHex;
4446

45-
4647
private int spacingAfterHeader = -12;
4748
private int spacingAfterEntry = -12;
4849
private int spacingAfterSection = 5;
4950
private float spacingBelowPopout = 10;
5051

51-
private bool showVes = true;
52-
private bool showOrb = true;
53-
private bool showSur = true;
54-
private bool showFlt = false;
55-
private bool showMan = true;
56-
private bool showTgt = false;
57-
private bool showStg = true;
52+
public bool showSettings = false;
53+
public bool showVes = true;
54+
public bool showOrb = true;
55+
public bool showSur = true;
56+
public bool showFlt = false;
57+
public bool showMan = true;
58+
public bool showTgt = false;
59+
public bool showStg = true;
5860

59-
private bool popoutVes, popoutOrb, popoutSur, popoutMan, popoutTgt, popoutFlt, popoutStg;
61+
public bool popoutSettings, popoutVes, popoutOrb, popoutSur, popoutMan, popoutTgt, popoutFlt, popoutStg;
6062

6163
private VesselComponent activeVessel;
6264
private SimulationObjectModel currentTarget;
@@ -132,6 +134,11 @@ public override void OnInitialized()
132134
fontSize = 8
133135
};
134136

137+
saveLoadBtnStyle = new GUIStyle(_spaceWarpUISkin.button)
138+
{
139+
alignment = TextAnchor.MiddleCenter
140+
};
141+
135142
closeBtnRect = new Rect(windowWidth - 23, 6, 16, 16);
136143

137144
tableHeaderLabelStyle = new GUIStyle(nameLabelStyle) { alignment = TextAnchor.MiddleRight };
@@ -141,20 +148,33 @@ public override void OnInitialized()
141148
"BTN-MicroEngineerBtn",
142149
AssetManager.GetAsset<Texture2D>($"{SpaceWarpMetadata.ModID}/images/icon.png"),
143150
delegate { showGUI = !showGUI; }
144-
145151
);
152+
153+
154+
InitializeRects();
155+
ResetLayout();
156+
// load window positions and states from disk, if file exists
157+
LoadLayoutState();
158+
}
159+
160+
private void InitializeRects()
161+
{
162+
mainGuiRect = settingsGuiRect = vesGuiRect = orbGuiRect = surGuiRect = fltGuiRect = manGuiRect = tgtGuiRect = stgGuiRect = new();
146163
}
147164

148-
void Awake()
165+
private void ResetLayout()
149166
{
150-
mainGuiRect = new Rect(Screen.width * 0.8f, Screen.height * 0.3f, 0, 0);
151-
vesGuiRect = new Rect(Screen.width * 0.6f, Screen.height * 0.3f, 0, 0);
152-
orbGuiRect = new Rect(Screen.width * 0.6f, Screen.height * 0.3f, 0, 0);
153-
surGuiRect = new Rect(Screen.width * 0.6f, Screen.height * 0.3f, 0, 0);
154-
fltGuiRect = new Rect(Screen.width * 0.6f, Screen.height * 0.3f, 0, 0);
155-
manGuiRect = new Rect(Screen.width * 0.6f, Screen.height * 0.3f, 0, 0);
156-
tgtGuiRect = new Rect(Screen.width * 0.6f, Screen.height * 0.3f, 0, 0);
157-
stgGuiRect = new Rect(Screen.width * 0.6f, Screen.height * 0.3f, 0, 0);
167+
popoutVes = popoutStg = popoutOrb = popoutSur = popoutFlt = popoutTgt = popoutMan = popoutSettings = false;
168+
mainGuiRect.position = new(Screen.width * 0.8f, Screen.height * 0.2f);
169+
Vector2 popoutWindowPosition = new(Screen.width * 0.6f, Screen.height * 0.2f);
170+
vesGuiRect.position = popoutWindowPosition;
171+
stgGuiRect.position = popoutWindowPosition;
172+
orbGuiRect.position = popoutWindowPosition;
173+
surGuiRect.position = popoutWindowPosition;
174+
fltGuiRect.position = popoutWindowPosition;
175+
tgtGuiRect.position = popoutWindowPosition;
176+
manGuiRect.position = popoutWindowPosition;
177+
settingsGuiRect.position = popoutWindowPosition;
158178
}
159179

160180
private void OnGUI()
@@ -176,6 +196,11 @@ private void OnGUI()
176196
);
177197
mainGuiRect.position = ClampToScreen(mainGuiRect.position, mainGuiRect.size);
178198

199+
if (showSettings && popoutSettings)
200+
{
201+
DrawPopoutWindow(ref settingsGuiRect, FillSettings);
202+
}
203+
179204
if (showVes && popoutVes)
180205
{
181206
DrawPopoutWindow(ref vesGuiRect, FillVessel);
@@ -256,11 +281,10 @@ private void FillMainGUI(int windowID)
256281
showTgt = GUILayout.Toggle(showTgt, "<b>TGT</b>", sectionToggleStyle);
257282
GUILayout.EndHorizontal();
258283

259-
260-
GUILayout.Space(-10);
261-
262284
GUILayout.BeginHorizontal();
263285
showMan = GUILayout.Toggle(showMan, "<b>MAN</b>", sectionToggleStyle);
286+
GUILayout.Space(26);
287+
showSettings = GUILayout.Toggle(showSettings, "<b>SET</b>", sectionToggleStyle);
264288
GUILayout.EndHorizontal();
265289

266290

@@ -269,6 +293,11 @@ private void FillMainGUI(int windowID)
269293
GUILayout.BeginHorizontal();
270294
GUILayout.EndHorizontal();
271295

296+
if (showSettings && !popoutSettings)
297+
{
298+
FillSettings();
299+
}
300+
272301
if (showVes && !popoutVes)
273302
{
274303
FillVessel();
@@ -307,6 +336,25 @@ private void FillMainGUI(int windowID)
307336
GUI.DragWindow(new Rect(0, 0, windowWidth, windowHeight));
308337
}
309338

339+
private void FillSettings(int _ = 0)
340+
{
341+
DrawSectionHeader("Settings", ref popoutSettings);
342+
343+
GUILayout.Space(10);
344+
GUILayout.BeginHorizontal();
345+
if (GUILayout.Button("SAVE LAYOUT", saveLoadBtnStyle))
346+
SaveLayoutState();
347+
GUILayout.Space(5);
348+
if (GUILayout.Button("LOAD LAYOUT", saveLoadBtnStyle))
349+
LoadLayoutState();
350+
GUILayout.Space(5);
351+
if (GUILayout.Button("RESET", saveLoadBtnStyle))
352+
ResetLayout();
353+
GUILayout.EndHorizontal();
354+
355+
DrawSectionEnd(popoutSettings);
356+
}
357+
310358
private void FillVessel(int _ = 0)
311359
{
312360
DrawSectionHeader("Vessel", ref popoutVes, activeVessel.DisplayName);
@@ -556,7 +604,7 @@ private string SituationToString(VesselSituations situation)
556604
VesselSituations.SubOrbital => "Suborbital",
557605
VesselSituations.Orbiting => "Orbiting",
558606
VesselSituations.Escaping => "Escaping",
559-
_ => "UNNOWN",
607+
_ => "UNKNOWN",
560608
};
561609
}
562610

@@ -671,5 +719,45 @@ private void GetAeroStats()
671719
}
672720
}
673721
}
722+
723+
private void SaveLayoutState()
724+
{
725+
LayoutState state = new(this);
726+
state.Save();
727+
}
728+
729+
private void LoadLayoutState()
730+
{
731+
LayoutState state = LayoutState.Load();
732+
733+
if (state != null)
734+
{
735+
showSettings = false;
736+
showVes = state.ShowVes;
737+
showOrb = state.ShowOrb;
738+
showSur = state.ShowSur;
739+
showFlt = state.ShowFlt;
740+
showMan = state.ShowMan;
741+
showTgt = state.ShowTgt;
742+
showStg = state.ShowStg;
743+
popoutSettings = state.IsPopoutSettings;
744+
popoutVes = state.IsPopoutVes;
745+
popoutOrb = state.IsPopoutOrb;
746+
popoutSur = state.IsPopoutSur;
747+
popoutFlt = state.IsPopOutFlt;
748+
popoutMan = state.IsPopOutMan;
749+
popoutTgt = state.IsPopOutTgt;
750+
popoutStg = state.IsPopOutStg;
751+
mainGuiRect.position = state.MainGuiPosition;
752+
settingsGuiRect.position = state.SettingsPosition;
753+
vesGuiRect.position = state.VesPosition;
754+
orbGuiRect.position = state.OrbPosition;
755+
surGuiRect.position = state.SurPosition;
756+
fltGuiRect.position = state.FltPosition;
757+
manGuiRect.position = state.ManPosition;
758+
tgtGuiRect.position = state.TgtPosition;
759+
stgGuiRect.position = state.StgPosition;
760+
}
761+
}
674762
}
675763
}

0 commit comments

Comments
 (0)