Files

125 lines
4.1 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace HotUpdate.UI
{
/// <summary>
/// UI_ConfirmDialog:公告 / 维护 / 通用确认。
/// </summary>
public sealed class ConfirmDialogView : MonoBehaviour
{
public const string PrefabName = "UI_ConfirmDialog";
TextMeshProUGUI _title;
TextMeshProUGUI _message;
Button _ok;
Button _cancel;
Button _close;
Action _onOk;
Action _onCancel;
RectTransform _okRect;
Vector2 _okAnchorMin, _okAnchorMax, _okPivot, _okPosition;
ScrollRect _messageScroll;
public struct ViewState
{
public string Title, Message;
public bool Visible, ShowCancel;
public Action OnOk, OnCancel;
}
public ViewState CaptureState() => new ViewState
{
Title = _title != null ? _title.text : string.Empty,
Message = _message != null ? _message.text : string.Empty,
Visible = gameObject.activeSelf,
ShowCancel = _cancel != null && _cancel.gameObject.activeSelf,
OnOk = _onOk,
OnCancel = _onCancel
};
public void RestoreState(ViewState state)
{
Show(state.Title, state.Message, state.OnOk, state.OnCancel, state.ShowCancel);
if (!state.Visible) Hide();
}
public void Bind()
{
_title = FindTmp("Txt_Title");
_message = FindTmp("Txt_Message");
_ok = FindBtn("Btn_Ok");
_cancel = FindBtn("Btn_Cancel");
_close = FindBtn("Btn_Close");
if (_ok != null)
{
_okRect = _ok.GetComponent<RectTransform>();
_okAnchorMin = _okRect.anchorMin;
_okAnchorMax = _okRect.anchorMax;
_okPivot = _okRect.pivot;
_okPosition = _okRect.anchoredPosition;
}
_messageScroll = GetComponentInChildren<ScrollRect>(true);
Wire(_ok, () => { _onOk?.Invoke(); Hide(); });
Wire(_cancel, () => { _onCancel?.Invoke(); Hide(); });
Wire(_close, () => { _onCancel?.Invoke(); Hide(); });
}
public void Show(
string title,
string message,
Action onOk = null,
Action onCancel = null,
bool showCancel = false)
{
if (_title != null) _title.text = title ?? string.Empty;
if (_message != null) _message.text = message ?? string.Empty;
_onOk = onOk;
_onCancel = onCancel;
if (_cancel != null) _cancel.gameObject.SetActive(showCancel);
if (_okRect != null)
{
_okRect.anchorMin = showCancel ? _okAnchorMin : new Vector2(0.5f, _okAnchorMin.y);
_okRect.anchorMax = showCancel ? _okAnchorMax : new Vector2(0.5f, _okAnchorMax.y);
_okRect.pivot = showCancel ? _okPivot : new Vector2(0.5f, _okPivot.y);
_okRect.anchoredPosition = showCancel ? _okPosition : new Vector2(0, _okPosition.y);
}
if (_messageScroll != null && _messageScroll.content != null)
{
_messageScroll.StopMovement();
_messageScroll.content.anchoredPosition = Vector2.zero;
}
gameObject.SetActive(true);
}
public void Hide()
{
gameObject.SetActive(false);
}
static void Wire(Button btn, Action action)
{
if (btn == null) return;
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() => action?.Invoke());
}
TextMeshProUGUI FindTmp(string name)
{
foreach (var c in GetComponentsInChildren<TextMeshProUGUI>(true))
if (c.name == name) return c;
return null;
}
Button FindBtn(string name)
{
foreach (var c in GetComponentsInChildren<Button>(true))
if (c.name == name) return c;
return null;
}
}
}