TH1/Unity/Assets/Scripts/TH1_UI/View/Outside/UIOutsideLibraryHeroPanelMono.cs

391 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using Animancer;
using Logic.Multilingual;
using RuntimeData;
using TH1_UI.HintUI;
using TH1Resource;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace TH1_UI.View.Outside
{
public class UIOutsideLibraryHeroPanelMono : MonoBehaviour
{
public TextMeshProUGUI TopTitle;
public Image Illustration;
public TextMeshProUGUI Name;
public TextMeshProUGUI CivName;
public Image CivBG;
public RectTransform CivRect;
public TextMeshProUGUI Desc;
public TextMeshProUGUI Dialogue;
public Image DialogueAvatar;
public List<UIOutsideLibraryAchieveItemMono> AchieveItems;
public Button SettingTabButton;
public Button BattleTabButton;
public Image SettingTabBG;
public Image BattleTabBG;
public RectTransform SettingContentRoot;
public RectTransform BattleContentRoot;
public Button CloseButton;
public Button LeftButton;
public Button RightButton;
public AnimancerComponent Animancer;
private const string HeroHintPanelPath = "Prefab/UI/Hint/HeroHintPanel";
private static readonly Color SelectedTabColor = new Color(0.42f, 0.22f, 0.42f, 1f);
private static readonly Color NormalTabColor = new Color(1f, 1f, 1f, 0.88f);
private static readonly Color SelectedTabTextColor = Color.white;
private static readonly Color NormalTabTextColor = new Color(0.25f, 0.23f, 0.42f, 1f);
private GiantType _currentGiantType = GiantType.None;
private HeroHintPanel _battlePanel;
private RectTransform _infoRoot;
private enum HeroPanelTab
{
Setting,
Battle
}
public void Init(Action onStartClick)
{
ResolveTabReferences();
BindTabButtons();
}
public bool SetContent(GiantType giantType)
{
if (!ContentGate.CanShowHeroIntroduction(giantType)) return false;
if (!Table.Instance.LibraryDataAssets.GetLibraryInfoByGiant(giantType, out var info)) return false;
if (!Table.Instance.UnitTypeDataAssets.GetUnitTypeInfo(new UnitFullType(UnitType.Giant, giantType, 1),
out var unitInfo)) return false;
var empire = unitInfo.GiantEmpire;
if (!Table.Instance.PlayerDataAssets.GetPlayerInfo(empire, out var playerInfo)) return false;
_currentGiantType = giantType;
ResolveTabReferences();
BindTabButtons();
SetUITextOrRaw(TopTitle,info.Name);
Illustration.sprite = info.Illust;
SetUITextOrRaw(Name,info.Name);
SetUITextOrRaw(CivName,playerInfo.CivName);
CivBG.color = playerInfo.Color;
SetUITextOrRaw(Desc,info.Desc);
SetUITextOrRaw(Dialogue,info.Diag);
DialogueAvatar.sprite = unitInfo.Sprite;
//Step #3 设定成就
SetAchieveItems(giantType);
CloseButton.onClick.RemoveAllListeners();
CloseButton.onClick.AddListener(Hide);
SelectTab(HeroPanelTab.Setting);
return true;
}
private void BindTabButtons()
{
if (SettingTabButton != null)
{
SettingTabButton.onClick.RemoveAllListeners();
SettingTabButton.onClick.AddListener(() => SelectTab(HeroPanelTab.Setting));
}
if (BattleTabButton != null)
{
BattleTabButton.onClick.RemoveAllListeners();
BattleTabButton.onClick.AddListener(() => SelectTab(HeroPanelTab.Battle));
}
}
private void ResolveTabReferences()
{
var infoRoot = ResolveInfoRoot();
if (infoRoot == null) return;
_infoRoot = infoRoot;
var tabRoot = infoRoot.Find("TabRoot");
if (tabRoot != null)
{
SettingTabButton ??= tabRoot.Find("SettingTabButton")?.GetComponent<Button>();
BattleTabButton ??= tabRoot.Find("BattleTabButton")?.GetComponent<Button>();
}
SettingTabBG ??= SettingTabButton != null ? SettingTabButton.GetComponent<Image>() : null;
BattleTabBG ??= BattleTabButton != null ? BattleTabButton.GetComponent<Image>() : null;
SettingContentRoot ??= infoRoot.Find("SettingContentRoot") as RectTransform;
BattleContentRoot ??= infoRoot.Find("BattleContentRoot") as RectTransform;
}
private RectTransform ResolveInfoRoot()
{
var root = FindInfoRootFrom(Name != null ? Name.transform : null);
if (root != null) return root;
root = FindInfoRootFrom(SettingTabButton != null ? SettingTabButton.transform : null);
if (root != null) return root;
root = FindInfoRootFrom(BattleTabButton != null ? BattleTabButton.transform : null);
if (root != null) return root;
return null;
}
private static RectTransform FindInfoRootFrom(Transform child)
{
var current = child;
while (current != null)
{
if (current.Find("TabRoot") != null ||
current.Find("SettingContentRoot") != null ||
current.Find("BattleContentRoot") != null)
{
return current as RectTransform;
}
current = current.parent;
}
return null;
}
private void SelectTab(HeroPanelTab tab)
{
var isSettingTab = tab == HeroPanelTab.Setting;
if (SettingContentRoot != null) SettingContentRoot.gameObject.SetActive(isSettingTab);
else SetLegacySettingContentActive(isSettingTab);
if (BattleContentRoot != null) BattleContentRoot.gameObject.SetActive(tab == HeroPanelTab.Battle);
SetTabVisual(SettingTabBG, tab == HeroPanelTab.Setting);
SetTabVisual(BattleTabBG, tab == HeroPanelTab.Battle);
SetTabTextVisual(SettingTabButton, tab == HeroPanelTab.Setting);
SetTabTextVisual(BattleTabButton, tab == HeroPanelTab.Battle);
if (tab == HeroPanelTab.Battle) RefreshBattlePanel();
}
private void SetLegacySettingContentActive(bool active)
{
SetGameObjectActive(Name, active);
SetGameObjectActive(CivRect != null && CivRect.parent != null ? CivRect.parent.gameObject : null, active);
SetGameObjectActive(Desc, active);
SetNamedInfoChildActive("DescTitle", active);
SetNamedInfoChildActive("DialogGroup", active);
if (active) SetAchieveItems(_currentGiantType);
else SetAchievementSectionActive(false);
}
private void SetNamedInfoChildActive(string childName, bool active)
{
if (_infoRoot == null || string.IsNullOrEmpty(childName)) return;
var child = _infoRoot.Find(childName);
if (child != null) child.gameObject.SetActive(active);
}
private static void SetGameObjectActive(Component component, bool active)
{
if (component != null) component.gameObject.SetActive(active);
}
private static void SetGameObjectActive(GameObject gameObject, bool active)
{
if (gameObject != null) gameObject.SetActive(active);
}
private static void SetTabVisual(Image image, bool selected)
{
if (image == null) return;
image.color = selected ? SelectedTabColor : NormalTabColor;
}
private static void SetTabTextVisual(Button button, bool selected)
{
if (button == null) return;
var text = button.GetComponentInChildren<TextMeshProUGUI>(true);
if (text == null) return;
text.color = selected ? SelectedTabTextColor : NormalTabTextColor;
}
private void RefreshBattlePanel()
{
if (_currentGiantType == GiantType.None || BattleContentRoot == null) return;
if (_battlePanel == null)
{
var prefab = ResourceLoader.Load<GameObject>(HeroHintPanelPath);
if (prefab == null) return;
var go = Instantiate(prefab, BattleContentRoot, false);
go.name = "BattleHeroHintPanel";
_battlePanel = go.GetComponent<HeroHintPanel>();
if (_battlePanel == null) return;
_battlePanel.SetEmbeddedMode(true, true);
var rect = go.GetComponent<RectTransform>();
if (rect != null)
{
rect.anchorMin = new Vector2(0f, 1f);
rect.anchorMax = new Vector2(1f, 1f);
rect.pivot = new Vector2(0.5f, 1f);
rect.anchoredPosition = Vector2.zero;
rect.sizeDelta = new Vector2(0f, 620f);
rect.localScale = Vector3.one;
}
}
_battlePanel.gameObject.SetActive(true);
_battlePanel.SetEmbeddedMode(true, true);
_battlePanel.SetHeroInfo(_currentGiantType);
}
public void SetNavigationButtons(bool canMoveLeft, bool canMoveRight, Action onLeftClick, Action onRightClick)
{
EnsureNavigationButtons();
SetNavigationButton(LeftButton, canMoveLeft, onLeftClick);
SetNavigationButton(RightButton, canMoveRight, onRightClick);
}
private void EnsureNavigationButtons()
{
if (LeftButton == null)
{
LeftButton = FindNavigationButton("LeftButton");
}
if (RightButton == null)
{
RightButton = FindNavigationButton("RightButton");
}
}
private Button FindNavigationButton(string buttonName)
{
var buttons = GetComponentsInChildren<Button>(true);
for (int i = 0; i < buttons.Length; i++)
{
if (buttons[i] != null && buttons[i].name == buttonName) return buttons[i];
}
return null;
}
private static void SetNavigationButton(Button button, bool active, Action onClick)
{
if (button == null) return;
button.gameObject.SetActive(active);
button.onClick.RemoveAllListeners();
if (active && onClick != null) button.onClick.AddListener(() => onClick.Invoke());
}
private static void SetUITextOrRaw(TextMeshProUGUI text, string value)
{
if (text == null) return;
var multilingual = text.gameObject.GetComponent<MultilingualTextMono>();
if (string.IsNullOrEmpty(value))
{
if (multilingual != null)
{
multilingual.ID = 0;
multilingual.Ban = true;
}
text.text = string.Empty;
return;
}
if (uint.TryParse(value, out _))
{
if (multilingual != null) multilingual.Ban = false;
MultilingualManager.Instance.SetUIText(text, value);
}
else
{
if (multilingual != null)
{
multilingual.ID = 0;
multilingual.Ban = true;
}
text.text = value;
}
}
private void SetAchieveItems(GiantType giantType)
{
if (AchieveItems.Count != 3) return;
if (!UIOutsideLibraryAchievementVisibility.ShouldShowHeroAchievements(giantType))
{
SetAchievementSectionActive(false);
return;
}
if (!AchievementDataManager.Instance.GetSmallIdByGiantType(giantType, out var smallid))
{
SetAchievementSectionActive(false);
return;
}
SetAchievementSectionActive(true);
for (int i = 0; i < 3; i++)
{
AchieveItems[i].gameObject.SetActive(true);
AchieveItems[i].SetContent(2,smallid,(uint)i + 1);
}
}
private void SetAchievementSectionActive(bool active)
{
Transform group = null;
if (AchieveItems.Count > 0 && AchieveItems[0] != null)
group = AchieveItems[0].transform.parent;
if (group != null)
{
group.gameObject.SetActive(active);
var title = group.parent != null ? group.parent.Find("AchieveTitle") : null;
if (title != null) title.gameObject.SetActive(active);
}
for (int i = 0; i < AchieveItems.Count; i++)
{
if (AchieveItems[i] != null) AchieveItems[i].gameObject.SetActive(active);
}
}
public void Show()
{
gameObject.SetActive(true);
LayoutRebuilder.ForceRebuildLayoutImmediate(CivRect);
Animancer.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeIn);
}
public void Hide()
{
var state = Animancer.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut);
state.Events.OnEnd = () => { gameObject.SetActive(false); };
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
}