Skip to content

Commit

Permalink
add in backwards compat for OnInputPopupValidateChar
Browse files Browse the repository at this point in the history
  • Loading branch information
misternebula committed Oct 11, 2024
1 parent 866e5c7 commit 09af8a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
9 changes: 6 additions & 3 deletions src/OWML.Common/Interfaces/Menus/IOWMLPopupInputMenu.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using UnityEngine.UI;
using System;
using UnityEngine.UI;

namespace OWML.Common.Interfaces.Menus
{
public interface IOWMLPopupInputMenu
{
public delegate bool InputPopupValidateCharEvent(string input, int charIndex, char addedChar);
[Obsolete("Use OnValidateChar instead.")]
public event PopupInputMenu.InputPopupValidateCharEvent OnInputPopupValidateChar;

public event InputPopupValidateCharEvent OnInputPopupValidateChar;
public delegate bool InputPopupValidateCharEvent(string input, int charIndex, char addedChar);
public event InputPopupValidateCharEvent OnValidateChar;

public void EnableMenu(bool value);

Expand Down
31 changes: 23 additions & 8 deletions src/OWML.ModHelper.Menus/CustomInputs/OWMLPopupInputMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class OWMLPopupInputMenu : PopupMenu, IOWMLPopupInputMenu
protected bool _virtualKeyboardOpen;

public event PopupInputMenu.InputPopupTextChangedEvent OnInputPopupTextChanged;
public event IOWMLPopupInputMenu.InputPopupValidateCharEvent OnInputPopupValidateChar;
public event IOWMLPopupInputMenu.InputPopupValidateCharEvent OnValidateChar;

[Obsolete("Use OnValidateChar instead.")]
public event PopupInputMenu.InputPopupValidateCharEvent OnInputPopupValidateChar;

public override void Awake()
{
Expand Down Expand Up @@ -152,20 +155,32 @@ private void OnTextFieldChanged()

private char OnValidateInput(string input, int charIndex, char addedChar)
{
bool flag = true;
if (this.OnInputPopupValidateChar != null)
var isValidCharacter = true;
if (OnInputPopupValidateChar != null)
{
var invocationList = OnInputPopupValidateChar.GetInvocationList();
for (var i = 0; i < invocationList.Length; i++)
{
var flag2 = (bool)invocationList[i].DynamicInvoke(new object[] { addedChar });
isValidCharacter = isValidCharacter && flag2;
}
}

if (OnValidateChar != null && isValidCharacter)
{
Delegate[] invocationList = this.OnInputPopupValidateChar.GetInvocationList();
for (int i = 0; i < invocationList.Length; i++)
var invocationList = OnValidateChar.GetInvocationList();
for (var i = 0; i < invocationList.Length; i++)
{
bool flag2 = (bool)invocationList[i].DynamicInvoke(new object[] { input, charIndex, addedChar });
flag = flag && flag2;
var flag2 = (bool)invocationList[i].DynamicInvoke(new object[] { input, charIndex, addedChar });
isValidCharacter = isValidCharacter && flag2;
}
}
if (flag)

if (isValidCharacter)
{
return addedChar;
}

return '\0';
}

Expand Down
8 changes: 7 additions & 1 deletion src/OWML.ModHelper.Menus/NewMenuSystem/OptionsMenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,13 @@ public IOWMLTextEntryElement AddTextEntryInput(Menu menu, string label, string i

if (isNumeric)
{
textInputPopup.OnInputPopupValidateChar += (string input, int charIndex, char addedChar) =>
textInputPopup.OnInputPopupValidateChar += c =>
{
var text = textInputPopup.GetInputText() + c;
return Regex.IsMatch(text, @"^\d*[,.]?\d*$");
};

textInputPopup.OnValidateChar += (string input, int charIndex, char addedChar) =>
{
var text = input.Insert(charIndex, addedChar.ToString());
return Regex.IsMatch(text, @"^-?\d*[,.]?\d*$");
Expand Down

0 comments on commit 09af8a9

Please sign in to comment.