Skip to content

Commit

Permalink
Fix all known problems with settings menu
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Sep 8, 2023
1 parent cc558ba commit 1bc95ee
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 47 deletions.
2 changes: 1 addition & 1 deletion SpaceWarp.Core/Backend/Modding/BepInExModAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace SpaceWarp.Backend.Modding;

public class BepInExModAdapter : ISpaceWarpMod
{
public BaseUnityPlugin Plugin;
public readonly BaseUnityPlugin Plugin;

public void OnPreInitialized()
{
Expand Down
24 changes: 15 additions & 9 deletions SpaceWarp.Core/Backend/Modding/PluginRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,21 @@ private static void RegisterSingleBepInExPlugin(BaseUnityPlugin plugin)
}
}

private static SpaceWarpPluginDescriptor GetBepInExDescriptor(BaseUnityPlugin plugin) => new(
null,
plugin.Info.Metadata.GUID,
plugin.Info.Metadata.Name,
BepInExToSWInfo(plugin.Info),
new DirectoryInfo(Path.GetDirectoryName(plugin.Info.Location)!),
false,
new BepInExConfigFile(plugin.Config)
);
private static SpaceWarpPluginDescriptor GetBepInExDescriptor(BaseUnityPlugin plugin)
{
var pluginAdapter = new BepInExModAdapter(plugin);
var descriptor = new SpaceWarpPluginDescriptor(
pluginAdapter,
plugin.Info.Metadata.GUID,
plugin.Info.Metadata.Name,
BepInExToSWInfo(plugin.Info),
new DirectoryInfo(Path.GetDirectoryName(plugin.Info.Location)!),
false,
new BepInExConfigFile(plugin.Config)
);
pluginAdapter.SWMetadata = descriptor;
return descriptor;
}

private static SpaceWarpPluginDescriptor GetBepInExDescriptor(PluginInfo info) => new(
null,
Expand Down
15 changes: 9 additions & 6 deletions SpaceWarp.UI/API/UI/Settings/ModsPropertyDrawers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static Func<ConfigEntryBase, GameObject> GenerateEnumDrawerFor(Type t)
var lab = ddCopy.GetChild("Label");
lab.GetComponent<Localize>().SetTerm(entry.Definition.Key);
lab.GetComponent<TextMeshProUGUI>().text = entry.Definition.Key;
var dropdown = ddCopy.GetChild("Setting").GetChild("KSP2DropDown");
var dropdown = ddCopy.GetChild("Setting").GetChild("BTN-Dropdown");
var extended = dropdown.GetComponent<DropdownExtended>();
// Start by clearing the options data
extended.options.Clear();
Expand Down Expand Up @@ -203,7 +203,7 @@ private static Func<string, IConfigEntry, GameObject> GenerateAbstractEnumDrawer
var lab = ddCopy.GetChild("Label");
lab.GetComponent<Localize>().SetTerm(name);
lab.GetComponent<TextMeshProUGUI>().text = name;
var dropdown = ddCopy.GetChild("Setting").GetChild("KSP2DropDown");
var dropdown = ddCopy.GetChild("Setting").GetChild("BTN-Dropdown");
var extended = dropdown.GetComponent<DropdownExtended>();
// Start by clearing the options data
extended.options.Clear();
Expand Down Expand Up @@ -492,7 +492,7 @@ private static GameObject CreateFromAcceptableValueList<T>(ConfigEntry<T> entry,
var lab = ddCopy.GetChild("Label");
lab.GetComponent<Localize>().SetTerm(entry.Definition.Key);
lab.GetComponent<TextMeshProUGUI>().text = entry.Definition.Key;
var dropdown = ddCopy.GetChild("Setting").GetChild("KSP2DropDown");
var dropdown = ddCopy.GetChild("Setting").GetChild("BTN-Dropdown");
var extended = dropdown.GetComponent<DropdownExtended>();
// Start by clearing the options data
extended.options.Clear();
Expand Down Expand Up @@ -551,12 +551,15 @@ private static GameObject CreateFromAcceptableValueRange<T>(ConfigEntry<T> entry
TypeCode.Single => x => (T)(object)x,
_ => x => throw new NotImplementedException(typeof(T).ToString())
};

slider.minValue = toFloat(acceptableValues.MinValue);
slider.maxValue = toFloat(acceptableValues.MaxValue);
slider.SetValueWithoutNotify(toFloat(entry.Value));
slider.onValueChanged.AddListener(value =>
{
// var trueValue = (acceptableValues.MaxValue-acceptableValues.MinValue) * (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(value)
var trueValue = (toFloat(acceptableValues.MaxValue) - toFloat(acceptableValues.MinValue)) * value +
toFloat(acceptableValues.MinValue);
// var trueValue = (toFloat(acceptableValues.MaxValue) - toFloat(acceptableValues.MinValue)) * value +
// toFloat(acceptableValues.MinValue);
var trueValue = value;
entry.Value = toT(trueValue) ?? entry.Value;
if (entry.Value != null) text.text = entry.Value.ToString();
Expand Down
64 changes: 34 additions & 30 deletions SpaceWarp.UI/UI/Settings/ModsSubMenu.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using KSP.UI;
using SpaceWarp.API.Configuration;
Expand Down Expand Up @@ -33,35 +34,6 @@ public void Start()
Destroy(child.gameObject);
}

foreach (var module in ModuleManager.AllSpaceWarpModules.Where(
mod => mod.ModuleConfiguration.Sections.Count > 0
))
{
GenerateTitle(module.Name).transform.SetParent(transform);
GenerateDivider().transform.SetParent(transform);
Dictionary<string, List<(string name, IConfigEntry entry)>> modConfigCategories = new();
foreach (var section in module.ModuleConfiguration!.Sections)
{
if (module.ModuleConfiguration[section].Count <= 0) continue;
var list = modConfigCategories[section] = new List<(string name, IConfigEntry entry)>();
list.AddRange(module.ModuleConfiguration[section].Select(
entry => (entry, module.ModuleConfiguration[section, entry])
));
}

foreach (var config in modConfigCategories)
{
var header = GenerateSectionHeader(config.Key);
header.transform.SetParent(transform);
foreach (var drawer in config.Value.Select(x => ModsPropertyDrawers.Drawer(x.name, x.entry))
.Where(drawer => drawer != null))
{
drawer.transform.SetParent(header.transform);
}

GenerateDivider().transform.SetParent(transform);
}
}

// Now here is where we go through every single mod
#pragma warning disable CS0618
Expand Down Expand Up @@ -102,7 +74,8 @@ public void Start()
foreach (var mod in PluginList.AllEnabledAndActivePlugins.Where(mod =>
mod.ConfigFile != null && mod.ConfigFile.Sections.Count > 0 &&
mod.ConfigFile.Sections.Any(x =>
mod.ConfigFile[x].Count > 0 && mod.Plugin is not BepInExModAdapter or BaseSpaceWarpPlugin)))
mod.ConfigFile[x].Count > 0) && mod.Plugin is not BepInExModAdapter &&
mod.Plugin is not BaseSpaceWarpPlugin && mod.Plugin != null))
{
GenerateTitle(mod.Name).transform.SetParent(transform);
GenerateDivider().transform.SetParent(transform);
Expand All @@ -127,6 +100,37 @@ public void Start()
GenerateDivider().transform.SetParent(transform);
}
}

foreach (var module in ModuleManager.AllSpaceWarpModules.Where(
mod => mod.ModuleConfiguration.Sections.Count > 0
))
{
GenerateTitle(module.Name).transform.SetParent(transform);
GenerateDivider().transform.SetParent(transform);
Dictionary<string, List<(string name, IConfigEntry entry)>> modConfigCategories = new();
foreach (var section in module.ModuleConfiguration!.Sections)
{
if (module.ModuleConfiguration[section].Count <= 0) continue;
var list = modConfigCategories[section] = new List<(string name, IConfigEntry entry)>();
list.AddRange(module.ModuleConfiguration[section].Select(
entry => (entry, module.ModuleConfiguration[section, entry])
));
}

foreach (var config in modConfigCategories)
{
var header = GenerateSectionHeader(config.Key);
header.transform.SetParent(transform);
foreach (var drawer in config.Value.Select(x => ModsPropertyDrawers.Drawer(x.name, x.entry))
.Where(drawer => drawer != null))
{
drawer.transform.SetParent(header.transform);
}

GenerateDivider().transform.SetParent(transform);
}
}

}

public override void OnShow()
Expand Down
2 changes: 1 addition & 1 deletion SpaceWarp.UI/UI/Settings/SettingsMenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void Setup()
var ddPrefab = Instantiate(GameObject.Find(DropdownPrefabPath));
Destroy(ddPrefab.GetComponent<UIValue_ReadBool_SetActive>());
ddPrefab.Persist();
var dropdown = ddPrefab.GetChild("Setting").GetChild("KSP2DropDown");
var dropdown = ddPrefab.GetChild("Setting").GetChild("BTN-Dropdown");
// This will be redone once I add a specific
Destroy(dropdown.GetComponent<UIValue_ReadDropdownOption_Text>());
Destroy(dropdown.GetComponent<SettingsElementDescriptionController>());
Expand Down

0 comments on commit 1bc95ee

Please sign in to comment.