Skip to content

Commit 40a5c5c

Browse files
authored
Merge pull request #9 from Safarte/fix-ns-resources-ui
Changes the NS Resources UI system to a reactive model
2 parents d82c700 + ae01d06 commit 40a5c5c

1 file changed

Lines changed: 49 additions & 19 deletions

File tree

src/CommunityResources/NonStageableResourcesUIController.cs

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using KSP.Game;
2+
using KSP.Messages;
23
using KSP.Sim.impl;
34
using KSP.Sim.ResourceSystem;
45
using UnityEngine;
@@ -12,7 +13,19 @@ internal class NonStageableResourcesUIController : KerbalMonoBehaviour
1213
{
1314
private VesselComponent _activeVessel;
1415
private GameObject _nsResourcesUI;
15-
private int _lastNSResourcesCount = 0;
16+
private bool _needNsUiUpdate = false;
17+
18+
private void Start()
19+
{
20+
Game.Messages.Subscribe<VesselChangedMessage>(OnVesselChanged);
21+
}
22+
23+
private void OnDestroy()
24+
{
25+
if (IsGameShuttingDown)
26+
return;
27+
Game.Messages.Unsubscribe<VesselChangedMessage>(OnVesselChanged);
28+
}
1629

1730
private void Update()
1831
{
@@ -22,31 +35,48 @@ private void Update()
2235
if (_activeVessel is null)
2336
return;
2437

38+
if (_needNsUiUpdate)
39+
{
40+
_needNsUiUpdate = false;
41+
42+
// Update Non Stageable Resources UI size
43+
UpdateNonStageableResourcesUI();
44+
}
45+
}
46+
47+
private void OnVesselChanged(MessageCenterMessage msg)
48+
{
49+
if (msg is not VesselChangedMessage vesselChangedMessage || vesselChangedMessage == null)
50+
return;
51+
52+
_needNsUiUpdate = true;
53+
}
54+
55+
/// <summary>
56+
/// Updates the height & position of the non-stageable resources UI
57+
/// </summary>
58+
/// <param name="nsResourcesCount"></param>
59+
private void UpdateNonStageableResourcesUI()
60+
{
2561
// Get number of non-stageable resources in the active vessel
2662
int nsResourcesCount = GetNonStageableResourcesCount(_activeVessel);
2763

28-
// Update UI only if resource count has changed
29-
if (nsResourcesCount != _lastNSResourcesCount)
30-
{
31-
// Find the non-stageable resources window game object
32-
_nsResourcesUI = GameObject.Find("GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Main Canvas/FlightHudRoot(Clone)/NonStageableResources(Clone)/KSP2UIWindow/Root/UIPanel");
33-
34-
if (_nsResourcesUI != null)
35-
{
36-
// Number of resources above the default maximum of 2 (EC & Monoprop)
37-
int extraResources = Math.Max(0, nsResourcesCount - 2);
64+
// Find the non-stageable resources window game object
65+
_nsResourcesUI = GameObject.Find("GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Main Canvas/FlightHudRoot(Clone)/NonStageableResources(Clone)/KSP2UIWindow/Root/UIPanel");
3866

39-
// Get the UI object's RectTransform
40-
RectTransform rect = _nsResourcesUI.GetComponent<RectTransform>();
67+
if (_nsResourcesUI != null)
68+
{
69+
// Number of resources above the default maximum of 2 (EC & Monoprop)
70+
int extraResources = Math.Max(0, nsResourcesCount - 2);
4171

42-
// Increase the UI object's vertical size
43-
rect.sizeDelta = new Vector2(0, 30 * extraResources);
72+
// Get the UI object's RectTransform
73+
RectTransform rect = _nsResourcesUI.GetComponent<RectTransform>();
4474

45-
// Shift the UI object down
46-
rect.localPosition = extraResources * 15 * Vector3.down;
75+
// Increase the UI object's vertical size
76+
rect.sizeDelta = new Vector2(0, 30 * extraResources);
4777

48-
_lastNSResourcesCount = nsResourcesCount;
49-
}
78+
// Shift the UI object down
79+
rect.localPosition = extraResources * 15 * Vector3.down;
5080
}
5181
}
5282

0 commit comments

Comments
 (0)