248 lines
8.4 KiB
C#
248 lines
8.4 KiB
C#
using System;
|
|
using Logic.Multilingual;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace TH1_UI.View.Outside
|
|
{
|
|
public class UIOutsideSelectAISettingRowMono : MonoBehaviour
|
|
{
|
|
public GameObject AvatarRoot;
|
|
public Image AvatarImage;
|
|
public TextMeshProUGUI ForcesText;
|
|
public Button ForceLeftButton;
|
|
public Button ForceRightButton;
|
|
public TextMeshProUGUI TeamText;
|
|
public Button TeamLeftButton;
|
|
public Button TeamRightButton;
|
|
|
|
private CivEnum _civ;
|
|
private ForceEnum _force;
|
|
private int _teamId;
|
|
private int _maxTeamId;
|
|
private string _forceNameOverride;
|
|
private Action<int> _onForceChanged;
|
|
private Action<int> _onTeamChanged;
|
|
private bool _showForceButtons;
|
|
private bool _showTeamButtons;
|
|
private Vector3 _avatarDefaultScale = Vector3.one;
|
|
private bool _avatarScaleCached;
|
|
|
|
public void SetContent(
|
|
CivEnum civ,
|
|
ForceEnum force,
|
|
int teamId,
|
|
int maxTeamId,
|
|
string forceNameOverride,
|
|
Action<int> onForceChanged,
|
|
Action<int> onTeamChanged,
|
|
bool showForceButtons = true,
|
|
bool showTeamButtons = true)
|
|
{
|
|
ResetButtons();
|
|
_civ = civ;
|
|
_force = force;
|
|
_teamId = Mathf.Max(1, teamId);
|
|
_maxTeamId = Mathf.Max(1, maxTeamId);
|
|
_forceNameOverride = forceNameOverride;
|
|
_onForceChanged = onForceChanged;
|
|
_onTeamChanged = onTeamChanged;
|
|
_showForceButtons = showForceButtons;
|
|
_showTeamButtons = showTeamButtons;
|
|
|
|
UpdatePlayerInfoView();
|
|
SetTeamText(_teamId);
|
|
BindButtons();
|
|
}
|
|
|
|
public void SetRandomSelfContent(int teamId, int maxTeamId)
|
|
{
|
|
ResetButtons();
|
|
_civ = CivEnum.Common;
|
|
_force = ForceEnum.Common;
|
|
_teamId = Mathf.Max(1, teamId);
|
|
_maxTeamId = Mathf.Max(1, maxTeamId);
|
|
_forceNameOverride = null;
|
|
_onForceChanged = null;
|
|
_onTeamChanged = null;
|
|
_showForceButtons = false;
|
|
_showTeamButtons = false;
|
|
|
|
SetRandomPlayerInfoView();
|
|
SetTeamText(_teamId);
|
|
BindButtons();
|
|
}
|
|
|
|
public void SetRandomContent(
|
|
int teamId,
|
|
int maxTeamId,
|
|
Action<int> onForceChanged,
|
|
Action<int> onTeamChanged)
|
|
{
|
|
ResetButtons();
|
|
_civ = CivEnum.Common;
|
|
_force = ForceEnum.Common;
|
|
_teamId = Mathf.Max(1, teamId);
|
|
_maxTeamId = Mathf.Max(1, maxTeamId);
|
|
_forceNameOverride = null;
|
|
_onForceChanged = onForceChanged;
|
|
_onTeamChanged = onTeamChanged;
|
|
_showForceButtons = true;
|
|
_showTeamButtons = true;
|
|
|
|
SetRandomPlayerInfoView();
|
|
SetTeamText(_teamId);
|
|
BindButtons();
|
|
}
|
|
|
|
private void ResetButtons()
|
|
{
|
|
ForceLeftButton?.onClick.RemoveAllListeners();
|
|
ForceRightButton?.onClick.RemoveAllListeners();
|
|
TeamLeftButton?.onClick.RemoveAllListeners();
|
|
TeamRightButton?.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
private void BindButtons()
|
|
{
|
|
if (ForceLeftButton != null) ForceLeftButton.gameObject.SetActive(_showForceButtons);
|
|
if (ForceRightButton != null) ForceRightButton.gameObject.SetActive(_showForceButtons);
|
|
if (TeamLeftButton != null) TeamLeftButton.gameObject.SetActive(_showTeamButtons);
|
|
if (TeamRightButton != null) TeamRightButton.gameObject.SetActive(_showTeamButtons);
|
|
|
|
if (_showForceButtons)
|
|
{
|
|
ForceLeftButton?.onClick.AddListener(() => ChangeForce(-1));
|
|
ForceRightButton?.onClick.AddListener(() => ChangeForce(1));
|
|
}
|
|
|
|
if (_showTeamButtons)
|
|
{
|
|
TeamLeftButton?.onClick.AddListener(() => ChangeTeam(-1));
|
|
TeamRightButton?.onClick.AddListener(() => ChangeTeam(1));
|
|
}
|
|
}
|
|
|
|
private void UpdatePlayerInfoView()
|
|
{
|
|
if (!Table.Instance.PlayerDataAssets.GetPlayerInfo(_civ, _force, out var playerInfo)) return;
|
|
|
|
if (ForcesText != null)
|
|
{
|
|
if (!string.IsNullOrEmpty(_forceNameOverride))
|
|
ForcesText.text = _forceNameOverride;
|
|
else
|
|
MultilingualManager.Instance.SetUIText(ForcesText, playerInfo.ForceName);
|
|
}
|
|
|
|
SetAvatarImage(ResolveLeaderIllustration(playerInfo), playerInfo.LeaderAvatar);
|
|
}
|
|
|
|
private Sprite ResolveLeaderIllustration(PlayerInfo playerInfo)
|
|
{
|
|
if (playerInfo?.LeaderIllustration != null)
|
|
return playerInfo.LeaderIllustration;
|
|
|
|
var path = GetLeaderIllustrationFallbackPath(_civ, _force);
|
|
return string.IsNullOrEmpty(path)
|
|
? null
|
|
: TH1Resource.ResourceLoader.Load<Sprite>(path);
|
|
}
|
|
|
|
private static string GetLeaderIllustrationFallbackPath(CivEnum civ, ForceEnum force)
|
|
{
|
|
if (civ == CivEnum.Sumerian && force == ForceEnum.Yuyuko)
|
|
return "ArtResources/TH1CharIllustrations/SaigyoujiForces/SumerianYuyuko";
|
|
if (civ == CivEnum.Malian && force == ForceEnum.Megumu)
|
|
return "ArtResources/TH1CharIllustrations/IizunamaruForces/MalianMegumu";
|
|
if (civ == CivEnum.Greek && force == ForceEnum.Cirno)
|
|
return "ArtResources/TH1CharIllustrations/CirnoForces/GreekCirno";
|
|
|
|
return null;
|
|
}
|
|
|
|
private void SetRandomPlayerInfoView()
|
|
{
|
|
if (ForcesText != null)
|
|
SetTextOrRaw(ForcesText, Table.Instance?.TextDataAssets?.OutsideSelectRandomEmpire);
|
|
|
|
SetAvatarImage(null, Table.Instance?.PlayerDataAssets?.CommonPlayerAvatar);
|
|
}
|
|
|
|
private static void SetTextOrRaw(TextMeshProUGUI text, string value)
|
|
{
|
|
if (text == null) return;
|
|
|
|
var multilingual = text.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);
|
|
return;
|
|
}
|
|
|
|
if (multilingual != null)
|
|
{
|
|
multilingual.ID = 0;
|
|
multilingual.Ban = true;
|
|
}
|
|
text.text = value;
|
|
}
|
|
|
|
private void SetAvatarImage(Sprite illustration, Sprite fallbackAvatar)
|
|
{
|
|
if (AvatarImage == null) return;
|
|
if (AvatarRoot != null) AvatarRoot.SetActive(true);
|
|
|
|
CacheAvatarScale();
|
|
bool useFallback = illustration == null && fallbackAvatar != null;
|
|
AvatarImage.sprite = illustration != null ? illustration : fallbackAvatar;
|
|
AvatarImage.rectTransform.localScale = useFallback ? _avatarDefaultScale * 0.5f : _avatarDefaultScale;
|
|
}
|
|
|
|
private void CacheAvatarScale()
|
|
{
|
|
if (_avatarScaleCached || AvatarImage == null) return;
|
|
_avatarDefaultScale = AvatarImage.rectTransform.localScale;
|
|
_avatarScaleCached = true;
|
|
}
|
|
|
|
private void SetTeamText(int teamId)
|
|
{
|
|
if (TeamText == null) return;
|
|
|
|
var multilingual = TeamText.GetComponent<MultilingualTextMono>();
|
|
if (multilingual != null)
|
|
{
|
|
multilingual.Ban = true;
|
|
multilingual.ID = 0;
|
|
}
|
|
|
|
var title = MultilingualManager.Instance.GetMultilingualText(Table.Instance.TextDataAssets.OutsideMultiplayTeamTitle);
|
|
TeamText.text = $"{title}{teamId}";
|
|
}
|
|
|
|
private void ChangeForce(int direction)
|
|
{
|
|
_onForceChanged?.Invoke(direction);
|
|
}
|
|
|
|
private void ChangeTeam(int direction)
|
|
{
|
|
_onTeamChanged?.Invoke(direction);
|
|
}
|
|
}
|
|
}
|