Files
Tarkov-UIFixes/Patches/ConfirmationDialogKeysPatches.cs
2024-06-03 01:45:12 -07:00

123 lines
4.3 KiB
C#

using Aki.Reflection.Patching;
using EFT.InputSystem;
using EFT.UI;
using HarmonyLib;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
namespace UIFixes
{
public static class ConfirmDialogKeysPatches
{
public static void Enable()
{
new DialogWindowPatch().Enable();
new SplitDialogPatch().Enable();
new ClickOutPatch().Enable();
new ClickOutSplitDialogPatch().Enable();
}
public class DialogWindowPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(R.DialogWindow.Type, "Update");
}
[PatchPostfix]
public static void Postfix(object __instance, bool ___bool_0)
{
var instance = new R.DialogWindow(__instance);
if (!___bool_0)
{
return;
}
if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Space))
{
instance.Accept();
return;
}
}
}
// Of course SplitDialogs are a *completely different dialog impelementation*
public class SplitDialogPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(ItemUiContext), nameof(ItemUiContext.TranslateCommand));
}
[PatchPrefix]
public static bool Prefix(ECommand command, ref InputNode.ETranslateResult __result, SplitDialog ___splitDialog_0)
{
// It's wild to me that they implement UI keyboard shortcuts via the in-raid movement keybinds
if (___splitDialog_0 != null && ___splitDialog_0.gameObject.activeSelf && command == ECommand.Jump)
{
___splitDialog_0.Accept();
__result = InputNode.ETranslateResult.Block;
return false;
}
return true;
}
}
public class ClickOutPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.DeclaredMethod(typeof(MessageWindow), nameof(MessageWindow.Show));
}
[PatchPostfix]
public static void Postfix(MessageWindow __instance)
{
if (!Settings.ClickOutOfDialogs.Value)
{
return;
}
// Note the space after firewall, because unity doesn't trim names and BSG is incompetent
Button button = __instance.transform.Find("Window/Firewall ")?.gameObject.GetOrAddComponent<Button>();
if (button != null)
{
button.transition = Selectable.Transition.None;
button.onClick.AddListener(__instance.Close);
__instance.R().UI.AddDisposable(button.onClick.RemoveAllListeners);
}
}
}
public class ClickOutSplitDialogPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
// Using method_0 because there's 2 Show(), and they have 10+ args and f that
return AccessTools.Method(typeof(SplitDialog), nameof(SplitDialog.method_0));
}
[PatchPostfix]
public static void Postfix(SplitDialog __instance)
{
if (!Settings.ClickOutOfDialogs.Value)
{
return;
}
// Note the space after firewall, because unity doesn't trim names and BSG is incompetent
Button button = __instance.transform.Find("Background")?.gameObject.GetOrAddComponent<Button>();
if (button != null)
{
button.transition = Selectable.Transition.None;
button.onClick.RemoveAllListeners(); // There's no disposable here so keeping the listener count down
button.onClick.AddListener(__instance.method_2);
}
}
}
}
}