2026-01-05 18:32:52 +08:00

270 lines
9.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 Animancer;
using Logic;
using Logic.Multilingual;
using RuntimeData;
using TH1_Core.Events;
using TH1_Core.Managers;
using TH1_Logic.Core;
using TH1Resource;
using TMPro;
using TMPro.Examples;
using UnityEngine;
using UnityEngine.UI;
namespace UI.LibraryUI
{
public enum LibrarySubUIType
{
None,
Force,
Giant,
Wonder,
Achievement
}
public abstract class LibrarySubUIBase
{
//成就大类
public LibrarySubUIType Type;
//成就大类的id
public int AchievementBigId;
//对应整个panel,listpanel和infopanel
public GameObject Panel;
public GameObject ListPanel;
public GameObject InfoPanel;
public GameObject ListTable;
public GameObject ListDefault;
public LibraryUI LibraryUI;
//进入该panel的总开关
public Button EnterButton;
//记录当前是否正在展示
public bool IsSelectPanel;
//默认list选择的目标
public Transform SelectedListItem;
public Transform DefaultListItem;
protected LibrarySubUIBase(LibrarySubUIType type, Button enterButton,GameObject panel,LibraryUI libraryUI)
{
LibraryUI = libraryUI;
EnterButton = enterButton;
Type = type;
Panel = panel;
ListPanel = panel.transform.Find("ListPanel").gameObject;
InfoPanel = panel.transform.Find("InfoPanel").gameObject;
ListTable = ListPanel.transform.Find("Scroll View/Viewport/Content").gameObject;
EnterButton.onClick.AddListener(() => { LibraryUI.SwitchToPanel(Type); });
}
//对外只有两个函数Show和Hide
public abstract void Show();
public abstract void Hide();
//更新listpanel的信息
protected abstract void UpdateListPanel();
//更新infopanel的信息
protected abstract void UpdateInfoPanel();
//switch到这个panel之前要做的初始化函数
protected abstract void SetDefaultItem();
protected virtual void DeselectButton()
{
//设置文字alpha=64
var text = EnterButton.gameObject.transform.Find("Text")?.GetComponent<TextMeshProUGUI>();
if (text != null)
{
Color color = text.color;
color.a = 64 / 255f;
text.color = color;
}
//设置button颜色
var button = EnterButton.GetComponent<Image>();
if (button != null)
button.color = new Color(136/255f, 136/255f, 136/255f, 93 / 255f);
}
protected virtual void SelectButton()
{
//设置文字alpha=255
var text = EnterButton.gameObject.transform.Find("Text")?.GetComponent<TextMeshProUGUI>();
if (text != null)
{
Color color = text.color;
color.a = 1f;
text.color = color;
}
//设置button颜色
var button = EnterButton.GetComponent<Image>();
if (button != null)
button.color = new Color(1f, 1f, 1f, 1f);
}
protected virtual void ClosePanel()
{
//topbar 的button要改为熄灭
DeselectButton();
//panel要隐藏
var anim = Panel.GetComponent<AnimancerComponent>();
if (anim != null)
anim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut);
Timer.Instance.TimerRegister(this,()=>{Panel.SetActive(false);},ResourceCache.Instance.AnimCache.UICommonPanelFadeOut.length,"LibraryUI_ClosePanel");
IsSelectPanel = false;
}
protected virtual void OpenPanel()
{
//topbar的button要改为选中
SelectButton();
Panel.SetActive(true);
//panel要显示
var anim = Panel.GetComponent<AnimancerComponent>();
if (anim != null)
anim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeIn);
IsSelectPanel = true;
}
}
public class LibraryUI
{
private Main _main;
private MapData _mapData;
public GameObject ROLibraryUI;
public bool NeedShow = false;
private bool _isShowing = false;
private bool _isAnimating = false;
//记录当前的panel之间是否正在互相切换
private bool _isSwitching = false;
public Dictionary<LibrarySubUIType, LibrarySubUIBase> SubUIDict = new();
public LibrarySubUIType SelectedSubUIType = LibrarySubUIType.None;
public LibraryUI(Main main, MapData mapData)
{
_main = main;
_mapData = mapData;
ROLibraryUI = UIManager.Instance.ROUIManager.transform.Find("GameUI/LibraryUI").gameObject;
ROLibraryUI.transform.Find("CloseButton").GetComponent<Button>().onClick.AddListener(
() => {
NeedShow = false;
EventManager.Publish(new ShowUIOutsideMenu());
//UIManager.Instance.GameUI.MainUI.NeedShow = true;
});
ROLibraryUI.gameObject.SetActive(false);
Init();
}
public void Init()
{
//新建四个tabs对象
SubUIDict[LibrarySubUIType.Giant] = new LibraryGiantUI(
LibrarySubUIType.Giant,ROLibraryUI.transform.Find("TopBar/Giant").GetComponent<Button>(),
ROLibraryUI.transform.Find("Giant").gameObject,this);
SubUIDict[LibrarySubUIType.Wonder] = new LibraryWonderUI(
LibrarySubUIType.Wonder,ROLibraryUI.transform.Find("TopBar/Wonder").GetComponent<Button>(),
ROLibraryUI.transform.Find("Wonder").gameObject,this);
//全部SetActiveFalse
ROLibraryUI.transform.Find("Giant").gameObject.SetActive(false);
ROLibraryUI.transform.Find("Wonder").gameObject.SetActive(false);
}
public void SwitchToPanel(LibrarySubUIType type)
{
//如果正在切换中return
if (_isSwitching) return;
//如果点击的就是当前展示的return
if (SelectedSubUIType == type) return;
_isSwitching = true;
//如果不是首次打开library
if (SelectedSubUIType != LibrarySubUIType.None)
{
//先关闭目前打开的panel
SubUIDict[SelectedSubUIType].Hide();
//再打开当前选中的panel
Timer.Instance.TimerRegister(this, () =>
{
SubUIDict[type].Show();
SelectedSubUIType = type;
_isSwitching = false;
},ResourceCache.Instance.AnimCache.UICommonPanelFadeIn.length,"LibraryUI_SwitchPanel");
}
//如果是首次打开library
else
{
SubUIDict[type].Show();
SelectedSubUIType = type;
_isSwitching = false;
}
}
public void Update()
{
if (_isAnimating) return;
if (NeedShow && !ROLibraryUI.activeSelf)
{
Show();
}
else if (!NeedShow && ROLibraryUI.activeSelf)
{
Hide();
}
}
public void Show()
{
if (_isShowing || _isAnimating) return;
_isShowing = true;
_isAnimating = true;
ROLibraryUI.SetActive(true);
var animancer = ROLibraryUI.GetComponent<AnimancerComponent>();
var fadeIn = ResourceCache.Instance.AnimCache.UICommonPanelFadeIn;
//初始默认打开giant界面
SwitchToPanel(LibrarySubUIType.Giant);
animancer.Play(fadeIn);
Timer.Instance.TimerRegister(ROLibraryUI, () =>
{
_isAnimating = false;
}, fadeIn.length,"LibraryUI_Show");
}
public void Hide()
{
if (!_isShowing || _isAnimating) return;
_isShowing = false;
_isAnimating = true;
var animancer = ROLibraryUI.GetComponent<AnimancerComponent>();
var fadeOut = Resources.Load<AnimationClip>("Animations/UI/LibraryUIFadeOut");
if (fadeOut != null) animancer.Play(fadeOut);
Timer.Instance.TimerRegister(ROLibraryUI, () =>
{
ROLibraryUI.SetActive(false);
_isAnimating = false;
}, fadeOut.length,"LibraryUI_Hide");
}
}
}