Main/System/HeroUI/HeroSelectBehaviour.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
@@ -10,17 +11,25 @@
    [SerializeField] Button foldBtn;    //收起按钮
    [SerializeField] Transform foldForm;    //收起容器
    [SerializeField] Button unFoldBtn;  //展开按钮
    [SerializeField] GroupButtonEx[] jobsBtn;
    [SerializeField] GroupButtonEx[] countrysBtn;
    [SerializeField] GroupButtonExManager jobManager;
    [SerializeField] GroupButtonExManager countryManager;
    [SerializeField] Toggle[] jobsBtn;
    [SerializeField] Toggle[] countrysBtn;
    [SerializeField] Toggle[] hurtTypeBtn;
    [SerializeField] Toggle[] specialType6Btn;
    [SerializeField] Transform specialTypeMoreRect;
    [SerializeField] ClickScreenOtherSpaceEvent clickScreenOtherSpaceEvent;
    [SerializeField] Button resetBtn;
    int m_Job = 0;
    int m_Country = 0;
    Toggle[] specialTypeMoreBtn;
    int m_Job = 0;  //职业 多选项按位存储
    int m_Country = 0;  //国家 多选项位按存储
    int m_HurtType = 0;  //伤害类型 多选项位按存储
    int m_SpecialType6 = 0;  //6大战斗属性 多选项位按存储
    int m_SpecialTypeMore = 0;  //特殊属性 多选项位按存储
    int foldState = 0;  //0 收起,1 展开
    //点击按钮需通知响应外部事件
    private Action<int, int> selectAction;
    private Action<List<int>> selectAction;
@@ -38,80 +47,190 @@
            RefreshFolState();
        });
        specialTypeMoreBtn = specialTypeMoreRect.GetComponentsInChildren<Toggle>();
        // 初始化特殊属性按钮的显示和文本
        for (int i = 0; i < specialTypeMoreBtn.Length; i++)
        {
            if (i < HeroUIManager.Instance.heroSpecialAttrsForSelect.Count)
            {
                int index = HeroUIManager.Instance.heroSpecialAttrsForSelect[i];
                specialTypeMoreBtn[i].SetActive(true);
                specialTypeMoreBtn[i].GetComponentInChildren<Text>().text = Language.Get($"HeroSpecialty2_{index}");
            }
            else
            {
                specialTypeMoreBtn[i].SetActive(false);
            }
        }
        // 添加所有Toggle监听器
        AddAllListeners();
        clickScreenOtherSpaceEvent.AddListener(() =>
        {
            if (foldState == 0)
                return;
            foldBtn.onClick.Invoke();
        });
        resetBtn.AddListener(Reset);
    }
    // 移除所有Toggle监听器
    void RemoveAllListeners()
    {
        for (int i = 0; i < jobsBtn.Length; i++)
        {
            int index = i;
            jobsBtn[i].AddListener(() =>
            jobsBtn[i].onValueChanged.RemoveAllListeners();
        }
        for (int i = 0; i < countrysBtn.Length; i++)
        {
            countrysBtn[i].onValueChanged.RemoveAllListeners();
        }
        for (int i = 0; i < hurtTypeBtn.Length; i++)
        {
            hurtTypeBtn[i].onValueChanged.RemoveAllListeners();
        }
        for (int i = 0; i < specialType6Btn.Length; i++)
        {
            specialType6Btn[i].onValueChanged.RemoveAllListeners();
        }
        for (int i = 0; i < specialTypeMoreBtn.Length; i++)
        {
            specialTypeMoreBtn[i].onValueChanged.RemoveAllListeners();
        }
    }
    // 添加所有Toggle监听器
    void AddAllListeners()
    {
        for (int i = 0; i < jobsBtn.Length; i++)
        {
            int index = i + 1;
            jobsBtn[i].onValueChanged.AddListener((bool value) =>
            {
                m_Job = index;
                RefreshJobsBtn();
                selectAction?.Invoke(m_Job, m_Country);
                m_Job = value ? m_Job | (1 << index) : m_Job & ~(1 << index);
                selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
            });
        }
        for (int i = 0; i < countrysBtn.Length; i++)
        {
            int index = i;
            countrysBtn[i].AddListener(() =>
            int index = i + 1;
            countrysBtn[i].onValueChanged.AddListener((bool value) =>
            {
                m_Country = index;
                RefreshCountryBtn();
                selectAction?.Invoke(m_Job, m_Country);
                m_Country = value ? m_Country | (1 << index) : m_Country & ~(1 << index);
                selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
            });
        }
        for (int i = 0; i < hurtTypeBtn.Length; i++)
        {
            int index = i + 1;
            hurtTypeBtn[i].onValueChanged.AddListener((bool value) =>
            {
                m_HurtType = value ? m_HurtType | (1 << index) : m_HurtType & ~(1 << index);
                selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
            });
        }
        for (int i = 0; i < specialType6Btn.Length; i++)
        {
            int index = i + 1;
            specialType6Btn[i].onValueChanged.AddListener((bool value) =>
            {
                m_SpecialType6 = value ? m_SpecialType6 | (1 << index) : m_SpecialType6 & ~(1 << index);
                selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
            });
        }
        for (int i = 0; i < specialTypeMoreBtn.Length; i++)
        {
            if (i < HeroUIManager.Instance.heroSpecialAttrsForSelect.Count)
            {
                int index = HeroUIManager.Instance.heroSpecialAttrsForSelect[i];
                specialTypeMoreBtn[i].onValueChanged.AddListener((bool value) =>
                {
                    m_SpecialTypeMore = value ? m_SpecialTypeMore | (1 << index) : m_SpecialTypeMore & ~(1 << index);
                    selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
                });
            }
        }
    }
    /// <summary>
    /// 国家职业筛选
    /// </summary>
    /// <param name="state"> 0收起,1展开</param>
    /// <param name="job"></param>
    /// <param name="country"></param>
    /// <param name="onRefresh"> 点击按钮需通知响应外部事件</param>
    public void Display(int state, int job, int country, Action<int, int> onRefresh)
    /// 回调参数: 职业,国家,伤害类型,6大战斗属性,特殊属性
    public void Display(int state, Action<List<int>> onRefresh)
    {
        foldState = state;
        m_Job = job;
        m_Country = country;
        RefreshFolState();
        RefreshJobsBtn();
        RefreshCountryBtn();
        selectAction = onRefresh;
        Reset();
    }
    //刷新全部分类
    void RefreshJobsBtn()
    {
        jobManager.SelectButton(jobsBtn[m_Job]);
    }
    //刷新全部分类
    void RefreshCountryBtn()
    {
        countryManager.SelectButton(countrysBtn[m_Country]);
    }
    //刷新展开收起状态
    void RefreshFolState()
    {
        unFoldForm.SetActive(foldState == 1);
        foldForm.SetActive(foldState == 0);
    }
    private void LateUpdate()
    {
        if (foldState == 0)
            return;
        if (Input.GetMouseButtonDown(0))
        if (foldState == 1)
        {
            if (!RectTransformUtility.RectangleContainsScreenPoint(this.transform as RectTransform, Input.mousePosition, CameraManager.uiCamera))
            {
                foldBtn.onClick.Invoke();
            }
            UIHelper.ForceRefreshLayout(unFoldForm).Forget();
        }
    }
    public static HeroSelectBehaviour Create(Transform heroSelectBehaviour)
    {
        var instanceGO = UIUtility.CreateWidget("HeroSelectBehaviour", "HeroSelectBehaviour");
        instanceGO.transform.SetParentEx(heroSelectBehaviour, Vector3.zero, Quaternion.identity, Vector3.one);
        return instanceGO.GetComponent<HeroSelectBehaviour>();
    }
    void Reset()
    {
        // 暂时移除所有监听器避免重置时多次触发
        RemoveAllListeners();
        m_Job = 0;
        m_Country = 0;
        m_HurtType = 0;
        m_SpecialType6 = 0;
        m_SpecialTypeMore = 0;
        for (int i = 0; i < jobsBtn.Length; i++)
        {
            jobsBtn[i].isOn = false;
        }
        for (int i = 0; i < countrysBtn.Length; i++)
        {
            countrysBtn[i].isOn = false;
        }
        for (int i = 0; i < hurtTypeBtn.Length; i++)
        {
            hurtTypeBtn[i].isOn = false;
        }
        for (int i = 0; i < specialType6Btn.Length; i++)
        {
            specialType6Btn[i].isOn = false;
        }
        for (int i = 0; i < specialTypeMoreBtn.Length; i++)
        {
            specialTypeMoreBtn[i].isOn = false;
        }
        // 重新添加监听器
        AddAllListeners();
        selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
    }
}