using System;
using Cysharp.Threading.Tasks;
using Framework;
using PhxhSDK;
using UnityEngine;
using UnityEngine.UI;
/*
╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ UIWindow 按钮绑定模块 (Button Binding) ║
╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ ║
║ 【模块功能】 ║
║ 本模块提供按钮绑定的核心功能: ║
║ • 手动按钮绑定 API - 在OnInit()中手动绑定自定义按钮 ║
║ ║
║ 注意:默认按钮的处理已移至独立的 partial 文件: ║
║ • UIWindow.DefaultCloseButton.cs - 处理默认关闭按钮(BgClose、BtnClose) ║
║ • UIWindow.DefaultHomeButton.cs - 处理默认回主界面按钮(BtnHome、BtnBackHome、HomeButton) ║
║ • UIWindow.DefaultBackButton.cs - 处理默认返回按钮(BtnBack、BackButton、BtnReturn) ║
║ ║
║ 【使用方式】 ║
║ ║
║ 在 OnInit() 中使用 BindButton() API 手动绑定: ║
║ ║
║ 示例: ║
║ protected override void OnInit() ║
║ { ║
║ // 通过路径绑定 ║
║ BindButton("BtnStart", OnStartClick); ║
║ ║
║ // 通过Button组件绑定 ║
║ BindButton(myButton, OnMyButtonClick); ║
║ ║
║ // 带参数绑定 ║
║ BindButton(itemButton, OnItemClick, itemData); ║
║ ║
║ // 静音绑定(不播放音效) ║
║ BindButton(silentButton, OnSilentClick, playAudio: false); ║
║ } ║
║ ║
║ 【更多信息】 ║
║ • 关闭按钮:UIWindow.DefaultCloseButton.cs ║
║ • 回主界面按钮:UIWindow.DefaultHomeButton.cs ║
║ • 返回按钮:UIWindow.DefaultBackButton.cs ║
║ ║
╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
*/
///
/// UI窗口基类 - 按钮绑定部分
/// 提供按钮绑定API和默认按钮自动绑定功能
///
public abstract partial class UIWindow
{
#region Button Binding Core - 按钮绑定核心逻辑
///
/// 处理按钮点击的音频逻辑
///
private void HandleButtonAudio(Button button, bool playAudio)
{
if (!playAudio) return;
var clickAudio = button.GetComponent();
if (clickAudio)
{
if (clickAudio.IsPlayAudio && !string.IsNullOrEmpty(clickAudio.AudioKey))
{
AudioManager.Instance.PlayAudio(clickAudio.AudioKey);
}
}
else
{
// do nothing, must use ClickAudio
DebugUtil.LogError("按钮未添加ClickAudio组件,请检查按钮绑定参数!");
}
}
///
/// 绑定按钮点击事件的核心逻辑(无参数)
///
private void BindButtonCore(Button button, Action action, bool playAudio = true)
{
if (button is null)
{
DebugUtil.LogError("传入按钮为空,无法绑定点击事件!");
return;
}
button.onClick.RemoveAllListeners();
button.onClick.AddListener(() =>
{
DebugUtil.Log("按钮被点击: {0}", button.gameObject.name);
if (UIManager.IsUIInputBlocked) return;
UIManager.Instance.BlockUIForNormalOperation();
HandleButtonAudio(button, playAudio);
action?.Invoke();
});
DebugUtil.Log("按钮绑定完成: {0}", button.gameObject.name);
}
///
/// 绑定按钮点击事件的核心逻辑(带参数)
///
private void BindButtonCore(Button button, Action action, T parameter, bool playAudio = true)
{
if (button is null) return;
button.onClick.RemoveAllListeners();
button.onClick.AddListener(() =>
{
if (UIManager.IsUIInputBlocked) return;
UIManager.Instance.BlockUIForNormalOperation();
HandleButtonAudio(button, playAudio);
if (parameter == null)
{
DebugUtil.LogError("=== 传入参数为空! ===");
}
action?.Invoke(parameter);
});
}
#endregion
#region Button Binding Public API - 手动绑定按钮的公共API
///
/// 通过路径绑定按钮
///
/// 按钮节点路径(相对于窗口根节点)
/// 点击回调
/// 是否播放点击音效(默认true)
/// Button组件
///
/// BindButton("TopBar/BtnSettings", OnSettingsClick);
///
public Button BindButton(string target, Action action, bool playAudio = true)
{
GameObject obj = transform.Find(target)?.gameObject;
if (obj is not null)
{
Button button = obj.GetComponent