Skip to content

Commit e69c666

Browse files
authored
Merge pull request #9 from KSP2Community/dev
Version 1.2.1
2 parents 8d7edf8 + de7d2fb commit e69c666

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ All life hacks can be toggled on or off in `Main menu` -> `Settings` -> `Mods` -
4040
take effect after the game is restarted. Life hacks are enabled by default unless otherwise noted.
4141
## Contributing
4242
If you'd like to contribute to this project, please take a look at
43-
[our wiki](https://github.com/KSP2Community/KerbalLifeHacks/wiki/Adding-your-hack).
43+
[our wiki](https://github.com/KSP2Community/KerbalLifeHacks/wiki/Adding-your-hack).
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Key,Type,Desc,English
2-
KerbalLifeHacks/Map/WarpToAp,text,,Warp to Apoapsis
3-
KerbalLifeHacks/Map/WarpToPe,text,,Warp to Periapsis
4-
KerbalLifeHacks/Map/WarpToSOI,text,,Warp to SOI Change
1+
Key,Type,Desc,English,Chinese (Simplified)
2+
KerbalLifeHacks/Map/WarpToAp,text,,Warp to Apoapsis,时间加速至远点
3+
KerbalLifeHacks/Map/WarpToPe,text,,Warp to Periapsis,时间加速至近点
4+
KerbalLifeHacks/Map/WarpToSOI,text,,Warp to SOI Change,时间加速至引力作用范围变化

plugin_template/swinfo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "Kerbal Life Hacks",
66
"description": "A counterpart to Community Fixes which contains quality of life improvements and tuning tweaks.",
77
"source": "https://github.com/KSP2Community/KerbalLifeHacks",
8-
"version": "1.2.0",
8+
"version": "1.2.1",
99
"version_check": "https://raw.githubusercontent.com/KSP2Community/KerbalLifeHacks/main/plugin_template/swinfo.json",
1010
"ksp2_version": {
1111
"min": "0.2.0",

src/KerbalLifeHacks/Hacks/IVAPortraitsToggler/AppBarButton.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace KerbalLifeHacks.Hacks.IVAPortraitsToggler;
1111
internal class AppBarButton
1212
{
1313
private readonly UIValue_WriteBool_Toggle _buttonState;
14+
private readonly GameObject _button;
1415

1516
public AppBarButton(
1617
string buttonTooltip,
@@ -35,35 +36,35 @@ public AppBarButton(
3536

3637
// Clone 'NonStageable-Resources' button
3738

38-
var barButton = UnityObject.Instantiate(nonStageableResources, list.transform);
39+
_button = UnityObject.Instantiate(nonStageableResources, list.transform);
3940
if (siblingIndex >= 0 && siblingIndex < list.transform.childCount - 1)
4041
{
41-
barButton.transform.SetSiblingIndex(siblingIndex);
42+
_button.transform.SetSiblingIndex(siblingIndex);
4243
}
4344

44-
barButton.name = buttonId;
45+
_button.name = buttonId;
4546

4647
// Change the tooltip
47-
barButton.GetComponent<BasicTextTooltipData>()._tooltipTitleKey = buttonTooltip;
48+
_button.GetComponent<BasicTextTooltipData>()._tooltipTitleKey = buttonTooltip;
4849

4950
// Change the icon
5051
var sprite = Appbar.GetAppBarIconFromTexture(buttonIcon);
51-
var icon = barButton.GetChild("Content").GetChild("GRP-icon");
52+
var icon = _button.GetChild("Content").GetChild("GRP-icon");
5253
var image = icon.GetChild("ICO-asset").GetComponent<Image>();
5354
image.sprite = sprite;
5455

5556
// Add our function call to the toggle
56-
var toggle = barButton.GetComponent<ToggleExtended>();
57+
var toggle = _button.GetComponent<ToggleExtended>();
5758
toggle.onValueChanged.AddListener(state => function(state));
58-
toggle.onValueChanged.AddListener(SetButtonState);
59+
toggle.onValueChanged.AddListener(SetState);
5960

6061
// Set the initial state of the button
61-
_buttonState = barButton.GetComponent<UIValue_WriteBool_Toggle>();
62+
_buttonState = _button.GetComponent<UIValue_WriteBool_Toggle>();
6263
_buttonState.valueKey = $"Is{buttonId}Visible";
6364
_buttonState.BindValue(new Property<bool>(false));
6465
}
6566

66-
public void SetButtonState(bool state)
67+
public void SetState(bool state)
6768
{
6869
if (_buttonState == null)
6970
{
@@ -72,4 +73,14 @@ public void SetButtonState(bool state)
7273

7374
_buttonState.SetValue(state);
7475
}
76+
77+
public void SetActive(bool isActive = true)
78+
{
79+
if (_button == null)
80+
{
81+
return;
82+
}
83+
84+
_button.SetActive(isActive);
85+
}
7586
}
Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using KSP.Game;
1+
using System.Collections;
2+
using KSP.Game;
23
using KSP.Messages;
4+
using KSP.Sim.impl;
35
using SpaceWarp.API.Assets;
6+
using SpaceWarp.API.Game;
47
using UnityEngine;
58

69
namespace KerbalLifeHacks.Hacks.IVAPortraitsToggler;
@@ -11,7 +14,7 @@ public class IVAPortraitsToggler : BaseHack
1114
private AppBarButton _buttonBar;
1215

1316
// ReSharper disable once InconsistentNaming, IdentifierTypo
14-
private Canvas _ivaportraits_canvas;
17+
private GameObject _ivaportraits;
1518

1619
public override void OnInitialized()
1720
{
@@ -21,29 +24,40 @@ public override void OnInitialized()
2124

2225
private void OnFlightViewEnteredMessage(MessageCenterMessage msg)
2326
{
24-
if (_ivaportraits_canvas == null)
27+
if (_ivaportraits == null)
2528
{
2629
var instruments = GameManager.Instance.Game.UI.FlightHud._instruments;
2730
// ReSharper disable once StringLiteralTypo
2831
instruments.TryGetValue("group_ivaportraits", out var ivaPortraits);
2932
if (ivaPortraits != null)
3033
{
31-
_ivaportraits_canvas = ivaPortraits._parentCanvas;
34+
_ivaportraits = ivaPortraits._parentCanvas.gameObject;
3235
}
3336
}
3437

3538
if (_buttonBar != null)
3639
{
40+
_buttonBar.SetActive();
3741
return;
3842
}
3943

4044
_buttonBar = new AppBarButton(
4145
"IVA Portraits",
4246
"BTN-IVA-Portraits",
4347
AssetManager.GetAsset<Texture2D>($"KerbalLifeHacks/images/IVAPortraitsToggler-icon.png"),
44-
ToggleIVAPortraitsCanvas,
48+
SetIVAPortraitsState,
4549
0
4650
);
51+
_buttonBar.SetActive();
52+
53+
try
54+
{
55+
StartCoroutine(HandleVessel(Vehicle.ActiveSimVessel.GetControlOwner().GlobalId));
56+
}
57+
catch
58+
{
59+
// ignored
60+
}
4761
}
4862

4963
private void OnVesselChangedMessage(MessageCenterMessage msg)
@@ -54,16 +68,23 @@ private void OnVesselChangedMessage(MessageCenterMessage msg)
5468
}
5569

5670
var vesselGuid = vessel.GetControlOwner().GlobalId;
71+
StartCoroutine(HandleVessel(vesselGuid));
72+
}
73+
74+
private IEnumerator HandleVessel(IGGuid vesselGuid)
75+
{
76+
yield return new WaitForUpdate();
77+
5778
var allKerbalsInSimObject = GameManager.Instance.Game.KerbalManager._kerbalRosterManager
5879
?.GetAllKerbalsInSimObject(vesselGuid);
5980
var state = allKerbalsInSimObject?.Count > 0;
6081

61-
ToggleIVAPortraitsCanvas(state);
62-
_buttonBar.SetButtonState(state);
82+
SetIVAPortraitsState(state);
83+
_buttonBar.SetState(state);
6384
}
6485

65-
public void ToggleIVAPortraitsCanvas(bool state)
86+
public void SetIVAPortraitsState(bool state)
6687
{
67-
_ivaportraits_canvas.enabled = state;
88+
_ivaportraits.SetActive(state);
6889
}
6990
}

0 commit comments

Comments
 (0)