Files
test/Assets/PerfectWorld/Scripts/UI/Dialogs/DlgStorageChangePW.cs
T
2026-05-27 10:55:35 +07:00

244 lines
9.1 KiB
C#

using BrewMonster.Network;
using BrewMonster.Scripts;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster.UI
{
/// <summary>
/// Change warehouse password (C++ CDlgStorageChangePW / Win_InputString3).
/// Dialog id for <see cref="CECUIManager.ShowUI"/>: <see cref="DialogId"/>.
/// </summary>
public class DlgStorageChangePW : AUIDialog
{
public const string DialogId = "DlgStorageChangePW";
const int StringIdPasswordMismatch = 254;
[SerializeField] private TMP_InputField oldPasswordInput;
[SerializeField] private TMP_InputField newPasswordInput;
[SerializeField] private TMP_InputField confirmPasswordInput;
[SerializeField] private Button confirmButton;
[SerializeField] private Button cancelButton;
[SerializeField] private Button closeButton;
public static DlgStorageChangePW GetDialog()
{
return EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan()?.GetDialog(DialogId) as DlgStorageChangePW;
}
public override void Awake()
{
base.Awake();
ResolveControls();
WireButtons();
}
public override void OnShowDialogue()
{
base.OnShowDialogue();
WireButtons();
ClearFields();
}
void ResolveControls()
{
oldPasswordInput = DlgStoragePasswordUiHelper.FindInputField(transform, oldPasswordInput, "DEFAULT_Txt_Input");
newPasswordInput = DlgStoragePasswordUiHelper.FindInputField(transform, newPasswordInput, "Txt_New");
confirmPasswordInput = DlgStoragePasswordUiHelper.FindInputField(transform, confirmPasswordInput, "Txt_NewConfirm");
EnsurePasswordFieldLayout();
DlgStoragePasswordUiHelper.ConfigurePasswordField(oldPasswordInput);
DlgStoragePasswordUiHelper.ConfigurePasswordField(newPasswordInput);
DlgStoragePasswordUiHelper.ConfigurePasswordField(confirmPasswordInput);
DlgStoragePasswordUiHelper.ResolveButton(ref confirmButton, transform, "confirm", "btn_decide", "btn_confirm", "ok");
DlgStoragePasswordUiHelper.ResolveButton(ref cancelButton, transform, "cancel", "idcanel", "btn_cancel", "btn_exit");
DlgStoragePasswordUiHelper.ResolveButton(ref closeButton, transform, "btn_close", "close");
}
void WireButtons()
{
DlgStoragePasswordUiHelper.ResolveButton(ref confirmButton, transform, "confirm", "btn_decide", "btn_confirm", "ok");
DlgStoragePasswordUiHelper.WireConfirmButton(transform, OnConfirm, confirmButton);
DlgStoragePasswordUiHelper.WireCancelButtons(transform, OnCancel);
}
void EnsurePasswordFieldLayout()
{
if (oldPasswordInput == null)
return;
if (newPasswordInput == null)
newPasswordInput = DlgStoragePasswordUiHelper.ClonePasswordField(oldPasswordInput, "Txt_New", new Vector2(0f, -22f));
if (confirmPasswordInput == null)
confirmPasswordInput = DlgStoragePasswordUiHelper.ClonePasswordField(oldPasswordInput, "Txt_NewConfirm", new Vector2(0f, -44f));
}
void ClearFields()
{
if (oldPasswordInput != null)
oldPasswordInput.SetTextWithoutNotify(string.Empty);
if (newPasswordInput != null)
newPasswordInput.SetTextWithoutNotify(string.Empty);
if (confirmPasswordInput != null)
confirmPasswordInput.SetTextWithoutNotify(string.Empty);
}
void OnConfirm()
{
string oldPw = oldPasswordInput != null ? oldPasswordInput.text : string.Empty;
string newPw = newPasswordInput != null ? newPasswordInput.text : string.Empty;
string confirmPw = confirmPasswordInput != null ? confirmPasswordInput.text : string.Empty;
if (newPw != confirmPw)
{
var gameUi = EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan();
gameUi?.ShowErrorMsg(GetStringFromTable(StringIdPasswordMismatch), string.Empty);
return;
}
UnityGameSession.c2s_CmdNPCSevChgTrashPsw(oldPw, newPw);
CloseDialog();
}
void OnCancel()
{
CECUIManager.Instance?.EndNpcTalkAfterStorageService();
}
void CloseDialog()
{
CECUIManager.Instance?.EndNpcTalkAfterStorageService();
}
}
static class DlgStoragePasswordUiHelper
{
public static TMP_InputField FindInputField(Transform root, TMP_InputField assigned, string controlName)
{
if (assigned != null)
return assigned;
var direct = root.Find(controlName);
if (direct != null && direct.TryGetComponent(out TMP_InputField directField))
return directField;
var all = root.GetComponentsInChildren<TMP_InputField>(true);
for (int i = 0; i < all.Length; i++)
{
if (all[i] != null && string.Equals(all[i].name, controlName, StringComparison.OrdinalIgnoreCase))
return all[i];
}
if (all.Length == 1 && string.Equals(controlName, "DEFAULT_Txt_Input", StringComparison.OrdinalIgnoreCase))
return all[0];
return null;
}
public static void ConfigurePasswordField(TMP_InputField field)
{
if (field == null)
return;
field.contentType = TMP_InputField.ContentType.Password;
field.inputType = TMP_InputField.InputType.Password;
}
public static TMP_InputField ClonePasswordField(TMP_InputField template, string name, Vector2 anchoredOffset)
{
if (template == null)
return null;
var clone = UnityEngine.Object.Instantiate(template, template.transform.parent);
clone.name = name;
var rt = clone.GetComponent<RectTransform>();
if (rt != null)
rt.anchoredPosition += anchoredOffset;
ConfigurePasswordField(clone);
return clone;
}
public static void ResolveButton(ref Button field, Transform root, params string[] nameHints)
{
if (field != null)
return;
var buttons = root.GetComponentsInChildren<Button>(true);
for (int i = 0; i < buttons.Length; i++)
{
var btn = buttons[i];
if (btn == null)
continue;
string n = btn.name.ToLowerInvariant();
for (int h = 0; h < nameHints.Length; h++)
{
if (n.Contains(nameHints[h]))
{
field = btn;
return;
}
}
}
}
public static void WireConfirmButton(Transform root, Action onConfirm, Button assignedConfirm = null)
{
if (onConfirm == null)
return;
if (assignedConfirm != null)
{
assignedConfirm.onClick.RemoveAllListeners();
assignedConfirm.onClick.AddListener(() => onConfirm());
return;
}
var buttons = root.GetComponentsInChildren<Button>(true);
for (int i = 0; i < buttons.Length; i++)
{
var btn = buttons[i];
if (btn == null || IsCancelButtonName(btn.name))
continue;
string n = btn.name.ToLowerInvariant();
if (n.Contains("confirm") || n.Contains("btn_decide") || n.Contains("btn_confirm") || n == "ok")
{
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() => onConfirm());
return;
}
}
}
public static void WireCancelButtons(Transform root, Action onCancel)
{
if (onCancel == null)
return;
var buttons = root.GetComponentsInChildren<Button>(true);
for (int i = 0; i < buttons.Length; i++)
{
var btn = buttons[i];
if (btn == null || !IsCancelButtonName(btn.name))
continue;
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() => onCancel());
}
}
static bool IsCancelButtonName(string buttonName)
{
if (string.IsNullOrEmpty(buttonName))
return false;
string n = buttonName.ToLowerInvariant();
if (n.Contains("confirm") || n.Contains("decide") || n == "ok")
return false;
if (n.Contains("add") || n.Contains("minus") || n.Contains("max"))
return false;
return n.Contains("cancel") || n.Contains("btn_exit") || n.Contains("idcanel")
|| n.Contains("btn_close") || n == "close";
}
}
}