Jump to content

[Answered] Detecting tab switches in the Astronaut Complex


Recommended Posts

Is there any way to detect when the users switch tabs in the Astronaut Complex between available, assigned, and KIA kerbals? I can't find any hooks to execute code whenever the tab switches, and I really don't want to have to poll in an OnGUI method.

Edited by JonDahm
Marking as answered.
Link to comment
Share on other sites

You can build yourself a way in. This example attaches a simple MonoBehaviour to each list which invokes a callback when MonoBehaviour.OnEnable is called:

public class ListenerInstaller : MonoBehaviour
{
public Action<AstronautComplexTest.Panel> CallbackAction;

private class ListenForOnEnable : MonoBehaviour
{
public Callback Callback;
private void OnEnable() { if (Callback != null) Callback(); }
}


private void Start()
{
if (CallbackAction == null)
{
Debug.LogError("No CallbackAction set");
return;
}

AddCallbackToPanel("panel_available", AstronautComplexTest.Panel.Available);
AddCallbackToPanel("panel_assigned", AstronautComplexTest.Panel.Assigned);
AddCallbackToPanel("panel_kia", AstronautComplexTest.Panel.Missing);

CallbackAction(AstronautComplexTest.Panel.Available);

Destroy(this);
}


private void AddCallbackToPanel(string panelName, AstronautComplexTest.Panel which)
{
var panel = FindPanel(panelName);
if (panel == null)
{
Debug.LogError("Couldn't find a panel called " + panelName);
return;
}

panel.gameObject.AddComponent<ListenForOnEnable>().Callback = () => CallbackAction(which);
}


private UIPanel FindPanel(string name)
{
return GetComponentsInChildren<UIPanel>().FirstOrDefault(p => p.name == name);
}


public static void Install(Action<AstronautComplexTest.Panel> callback)
{
var complex = UIManager.instance.gameObject.GetComponentsInChildren<CMAstronautComplex>(true).FirstOrDefault();
if (complex == null)
{
Debug.LogError("Didn't find CMAstronautComplex");
return;
}

complex.transform.Find("CrewPanels/panel_enlisted").gameObject.AddComponent<ListenerInstaller>().CallbackAction =
callback;
}
}


[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class AstronautComplexTest : MonoBehaviour
{
public enum Panel
{
Available,
Assigned,
Missing
}


private void Start()
{
ListenerInstaller.Install(OnActivePanelChanged);
}


private void OnActivePanelChanged(Panel which)
{
Debug.LogWarning("Panel changed to " + which);
}
}

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...