mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Enum options
This commit is contained in:
1 parent
947a202c55
commit
0d746869de
7 files changed
+103
-7
No files matched your search
@@ -1,14 +1,25 @@
|
||||
using MiraAPI.GameOptions;
|
||||
using MiraAPI.GameOptions.Attributes;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.Example
|
||||
{
|
||||
public class ExampleOptions : IModdedOptionGroup
|
||||
{
|
||||
public string GroupName => "General Group";
|
||||
public Color GroupColor => Color.red;
|
||||
|
||||
[ModdedToggleOption("Use Thing")] public bool useThing { get; set; } = true;
|
||||
[ModdedToggleOption("Use another thing")] public bool useAnotherThing { get; set; } = false;
|
||||
[ModdedNumberOption("Sussy level", min: 0, max: 10)] public float sussyLevel { get; set; } = 4f;
|
||||
[ModdedStringOption("Sus choices", ["hello", "hello 2", "hello 3"])] public int susChoices { get; set; } = 0;
|
||||
[ModdedEnumOption("Enum test", typeof(TestEnum))] public TestEnum enumTest { get; set; } = TestEnum.Hello;
|
||||
}
|
||||
|
||||
public enum TestEnum
|
||||
{
|
||||
Hello,
|
||||
Hi,
|
||||
Bye
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public partial class ExamplePlugin : BasePlugin, IMiraConfig
|
||||
public static ExamplePlugin Instance { get; private set; }
|
||||
public ModdedOptionTabSettings TabSettings => new ModdedOptionTabSettings()
|
||||
{
|
||||
Title = "Example Mod",
|
||||
Title = "MiraAPI Example Mod",
|
||||
TabIcon = MiraAssets.Empty
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
using MiraAPI.GameOptions.OptionTypes;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MiraAPI.GameOptions.Attributes
|
||||
{
|
||||
public class ModdedEnumOptionAttribute : ModdedOptionAttribute
|
||||
{
|
||||
public Type EnumType;
|
||||
public ModdedEnumOptionAttribute(string title, Type enumType, Type roleType = null) : base(title, roleType)
|
||||
{
|
||||
EnumType = enumType;
|
||||
}
|
||||
|
||||
public override IModdedOption CreateOption(object value, PropertyInfo property)
|
||||
{
|
||||
var opt = new ModdedEnumOption(Title, (int)value, EnumType, RoleType);
|
||||
return opt;
|
||||
}
|
||||
|
||||
public override void SetValue(object value)
|
||||
{
|
||||
ModdedEnumOption opt = HolderOption as ModdedEnumOption;
|
||||
opt.SetValue((int)value);
|
||||
}
|
||||
|
||||
public override object GetValue()
|
||||
{
|
||||
ModdedEnumOption opt = HolderOption as ModdedEnumOption;
|
||||
return Enum.ToObject(EnumType.GetType(), opt.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Reactor.Localization.Utilities;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.GameOptions.OptionTypes
|
||||
{
|
||||
public class ModdedEnumOption : ModdedOption<int>
|
||||
{
|
||||
public string[] Values;
|
||||
|
||||
public ModdedEnumOption(string title, int defaultValue, Type enumType, System.Type roleType) : base(title, defaultValue, roleType)
|
||||
{
|
||||
Values = Enum.GetNames(enumType);
|
||||
}
|
||||
|
||||
public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, Transform container)
|
||||
{
|
||||
var stringOption = UnityEngine.Object.Instantiate(stringOpt, container);
|
||||
|
||||
stringOption.name = Title;
|
||||
stringOption.Title = StringName;
|
||||
stringOption.Value = Value;
|
||||
stringOption.Values = Values.Select(CustomStringName.CreateAndRegister).ToArray();
|
||||
stringOption.OnValueChanged = (Il2CppSystem.Action<OptionBehaviour>)ValueChanged;
|
||||
stringOption.OnEnable();
|
||||
|
||||
OptionBehaviour = stringOption;
|
||||
|
||||
return stringOption;
|
||||
}
|
||||
|
||||
public override string GetHudStringText()
|
||||
{
|
||||
return Title + ": " + Values[Value];
|
||||
}
|
||||
|
||||
public override int GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour)
|
||||
{
|
||||
return optionBehaviour.GetInt();
|
||||
}
|
||||
|
||||
public override void OnValueChanged(int newValue)
|
||||
{
|
||||
if (OptionBehaviour is null) return;
|
||||
|
||||
StringOption opt = OptionBehaviour as StringOption;
|
||||
opt.Value = newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,12 +65,13 @@ public static class ModdedOptionsTab
|
||||
}));
|
||||
}
|
||||
|
||||
public static GameObject CreateHeader(ToggleOption toggleOpt, Transform container, string title)
|
||||
public static GameObject CreateHeader(ToggleOption toggleOpt, Transform container, string title, Color color)
|
||||
{
|
||||
var header = Object.Instantiate(toggleOpt, container);
|
||||
|
||||
header.Title = StringNames.None;
|
||||
header.TitleText.text = title;
|
||||
header.TitleText.color = color;
|
||||
header.name = "Header";
|
||||
|
||||
var checkBox = header.transform.FindChild("CheckBox")?.gameObject;
|
||||
@@ -146,7 +147,7 @@ public static class ModdedOptionsTab
|
||||
var launchpadGroup = newSettings.transform.FindChild("GameGroup").gameObject;
|
||||
var text = launchpadGroup.transform.FindChild("Text").gameObject.GetComponent<TextMeshPro>();
|
||||
text.gameObject.GetComponent<TextTranslatorTMP>().Destroy();
|
||||
text.text = $"{config.TabSettings.Title}";
|
||||
text.text = $"{config.TabSettings.Title} Options";
|
||||
|
||||
return newSettings;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public static class GameSettingsMenuPatches
|
||||
|
||||
foreach (var group in ModdedOptionsManager.Groups.Where(group => group.AdvancedRole == null && group.ParentMod == pair.Value))
|
||||
{
|
||||
group.Header = ModdedOptionsTab.CreateHeader(toggleOpt, container, group.GroupName);
|
||||
group.Header = ModdedOptionsTab.CreateHeader(toggleOpt, container, group.GroupName, group.GroupColor);
|
||||
CreateOptionsFor(__instance, toggleOpt, numberOpt, stringOpt, container, ModdedOptionsManager.Options.Where(x => x.Group == group).ToList());
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ public static class ToHudStringPatch
|
||||
return;
|
||||
}
|
||||
|
||||
var sb = new StringBuilder($"<size=180%><b>{currentPlugin.TabSettings.Title}:</b></size>\n<size=130%>");
|
||||
var sb = new StringBuilder($"<size=180%><b>{currentPlugin.TabSettings.Title} Options:</b></size>\n<size=130%>");
|
||||
var groupsWithRoles = ModdedOptionsManager.Groups.Where(group => group.AdvancedRole != null && group.ParentMod == currentPlugin);
|
||||
var groupsWithoutRoles = ModdedOptionsManager.Groups.Where(group => group.AdvancedRole == null && group.ParentMod == currentPlugin);
|
||||
|
||||
@@ -84,7 +84,7 @@ public static class ToHudStringPatch
|
||||
continue;
|
||||
}
|
||||
|
||||
sb.AppendLine($"\n<size=160%><b>{group.GroupName}</b></size>");
|
||||
sb.AppendLine($"\n<size=160%><b>{group.GroupColor.ToTextColor()}{group.GroupName}</color></b></size>");
|
||||
AddOptions(sb, ModdedOptionsManager.Options.Where(x => x.Group == group).ToList());
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ public static class ToHudStringPatch
|
||||
continue;
|
||||
}
|
||||
|
||||
sb.AppendLine($"<size=140%><b>{group.GroupName}</b></size><size=120%>");
|
||||
sb.AppendLine($"<size=140%><b>{group.GroupColor.ToTextColor()}{group.GroupName}</color></b></size><size=120%>");
|
||||
AddOptions(sb, ModdedOptionsManager.Options.Where(x => x.Group == group).ToList());
|
||||
sb.Append("</size>\n");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user