using AppUI.Manager.View;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace AppUI.View.Component
{
///
/// 运行时挂在 Resources/Manager/ModuleView 实例上,绑定层级:
/// ModuleView / BG、Frame / GameObject(Image + Text TMP)、BtnAgree;拒绝按钮节点若存在则仅隐藏不参与逻辑。
/// 弹出与关闭参考 的 Tip:Frame 缩放进入,同意后缩放消失再 。
///
public class ModuleView : MonoBehaviour
{
const string PathFrame = "Frame";
const string PathContentRow = "Frame/GameObject";
const string PathContentImage = "Frame/GameObject/Image";
const string PathContentTextTmp = "Frame/GameObject/Text (TMP)";
const string PathContentTextLegacy = "Frame/Text";
const string PathBtnReject = "Frame/BtnReject";
const string PathBtnAgree = "Frame/BtnAgree";
const string PathButtonLabelTmp = "Text (TMP)";
const float PopInDuration = 0.25f;
const float PopOutDuration = 0.3f;
public string text;
public string textKey;
public object[] textFormatArgs = { };
/// 左侧图标;为 null 则保持预制体默认且不隐藏。
public Sprite contentIcon;
public UnityAction onAgree;
public string onAgreeTextKey;
/// 为 true(默认)时,点击同意会先播放收起动画再销毁自身。
public bool willDestroyAfterClick = true;
bool _dismissing;
Tween _popTween;
public static ModuleView Show()
{
ViewConfig viewConfig = ViewManager.GetConfig(UIPrefabType.ModuleView);
GameObject go = Instantiate(Resources.Load(viewConfig.path));
return go.AddComponent();
}
void OnDestroy()
{
_popTween?.Kill();
transform.DOKill(true);
}
void Start()
{
HideRejectIfPresent();
BindContentText();
ApplyContentIcon();
BindAgreeButton();
TrySetButtonLabelKey(PathBtnAgree, onAgreeTextKey);
var frameRt = transform.Find(PathFrame) as RectTransform;
if (frameRt != null)
LayoutRebuilder.ForceRebuildLayoutImmediate(frameRt);
PlayPopIn();
}
void HideRejectIfPresent()
{
Transform reject = transform.Find(PathBtnReject);
if (reject != null)
reject.gameObject.SetActive(false);
}
void PlayPopIn()
{
var frame = transform.Find(PathFrame) as RectTransform;
if (frame == null)
return;
frame.DOKill();
frame.localScale = Vector3.zero;
_popTween = frame
.DOScale(1f, PopInDuration)
.SetEase(Ease.OutBack)
.SetUpdate(true);
}
/// 与点击同意后的收起一致:缩放 Frame 后销毁(不再次触发 )。
public void CloseWithTween()
{
if (_dismissing)
return;
if (!willDestroyAfterClick)
return;
_dismissing = true;
PlayPopOutAndDestroy();
}
void BindAgreeButton()
{
Transform btnTr = transform.Find(PathBtnAgree);
if (btnTr == null)
return;
var button = btnTr.GetComponent