Skip to content

Commit

Permalink
Merge pull request #7 from All-Of-Us-Mods/dev
Browse files Browse the repository at this point in the history
Button fix, MiraNumberSuffixes, checkmark fix,
  • Loading branch information
XtraCube authored Aug 27, 2024
2 parents d54c47c + 6bc6ab7 commit 1cb11be
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 32 deletions.
3 changes: 2 additions & 1 deletion MiraAPI.Example/Options/ExampleOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MiraAPI.GameOptions;
using MiraAPI.GameOptions.Attributes;
using MiraAPI.Utilities;
using UnityEngine;

namespace MiraAPI.Example.Options;
Expand All @@ -15,7 +16,7 @@ public class ExampleOptions : AbstractOptionGroup
[ModdedToggleOption("Toggle Opt 2")]
public bool ToggleOpt2 { get; set; } = true;

[ModdedNumberOption("Number Opt", min: 0, max: 10)]
[ModdedNumberOption("Number Opt", min: 0, max: 10, suffixType: MiraNumberSuffixes.Percent)]
public float NumberOpt { get; set; } = 4f;

[ModdedStringOption("String Opt", ["Hello", "Hey", "Bye"])]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiraAPI.GameOptions.OptionTypes;
using MiraAPI.Utilities;
using System;
using System.Reflection;

Expand All @@ -9,7 +10,7 @@ public class ModdedNumberOptionAttribute(
float min,
float max,
float increment = 1,
NumberSuffixes suffixType = NumberSuffixes.None,
MiraNumberSuffixes suffixType = MiraNumberSuffixes.None,
bool zeroInfinity = false,
Type roleType = null)
: ModdedOptionAttribute(title, roleType)
Expand Down
36 changes: 18 additions & 18 deletions MiraAPI/GameOptions/OptionTypes/ModdedNumberOption.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using System;
using AmongUs.GameOptions;
using AmongUs.GameOptions;
using MiraAPI.Networking;
using MiraAPI.Utilities;
using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace MiraAPI.GameOptions.OptionTypes;

public class ModdedNumberOption : ModdedOption<float>
{
public float Min { get;}
public float Min { get; }

public float Max { get; }

public float Increment { get; }
public NumberSuffixes SuffixType { get; }

public MiraNumberSuffixes SuffixType { get; }

public bool ZeroInfinity { get; }
public ModdedNumberOption(string title, float defaultValue, float min, float max, float increment, NumberSuffixes suffixType, bool zeroInfinity=false, Type roleType=null) : base(title, defaultValue, roleType)

public ModdedNumberOption(string title, float defaultValue, float min, float max, float increment, MiraNumberSuffixes suffixType, bool zeroInfinity = false, Type roleType = null) : base(title, defaultValue, roleType)
{
Min = min;
Max = max;
Expand All @@ -28,18 +28,18 @@ public ModdedNumberOption(string title, float defaultValue, float min, float max
ZeroInfinity = zeroInfinity;

Value = Mathf.Clamp(defaultValue, min, max);

Data = ScriptableObject.CreateInstance<FloatGameSetting>();

var data = (FloatGameSetting)Data;
data.Type = global::OptionTypes.Float;
data.Title = StringName;
data.Value = Value;
data.Increment = Increment;
data.ValidRange = new FloatRange(Min, Max);
data.FormatString = Increment % 1 == 0 && Value % 1 == 0 && Min % 1 == 0 && Max % 1 == 0 ? "0" : "0.0";
data.FormatString = Increment % 1 == 0 && Value % 1 == 0 && Min % 1 == 0 && Max % 1 == 0 ? "0" : "0.0";
data.ZeroIsInfinity = ZeroInfinity;
data.SuffixType = SuffixType;
data.SuffixType = (NumberSuffixes)SuffixType;
data.OptionName = FloatOptionNames.Invalid;
}

Expand All @@ -49,17 +49,17 @@ public override OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOptio

numberOption.SetUpFromData(Data, 20);
numberOption.OnValueChanged = (Il2CppSystem.Action<OptionBehaviour>)ValueChanged;

numberOption.Title = StringName;
numberOption.Value = Value;
numberOption.Increment = Increment;
numberOption.ValidRange = new FloatRange(Min, Max);
numberOption.FormatString = ((FloatGameSetting)Data).FormatString;
numberOption.ZeroIsInfinity = ZeroInfinity;
numberOption.SuffixType = SuffixType;
numberOption.SuffixType = (NumberSuffixes)SuffixType;
numberOption.floatOptionName = FloatOptionNames.Invalid;


OptionBehaviour = numberOption;

return numberOption;
Expand Down
3 changes: 2 additions & 1 deletion MiraAPI/Hud/CustomActionButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MiraAPI.Utilities.Assets;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace MiraAPI.Hud;

Expand Down Expand Up @@ -95,7 +96,7 @@ internal void CreateButton(Transform parent)
}

var pb = Button.GetComponent<PassiveButton>();
pb.OnClick.RemoveAllListeners();
pb.OnClick = new Button.ButtonClickedEvent();
pb.OnClick.AddListener((UnityAction)ClickHandler);
}

Expand Down
7 changes: 3 additions & 4 deletions MiraAPI/Patches/Options/GameOptionsMenuPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using MiraAPI.Utilities.Assets;
using Reactor.Localization.Utilities;
using Reactor.Utilities.Extensions;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
Expand Down Expand Up @@ -102,7 +101,7 @@ public static bool SettingsPatch(GameOptionsMenu __instance)

var options = group.Options.Select(opt => opt.CreateOption(__instance.checkboxOrigin,
__instance.numberOptionOrigin, __instance.stringOptionOrigin, __instance.settingsContainer));

foreach (var newOpt in options)
{
newOpt.SetClickMask(__instance.ButtonClickMask);
Expand Down Expand Up @@ -137,10 +136,10 @@ public static bool SettingsPatch(GameOptionsMenu __instance)
if (newOpt is ToggleOption toggle)
{
toggle.CheckMark.sprite = MiraAssets.Checkmark.LoadAsset();

toggle.CheckMark.color = group.GroupColor != Color.clear ? group.GroupColor : MiraAssets.AcceptedTeal;
var rend = toggle.CheckMark.transform.parent.FindChild("ActiveSprite").GetComponent<SpriteRenderer>();
rend.sprite = MiraAssets.CheckmarkBox.LoadAsset();
rend.color = group.GroupColor;
rend.color = group.GroupColor != Color.clear ? group.GroupColor : MiraAssets.AcceptedTeal;
}

__instance.Children.Add(newOpt);
Expand Down
42 changes: 42 additions & 0 deletions MiraAPI/Patches/Options/SettingPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using HarmonyLib;
using MiraAPI.Utilities;
using UnityEngine;

namespace MiraAPI.Patches.Options;
[HarmonyPatch]
public class SettingPatches
{
[HarmonyPrefix]
[HarmonyPatch(typeof(FloatGameSetting), nameof(FloatGameSetting.GetValueString))]
public static bool ValueStringPatch(FloatGameSetting __instance, ref string __result, [HarmonyArgument(0)] float value)
{
string result = string.Empty;
MiraNumberSuffixes suffix = (MiraNumberSuffixes)__instance.SuffixType;

if (__instance.ZeroIsInfinity && Mathf.Abs(value) < 0.0001f)
{
result = "<b>∞</b>";
}
else if (suffix == MiraNumberSuffixes.None)
{
result = value.ToString(__instance.FormatString);
}
else if (suffix == MiraNumberSuffixes.Multiplier)
{
result = value.ToString(__instance.FormatString) + "x";
}
else if (suffix == MiraNumberSuffixes.Percent)
{
result = value.ToString(__instance.FormatString) + "%";
}
else
{
result = DestroyableSingleton<TranslationController>.Instance.GetString(StringNames.GameSecondsAbbrev, (Il2CppSystem.Object[])(
[
value.ToString(__instance.FormatString)
]));
}
__result = result;
return false;
}
}
4 changes: 2 additions & 2 deletions MiraAPI/Patches/PlayerControlPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static void PlayerControlFixedUpdatePostfix(PlayerControl __instance)

foreach (var button in CustomButtonManager.CustomButtons)
{
if (__instance.Data?.Role != null)
if (__instance.Data?.Role == null)
{
return;
continue;
}

if (!button.Enabled(__instance.Data?.Role))
Expand Down
Binary file modified MiraAPI/Resources/Checkmark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion MiraAPI/Utilities/Assets/MiraAssets.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace MiraAPI.Utilities.Assets;
using UnityEngine;

namespace MiraAPI.Utilities.Assets;

public static class MiraAssets
{
Expand All @@ -9,4 +11,7 @@ public static class MiraAssets
public static readonly LoadableResourceAsset Cog = new("MiraAPI.Resources.Cog.png");
public static readonly LoadableResourceAsset Checkmark = new("MiraAPI.Resources.Checkmark.png");
public static readonly LoadableResourceAsset CheckmarkBox = new("MiraAPI.Resources.CheckMarkBox.png");


public static readonly Color32 AcceptedTeal = new Color32(43, 233, 198, 255);
}
9 changes: 5 additions & 4 deletions MiraAPI/Utilities/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public static DeadBody GetBodyById(byte id)
return Object.FindObjectsOfType<DeadBody>().FirstOrDefault(body => body.ParentId == id);
}

public static string GetSuffix(NumberSuffixes suffix)
public static string GetSuffix(MiraNumberSuffixes suffix)
{
return suffix switch
{
NumberSuffixes.None => string.Empty,
NumberSuffixes.Multiplier => "x",
NumberSuffixes.Seconds => "s",
MiraNumberSuffixes.None => string.Empty,
MiraNumberSuffixes.Multiplier => "x",
MiraNumberSuffixes.Seconds => "s",
MiraNumberSuffixes.Percent => "%",
_ => string.Empty
};
}
Expand Down
8 changes: 8 additions & 0 deletions MiraAPI/Utilities/MiraNumberSuffixes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MiraAPI.Utilities;
public enum MiraNumberSuffixes
{
None,
Multiplier,
Seconds,
Percent
}

0 comments on commit 1cb11be

Please sign in to comment.