ShanxiKnowledgeBase/SXElectricalInspection/Assets/Adam/Scripts/ToolsItemManager.cs

325 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Components;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
//============================================================
//支持中文文件使用UTF-8编码
//@author YangHua
//@create 20230914
//@company QianHuo
//
//@description:工具包按钮管理
//============================================================
public class ToolsItemManager : MonoSingleton<ToolsItemManager>
{
public GameObject toolPanel;
public List<ToolItem> toolItems = new List<ToolItem>();
public ToolItem toolItemPrefab;
public Transform toolsContent;
public Button closeBtn;
public UnityEvent<GameObject> ItemClick;
public Button recoverBtn;
public UnityEvent recoverEvent = new UnityEvent();
public Toggle bottomToolToggle;
public GameObject currentTool;
public GameObject ServerRecorderPanel;
public ToolParentItem toolParentItemPrefab;
private void Start()
{
closeBtn.onClick.AddListener(() => { SwictchToolPanel(false); });
recoverBtn.onClick.AddListener(() =>
{
recoverEvent?.Invoke();
RecoverCurrentInstantiateTool(currentTool);
});
recoverBtn.gameObject.SetActive(false);
ServerRecorderPanel.SetActive(false);
}
public void SwictchToolPanel(bool isActive)
{
toolPanel.SetActive(isActive);
}
/// <summary>
/// 在工具间生成图标
/// </summary>
/// <param name="toolName"></param>
public void CreatToolItem(GameObject currentModel, Vector3 pos, Vector3 RotPos)
{
//bool isHave = false;
//if (!currentModel.name.Contains("钳形电流表"))
//{
// for (int i = 0; i < toolItems.Count; i++)
// {
// string nameTemp;
// if (currentModel.name.Contains("_"))
// {
// nameTemp = currentModel.name.Split("_")[0];
// }
// else
// {
// nameTemp = currentModel.name;
// }
// if (toolItems[i] != null && toolItems[i].toolName.Contains(nameTemp))
// {
// toolItems[i].SetCount();
// isHave = true;
// }
// }
//}
//if (isHave) return;
ToolItem tTemp = Instantiate(toolItemPrefab, toolsContent);
tTemp.SetValue(currentModel.name, currentModel.transform, pos, RotPos);
tTemp.SetState(true);
if (tTemp.toolName == "工作卡")
{
tTemp.selfButton.interactable = true;
tTemp.selfButton.onClick.AddListener(() =>
{
UIManager.Instance.jobCardController.jobCardPanel.SetActive(true);
SetToggleState();
UIManager.Instance.bottomCotroller.SwitchFirstPerson(false);
});
}
else if (tTemp.toolName == "服务器记录仪")
{
tTemp.selfButton.onClick.AddListener(() =>
{
StopCoroutine(WaitShowServerRecorderPanel());
StartCoroutine(WaitShowServerRecorderPanel());
SetToggleState();
});
}
else if (tTemp.toolName == "梯子" || tTemp.toolName == "梯子_钢")
{
tTemp.selfButton.onClick.AddListener(() =>
{
if (GameManager.Instance.ladderCtr != null)
{
GameManager.Instance.ladderCtr.SwitchLadder(tTemp.toolName);
UIManager.Instance.bottomCotroller.updownLadder.gameObject.SetActive(true);
}
});
}
else
{
tTemp.selfButton.onClick.AddListener(() =>
{
ItemClick?.Invoke(tTemp.prefab);
recoverBtn.gameObject.SetActive(true);
bottomToolToggle.interactable = false;
SetToggleState();
});
}
toolItems.Add(tTemp);
MergedParts(toolItems);
//for (int i = 0; i < toolItems.Count; i++)
//{
// int index = i;
// int count = 0;
// if (toolItems[index] != null && toolItems[index].toolName.Contains(item))
// {
// count++;
// ToolParentItem temp = Instantiate(toolParentItemPrefab, toolsContent);
// temp.SetIconAndCountText(toolItems[index].showIcon.sprite, count);
// toolItems[index].transform.SetParent(temp.content);
// toolItems[index].GetComponent<RectTransform>().sizeDelta = new Vector2(138, 138);
// }
//}
}
public Dictionary<string, List<ToolItem>> mergedItemsHave = new Dictionary<string, List<ToolItem>>();
public List<ToolParentItem> toolParentItems = new List<ToolParentItem>();
public Transform smallToolBarParent;
private void MergedParts(List<ToolItem> toollist)
{
Dictionary<string, List<ToolItem>> mergedItems = new Dictionary<string, List<ToolItem>>();
foreach (var item in toollist)
{
if (item != null)
{
string parts = item.toolName.Split('_')[0];
if (mergedItems.ContainsKey(parts))
{
mergedItems[parts].Add(item);
}
else
{
mergedItems.Add(parts, new List<ToolItem>() { item });
}
}
}
foreach (var item in mergedItems)
{
Debug.Log($"{item.Key}++{item.Value.Count}");
if (!mergedItemsHave.ContainsKey(item.Key))
{
if (item.Value.Count > 1)
{
ToolParentItem temp = Instantiate(toolParentItemPrefab, toolsContent);
temp.SetCount(item.Value[0].showIcon.sprite, item.Value.Count);
temp.wearAndUse = item.Value[0].wearAndUse;
temp.SetIconAndCountText(smallToolBarParent);
temp.gameObject.name = item.Key;
foreach (var itemT in item.Value)
{
itemT.transform.SetParent(temp.content);
itemT.GetComponent<RectTransform>().sizeDelta = new Vector2(138, 138);
itemT.toolParentItem = temp;
}
toolParentItems.Add(temp);
mergedItemsHave.Add(item.Key, item.Value);
}
}
else
{
Debug.Log("已经包含");
ToolParentItem temp = mergedItemsHave[item.Key][0].toolParentItem;
temp.wearAndUse = item.Value[0].wearAndUse;
temp.SetCount(item.Value[0].showIcon.sprite, item.Value.Count);
mergedItemsHave[item.Key].Clear();
if (item.Value.Count > 0)
{
foreach (var itemT in item.Value)
{
mergedItemsHave[item.Key].Add(itemT);
itemT.transform.SetParent(temp.content);
itemT.GetComponent<RectTransform>().sizeDelta = new Vector2(138, 138);
itemT.toolParentItem = temp;
}
}
}
}
}
/// <summary>
/// 按钮自身的关闭按钮点击的时候调用
/// </summary>
/// <param name="value"></param>
public void RemoveMergedItemsHave(ToolItem value)
{
string tempName = value.toolName.Split('_')[0];
ToolParentItem toolParentItemTemp = mergedItemsHave[tempName][0].toolParentItem;
for (int i = 0; i < mergedItemsHave.Count; i++)
{
if (mergedItemsHave[tempName].Contains(value))
{
mergedItemsHave[tempName].Remove(value);
}
}
if (mergedItemsHave[tempName].Count < 1)
{
for (int i = 0; i < toolParentItems.Count; i++)
{
if (toolParentItems[i] != null && toolParentItems[i].gameObject.name == tempName)
{
Destroy(toolParentItems[i].smallToolBar);
Destroy(toolParentItems[i].gameObject);
}
}
mergedItemsHave.Remove(tempName);
}
else
{
toolParentItemTemp.SetCount(mergedItemsHave[tempName][0].showIcon.sprite, mergedItemsHave[tempName].Count);
}
}
public void RemoveNull()
{
for (int i = 0; i < toolItems.Count; i++)
{
if (toolItems[i] == null)
toolItems.RemoveAt(i);
}
}
public void SetToggleState()
{
bottomToolToggle.isOn = false;
SwictchToolPanel(false);
}
/// <summary>
///
/// </summary>
/// <param name="tool"></param>
public void RecoverCurrentInstantiateTool(GameObject tool)
{
if (tool != null)
{
if (tool.name == "工作证(Clone)" && GameManager.Instance != null && GameManager.Instance.GonZuoZheng)
{
GameManager.Instance.npcwayPoints.StartMove();
}
Destroy(tool);
}
bottomToolToggle.interactable = true;
recoverBtn.gameObject.SetActive(false);
}
/// <summary>
/// 需要3D去调用
/// </summary>
public void SwitchAllToolItems()
{
for (int i = 0; i < toolItems.Count; i++)
{
if (toolItems[i] != null)
toolItems[i].SwitchSelfIsClick();
else
toolItems.RemoveAt(i);
}
SwitchToolsItemIsWear();
}
/// <summary>
/// 判断是否是穿戴工具
/// </summary>
public void SwitchToolsItemIsWear()
{
for (int i = 0; i < toolItems.Count; i++)
{
if (toolItems[i] != null && toolItems[i].wearAndUse == WearAndUse.wear)
toolItems[i].gameObject.SetActive(false);
}
for (int i = 0; i < toolParentItems.Count; i++)
{
if (toolParentItems[i] != null && toolParentItems[i].wearAndUse == WearAndUse.wear)
toolParentItems[i].gameObject.SetActive(false);
}
}
public void ResetState()
{
for (int i = 0; i < toolItems.Count; i++)
{
toolItems[i].SetState(true);
}
}
public IEnumerator WaitShowServerRecorderPanel()
{
ServerRecorderPanel.gameObject.SetActive(true);
yield return new WaitForSeconds(2f);
FractionManager.Instance.CheckCallCustomOpenServer(true);
ServerRecorderPanel.gameObject.SetActive(false);
}
}