2025-05-23 01:16:50 +08:00

154 lines
4.6 KiB
C#

using UnityEngine;
using Logic;
using RuntimeData;
using Animancer;
using TMPro;
using UnityEngine.UI;
public class MainUI
{
private Main _main;
private MapData _mapData;
public GameObject ROMainUI;
public Button NewGameButton;
public Button ResumeGameButton;
public Button StoryModeButton;
public Button LibraryButton;
public Button SettingButton;
public Button HistoryButton;
public Button AboutButton;
public bool NeedShow = false;
private bool _isShowing = false;
private bool _isAnimating = false;
private float _fadeDuration = 0.2f;
public MainUI(Main main, MapData mapData)
{
_main = main;
_mapData = mapData;
ROMainUI = _main.UIManager.ROUIManager.transform.Find("MainUI").transform.Find("MainUI").gameObject;
ROMainUI.gameObject.SetActive(false);
// 绑定所有按钮
NewGameButton = ROMainUI.transform.Find("ButtonRow/NewGameButton").GetComponent<Button>();
ResumeGameButton = ROMainUI.transform.Find("ButtonRow/ResumeGameButton").GetComponent<Button>();
StoryModeButton = ROMainUI.transform.Find("ButtonRow/StoryModeButton").GetComponent<Button>();
LibraryButton = ROMainUI.transform.Find("ButtonRow/LibraryButton").GetComponent<Button>();
HistoryButton = ROMainUI.transform.Find("ButtonRow/HistoryButton").GetComponent<Button>();
SettingButton = ROMainUI.transform.Find("ButtonRow2/SettingButton").GetComponent<Button>();
AboutButton = ROMainUI.transform.Find("ButtonRow2/AboutButton").GetComponent<Button>();
NewGameButton.onClick.AddListener(OnNewGameClicked);
ResumeGameButton.onClick.AddListener(OnResumeGameClicked);
StoryModeButton.onClick.AddListener(OnStoryModeClicked);
LibraryButton.onClick.AddListener(OnLibraryClicked);
SettingButton.onClick.AddListener(OnSettingClicked);
HistoryButton.onClick.AddListener(OnHistoryClicked);
AboutButton.onClick.AddListener(OnAboutClicked);
//处理继续游戏的按钮
if (!_main.HasArchive())
{
ROMainUI.transform.Find("ButtonRow").Find("ResumeGameButton").Find("Text")
.GetComponent<TextMeshProUGUI>().text = "继续(无存档)";
}
else
ROMainUI.transform.Find("ButtonRow").Find("ResumeGameButton").Find("Text")
.GetComponent<TextMeshProUGUI>().text = "继续游戏";
}
public void Update()
{
if (_isAnimating) return;
if (NeedShow && !ROMainUI.activeSelf)
{
Show();
}
else if (!NeedShow && ROMainUI.activeSelf)
{
Hide();
}
}
public void Show()
{
if (_isShowing || _isAnimating) return;
_isShowing = true;
_isAnimating = true;
ROMainUI.SetActive(true);
AnimancerComponent animancer = ROMainUI.GetComponent<AnimancerComponent>();
AnimationClip fadeInClip = Resources.Load<AnimationClip>("Animations/UI/SettingPanelFadeIn");
if (fadeInClip != null)
animancer.Play(fadeInClip);
Timer.Instance.TimerRegister(ROMainUI, () => { _isAnimating = false; }, _fadeDuration);
}
public void Hide()
{
if (!_isShowing || _isAnimating) return;
_isShowing = false;
_isAnimating = true;
AnimancerComponent animancer = ROMainUI.GetComponent<AnimancerComponent>();
AnimationClip fadeOutClip = Resources.Load<AnimationClip>("Animations/UI/SettingPanelFadeOut");
if (fadeOutClip != null)
animancer.Play(fadeOutClip);
Timer.Instance.TimerRegister(ROMainUI, () =>
{
ROMainUI.SetActive(false);
_isAnimating = false;
}, _fadeDuration);
}
private void OnNewGameClicked()
{
Debug.Log("Show!!!");
NeedShow = false;
_main.UIManager.MainUIManager.ChooseUI.NeedShow = true;
}
private void OnResumeGameClicked()
{
//Debug.Log("Resume Game Clicked");
if(_main.HasArchive())
_main.ContinueGame();
NeedShow = false;
}
private void OnStoryModeClicked()
{
Debug.Log("Story Mode Clicked");
}
private void OnLibraryClicked()
{
NeedShow = false;
_main.UIManager.MainUIManager.LibraryUI.NeedShow = true;
}
private void OnSettingClicked()
{
_main.UIManager.SettingUI.NeedShow = true;
}
private void OnHistoryClicked()
{
_main.UIManager.MainUIManager.HistoryUI.NeedShow = true;
}
private void OnAboutClicked()
{
_main.UIManager.MainUIManager.AboutUI.NeedShow = true;
}
}