mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Options management cleanup + fixes
This commit is contained in:
1 parent
9a9367f2b9
commit
c15ba69533
11 files changed
+73
-50
No files matched your search
@@ -9,7 +9,7 @@ public interface IModdedOption
|
||||
{
|
||||
public uint Id { get; }
|
||||
public BaseGameSetting Data { get; }
|
||||
public IModdedOptionGroup Group { get; set; }
|
||||
public bool HasGroup { get; set; }
|
||||
public IMiraPlugin ParentMod { get; set; }
|
||||
public Type AdvancedRole { get; set; }
|
||||
public OptionBehaviour OptionBehaviour { get; protected set; }
|
||||
|
||||
@@ -12,12 +12,16 @@ namespace MiraAPI.GameOptions;
|
||||
|
||||
public class ModdedOptionsManager
|
||||
{
|
||||
public static readonly Dictionary<uint, IModdedOption> ModdedOptions = new();
|
||||
public static readonly List<IModdedOption> Options = [];
|
||||
public static readonly List<IModdedOptionGroup> Groups = [];
|
||||
private static readonly Dictionary<PropertyInfo, ModdedOptionAttribute> OptionAttributes = new();
|
||||
public static readonly Dictionary<Type, IModdedOptionGroup> OriginalTypes = new();
|
||||
public static uint NextId = 1;
|
||||
|
||||
public static readonly Dictionary<uint, IModdedOption> ModdedOptions = new();
|
||||
public static readonly Dictionary<IModdedOptionGroup, List<IModdedOption>> GroupedOptions = new();
|
||||
|
||||
public static readonly List<IModdedOptionGroup> Groups = [];
|
||||
public static readonly Dictionary<Type, IModdedOptionGroup> TypeToGroup = new();
|
||||
|
||||
public static uint NextId => _nextId++;
|
||||
private static uint _nextId = 1;
|
||||
|
||||
public static IModdedOption RegisterOption(Type type, ModdedOptionAttribute attribute, PropertyInfo property)
|
||||
{
|
||||
@@ -50,11 +54,12 @@ public class ModdedOptionsManager
|
||||
|
||||
OptionAttributes.Add(property, attribute);
|
||||
|
||||
if (OriginalTypes.ContainsKey(type))
|
||||
if (TypeToGroup.ContainsKey(type))
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Grouping {attribute.Title} with {OriginalTypes[type].GroupName}");
|
||||
result.Group = OriginalTypes[type];
|
||||
result.AdvancedRole = OriginalTypes[type].AdvancedRole;
|
||||
Logger<MiraApiPlugin>.Error($"Grouping {attribute.Title} with {TypeToGroup[type].GroupName}");
|
||||
GroupedOptions[TypeToGroup[type]].Add(result);
|
||||
result.HasGroup = true;
|
||||
result.AdvancedRole = TypeToGroup[type].AdvancedRole;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ using Reactor.Localization.Utilities;
|
||||
using System;
|
||||
using MiraAPI.Networking;
|
||||
using Reactor.Networking.Rpc;
|
||||
using Reactor.Utilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.GameOptions.OptionTypes;
|
||||
@@ -13,6 +12,8 @@ public abstract class ModdedOption<T> : IModdedOption
|
||||
{
|
||||
public uint Id { get; }
|
||||
public BaseGameSetting Data { get; protected init; }
|
||||
public bool HasGroup { get; set; } = false;
|
||||
public IMiraPlugin ParentMod { get; set; } = null;
|
||||
public T Value { get; protected set; }
|
||||
public T DefaultValue { get; init; }
|
||||
public Action<T> ChangedEvent { get; set; }
|
||||
@@ -21,8 +22,6 @@ public abstract class ModdedOption<T> : IModdedOption
|
||||
public Func<bool> Visible { get; set; }
|
||||
public Type AdvancedRole { get; set; }
|
||||
public OptionBehaviour OptionBehaviour { get; set; }
|
||||
public IModdedOptionGroup Group { get; set; } = null;
|
||||
public IMiraPlugin ParentMod { get; set; } = null;
|
||||
|
||||
protected ModdedOption(string title, T defaultValue, Type roleType) : this()
|
||||
{
|
||||
@@ -40,8 +39,7 @@ public abstract class ModdedOption<T> : IModdedOption
|
||||
|
||||
private ModdedOption()
|
||||
{
|
||||
Id = ModdedOptionsManager.NextId++;
|
||||
ModdedOptionsManager.Options.Add(this);
|
||||
Id = ModdedOptionsManager.NextId;
|
||||
ModdedOptionsManager.ModdedOptions.Add(Id, this);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
<EmbeddedResource Include="Resources\NextButton.png" />
|
||||
<EmbeddedResource Include="Resources\NextButtonActive.png" />
|
||||
<EmbeddedResource Include="Resources\ExampleButton.png" />
|
||||
<EmbeddedResource Include="Resources\Empty.png" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -22,7 +22,10 @@ public static class GameOptionsMenuPatch
|
||||
__instance.MapPicker.gameObject.SetActive(false);
|
||||
|
||||
float num = 2.1f;
|
||||
foreach (IModdedOptionGroup group in GameSettingMenuPatches.SelectedMod.OptionGroups)
|
||||
|
||||
var filteredGroups = GameSettingMenuPatches.SelectedMod.OptionGroups.Where(x => x.AdvancedRole is null);
|
||||
|
||||
foreach (IModdedOptionGroup group in filteredGroups)
|
||||
{
|
||||
CategoryHeaderMasked categoryHeaderMasked = Object.Instantiate(__instance.categoryHeaderOrigin, Vector3.zero, Quaternion.identity, __instance.settingsContainer);
|
||||
categoryHeaderMasked.SetHeader(CustomStringName.CreateAndRegister(group.GroupName), 20);
|
||||
@@ -35,7 +38,7 @@ public static class GameOptionsMenuPatch
|
||||
categoryHeaderMasked.transform.localPosition = new Vector3(-0.903f, num, -2f);
|
||||
num -= 0.63f;
|
||||
|
||||
foreach (var opt in ModdedOptionsManager.Options.Where(opt => opt.Group is not null && opt.AdvancedRole is null && opt.Group.GroupName == group.GroupName))
|
||||
foreach (var opt in ModdedOptionsManager.GroupedOptions[group])
|
||||
{
|
||||
OptionBehaviour newOpt = opt.CreateOption(__instance.checkboxOrigin, __instance.numberOptionOrigin, __instance.stringOptionOrigin, __instance.settingsContainer);
|
||||
newOpt.transform.localPosition = new Vector3(0.952f, num, -2f);
|
||||
@@ -62,36 +65,46 @@ public static class GameOptionsMenuPatch
|
||||
}
|
||||
}
|
||||
|
||||
CategoryHeaderMasked ungroupedHeader = Object.Instantiate(__instance.categoryHeaderOrigin, Vector3.zero, Quaternion.identity, __instance.settingsContainer);
|
||||
ungroupedHeader.SetHeader(CustomStringName.CreateAndRegister("Ungrouped Options"), 20);
|
||||
ungroupedHeader.transform.localScale = Vector3.one * 0.63f;
|
||||
ungroupedHeader.transform.localPosition = new Vector3(-0.903f, num, -2f);
|
||||
var filteredOptions = GameSettingMenuPatches.SelectedMod.Options.Where(x => x.AdvancedRole is null && !x.HasGroup).ToList();
|
||||
|
||||
num -= 0.63f;
|
||||
|
||||
foreach (var opt in ModdedOptionsManager.Options.Where(opt => opt.Group is null && opt.AdvancedRole is null))
|
||||
if (filteredOptions.Count > 0)
|
||||
{
|
||||
OptionBehaviour newOpt = opt.CreateOption(__instance.checkboxOrigin, __instance.numberOptionOrigin, __instance.stringOptionOrigin, __instance.settingsContainer);
|
||||
newOpt.transform.localPosition = new Vector3(0.952f, num, -2f);
|
||||
newOpt.SetClickMask(__instance.ButtonClickMask);
|
||||
newOpt.SetUpFromData(newOpt.data, 20);
|
||||
|
||||
SpriteRenderer[] componentsInChildren = newOpt.GetComponentsInChildren<SpriteRenderer>(true);
|
||||
for (int i = 0; i < componentsInChildren.Length; i++)
|
||||
CategoryHeaderMasked ungroupedHeader = Object.Instantiate(__instance.categoryHeaderOrigin, Vector3.zero,
|
||||
Quaternion.identity, __instance.settingsContainer);
|
||||
ungroupedHeader.SetHeader(CustomStringName.CreateAndRegister("Ungrouped Options"), 20);
|
||||
ungroupedHeader.transform.localScale = Vector3.one * 0.63f;
|
||||
ungroupedHeader.transform.localPosition = new Vector3(-0.903f, num, -2f);
|
||||
|
||||
num -= 0.63f;
|
||||
|
||||
|
||||
foreach (var opt in filteredOptions)
|
||||
{
|
||||
componentsInChildren[i].material.SetInt(PlayerMaterial.MaskLayer, 20);
|
||||
OptionBehaviour newOpt = opt.CreateOption(__instance.checkboxOrigin, __instance.numberOptionOrigin,
|
||||
__instance.stringOptionOrigin, __instance.settingsContainer);
|
||||
newOpt.transform.localPosition = new Vector3(0.952f, num, -2f);
|
||||
newOpt.SetClickMask(__instance.ButtonClickMask);
|
||||
newOpt.SetUpFromData(newOpt.data, 20);
|
||||
|
||||
SpriteRenderer[] componentsInChildren = newOpt.GetComponentsInChildren<SpriteRenderer>(true);
|
||||
for (int i = 0; i < componentsInChildren.Length; i++)
|
||||
{
|
||||
componentsInChildren[i].material.SetInt(PlayerMaterial.MaskLayer, 20);
|
||||
}
|
||||
|
||||
foreach (TextMeshPro textMeshPro in newOpt.GetComponentsInChildren<TextMeshPro>(true))
|
||||
{
|
||||
textMeshPro.fontMaterial.SetFloat(ShaderID.StencilComp, 3f);
|
||||
textMeshPro.fontMaterial.SetFloat(ShaderID.Stencil, 20);
|
||||
}
|
||||
|
||||
__instance.Children.Add(newOpt);
|
||||
|
||||
num -= 0.45f;
|
||||
|
||||
newOpt.Initialize();
|
||||
}
|
||||
foreach (TextMeshPro textMeshPro in newOpt.GetComponentsInChildren<TextMeshPro>(true))
|
||||
{
|
||||
textMeshPro.fontMaterial.SetFloat(ShaderID.StencilComp, 3f);
|
||||
textMeshPro.fontMaterial.SetFloat(ShaderID.Stencil, 20);
|
||||
}
|
||||
|
||||
__instance.Children.Add(newOpt);
|
||||
|
||||
num -= 0.45f;
|
||||
|
||||
newOpt.Initialize();
|
||||
}
|
||||
|
||||
__instance.scrollBar.SetYBoundsMax(-num - 1.65f);
|
||||
|
||||
@@ -139,8 +139,10 @@ public static class RoleSettingMenuPatches
|
||||
__instance.advancedSettingChildren.Clear();
|
||||
|
||||
float num = -0.872f;
|
||||
foreach (var option in GameSettingMenuPatches.SelectedMod.Options.Where(option => option.AdvancedRole != null && option.AdvancedRole == role.GetType()))
|
||||
foreach (var option in GameSettingMenuPatches.SelectedMod.Options)
|
||||
{
|
||||
if (option.AdvancedRole is not null && option.AdvancedRole != role.GetType()) continue;
|
||||
|
||||
OptionBehaviour newOpt = option.CreateOption(__instance.checkboxOrigin, __instance.numberOptionOrigin, __instance.stringOptionOrigin, __instance.AdvancedRolesSettings.transform);
|
||||
newOpt.transform.localPosition = new Vector3(2.17f, num, -2f);
|
||||
newOpt.SetClickMask(__instance.ButtonClickMask);
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using HarmonyLib;
|
||||
using MiraAPI.GameModes;
|
||||
using MiraAPI.GameOptions;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.Patches.Roles;
|
||||
|
||||
@@ -74,9 +74,11 @@ public class MiraPluginManager
|
||||
if (typeof(IModdedOptionGroup).IsAssignableFrom(type))
|
||||
{
|
||||
IModdedOptionGroup group = (IModdedOptionGroup)Activator.CreateInstance(type);
|
||||
if (group == null) continue;
|
||||
|
||||
ModdedOptionsManager.Groups.Add(group);
|
||||
ModdedOptionsManager.OriginalTypes.Add(type, group);
|
||||
ModdedOptionsManager.TypeToGroup.Add(type, group);
|
||||
ModdedOptionsManager.GroupedOptions.Add(group, []);
|
||||
pluginInfo.OptionGroups.Add(group);
|
||||
|
||||
foreach (var property in type.GetProperties())
|
||||
@@ -84,9 +86,13 @@ public class MiraPluginManager
|
||||
if (typeof(IModdedOption).IsAssignableFrom(property.PropertyType))
|
||||
{
|
||||
IModdedOption option = (IModdedOption)property.GetValue(group);
|
||||
group.Options.Add(option);
|
||||
option.Group = group;
|
||||
if (option == null) continue;
|
||||
|
||||
option.HasGroup = true;
|
||||
option.AdvancedRole = group.AdvancedRole;
|
||||
|
||||
ModdedOptionsManager.GroupedOptions[group].Add(option);
|
||||
pluginInfo.Options.Add(option);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 6.2 KiB |
@@ -2,7 +2,7 @@
|
||||
|
||||
public static class MiraAssets
|
||||
{
|
||||
public static readonly LoadableResourceAsset Empty = new("");
|
||||
public static readonly LoadableResourceAsset Empty = new("MiraAPI.Resources.Empty.png");
|
||||
|
||||
public static readonly LoadableResourceAsset NextButton = new("MiraAPI.Resources.NextButton.png");
|
||||
public static readonly LoadableResourceAsset NextButtonActive = new("MiraAPI.Resources.NextButtonActive.png");
|
||||
|
||||
@@ -10,7 +10,7 @@ public static class Extensions
|
||||
{
|
||||
public static bool IsCustom(this OptionBehaviour optionBehaviour)
|
||||
{
|
||||
return ModdedOptionsManager.Options.Any(opt => opt.OptionBehaviour && opt.OptionBehaviour.Equals(optionBehaviour));
|
||||
return ModdedOptionsManager.ModdedOptions.Values.Any(opt => opt.OptionBehaviour && opt.OptionBehaviour.Equals(optionBehaviour));
|
||||
}
|
||||
|
||||
public static Color DarkenColor(this Color color)
|
||||
|
||||
Reference in new issue
Block a user