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

Commit

Permalink
Settings moved inside PluginData, Settings class handles all settings…
Browse files Browse the repository at this point in the history
… related tasks
  • Loading branch information
Crzyrndm committed Mar 9, 2016
1 parent 3280a3a commit 40df0a2
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 57 deletions.
2 changes: 1 addition & 1 deletion FilterExtension/ConfigNodes/customSubCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public bool checkFilters(AvailablePart part, int depth = 0)
if (part.category == PartCategories.none && Editor.blackListedParts.Contains(part.name))
return false;
}
if (!unPurchasedOverride && Core.Instance.hideUnpurchased && !ResearchAndDevelopment.PartModelPurchased(part) && !ResearchAndDevelopment.IsExperimentalPart(part))
if (!unPurchasedOverride && Settings.hideUnpurchased && !ResearchAndDevelopment.PartModelPurchased(part) && !ResearchAndDevelopment.IsExperimentalPart(part))
return false;

PartModuleFilter pmf;
Expand Down
32 changes: 8 additions & 24 deletions FilterExtension/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ public class Core : MonoBehaviour


// Config has options to disable the FbM replacement, and the default Category/SC and sort method
public bool hideUnpurchased = true;
public bool debug = false;
public bool setAdvanced = true;
public bool replaceFbM = true;
public string categoryDefault = "";
public string subCategoryDefault = "";


const string fallbackIcon = "stockIcon_fallback";

Expand Down Expand Up @@ -75,18 +70,7 @@ void Awake()
/// </summary>
private void getConfigs()
{
ConfigNode settings = GameDatabase.Instance.GetConfigNodes("FilterSettings").FirstOrDefault();
if (settings != null)
{
bool.TryParse(settings.GetValue("hideUnpurchased"), out hideUnpurchased);
bool.TryParse(settings.GetValue("debug"), out debug);
if (!bool.TryParse(settings.GetValue("setAdvanced"), out setAdvanced))
setAdvanced = true;
if (!bool.TryParse(settings.GetValue("replaceFbM"), out replaceFbM))
replaceFbM = true;
categoryDefault = settings.GetValue("categoryDefault");
subCategoryDefault = settings.GetValue("subCategoryDefault");
}
Settings.LoadSettings();

ConfigNode[] nodes = GameDatabase.Instance.GetConfigNodes("FilterRename");
for (int i = 0; i < nodes.Length; i++)
Expand Down Expand Up @@ -179,8 +163,8 @@ private void getPartData()
if (p.partPrefab.Modules.Contains("PartModuleFilter"))
filterModules.Add(p.name, (PartModuleFilter)p.partPrefab.Modules["PartModuleFilter"]);
}
if (replaceFbM)

if (Settings.replaceFbM)
processFilterByManufacturer(modNames);
}

Expand Down Expand Up @@ -371,8 +355,8 @@ public static void setSelectedCategory()
PartCategorizer.Category Filter = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.activeButton.State == RUIToggleButtonTyped.ButtonState.TRUE);
if (Filter != null)
Filter.button.activeButton.SetFalse(Filter.button.activeButton, RUIToggleButtonTyped.ClickType.FORCED);
Filter = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.categoryName == instance.categoryDefault);

Filter = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.categoryName == Settings.categoryDefault);
if (Filter != null)
Filter.button.activeButton.SetTrue(Filter.button.activeButton, RUIToggleButtonTyped.ClickType.FORCED);
else
Expand Down Expand Up @@ -499,7 +483,7 @@ private static void loadIcons()
i++;
if (i != 1000)
name = name + i.ToString();
if (instance.debug)
if (Settings.debug)
Log("Duplicated texture name \"" + t.name.Split(new char[] { '/', '\\' }).Last() + "\" at:\r\n" + t.name + "\r\n New reference is: " + name);
}

Expand Down Expand Up @@ -571,7 +555,7 @@ public static bool checkSubCategoryHasParts(customSubCategory sC, string categor
return true;
}

if (instance.debug)
if (Settings.debug)
{
if (!string.IsNullOrEmpty(category))
Log(sC.subCategoryTitle + " in category " + category + " has no valid parts and was not initialised");
Expand Down
12 changes: 6 additions & 6 deletions FilterExtension/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ IEnumerator editorInit()

while (PartCategorizer.Instance == null)
yield return null;
if (Core.Instance.debug)
if (Settings.debug)
Core.Log("Starting on Stock Filters");
// stock filters
// If I edit them later everything breaks
Expand All @@ -55,7 +55,7 @@ IEnumerator editorInit()
// frames after the flag is set to wait before initialising. Minimum of two for things to work consistently
for (int i = 0; i < 4; i++)
yield return null;
if (Core.Instance.debug)
if (Settings.debug)
Core.Log("Starting on general categories");

// all FE categories
Expand All @@ -68,7 +68,7 @@ IEnumerator editorInit()
// wait again so icon edits don't occur immediately and cause breakages
for (int i = 0; i < 4; i++)
yield return null;
if (Core.Instance.debug)
if (Settings.debug)
Core.Log("Starting on late categories");

// generate the set of parts to block
Expand All @@ -92,7 +92,7 @@ IEnumerator editorInit()
// Remove any category with no subCategories (causes major breakages if selected).
for (int i = 0; i < 4; i++)
yield return null;
if (Core.Instance.debug)
if (Settings.debug)
Core.Log("Starting on removing categories");
List<PartCategorizer.Category> catsToDelete = PartCategorizer.Instance.filters.FindAll(c => c.subcategories.Count == 0);
foreach (PartCategorizer.Category cat in catsToDelete)
Expand All @@ -102,12 +102,12 @@ IEnumerator editorInit()
}

// make the categories visible
if (Core.Instance.setAdvanced)
if (Settings.setAdvanced)
PartCategorizer.Instance.SetAdvancedMode();

for (int i = 0; i < 4; i++)
yield return null;
if (Core.Instance.debug)
if (Settings.debug)
Core.Log("Refreshing parts list");
Core.setSelectedCategory();

Expand Down
1 change: 1 addition & 0 deletions FilterExtension/FilterExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="PartModuleFilter.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Utility\Extensions.cs" />
<Compile Include="Utility\GUIUtils.cs" />
<Compile Include="Utility\PartType.cs" />
<Compile Include="ConfigNodes\Filter.cs" />
<Compile Include="ConfigNodes\customSubCategory.cs" />
Expand Down
85 changes: 59 additions & 26 deletions FilterExtension/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using UnityEngine;

namespace FilterExtensions
{
using Utility;

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
class Settings : MonoBehaviour
{
Rect settingsRect = new Rect(Screen.width / 2, Screen.height / 2, 400, 0);
static bool showWindow;
private static ApplicationLauncherButton btnLauncher;

public const string RelativeSettingsPath = "GameData/000_FilterExtensions/PluginData/";

public static bool hideUnpurchased = true;
public static bool debug = false;
public static bool setAdvanced = true;
public static bool replaceFbM = true;
public static string categoryDefault = "";
public static string subCategoryDefault = "";

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

LoadSettings();
}

public void OnDestroy()
{
ConfigNode settingsNode = new ConfigNode("FilterSettings");
settingsNode.AddValue("hideUnpurchased", Core.Instance.hideUnpurchased);
settingsNode.AddValue("debug", Core.Instance.debug);
settingsNode.AddValue("setAdvanced", Core.Instance.setAdvanced);
settingsNode.AddValue("replaceFbM", Core.Instance.replaceFbM);
settingsNode.AddValue("categoryDefault", Core.Instance.categoryDefault);
settingsNode.AddValue("subCategoryDefault", Core.Instance.subCategoryDefault);

ConfigNode nodeToWrite = new ConfigNode();
nodeToWrite.AddNode(settingsNode);

#warning Need to move the save location inside PluginData so MM can't see it, cache it, and then recache it when settings are changed (or maybe just change the extension. Same effect though)
nodeToWrite.Save(KSPUtil.ApplicationRootPath.Replace("\\", "/") + "GameData/000_FilterExtensions/Settings.cfg");
SaveSettings();
}

public void OnGUI()
Expand All @@ -46,22 +48,53 @@ public void OnGUI()
settingsRect = GUILayout.Window(6548792, settingsRect, drawWindow, "Filter Extensions Settings");
}

private void drawWindow(int id)
public static void LoadSettings()
{
Core.Instance.debug = GUILayout.Toggle(Core.Instance.debug, "Enable logging");
Core.Instance.setAdvanced = GUILayout.Toggle(Core.Instance.setAdvanced, "Default to Advanced mode");
Core.Instance.hideUnpurchased = GUILayout.Toggle(Core.Instance.hideUnpurchased, "Hide unpurchased parts");
Core.Instance.replaceFbM = GUILayout.Toggle(Core.Instance.replaceFbM, "Sort parts by folder in manufacturer tab (requires restart)");
if (File.Exists(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath + "Settings.cfg"))
{
ConfigNode settings = ConfigNode.Load(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath + "Settings.cfg");
if (settings != null)
{
bool.TryParse(settings.GetValue("hideUnpurchased"), out hideUnpurchased);
bool.TryParse(settings.GetValue("debug"), out debug);
if (!bool.TryParse(settings.GetValue("setAdvanced"), out setAdvanced))
setAdvanced = true;
if (!bool.TryParse(settings.GetValue("replaceFbM"), out replaceFbM))
replaceFbM = true;
categoryDefault = settings.GetValue("categoryDefault");
if (categoryDefault == null)
categoryDefault = string.Empty;
subCategoryDefault = settings.GetValue("subCategoryDefault");
if (subCategoryDefault == null)
subCategoryDefault = string.Empty;
}
}
}

public static void SaveSettings()
{
ConfigNode settingsNode = new ConfigNode("FilterSettings");
settingsNode.AddValue("hideUnpurchased", hideUnpurchased);
settingsNode.AddValue("debug", debug);
settingsNode.AddValue("setAdvanced", setAdvanced);
settingsNode.AddValue("replaceFbM", replaceFbM);
settingsNode.AddValue("categoryDefault", categoryDefault);
settingsNode.AddValue("subCategoryDefault", subCategoryDefault);

GUILayout.BeginHorizontal();
GUILayout.Label("Default Category");
Core.Instance.categoryDefault = GUILayout.TextField(Core.Instance.categoryDefault);
GUILayout.EndHorizontal();
if (!Directory.Exists(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath))
Directory.CreateDirectory(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath);
settingsNode.Save(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath + "Settings.cfg");
}

private void drawWindow(int id)
{
debug = GUILayout.Toggle(debug, "Enable logging");
setAdvanced = GUILayout.Toggle(setAdvanced, "Default to Advanced mode");
hideUnpurchased = GUILayout.Toggle(hideUnpurchased, "Hide unpurchased parts");
replaceFbM = GUILayout.Toggle(replaceFbM, "Sort parts by folder in manufacturer tab (requires restart)");

GUILayout.BeginHorizontal();
GUILayout.Label("Default Sub-category");
Core.Instance.subCategoryDefault = GUILayout.TextField(Core.Instance.subCategoryDefault);
GUILayout.EndHorizontal();
GUIUtils.DrawLabelPlusBox("Default Category", ref categoryDefault);
GUIUtils.DrawLabelPlusBox("Default Sub-category", ref subCategoryDefault);

GUI.DragWindow();
}
Expand Down
24 changes: 24 additions & 0 deletions FilterExtension/Utility/GUIUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace FilterExtensions.Utility
{
public static class GUIUtils
{
/// <summary>
/// Draws a label and text box on the same row
/// </summary>
/// <param name="label"></param>
/// <param name="text"></param>
public static void DrawLabelPlusBox(string label, ref string text)
{
GUILayout.BeginHorizontal();
GUILayout.Label(label);
text = GUILayout.TextField(text);
GUILayout.EndHorizontal();
}
}
}
Binary file modified GameData/000_FilterExtensions/FilterExtensions.dll
Binary file not shown.

0 comments on commit 40df0a2

Please sign in to comment.