TH1/My project/Assets/Scripts/UI/LibraryUI.cs
2025-07-10 17:54:39 +08:00

191 lines
6.2 KiB
C#

using UnityEngine;
using Logic;
using RuntimeData;
using Animancer;
using UnityEngine.UI;
using System.Collections.Generic;
public enum LibraryTabType
{
Force,
Giant,
Wonder,
Achievement
}
public class LibraryUI
{
private Main _main;
private MapData _mapData;
public GameObject ROLibraryUI;
public bool NeedShow = false;
private bool _isShowing = false;
private bool _isAnimating = false;
private float _fadeDuration = 0.2f;
public Button ForceButton;
public Button GiantButton;
public Button WonderButton;
public Button AchievementButton;
private class LibraryTab
{
public LibraryTabType Type;
public GameObject Panel;
public string FadeInClipPath;
public string FadeOutClipPath;
public LibraryTab(LibraryTabType type, GameObject panel, string fadeIn, string fadeOut)
{
Type = type;
Panel = panel;
FadeInClipPath = fadeIn;
FadeOutClipPath = fadeOut;
}
}
private Dictionary<LibraryTabType, LibraryTab> _tabs = new();
private LibraryTabType? _currentTab = null;
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;
UIManager.Instance.GameUI.MainUI.NeedShow = true;
});
ROLibraryUI.gameObject.SetActive(false);
Init();
}
public void Init()
{
ForceButton = ROLibraryUI.transform.Find("TopBar/Force").GetComponent<Button>();
GiantButton = ROLibraryUI.transform.Find("TopBar/Giant").GetComponent<Button>();
WonderButton = ROLibraryUI.transform.Find("TopBar/Wonder").GetComponent<Button>();
AchievementButton = ROLibraryUI.transform.Find("TopBar/Achievement").GetComponent<Button>();
_tabs[LibraryTabType.Force] = new LibraryTab(
LibraryTabType.Force,
ROLibraryUI.transform.Find("Force").gameObject,
"Animations/UI/LibraryForceUIFadeIn",
"Animations/UI/LibraryForceUIFadeOut");
_tabs[LibraryTabType.Giant] = new LibraryTab(
LibraryTabType.Giant,
ROLibraryUI.transform.Find("Giant").gameObject,
"Animations/UI/LibraryGiantUIFadeIn",
"Animations/UI/LibraryGiantUIFadeOut");
_tabs[LibraryTabType.Wonder] = new LibraryTab(
LibraryTabType.Wonder,
ROLibraryUI.transform.Find("Wonder").gameObject,
"Animations/UI/LibraryWonderUIFadeIn",
"Animations/UI/LibraryWonderUIFadeOut");
_tabs[LibraryTabType.Achievement] = new LibraryTab(
LibraryTabType.Achievement,
ROLibraryUI.transform.Find("Achievement").gameObject,
"Animations/UI/LibraryAchievementUIFadeIn",
"Animations/UI/LibraryAchievementUIFadeOut");
ForceButton.onClick.AddListener(() => SwitchTab(LibraryTabType.Force));
GiantButton.onClick.AddListener(() => SwitchTab(LibraryTabType.Giant));
WonderButton.onClick.AddListener(() => SwitchTab(LibraryTabType.Wonder));
AchievementButton.onClick.AddListener(() => SwitchTab(LibraryTabType.Achievement));
var fa = ROLibraryUI.transform.Find("Wonder").Find("WonderListPanel").Find("WonderList");
for (int i = 0; i < 4; i++)
for(int j = 0;j < 7; j++)
{
var p = fa.Find($"LibraryWonder{i}").Find($"LibraryWonderItem ({j})");
/*p.Find("TribeIconMask").Find("GroundIcon").GetComponent<Image>().sprite
= null;*/
p.Find("Size/TribeIconMask/TribeIcon").GetComponent<Image>().sprite
= Table.Instance.GridAndResourceDataAssets.WonderInfoList[i * 7 + j].Sprite;
}
}
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 = Resources.Load<AnimationClip>("Animations/UI/LibraryUIFadeIn");
if (fadeIn != null) animancer.Play(fadeIn);
Timer.Instance.TimerRegister(ROLibraryUI, () =>
{
_isAnimating = false;
// ✅ 显示主面板后,默认打开 Force 面板
SwitchTab(LibraryTabType.Force);
}, _fadeDuration);
}
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;
}, _fadeDuration);
}
private void SwitchTab(LibraryTabType target)
{
if (_currentTab == target) return;
if (_currentTab.HasValue)
{
var oldTab = _tabs[_currentTab.Value];
var animancerOut = oldTab.Panel.GetComponent<AnimancerComponent>();
var fadeOut = Resources.Load<AnimationClip>(oldTab.FadeOutClipPath);
if (fadeOut != null) animancerOut.Play(fadeOut);
Timer.Instance.TimerRegister(oldTab.Panel, () => oldTab.Panel.SetActive(false), _fadeDuration);
}
var newTab = _tabs[target];
newTab.Panel.SetActive(true);
var animancerIn = newTab.Panel.GetComponent<AnimancerComponent>();
var fadeIn = Resources.Load<AnimationClip>(newTab.FadeInClipPath);
if (fadeIn != null) animancerIn.Play(fadeIn);
_currentTab = target;
}
}