Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve GUI window handling #250

Merged
merged 5 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Source/Tanks/FuelTankList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@ public void Load (ConfigNode node)
}

public void Save (ConfigNode node)
{
Save(node, true);
}

public void Save (ConfigNode node, bool includeEmpty)
{
foreach (FuelTank tank in this) {
ConfigNode tankNode = new ConfigNode ("TANK");
tank.Save (tankNode);
node.AddNode (tankNode);
// Don't spam save files with empty tank nodes, only save the relevant stuff
if (includeEmpty || tank.amount > 0 || tank.maxAmount > 0)
{
ConfigNode tankNode = new ConfigNode ("TANK");
tank.Save (tankNode);
node.AddNode (tankNode);
}
}
}

Expand Down
73 changes: 24 additions & 49 deletions Source/Tanks/ModuleFuelTanks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public UnmanagedResource(string name, double amount, double maxAmount)

public bool fueledByLaunchClamp = false;

private const string guiGroupName = "RealFuels";
private const string guiGroupDisplayName = "Real Fuels";

private static double MassMult
{
get {
Expand Down Expand Up @@ -361,18 +364,6 @@ public string GetModuleTitle ()
return "Modular Fuel Tank";
}

void OnActionGroupEditorOpened ()
{
Events["HideUI"].active = false;
Events["ShowUI"].active = false;
}

void OnActionGroupEditorClosed ()
{
Events["HideUI"].active = false;
Events["ShowUI"].active = true;
}

public void Start() // not just when activated
{
if (!compatible) {
Expand All @@ -388,18 +379,12 @@ public override void OnStart(StartState state)
}
enabled = true; // just in case...

Events["HideUI"].active = false;
Events["ShowUI"].active = true;


if (isEditor)
{
GameEvents.onPartAttach.Add(onPartAttach);
GameEvents.onPartRemove.Add(onPartRemove);
GameEvents.onEditorShipModified.Add(onEditorShipModified);
GameEvents.onPartActionUIDismiss.Add(OnPartActionGuiDismiss);
TankWindow.OnActionGroupEditorOpened.Add(OnActionGroupEditorOpened);
TankWindow.OnActionGroupEditorClosed.Add(OnActionGroupEditorClosed);

if (part.symmetryCounterparts.Count > 0) {
UpdateTankType(false);
Expand All @@ -425,8 +410,6 @@ void OnDestroy ()
GameEvents.onEditorShipModified.Remove (onEditorShipModified);
GameEvents.onPartActionUIDismiss.Remove (OnPartActionGuiDismiss);
TankWindow.HideGUI();
TankWindow.OnActionGroupEditorOpened.Remove (OnActionGroupEditorOpened);
TankWindow.OnActionGroupEditorClosed.Remove (OnActionGroupEditorClosed);
}

public override void OnSave (ConfigNode node)
Expand All @@ -436,7 +419,7 @@ public override void OnSave (ConfigNode node)
}

node.AddValue ("volume", volume.ToString ("G17")); // no KSPField support for doubles
tankList.Save (node);
tankList.Save (node, false);
}

const int wait_frames = 2;
Expand All @@ -448,6 +431,8 @@ private IEnumerator WaitAndUpdate (ShipConstruct ship)
yield return null;
}

// This might be overkill
UpdateUsedBy ();
PartResourcesChanged ();
}

Expand Down Expand Up @@ -505,7 +490,7 @@ private void onPartRemove (GameEvents.HostTargetAction<Part, Part> hostTarget)
private void OnPartActionGuiDismiss(Part p)
{
if (p == part) {
HideUI ();
showUI = false;
}
}

Expand All @@ -522,15 +507,18 @@ public void Update ()
bool inEditorActionsScreen = (EditorLogic.fetch?.editorScreen == EditorScreen.Actions);
bool partIsSelectedInActionsScreen = inEditorActionsScreen && (EditorActionGroups.Instance?.GetSelectedParts().Contains(part) ?? false);

if (partIsSelectedInActionsScreen) {
if (partIsSelectedInActionsScreen || showUI) {
TankWindow.ShowGUI (this);
}
}
else {
TankWindow.HideGUIForModule (this);
}
}

// The active fuel tanks. This will be the list from the tank type, with any overrides from the part file.
internal FuelTankList tankList = new FuelTankList ();

[KSPField (isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Tank Type"), UI_ChooseOption (scene = UI_Scene.Editor)]
[KSPField (isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Tank Type", groupName = guiGroupName, groupDisplayName = guiGroupDisplayName), UI_ChooseOption (scene = UI_Scene.Editor)]
public string type = "Default";
private string oldType;

Expand Down Expand Up @@ -668,7 +656,7 @@ private void UpdateTankType (bool initializeAmounts = true)
// The total tank volume. This is prior to utilization
public double totalVolume;

[KSPField (isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Utilization", guiUnits = "%", guiFormat = "F0"),
[KSPField (isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Utilization", guiUnits = "%", guiFormat = "F0", groupName = guiGroupName, groupDisplayName = guiGroupDisplayName),
UI_FloatRange (minValue = 1, maxValue = 100, stepIncrement = 1, scene = UI_Scene.Editor)]
public float utilization = -1;
private float oldUtilization = -1;
Expand All @@ -685,7 +673,7 @@ private void UpdateTankType (bool initializeAmounts = true)
// no double support for KSPFields - [KSPField (isPersistant = true)]
public double volume;

[KSPField (isPersistant = false, guiActive = false, guiActiveEditor = true, guiName = "Volume")]
[KSPField (isPersistant = false, guiActive = false, guiActiveEditor = true, guiName = "Volume", groupName = guiGroupName, groupDisplayName = guiGroupDisplayName)]
public string volumeDisplay;

public double UsedVolume
Expand Down Expand Up @@ -797,7 +785,7 @@ private void InitializeUtilization ()
public float mass;
internal bool massDirty = true;

[KSPField (isPersistant = false, guiActive = false, guiActiveEditor = true, guiName = "Mass")]
[KSPField (isPersistant = false, guiActive = false, guiActiveEditor = true, guiName = "Mass", groupName = guiGroupName, groupDisplayName = guiGroupDisplayName)]
public string massDisplay;

// public so they copy
Expand Down Expand Up @@ -1028,27 +1016,12 @@ public void PartResourcesChanged ()
massDirty = true;
}

[KSPEvent (guiActiveEditor = true, guiName = "Hide Tank UI", active = false)]
public void HideUI ()
{
TankWindow.HideGUI ();
UpdateMenus (false);
}

[KSPEvent (guiActiveEditor = true, guiName = "Show Tank UI", active = false)]
public void ShowUI ()
{
TankWindow.ShowGUI (this);
UpdateMenus (true);
}

void UpdateMenus (bool visible)
{
Events["HideUI"].active = visible;
Events["ShowUI"].active = !visible;
}
[KSPField(guiActiveEditor = true, guiName = "Tank UI", groupName = guiGroupName, groupDisplayName = guiGroupDisplayName)]
[UI_Toggle(enabledText = "Hide", disabledText = "Show")]
[NonSerialized]
public bool showUI;

[KSPEvent (guiName = "Remove All Tanks", guiActive = false, guiActiveEditor = true, name = "Empty")]
[KSPEvent (guiName = "Remove All Tanks", guiActive = false, guiActiveEditor = true, name = "Empty", groupName = guiGroupName, groupDisplayName = guiGroupDisplayName)]
public void Empty ()
{
for (int i = 0; i < tankList.Count; i++) {
Expand Down Expand Up @@ -1146,7 +1119,9 @@ public void UpdateUsedBy ()
name = "MFT" + idx++,
guiActive = false,
guiActiveEditor = activeEditor,
guiName = info.Label
guiName = info.Label,
groupName = guiGroupName,
groupDisplayName = guiGroupDisplayName
};
FuelInfo info1 = info;
BaseEvent button = new BaseEvent (Events, kspEvent.name, () => ConfigureFor (info1), kspEvent) {
Expand Down
2 changes: 1 addition & 1 deletion Source/Tanks/TankDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void Load (ConfigNode node)
public void Save (ConfigNode node)
{
ConfigNode.CreateConfigFromObject (this, node);
tankList.Save (node);
tankList.Save (node, true);
}

public bool canHave
Expand Down
12 changes: 10 additions & 2 deletions Source/Tanks/TankWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ public static void HideGUI ()
EditorLogic editor = EditorLogic.fetch;
if(editor != null)
editor.Unlock("MFTGUILock");
Debug.Log(StackTraceUtility.ExtractStackTrace());
//Debug.Log(StackTraceUtility.ExtractStackTrace());
}

public static void ShowGUI (ModuleFuelTanks tank_module)
public static void HideGUIForModule(ModuleFuelTanks tank_module)
{
if (instance != null && instance.tank_module == tank_module)
{
HideGUI();
}
}

public static void ShowGUI (ModuleFuelTanks tank_module)
{
if (instance != null) {
instance.tank_module = tank_module;
Expand Down