Jump to content

Recommended Posts

I'm aware it's very basic so I'm sorry if it's been answered before. I have looked.

This about the third time I've tried to get into modding ksp and each time I end up drowning in data I either don't need or that's out dated.

I'm a descent programmer and have plenty of time to fiddle with things to figure them out, but I at least need a jumping off point.

What I need is an in flight app button that opens a "hello world" GUI page. From there I'm sure I can solve most of my questions myself.

Thanks much.

Link to comment
Share on other sites

Hey!  I know exactly how you feel.  Modding KSP can feel...suffocating.  Here's my current class for a GUI that I've been reverse-engineering.  I hope it helps!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using UnityEngine;
using KSP;
using KSP.UI.Screens;//required for ApplicationLauncherButton type

namespace PopulationMod
{
  [KSPAddon(KSPAddon.Startup.EveryScene, false)]

  public class PopModGUI : MonoBehaviour
  {
    internal static string assetFolder = "PopulationMod/CivilianManagement/Assets/";
    private static ApplicationLauncherButton CivPopButton = null;
    static bool CivPopGUIOn = false;
    internal bool CivPopTooltip = false;
    private GUIStyle _windowstyle, _labelstyle;
    private bool hasInitStyles = false;

    private static ApplicationLauncherButton appButton = null;

    /// <summary>
    /// Awake this instance.  Pre-existing method in Unity that runs before KSP loads.
    /// </summary>
    public void Awake()
    {
      //Debug.Log (debuggingClass.modName + "Starting Awake()");//What I am using to debug
      DontDestroyOnLoad(this);
      GameEvents.onGUIApplicationLauncherReady.Add(OnAppLauncherReady);//when AppLauncher can take apps, give it OnAppLauncherReady (mine)
      GameEvents.onGUIApplicationLauncherDestroyed.Add (OnAppLauncherDestroyed);//Not sure what this does
    }

    public void OnAppLauncherDestroyed(){
      if (appButton != null){
        OnToggleFalse ();
        ApplicationLauncher.Instance.RemoveApplication (appButton);
      }
    }
      

    /// <summary>
    /// Raises the app launcher ready event.  Method to create an app button on the AppLauncher, as well as tell
    /// what/how the GUI is loaded.
    /// </summary>
    public void OnAppLauncherReady()
    {
      InitStyle();
      string iconFile = "PopModIcon";//This is the name of the file that stores the mod's icon to be used in the appLauncher
      if (HighLogic.LoadedScene == GameScenes.SPACECENTER && appButton == null){//i.e. if running for the first time
        appButton = ApplicationLauncher.Instance.AddModApplication(
          OnToggleTrue,                           //Run OnToggleTrue() when user clicks button
          OnToggleFalse,                          //Run OnToggleFalse() when user clicks button again
          null, null, null, null,                 //do nothing during hover, exiting, enable/disable
          ApplicationLauncher.AppScenes.ALWAYS,   //When to show applauncher/GUI button
          GameDatabase.Instance.GetTexture(assetFolder+iconFile , false));//where to look for mod applauncher icon
        //Debug.Log (debuggingClass.modName + "Finishing Awake()");//What I am using to debug
      }
      CivPopGUIOn = false;
    }


    /// <summary>
    /// Presumably what to do when the user opens/clicks the button.  Called from OnAppLauncherReady.
    /// </summary>
    private static void OnToggleTrue()
    {
      //Debug.Log (debuggingClass.modName + "Starting OnToggleTrue()");
      CivPopGUIOn = true;//turns on flag for GUI
      //Debug.Log (debuggingClass.modName + "Turning on GUI");
    }

    /// <summary>
    /// Presumably what to do when the user closes the button.  Called from OnAppLauncherReady.
    /// </summary>
    private static void OnToggleFalse()
    {
      //Debug.Log (debuggingClass.modName + "Starting OnToggleFalse()");
      CivPopGUIOn = false;//turns off flag for GUI
      //Debug.Log (debuggingClass.modName + "Turning off GUI");
    }
      
    /// <summary>
    /// I'm not sure what this is for...but it was already here and it seems to work.
    /// </summary>
    private void InitStyle()
    {
      _windowstyle = new GUIStyle(HighLogic.Skin.window);
      _labelstyle = new GUIStyle(HighLogic.Skin.label);
      hasInitStyles = true;
    }

    /// <summary>
    /// OnGUI() is called by the game and every time it refreshes the GUI.  I just need it to check if the GUI is
    /// enabled and if it is, show it.
    /// </summary>
    public void OnGUI()
    {//Executes code whenever screen refreshes.  Extension to enable use of button along main bar on top-right of screen.
      if (CivPopGUIOn) {
        PopulationManagementGUI ();//If button is enabled, display rectangle.
      }//end if
    }//end OnGui extension

    /// <summary>
    /// This method controls how the window actually looks when the HUD window is displayed.
    /// </summary>
    void PopulationManagementGUI(){
      GUI.BeginGroup(new Rect(Screen.width / 2 - 250, Screen.height / 2 - 250, 500, 500));
      GUILayout.BeginVertical("box");
      GUILayout.Label("CIVPOP PlaceHolder GUI");
      if (GUILayout.Button("Close this Window", GUILayout.Width(200f)))
        OnToggleFalse();
      GUILayout.Label ("Can this display?");
      GUILayout.EndVertical();
      GUI.EndGroup();
    }
  }
}

A quick note:  I am using DebuggingClass to debug and it should be able to be ignored.  Debug.Log ("message") prints its contents (message) to the KSP.log file.  

Link to comment
Share on other sites

At a quick glance, things look right. I've never used the BeginGroup method, I use the Window method instead though.

Having said that, can you stick a Debug.Log inside your PopulationManagemenGUI method just to make sure the method is being called correctly?

D.

edit: If you are okay switching to the GUI.Window method, I use it here: https://github.com/SirDiazo/TWR1/blob/master/TWR1/TWR1.cs#L393

to generate this window:Controls.jpg

The linked code creates two windows, this picture is the first one at line 397.

Edited by Diazo
Link to comment
Share on other sites

@JediMasterSterling1

I've been using the New Community API Documentation as well as the source code for a few mods. 

 

edit:  Oops.  I think I misunderstood.  IIRC I only need references to Assembly-CSharp.dll, KSPUtil.dll, and UnityEngine.dll.

But just in case, here are what I currently have enabled for the whole project (of which only some are needed):

Assembly-CSharp.dll

Assembly-CSharp-firstpass.dll

KSPCore.dll

KSPUtil.dll

SaveUpgradeEnginePipeline.Core.dll

UnityEngine.dll

UnityEngine.UI.dll

Edited by Tralfagar
Added References
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...