TH1/Unity/Assets/Scripts/TH1_UI/Controller/Outside/UIOutsideWikiController.cs
2026-05-03 01:41:06 +08:00

155 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using TH1_Core.Events;
using TH1_Core.Managers;
using TH1_Logic.MatchConfig;
using TH1_UI.Controller.Base;
using TH1_UI.View.Outside;
using UnityEngine;
namespace TH1_UI.Controller.Outside
{
public class UIOutsideWikiController : ViewController<UIOutsideWikiView>
{
public UIOutsideWikiController() { }
private ShowUIOutsideWiki _evt;
private bool _hasBigSelect;
private bool _hasSmallSelect;
private bool _currentBigHasSmalls;
private WikiType _selectedBigType;
private WikiType _selectedSmallType;
private readonly List<WikiType> _requiredTypes = new List<WikiType>(2);
private readonly List<WikiItem> _wikiItemBuffer = new List<WikiItem>();
protected override void RegisterEventCallback()
{
base.RegisterEventCallback();
if (WindowScript != null)
{
WindowScript.OnBigSelectClick += _OnBigSelectClick;
WindowScript.OnSmallSelectClick += _OnSmallSelectClick;
WindowScript.OnWikiListItemClick += _OnWikiListItemClick;
WindowScript.OnBtnCloseClick += _OnBtnCloseClick;
}
}
protected override void UnregisterEventCallback()
{
if (WindowScript != null)
{
WindowScript.OnBigSelectClick -= _OnBigSelectClick;
WindowScript.OnSmallSelectClick -= _OnSmallSelectClick;
WindowScript.OnWikiListItemClick -= _OnWikiListItemClick;
WindowScript.OnBtnCloseClick -= _OnBtnCloseClick;
}
base.UnregisterEventCallback();
}
private void _OnBtnCloseClick()
{
Close();
}
protected override void OnOpen()
{
base.OnOpen();
// Wiki 是浮窗,需要置顶到 OutsideUI 层最上方,避免被其它 OutsideUI view 覆盖
if (WindowObj != null)
{
WindowObj.transform.SetAsLastSibling();
}
ResetSelection();
if (_openParameter is ShowUIOutsideWiki evt)
{
_evt = evt;
WindowScript?.SetContent(evt);
}
else
{
Debug.LogWarning("[UIOutsideWikiController] Opened without valid parameters.");
WindowScript?.SetContent(new ShowUIOutsideWiki());
}
// 默认选中第一个 BigSelect其点击回调会进一步触发 Small 默认选中和列表首项展示)
WindowScript?.SelectFirstBig();
}
private void _OnBigSelectClick(WikiType wikiType)
{
_selectedBigType = wikiType;
_hasBigSelect = true;
// 切换 Big 时重置 Small 选中(旧 Small 不再属于当前 Big group
_hasSmallSelect = false;
_currentBigHasSmalls = WindowScript != null && WindowScript.ShowSmallSelectButtonsForBig(wikiType);
// 当前 Big 下若有 Small 子分类,默认选中第一个(其点击回调会刷新列表并选首项)
if (_currentBigHasSmalls)
{
if (WindowScript != null && WindowScript.SelectFirstSmallIfAny()) return;
}
RefreshWikiList();
}
private void _OnSmallSelectClick(WikiType wikiType)
{
_selectedSmallType = wikiType;
_hasSmallSelect = true;
RefreshWikiList();
}
private void _OnWikiListItemClick(WikiItem wikiItem)
{
WindowScript?.SetTitle(wikiItem);
}
private void RefreshWikiList()
{
if (WindowScript == null) return;
// 必须先选 Big如果当前 Big 有 Small 则还需选中 Small否则清空列表。
if (!_hasBigSelect || (_currentBigHasSmalls && !_hasSmallSelect))
{
_wikiItemBuffer.Clear();
WindowScript.ClearWikiList();
WindowScript.ClearTitle();
return;
}
if (Table.Instance?.WikiData == null)
{
_wikiItemBuffer.Clear();
WindowScript.ClearWikiList();
WindowScript.ClearTitle();
return;
}
_requiredTypes.Clear();
_requiredTypes.Add(_selectedBigType);
if (_currentBigHasSmalls) _requiredTypes.Add(_selectedSmallType);
Table.Instance.WikiData.FindByTypes(_requiredTypes, _wikiItemBuffer);
WindowScript.RefreshWikiList(_wikiItemBuffer);
WindowScript.ClearTitle();
// 列表非空时,默认选中第一项并展示其 Content
WindowScript.SelectFirstWikiListItemIfAny();
}
private void ResetSelection()
{
_hasBigSelect = false;
_hasSmallSelect = false;
_currentBigHasSmalls = false;
_requiredTypes.Clear();
_wikiItemBuffer.Clear();
}
public override bool Close()
{
WindowScript?.OnCloseView();
ResetSelection();
return base.Close();
}
}
}