Skip to content

Commit

Permalink
Merge pull request #9 from KSP2Community/dev
Browse files Browse the repository at this point in the history
Version 1.2.1
  • Loading branch information
jan-bures authored Jan 24, 2024
2 parents 8d7edf8 + de7d2fb commit e69c666
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ All life hacks can be toggled on or off in `Main menu` -> `Settings` -> `Mods` -
take effect after the game is restarted. Life hacks are enabled by default unless otherwise noted.
## Contributing
If you'd like to contribute to this project, please take a look at
[our wiki](https://github.com/KSP2Community/KerbalLifeHacks/wiki/Adding-your-hack).
[our wiki](https://github.com/KSP2Community/KerbalLifeHacks/wiki/Adding-your-hack).
8 changes: 4 additions & 4 deletions plugin_template/localizations/warp_to_orbital_point.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Key,Type,Desc,English
KerbalLifeHacks/Map/WarpToAp,text,,Warp to Apoapsis
KerbalLifeHacks/Map/WarpToPe,text,,Warp to Periapsis
KerbalLifeHacks/Map/WarpToSOI,text,,Warp to SOI Change
Key,Type,Desc,English,Chinese (Simplified)
KerbalLifeHacks/Map/WarpToAp,text,,Warp to Apoapsis,时间加速至远点
KerbalLifeHacks/Map/WarpToPe,text,,Warp to Periapsis,时间加速至近点
KerbalLifeHacks/Map/WarpToSOI,text,,Warp to SOI Change,时间加速至引力作用范围变化
2 changes: 1 addition & 1 deletion plugin_template/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Kerbal Life Hacks",
"description": "A counterpart to Community Fixes which contains quality of life improvements and tuning tweaks.",
"source": "https://github.com/KSP2Community/KerbalLifeHacks",
"version": "1.2.0",
"version": "1.2.1",
"version_check": "https://raw.githubusercontent.com/KSP2Community/KerbalLifeHacks/main/plugin_template/swinfo.json",
"ksp2_version": {
"min": "0.2.0",
Expand Down
29 changes: 20 additions & 9 deletions src/KerbalLifeHacks/Hacks/IVAPortraitsToggler/AppBarButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace KerbalLifeHacks.Hacks.IVAPortraitsToggler;
internal class AppBarButton
{
private readonly UIValue_WriteBool_Toggle _buttonState;
private readonly GameObject _button;

public AppBarButton(
string buttonTooltip,
Expand All @@ -35,35 +36,35 @@ public AppBarButton(

// Clone 'NonStageable-Resources' button

var barButton = UnityObject.Instantiate(nonStageableResources, list.transform);
_button = UnityObject.Instantiate(nonStageableResources, list.transform);
if (siblingIndex >= 0 && siblingIndex < list.transform.childCount - 1)
{
barButton.transform.SetSiblingIndex(siblingIndex);
_button.transform.SetSiblingIndex(siblingIndex);
}

barButton.name = buttonId;
_button.name = buttonId;

// Change the tooltip
barButton.GetComponent<BasicTextTooltipData>()._tooltipTitleKey = buttonTooltip;
_button.GetComponent<BasicTextTooltipData>()._tooltipTitleKey = buttonTooltip;

// Change the icon
var sprite = Appbar.GetAppBarIconFromTexture(buttonIcon);
var icon = barButton.GetChild("Content").GetChild("GRP-icon");
var icon = _button.GetChild("Content").GetChild("GRP-icon");
var image = icon.GetChild("ICO-asset").GetComponent<Image>();
image.sprite = sprite;

// Add our function call to the toggle
var toggle = barButton.GetComponent<ToggleExtended>();
var toggle = _button.GetComponent<ToggleExtended>();
toggle.onValueChanged.AddListener(state => function(state));
toggle.onValueChanged.AddListener(SetButtonState);
toggle.onValueChanged.AddListener(SetState);

// Set the initial state of the button
_buttonState = barButton.GetComponent<UIValue_WriteBool_Toggle>();
_buttonState = _button.GetComponent<UIValue_WriteBool_Toggle>();
_buttonState.valueKey = $"Is{buttonId}Visible";
_buttonState.BindValue(new Property<bool>(false));
}

public void SetButtonState(bool state)
public void SetState(bool state)
{
if (_buttonState == null)
{
Expand All @@ -72,4 +73,14 @@ public void SetButtonState(bool state)

_buttonState.SetValue(state);
}

public void SetActive(bool isActive = true)
{
if (_button == null)
{
return;
}

_button.SetActive(isActive);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using KSP.Game;
using System.Collections;
using KSP.Game;
using KSP.Messages;
using KSP.Sim.impl;
using SpaceWarp.API.Assets;
using SpaceWarp.API.Game;
using UnityEngine;

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

// ReSharper disable once InconsistentNaming, IdentifierTypo
private Canvas _ivaportraits_canvas;
private GameObject _ivaportraits;

public override void OnInitialized()
{
Expand All @@ -21,29 +24,40 @@ public override void OnInitialized()

private void OnFlightViewEnteredMessage(MessageCenterMessage msg)
{
if (_ivaportraits_canvas == null)
if (_ivaportraits == null)
{
var instruments = GameManager.Instance.Game.UI.FlightHud._instruments;
// ReSharper disable once StringLiteralTypo
instruments.TryGetValue("group_ivaportraits", out var ivaPortraits);
if (ivaPortraits != null)
{
_ivaportraits_canvas = ivaPortraits._parentCanvas;
_ivaportraits = ivaPortraits._parentCanvas.gameObject;
}
}

if (_buttonBar != null)
{
_buttonBar.SetActive();
return;
}

_buttonBar = new AppBarButton(
"IVA Portraits",
"BTN-IVA-Portraits",
AssetManager.GetAsset<Texture2D>($"KerbalLifeHacks/images/IVAPortraitsToggler-icon.png"),
ToggleIVAPortraitsCanvas,
SetIVAPortraitsState,
0
);
_buttonBar.SetActive();

try
{
StartCoroutine(HandleVessel(Vehicle.ActiveSimVessel.GetControlOwner().GlobalId));
}
catch
{
// ignored
}
}

private void OnVesselChangedMessage(MessageCenterMessage msg)
Expand All @@ -54,16 +68,23 @@ private void OnVesselChangedMessage(MessageCenterMessage msg)
}

var vesselGuid = vessel.GetControlOwner().GlobalId;
StartCoroutine(HandleVessel(vesselGuid));
}

private IEnumerator HandleVessel(IGGuid vesselGuid)
{
yield return new WaitForUpdate();

var allKerbalsInSimObject = GameManager.Instance.Game.KerbalManager._kerbalRosterManager
?.GetAllKerbalsInSimObject(vesselGuid);
var state = allKerbalsInSimObject?.Count > 0;

ToggleIVAPortraitsCanvas(state);
_buttonBar.SetButtonState(state);
SetIVAPortraitsState(state);
_buttonBar.SetState(state);
}

public void ToggleIVAPortraitsCanvas(bool state)
public void SetIVAPortraitsState(bool state)
{
_ivaportraits_canvas.enabled = state;
_ivaportraits.SetActive(state);
}
}

0 comments on commit e69c666

Please sign in to comment.