Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
playing with the new UI
Browse files Browse the repository at this point in the history
Settings window made with a UI prefab, is not draggable so far but
otherwise appears to work as advertised
  • Loading branch information
Crzyrndm committed May 10, 2016
1 parent 82e9231 commit 51cf976
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
4 changes: 4 additions & 0 deletions FilterExtension/FilterExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<HintPath>..\..\..\Desktop\Kerbal Space Program Dev\KSP_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="KSPAssets">
<HintPath>..\..\..\Desktop\Kerbal Space Program Dev\KSP_Data\Managed\KSPAssets.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="KSPUtil">
<HintPath>..\..\..\Desktop\Kerbal Space Program Dev\KSP_Data\Managed\KSPUtil.dll</HintPath>
<Private>False</Private>
Expand Down
123 changes: 120 additions & 3 deletions FilterExtension/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
using System.Linq;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

namespace FilterExtensions
{
using Utility;
using KSP.UI.Screens;
using KSP.UI;

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
class Settings : MonoBehaviour
{
static Canvas settingsCanvasPrefab;
static Canvas windowInstance;

Rect settingsRect = new Rect(Screen.width / 2, Screen.height / 2, 400, 0);
static bool showWindow;
private static ApplicationLauncherButton btnLauncher;
Expand All @@ -22,23 +27,29 @@ class Settings : MonoBehaviour
public static bool debug = false;
public static bool setAdvanced = true;
public static bool replaceFbM = true;
public static string categoryDefault = "";
public static string subCategoryDefault = "";
public static string categoryDefault = string.Empty;
public static string subCategoryDefault = string.Empty;

public void Start()
{
showWindow = false;
if (btnLauncher == null)
btnLauncher = ApplicationLauncher.Instance.AddModApplication(() => showWindow = !showWindow, () => showWindow = !showWindow,
btnLauncher = ApplicationLauncher.Instance.AddModApplication(toggleSettingsVisible, toggleSettingsVisible,
null, null, null, null, ApplicationLauncher.AppScenes.SPACECENTER,
GameDatabase.Instance.GetTexture("000_FilterExtensions/Icons/FilterCreator", false));

LoadSettings();

if (settingsCanvasPrefab == null)
KSPAssets.Loaders.AssetLoader.LoadAssets(bundleLoaded, KSPAssets.Loaders.AssetLoader.GetAssetDefinitionWithName("000_FilterExtensions/fesettings", "SettingsPanel"));
else
InstantiateCanvas();
}

public void OnDestroy()
{
SaveSettings();
windowInstance = null;
}

public void OnGUI()
Expand Down Expand Up @@ -99,5 +110,111 @@ private void drawWindow(int id)

GUI.DragWindow();
}

void bundleLoaded(KSPAssets.Loaders.AssetLoader.Loader loader)
{
for (int i = 0; i < loader.definitions.Length; ++i)
{
UnityEngine.Object obj = loader.objects[i];
if (obj == null || obj.name != "SettingsPanel")
continue;

Canvas c = (obj as GameObject).GetComponent<Canvas>();
if (c != null)
{
settingsCanvasPrefab = c;
break;
}
}
if (settingsCanvasPrefab == null)
{
Core.Log("No settings canvas prefab found", Core.LogLevel.Warn);
return;
}
settingsCanvasPrefab.enabled = false;
InstantiateCanvas();
}

static void toggleSettingsVisible()
{
if (windowInstance != null)
{
windowInstance.enabled = !windowInstance.enabled;
}
}

private void InstantiateCanvas()
{
windowInstance = Instantiate(settingsCanvasPrefab);

Toggle[] boolSettings = windowInstance.GetComponentsInChildren<Toggle>();
foreach (Toggle t in boolSettings)
{
switch (t.name)
{
case "tgl_Debug":
t.isOn = Settings.debug;
t.onValueChanged.AddListener(dbg_toggleChanged);
break;
case "tgl_unpurchased":
t.isOn = Settings.hideUnpurchased;
t.onValueChanged.AddListener(hide_toggleChanged);
break;
case "tgl_advanced":
t.isOn = Settings.setAdvanced;
t.onValueChanged.AddListener(setAdv_toggleChanged);
break;
case "tgl_replaceFBM":
t.isOn = Settings.hideUnpurchased;
t.onValueChanged.AddListener(rplFbM_toggleChanged);
break;
}
}
InputField[] textSettings = windowInstance.GetComponentsInChildren<InputField>();
foreach (InputField input in textSettings)
{
switch (input.name)
{
case "input_category":
input.text = Settings.categoryDefault;
input.onValueChange.AddListener(cat_txtInputChanged);
break;
case "input_subcategory":
input.text = Settings.subCategoryDefault;
input.onValueChange.AddListener(subCat_txtInputChanged);
break;
}
}
}

public void dbg_toggleChanged(bool newValue)
{
Settings.debug = newValue;
}

public void hide_toggleChanged(bool newValue)
{
Settings.hideUnpurchased = newValue;
}

public void setAdv_toggleChanged(bool newValue)
{
Settings.setAdvanced = newValue;
}

public void rplFbM_toggleChanged(bool newValue)
{
Settings.replaceFbM = newValue;
}

public void cat_txtInputChanged(string newValue)
{
Settings.categoryDefault = newValue;
}

public void subCat_txtInputChanged(string newValue)
{
Settings.subCategoryDefault = newValue;
}
}
}
Binary file modified GameData/000_FilterExtensions/FilterExtensions.dll
Binary file not shown.
Binary file added GameData/000_FilterExtensions/fesettings.ksp
Binary file not shown.

0 comments on commit 51cf976

Please sign in to comment.