mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
Compare commits
1
Commits
dev
..
multi-select
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8340ebba4e |
No files matched your search
@@ -0,0 +1,101 @@
|
||||
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||
using MiraAPI.Translation;
|
||||
using Reactor.Utilities.Attributes;
|
||||
|
||||
namespace MiraAPI.GameOptions.Components;
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="BaseGameSetting"/> implementation that supports multi-select options.
|
||||
/// </summary>
|
||||
/// <param name="cppPtr">The C++ pointer to the object.</param>
|
||||
[RegisterInIl2Cpp]
|
||||
public class FlagsGameSetting(nint cppPtr) : BaseGameSetting(cppPtr)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the value currently held by the setting.
|
||||
/// </summary>
|
||||
public int Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the integer value used to denote that all flags have been selected.
|
||||
/// </summary>
|
||||
public int All { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets every defined flag within the enum.
|
||||
/// </summary>
|
||||
public Il2CppStructArray<int> Flags { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the string names associated with every flag.
|
||||
/// </summary>
|
||||
public Il2CppStructArray<StringNames> Values { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the string name for when no value is selected.
|
||||
/// </summary>
|
||||
public StringNames NoneName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the string name for when all values are selected.
|
||||
/// </summary>
|
||||
public StringNames AllName { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string GetValueString(float value)
|
||||
{
|
||||
return GetValueString((int)value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value display string.
|
||||
/// </summary>
|
||||
/// <param name="value">The enum value as an int.</param>
|
||||
/// <returns>A value string.</returns>
|
||||
public string GetValueString(int value)
|
||||
{
|
||||
if (value == 0)
|
||||
{
|
||||
return TranslationController.Instance.GetString(NoneName).Translate();
|
||||
}
|
||||
|
||||
if (value == All)
|
||||
{
|
||||
return TranslationController.Instance.GetString(AllName).Translate();
|
||||
}
|
||||
|
||||
var activeCount = 0;
|
||||
var firstActiveName = StringNames.None;
|
||||
var foundFirst = false;
|
||||
|
||||
for (var i = 0; i < Flags.Length; i++)
|
||||
{
|
||||
var flag = Flags[i];
|
||||
|
||||
if (flag == 0 || (value & flag) != flag)
|
||||
continue;
|
||||
|
||||
if (!foundFirst)
|
||||
{
|
||||
firstActiveName = Values[i];
|
||||
foundFirst = true;
|
||||
}
|
||||
|
||||
activeCount++;
|
||||
}
|
||||
|
||||
if (activeCount == 0)
|
||||
{
|
||||
return TranslationController.Instance.GetString(NoneName).Translate();
|
||||
}
|
||||
|
||||
var firstNameString = TranslationController.Instance.GetString(firstActiveName).Translate();
|
||||
|
||||
if (activeCount == 1)
|
||||
{
|
||||
return firstNameString;
|
||||
}
|
||||
|
||||
return MiraLocaleManager.GetParsed("MiraApi.Compound", string.Empty, firstNameString, activeCount - 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||
using MiraAPI.Translation;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace MiraAPI.GameOptions.Components;
|
||||
|
||||
/// <summary>
|
||||
/// An option behaviour that acts as a container for dynamically generated flag toggles.
|
||||
/// </summary>
|
||||
/// <param name="cppPtr">The pointer to the C++ object.</param>
|
||||
[SuppressMessage("Style", "IDE0051:Remove unused private members", Justification = "Unity convention.")]
|
||||
public class FlagsOption(nint cppPtr) : OptionBehaviour(cppPtr)
|
||||
{
|
||||
public TextMeshPro TitleText;
|
||||
public TextMeshPro ValueText;
|
||||
public int Value;
|
||||
public Il2CppStructArray<StringNames> Values;
|
||||
|
||||
private int oldValue = -1;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public override int GetInt()
|
||||
{
|
||||
return Value;
|
||||
}
|
||||
|
||||
public override void SetUpFromData(BaseGameSetting data, int maskLayer)
|
||||
{
|
||||
base.SetUpFromData(data, maskLayer);
|
||||
var stringGameSetting = data.TryCast<FlagsGameSetting>();
|
||||
|
||||
if (stringGameSetting == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Title = stringGameSetting.Title;
|
||||
Value = stringGameSetting.Value;
|
||||
Values = stringGameSetting.Values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using MiraAPI.GameOptions.Components;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.Translation;
|
||||
using MiraAPI.Utilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.GameOptions.OptionTypes;
|
||||
|
||||
// TODO: Add documentation once completed
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
public interface IMultiSelectOption
|
||||
{
|
||||
int Value { get; }
|
||||
}
|
||||
|
||||
public class ModdedMultiSelectOption<T> : ModdedOption<T>, IMultiSelectOption
|
||||
where T : struct, Enum
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets an array of int values where only a bit is turned on to denote each flag contained within <typeparamref name="T"/>.
|
||||
/// </summary>
|
||||
public int[] Flags { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string values of the option.
|
||||
/// </summary>
|
||||
public string[] Values { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the String Name values of the option.
|
||||
/// </summary>
|
||||
public StringNames[] StringNameValues { get; } = [];
|
||||
|
||||
public StringNames NoneName { get; }
|
||||
|
||||
public StringNames AllName { get; }
|
||||
|
||||
public int All { get; }
|
||||
|
||||
int IMultiSelectOption.Value => Convert.ToInt32(Value, NumberFormatInfo.InvariantInfo);
|
||||
|
||||
public ModdedMultiSelectOption(string groupPrefix, string title, T defaultValue, string idPrefix, T[]? allowedValues = null, string[]? values = null, string? noneId = null, string? allId = null, bool includeInPreset = true)
|
||||
: base(groupPrefix, title, defaultValue, includeInPreset)
|
||||
{
|
||||
var enumType = typeof(T);
|
||||
|
||||
if (!enumType.IsDefined<FlagsAttribute>(false))
|
||||
throw new ArgumentException($"{enumType.Name} does not have [Flags], and therefore cannot be used for multi-select options.");
|
||||
|
||||
var targetValues = allowedValues ?? Enum.GetValues<T>();
|
||||
|
||||
var tuples = targetValues
|
||||
.Select(x => (Val: x, Bit: Convert.ToInt32(x, NumberFormatInfo.InvariantInfo)))
|
||||
.Where(val => val.Bit > 0 && (val.Bit & (val.Bit - 1)) == 0)
|
||||
.DistinctBy(x => x.Bit)
|
||||
.ToArray();
|
||||
|
||||
if (values != null && values.Length != tuples.Length)
|
||||
{
|
||||
throw new ArgumentException($"The number of custom values ({values.Length}) must exactly match the number of allowed single-bit flags ({tuples.Length}).", nameof(values));
|
||||
}
|
||||
|
||||
var all = 0;
|
||||
|
||||
foreach (var (_, bit) in tuples)
|
||||
{
|
||||
all |= bit;
|
||||
}
|
||||
|
||||
All = all;
|
||||
|
||||
var defaultBits = Convert.ToInt32(defaultValue, NumberFormatInfo.InvariantInfo);
|
||||
|
||||
if ((defaultBits & ~All) != 0)
|
||||
{
|
||||
throw new ArgumentException($"The defaultValue contains flags that are not present in the allowed subset.", nameof(defaultValue));
|
||||
}
|
||||
|
||||
idPrefix = idPrefix.Replace("%type%", enumType.Name);
|
||||
|
||||
Flags = [.. tuples.Select(x => x.Bit)];
|
||||
Values = values ?? [.. tuples.Select(x => x.Val.ToString())];
|
||||
AllName = MiraLocaleManager.GetOrCreateLocaleString(idPrefix + (allId ?? "All"));
|
||||
NoneName = MiraLocaleManager.GetOrCreateLocaleString(idPrefix + (noneId ?? "None"));
|
||||
StringNameValues = [.. Values.Select(x => MiraLocaleManager.GetOrCreateLocaleString(idPrefix + x))];
|
||||
|
||||
var data = ScriptableObject.CreateInstance<FlagsGameSetting>();
|
||||
data.Type = global::OptionTypes.MultipleChoice; // How nice of Innersloth to provide us with a pre-defined enum value
|
||||
data.Value = defaultBits;
|
||||
data.All = All;
|
||||
data.Flags = Flags;
|
||||
data.Values = StringNameValues;
|
||||
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public ModdedMultiSelectOption(string groupPrefix, string title, T defaultValue, T[]? allowedValues = null, string[]? values = null, string? noneId = null, string? allId = null, bool includeInPreset = true)
|
||||
: this(groupPrefix, title, defaultValue, string.Empty, allowedValues, values, noneId, allId, includeInPreset)
|
||||
{
|
||||
}
|
||||
|
||||
public ModdedMultiSelectOption(string title, T defaultValue, string idPrefix, T[]? allowedValues = null, string[]? values = null, string? noneId = null, string? allId = null, bool includeInPreset = true)
|
||||
: this(string.Empty, title, defaultValue, idPrefix, allowedValues, values, noneId, allId, includeInPreset)
|
||||
{
|
||||
}
|
||||
|
||||
public ModdedMultiSelectOption(string title, T defaultValue, T[]? allowedValues = null, string[]? values = null, string? noneId = null, string? allId = null, bool includeInPreset = true)
|
||||
: this(string.Empty, title, defaultValue, string.Empty, allowedValues, values, noneId, allId, includeInPreset)
|
||||
{
|
||||
}
|
||||
|
||||
public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, PlayerOption playerOpt, Transform container)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override float GetFloatData()
|
||||
{
|
||||
return Convert.ToInt32(_value, NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
|
||||
public override NetData GetNetData()
|
||||
{
|
||||
var buffer = new byte[Unsafe.SizeOf<T>()];
|
||||
Unsafe.WriteUnaligned(ref buffer[0], _value);
|
||||
return new(Id, buffer);
|
||||
}
|
||||
|
||||
public override void HandleNetData(byte[] data)
|
||||
{
|
||||
SetValue(Unsafe.ReadUnaligned<T>(ref data[0]));
|
||||
}
|
||||
|
||||
public override T GetValueFromOptionBehaviour(OptionBehaviour optionBehaviour)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override void OnValueChanged(T newValue)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,9 @@
|
||||
<string name="Activated">Activated</string>
|
||||
<string name="Deactivated">Deactivated</string>
|
||||
<string name="MiraApi.AbbreviatedDistance">unit(s)</string>
|
||||
<string name="All">All</string>
|
||||
<string name="None">None</string>
|
||||
<string name="MiraApi.Compound">{0} + {1}</string>
|
||||
|
||||
<string name="MiraApi.Option.Group.HnsCrewmate">Crewmates</string>
|
||||
<string name="MiraApi.HnsCrewmate.Option.PlayerSpeed">Player Speed</string>
|
||||
|
||||
@@ -249,6 +249,23 @@ public static class MiraLocaleManager
|
||||
return GetParsed(language, key, string.Empty, parseList);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GetParsed(MiraLanguage, string, ValueTuple{string, string}[], string)"/>
|
||||
public static string GetParsed(
|
||||
string key,
|
||||
string fallback,
|
||||
params object[] parseList)
|
||||
{
|
||||
return GetParsed(CurrentLanguage, key, fallback, parseList);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GetParsed(MiraLanguage, string, ValueTuple{string, string}[], string)"/>
|
||||
public static string GetParsed(
|
||||
string key,
|
||||
params object[] parseList)
|
||||
{
|
||||
return GetParsed(CurrentLanguage, key, string.Empty, parseList);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a formatted translation ID string using a mod ID, a specific ID part, and a suffix.
|
||||
/// </summary>
|
||||
|
||||
@@ -14,4 +14,15 @@ public static class TranslationExtensions
|
||||
{
|
||||
return MiraLocaleManager.Get(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Translates a key to the current language.
|
||||
/// </summary>
|
||||
/// <param name="key">The translation key.</param>
|
||||
/// <param name="args">The values to format the string with.</param>
|
||||
/// <returns>The corresponding translation.</returns>
|
||||
public static string Translate(this string key, params object[] args)
|
||||
{
|
||||
return MiraLocaleManager.GetParsed(key, string.Empty, args);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user