NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/UI_VehiclePartTreeControlle...

390 lines
12 KiB
C#

using Cysharp.Threading.Tasks;
using Framework;
using Gameplay;
using Gameplay.Common;
using Gameplay.Net;
using PhxhSDK;
using TMPro;
using UnityEngine;
using Button = UnityEngine.UI.Button;
using Image = UnityEngine.UI.Image;
using Toggle = UnityEngine.UI.Toggle;
public class UI_VehiclePartTreeController : UIWindow
{
//UI GameObj
///Auto Gen Start///
public Image Image_Bg;
public Image Image_TreeBg1;
//public Button Button_Return;
public Button Button_back;
public TMP_Text Text_Back;
public Button Button_Home;
public Image Image_TreeBg0;
public Toggle Toggle_Dev;
public Toggle Toggle_Reinforce;
public Toggle Toggle_BlueMap;
public Toggle Toggle_Repair;
public PhxhScrollView ScrollView_PartsTree;
public Button Button_Back;
///Auto Gen End///
private long curVehicleID = -1;
private GameObject uiPrefab;
private GameObject highLightNode;
public async static UniTask<UIWindow> Open(object data = null)
{
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_VehiclePartTree, data);
}
// deal with input data
public override void PreLoad(object data = null)
{
curVehicleID = (long)data;
var path = string.Format(UIManager.UI_PREFAB_PATH, "Interface_Vehicle/TechTree/TechTree_" + curVehicleID);
PostPreload<GameObject>(path).Forget();
}
// Use this for initialization
public override void OnInit()
{
//bind UI GameObj
UI_VehiclePartTreeBinder.GetComponents(this);
var currencyList = GetComponent<UI_CurrencyList>("UI_CurrencyList");
currencyList.SetData(GLConfig.Inst.data.DiamondItemId, 12);//GLConfig.Inst.data.GoldItemId
//Button_Back.gameObject.AddComponent<WarningCode>();
BindButton(Button_back, OnClickBack);
BindButton(Button_Home, OnClickHome);
BindButton(Button_Back, () =>
{
var parentID = VehicleCultivateManager.Instance.GetVehicleParent(curVehicleID).ID;
var cfg = TableManager.Instance.Tables.VehicleConfig.DataMap[(int)parentID];
if (cfg == null)
return;
if (cfg.HideInTree)
{
CommonUtils.HideVehicleTip();
return;
}
curVehicleID = parentID;
UpdateTreePrefab();
});
var path = string.Format(UIManager.UI_PREFAB_PATH, "Interface_Vehicle/TechTree/TechTree_" + curVehicleID);
try
{
uiPrefab = GameObject.Instantiate(GetPreLoadResult<GameObject>(path), ScrollView_PartsTree.content, false);
var uiPrefabSize = uiPrefab.GetComponent<RectTransform>().sizeDelta;
ScrollView_PartsTree.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, uiPrefabSize.y);
ScrollView_PartsTree.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, uiPrefabSize.x);
}
catch
{
DebugUtil.LogError("科技树预制体加载失败:" + curVehicleID + ",请检查是否已生成");
}
EventManager.Instance.Register(EventManager.EventName.VehecleUpgrade, UpdateTreeNode);
highLightNode = CommonUtils.GetGameObject(uiPrefab, "v_" + curVehicleID);
if (highLightNode != null)
{
var img = CommonUtils.GetComponent<Image>(highLightNode, "Image_CurrentVehicle/Image_SelectBorder");
if (img != null)
{
highLightNode = img.gameObject;
img.enabled = true;
}
}
UI_VehiclePartTreeDetailController.Open(curVehicleID, OnReleaseCallback: HideHighlight).Forget();
}
//invoke when open window
protected override void OnShowWindow(object data = null)
{
UpdateTreeNode();
var currencyList = GetComponent<UI_CurrencyList>("UI_CurrencyList");
currencyList.Refresh();
}
//invoke when reload window
protected override void OnReloadWindow(object data = null)
{
}
//invoke when close window
protected override void OnHideWindow()
{
}
//invoke when destroy
public override void OnRelease()
{
base.OnRelease();
CommonUtils.ScreenshotReset();
EventManager.Instance.Unregister(EventManager.EventName.VehecleUpgrade, UpdateTreeNode);
}
/// <summary>
/// 加载树形结构预制体
/// </summary>
async void UpdateTreePrefab()
{
var path = string.Format(UIManager.UI_PREFAB_PATH, "Interface_Vehicle/TechTree/TechTree_" + curVehicleID);
var go = await AssetManager.Instance.LoadAssetAsync<GameObject>(path);
if (go == null)
{
DebugUtil.LogError("go is null");
return;
}
if (uiPrefab != null)
{
Destroy(uiPrefab);
}
uiPrefab = GameObject.Instantiate(go, ScrollView_PartsTree.content, false);
var uiPrefabSize = uiPrefab.GetComponent<RectTransform>().sizeDelta;
ScrollView_PartsTree.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, uiPrefabSize.y);
ScrollView_PartsTree.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, uiPrefabSize.x);
UpdateTreeNode();
highLightNode = CommonUtils.GetGameObject(uiPrefab, "v_" + curVehicleID);
if (highLightNode != null)
{
var img = CommonUtils.GetComponent<Image>(highLightNode, "Image_CurrentVehicle/Image_SelectBorder");
if (img != null)
{
highLightNode = img.gameObject;
img.enabled = true;
}
}
UI_VehiclePartTreeDetailController.Open(curVehicleID, OnReleaseCallback: HideHighlight).Forget();
}
/// <summary>
/// 更新树形结构节点显示内容
/// </summary>
void UpdateTreeNode()
{
var partTree = VehicleCultivateManager.Instance.GetPartialTree(curVehicleID);
if (VehicleCultivateManager.Instance.GetVehicleParent(curVehicleID) == null)
{
Button_Back.gameObject.SetActive(false);
}
else
{
Button_Back.gameObject.SetActive(true);
}
if (partTree == null)
{
DebugUtil.LogError("partTree is null");
return;
}
GameObject rootVehicleNode = CommonUtils.GetGameObject(uiPrefab, "v_" + curVehicleID);
CommonUtils.GetComponent<TMP_Text>(rootVehicleNode, "Image_CurrentVehicle/Text_PartName").text
= VehicleDataHelper.GetVehicleLocalizeName((int)curVehicleID);
CommonUtils.GetComponent<TMP_Text>(rootVehicleNode, "Image_CurrentVehicle/Text_VehicleTypeName").text
= VehicleDataHelper.GetVehicleCategory((int)curVehicleID);
VehicleInfo data = Actor.Instance.Character.GetVehicleDevelopment(curVehicleID);
foreach (var node in partTree)
{
var id = node.ID;
var treeNode = CommonUtils.GetGameObject(uiPrefab, "v_" + id);
DebugUtil.Log("find node: " + id);
if (treeNode == null)
{
DebugUtil.LogError("treeNode is null");
continue;
}
var button = treeNode.GetComponent<Button>();
if (button == null)
{
DebugUtil.LogError("button is null");
}
BindButton(button, () => OnNodeClick(node));
bool unlock = false;
if (node is VehicleTreeNode vehicleNode)
{
unlock = Actor.Instance.Character.GetVehicleDevelopment(vehicleNode.ID) != null;
#region name
var Text_name = CommonUtils.GetGameObject(treeNode.transform.GetChild(0).gameObject, "Text_Name");//GetComponent<TMP_Text>(treeNode.transform.GetChild(0).gameObject, "Text_Name");
if (Text_name != null)
{
Text_name.GetComponent<TMP_Text>().text = VehicleDataHelper.GetVehicleLocalizeName((int)vehicleNode.ID);
}
#endregion
#region vehicle_level
var Text_level = CommonUtils.GetComponent<TMP_Text>(treeNode.transform.GetChild(0).gameObject, "Text_Level");
if (Text_level != null)
{
var vehicleLevel = 1;
if (TableManager.Instance.Tables.VehicleConfig.DataMap.ContainsKey((int)vehicleNode.ID))
vehicleLevel = TableManager.Instance.Tables.VehicleConfig.DataMap[(int)vehicleNode.ID].VehicleLevel;
Text_level.text = CommonUtils.GetRomanCode(vehicleLevel);
}
#endregion
}
else if (node is VehicleComponentTreeNode componentNode)
{
if (data != null)
unlock = data.UpgradedComponents.Contains((uint)componentNode.ID);
#region component_level
var Text_level = CommonUtils.GetComponent<TMP_Text>(treeNode.transform.GetChild(0).gameObject, "Text_Level");
if (Text_level != null)
{
int compLevel = (int)(componentNode.ID % 100);
Text_level.text = CommonUtils.GetRomanCode(compLevel);
}
#endregion
}
var tag = treeNode.transform.GetChild(0).Find("Image_Unlock");
var cost = treeNode.transform.GetChild(0).Find("Text_Cost");
var vehicleCostText = treeNode.transform.GetChild(0).Find("Text_CostValue");
var vehicleCostBG = treeNode.transform.GetChild(0).Find("Image_Bar");
if (tag == null)
{
DebugUtil.LogWarning("tag is null");
}
if (!unlock)
{
if (tag != null)
tag.gameObject.SetActive(false);
if (cost != null)
{
cost.gameObject.SetActive(true);
var itemCost = node.GetUpgradeCost();
cost.GetComponent<TMP_Text>().text = itemCost.Counts[0].ToString();
}
if (vehicleCostText != null)
{
vehicleCostText.gameObject.SetActive(true);
var itemCost = node.GetUpgradeCost();
vehicleCostText.GetComponent<TMP_Text>().text = itemCost.Counts[0].ToString();
}
if (vehicleCostBG != null)
{
vehicleCostBG.gameObject.SetActive(true);
}
}
else
{
if (tag != null)
tag.gameObject.SetActive(true);
if (cost != null)
cost.gameObject.SetActive(false);
if (vehicleCostText != null)
vehicleCostText.gameObject.SetActive(false);
if (vehicleCostBG != null)
vehicleCostBG.gameObject.SetActive(false);
}
}
}
/// <summary>
/// 树节点点击事件
/// </summary>
/// <param name="node"></param>
void OnNodeClick(TreeNodeBase node)
{
var id = node.ID;
DebugUtil.Log("click node: " + id);
HideHighlight();
if (node is VehicleTreeNode Vehicle)
{
var cfg = TableManager.Instance.Tables.VehicleConfig.GetOrDefault((int)Vehicle.ID);
if (cfg == null)
return;
if (cfg.HideInTree)
{
CommonUtils.HideVehicleTip();
return;
}
if (curVehicleID != Vehicle.ID)
{
//curVehicleID = Vehicle.ID;
//UpdateTreePrefab();
var vehicle = Vehicle.ID;
highLightNode = CommonUtils.GetGameObject(uiPrefab, "v_" + id);
if (highLightNode != null)
{
var img = CommonUtils.GetComponent<Image>(highLightNode, "Image_PartTreeVehicle/Image_SelectBorder");
if (img != null)
{
highLightNode = img.gameObject;
img.enabled = true;
}
}
UI_VehiclePartTreeDetailController.Open(id, EnterPartTree: () =>
{
curVehicleID = vehicle;
UpdateTreePrefab();
}, OnReleaseCallback: HideHighlight).Forget();
}
else
{
highLightNode = CommonUtils.GetGameObject(uiPrefab, "v_" + id);
if (highLightNode != null)
{
var img = CommonUtils.GetComponent<Image>(highLightNode, "Image_CurrentVehicle/Image_SelectBorder");
if (img != null)
{
highLightNode = img.gameObject;
img.enabled = true;
}
}
UI_VehiclePartTreeDetailController.Open(id, OnReleaseCallback: HideHighlight).Forget();
}
}
else
{
highLightNode = CommonUtils.GetGameObject(uiPrefab, "v_" + id);
if (highLightNode != null)
{
var img = CommonUtils.GetComponent<Image>(highLightNode, "Image_Part/Image_SelectBorder");
if(img != null)
{
highLightNode = img.gameObject;
img.enabled = true;
}
}
UI_VehiclePartTreeDetailController.Open(id, OnReleaseCallback: HideHighlight).Forget();
}
}
void OnClickBack()
{
CloseWindow();
}
void OnClickHome()
{
CommonUtils.HomeToMain();
}
void HideHighlight()
{
if (highLightNode != null)
CommonUtils.GetComponent<Image>(highLightNode).enabled = false;
}
}