262 lines
8.9 KiB
C#
262 lines
8.9 KiB
C#
using BrewMonster.Network;
|
|
using BrewMonster.Scripts;
|
|
using BrewMonster.Scripts.Managers;
|
|
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BrewMonster.UI
|
|
{
|
|
/// <summary>
|
|
/// Numeric input dialog (C++ CDlgInputNO / DlgInputNO). Dialog id: <c>DlgQuantity</c>.
|
|
/// </summary>
|
|
public class DlgQuantity : AUIDialog
|
|
{
|
|
public enum QuantityMode
|
|
{
|
|
None = 0,
|
|
/// <summary>Gửi tiền vào kho — C++ INPUTNO_STORAGE_IVTR_MONEY (Win_Inventory choosemoney + storage open).</summary>
|
|
StorageDepositMoney,
|
|
/// <summary>Rút tiền ra — C++ INPUTNO_STORAGE_TRASH_MONEY (Win_Storage choosemoney).</summary>
|
|
StorageWithdrawMoney,
|
|
/// <summary>Gửi tiền vào kho tài khoản — same C2S as storage, with is_accountbox=1.</summary>
|
|
AccountStorageDepositMoney,
|
|
/// <summary>Rút tiền ra từ kho tài khoản — same C2S as storage, with is_accountbox=1.</summary>
|
|
AccountStorageWithdrawMoney,
|
|
}
|
|
|
|
[Header("Amount")]
|
|
[SerializeField] private TMP_InputField amountInput;
|
|
[SerializeField] private Text amountTextLegacy;
|
|
|
|
[Header("Buttons (C++: confirm / IDCANCEL / add / minus / max)")]
|
|
[SerializeField] private Button confirmButton;
|
|
[SerializeField] private Button cancelButton;
|
|
[SerializeField] private Button addButton;
|
|
[SerializeField] private Button minusButton;
|
|
[SerializeField] private Button maxButton;
|
|
|
|
private QuantityMode _mode = QuantityMode.None;
|
|
private int _min = 1;
|
|
private int _max = 1;
|
|
private int _current = 1;
|
|
private Action<int, QuantityMode> _onConfirmed;
|
|
|
|
public static DlgQuantity GetDialog()
|
|
{
|
|
return EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan()?.GetDialog("DlgQuantity") as DlgQuantity;
|
|
}
|
|
|
|
/// <summary>Open quantity panel; on confirm invokes callback then sends C2S when mode is storage money.</summary>
|
|
public static void Show(QuantityMode mode, int maxAmount, int defaultAmount = 1, Action<int, QuantityMode> onConfirmed = null)
|
|
{
|
|
var dlg = GetDialog();
|
|
if (dlg == null)
|
|
{
|
|
Debug.LogError("[DlgQuantity] Dialog 'DlgQuantity' not found. Add prefab to DialogScriptTableObject (id DlgQuantity).");
|
|
return;
|
|
}
|
|
|
|
CECUIManager.Instance?.ShowDialogOverlay("DlgQuantity");
|
|
dlg.Open(mode, maxAmount, defaultAmount, onConfirmed);
|
|
}
|
|
|
|
public override void Awake()
|
|
{
|
|
base.Awake();
|
|
ResolveControls();
|
|
WireButtons();
|
|
}
|
|
|
|
void ResolveControls()
|
|
{
|
|
if (amountInput == null)
|
|
{
|
|
amountInput = GetComponentInChildren<TMP_InputField>(true);
|
|
if (amountInput != null && amountInput.name.Contains("DEFAULT", StringComparison.OrdinalIgnoreCase) == false)
|
|
{
|
|
var all = GetComponentsInChildren<TMP_InputField>(true);
|
|
for (int i = 0; i < all.Length; i++)
|
|
{
|
|
if (all[i] != null && all[i].name.IndexOf("Txt_No", StringComparison.OrdinalIgnoreCase) >= 0)
|
|
{
|
|
amountInput = all[i];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ResolveButton(ref confirmButton, "confirm", "btn_confirm", "ok");
|
|
ResolveButton(ref cancelButton, "cancel", "idcanel", "btn_cancel", "close");
|
|
ResolveButton(ref addButton, "add", "btn_add", "incre", "plus");
|
|
ResolveButton(ref minusButton, "minus", "btn_minus", "reduce", "decre");
|
|
ResolveButton(ref maxButton, "max", "btn_max");
|
|
}
|
|
|
|
void ResolveButton(ref Button field, params string[] nameHints)
|
|
{
|
|
if (field != null)
|
|
return;
|
|
var buttons = 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void WireButtons()
|
|
{
|
|
if (confirmButton != null)
|
|
{
|
|
confirmButton.onClick.RemoveAllListeners();
|
|
confirmButton.onClick.AddListener(OnConfirm);
|
|
}
|
|
if (cancelButton != null)
|
|
{
|
|
cancelButton.onClick.RemoveAllListeners();
|
|
cancelButton.onClick.AddListener(OnCancel);
|
|
}
|
|
if (addButton != null)
|
|
{
|
|
addButton.onClick.RemoveAllListeners();
|
|
addButton.onClick.AddListener(OnAdd);
|
|
}
|
|
if (minusButton != null)
|
|
{
|
|
minusButton.onClick.RemoveAllListeners();
|
|
minusButton.onClick.AddListener(OnMinus);
|
|
}
|
|
if (maxButton != null)
|
|
{
|
|
maxButton.onClick.RemoveAllListeners();
|
|
maxButton.onClick.AddListener(OnMax);
|
|
}
|
|
|
|
if (amountInput != null)
|
|
{
|
|
amountInput.onValueChanged.RemoveAllListeners();
|
|
amountInput.onValueChanged.AddListener(OnAmountInputChanged);
|
|
amountInput.contentType = TMP_InputField.ContentType.IntegerNumber;
|
|
}
|
|
}
|
|
|
|
public void Open(QuantityMode mode, int maxAmount, int defaultAmount, Action<int, QuantityMode> onConfirmed)
|
|
{
|
|
_mode = mode;
|
|
_onConfirmed = onConfirmed;
|
|
_max = Mathf.Max(0, maxAmount);
|
|
_min = _max > 0 ? 1 : 0;
|
|
_current = Mathf.Clamp(defaultAmount, _min, Mathf.Max(_min, _max));
|
|
if (_max <= 0)
|
|
_current = 0;
|
|
|
|
ApplyAmountToUi();
|
|
Show(true);
|
|
}
|
|
|
|
void ApplyAmountToUi()
|
|
{
|
|
string text = _current.ToString();
|
|
if (amountInput != null)
|
|
amountInput.SetTextWithoutNotify(text);
|
|
if (amountTextLegacy != null)
|
|
amountTextLegacy.text = text;
|
|
}
|
|
|
|
int ReadAmountFromUi()
|
|
{
|
|
if (amountInput != null && int.TryParse(amountInput.text, out int v))
|
|
return v;
|
|
if (amountTextLegacy != null && int.TryParse(amountTextLegacy.text, out v))
|
|
return v;
|
|
return _current;
|
|
}
|
|
|
|
void SetAmount(int value)
|
|
{
|
|
if (_max <= 0)
|
|
{
|
|
_current = 0;
|
|
}
|
|
else
|
|
{
|
|
_current = Mathf.Clamp(value, _min, _max);
|
|
}
|
|
ApplyAmountToUi();
|
|
}
|
|
|
|
void OnAmountInputChanged(string _)
|
|
{
|
|
SetAmount(ReadAmountFromUi());
|
|
}
|
|
|
|
void OnAdd() => SetAmount(_current + 1);
|
|
void OnMinus() => SetAmount(_current - 1);
|
|
void OnMax() => SetAmount(_max);
|
|
|
|
void OnConfirm()
|
|
{
|
|
if (_max <= 0)
|
|
{
|
|
OnCancel();
|
|
return;
|
|
}
|
|
|
|
int n = ReadAmountFromUi();
|
|
if (n < _min)
|
|
n = _min;
|
|
if (n > _max)
|
|
n = _max;
|
|
if (n <= 0)
|
|
return;
|
|
|
|
ExecuteMode(n);
|
|
_onConfirmed?.Invoke(n, _mode);
|
|
Close();
|
|
}
|
|
|
|
void ExecuteMode(int amount)
|
|
{
|
|
switch (_mode)
|
|
{
|
|
case QuantityMode.StorageDepositMoney:
|
|
// C++ INPUTNO_STORAGE_IVTR_MONEY: ExgTrashBoxMoney(iTrash=0, iIvtr=amount)
|
|
UnityGameSession.c2s_CmdExgTrashBoxMoney(amount, 0);
|
|
break;
|
|
case QuantityMode.StorageWithdrawMoney:
|
|
// C++ INPUTNO_STORAGE_TRASH_MONEY: ExgTrashBoxMoney(iTrash=amount, iIvtr=0)
|
|
UnityGameSession.c2s_CmdExgTrashBoxMoney(0, amount);
|
|
break;
|
|
case QuantityMode.AccountStorageDepositMoney:
|
|
UnityGameSession.c2s_CmdExgTrashBoxMoney(amount, 0, true);
|
|
break;
|
|
case QuantityMode.AccountStorageWithdrawMoney:
|
|
UnityGameSession.c2s_CmdExgTrashBoxMoney(0, amount, true);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void OnCancel() => Close();
|
|
|
|
void Close()
|
|
{
|
|
_mode = QuantityMode.None;
|
|
_onConfirmed = null;
|
|
CECUIManager.Instance?.HideDialogOverlay("DlgQuantity");
|
|
}
|
|
}
|
|
}
|