上海杨浦大连路地铁站单机版电子沙盘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

552 lines
18 KiB

using AX.MessageSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
using UnityEngine.UI;
public class SecondNodeManager : MonoBehaviour
{
public Button addButton;
public Button deleteButton;
public Button editorButton;
public Button saveButton;
public Button addTagButton;
public Toggle recordStop;
public Transform itemParent;
public GameObject nodeWindow;
public GameObject tagWindow;
public ReplayManager replayManager;
private GameObject itemPrefab;
private List<SecondNodeObject> list;
public static SecondNodeObject selectNode;
public GameObject tagListPanel;
private void Awake()
{
MessageDispatcher.AddListener("FirstNodeSelected", DrawItem);
MessageDispatcher.AddListener("DataInitializationComplete", SetItemUI);
}
// Use this for initialization
void Start()
{
addButton.onClick.AddListener(AddNodeWindow);
deleteButton.onClick.AddListener(DeleteNodeWindow);
editorButton.onClick.AddListener(EditorNodeWindow);
saveButton.onClick.AddListener(SaveNodeWindow);
addTagButton.onClick.AddListener(AddTagWindow);
recordStop.onValueChanged.AddListener(ChangeRecordStatus);
//list = DisasterManager.secondNodeObjects;
itemPrefab = Resources.Load("UI/SecondNodeItem") as GameObject;
MessageDispatcher.AddListener("DeleteFirstNode", DestoryItem);
MessageDispatcher.AddListener("PlayStatusChanged", SetUIByPlayStatus);
SetUI();
}
// Update is called once per frame
void Update()
{
}
private void OnDestroy()
{
MessageDispatcher.RemoveListener("FirstNodeSelected", DrawItem);
MessageDispatcher.RemoveListener("DeleteFirstNode", DestoryItem);
MessageDispatcher.RemoveListener("PlayStatusChanged", SetUIByPlayStatus);
MessageDispatcher.RemoveListener("DataInitializationComplete", SetItemUI);
}
private void SetItemUI(IMessage obj)
{
selectNode = null;
foreach (Transform item in itemParent)
{
Destroy(item.gameObject);
}
}
/// <summary>
/// 一级节点更改后绘制二级节点
/// </summary>
/// <param name="data"></param>
private void DrawItem(IMessage data)
{
selectNode = null;
foreach (Transform item in itemParent)
{
Destroy(item.gameObject);
}
var firstNode = FirstNodeManager.selectNode;
if (itemPrefab == null)
{
itemPrefab = Resources.Load("UI/SecondNodeItem") as GameObject;
}
list = DisasterManager.secondNodeObjects;
foreach (var obj in list)
{
if (obj.ParentID == firstNode.nodeID)
{
AddNodeItem(obj);
}
}
}
/// <summary>
/// 一级节点删除后删除二级节点
/// </summary>
/// <param name="data"></param>
private void DestoryItem(IMessage data)
{
selectNode = null;
foreach (Transform item in itemParent)
{
var obj = item.GetComponent<SecondNodeItem>().obj;
DeleteXml(obj);//xml文件中删除
list.Remove(obj);
Destroy(item.gameObject);
}
}
/// <summary>
/// 添加节点
/// </summary>
private void AddNodeWindow()
{
if (FirstNodeManager.selectNode == null)
{
ResourceLoadWindow.Instance.LoadTextHintWindow("请先选择一级节点", 0.5f);
return;
}
ReplaySetting.PlayStatus = PlayStatus.isEditor;//编辑状态
RecordManager.Instance.RecordTimer = 0;
RecordManager.Instance.jsonData = new JsonData();
recordStop.isOn = false;
}
/// <summary>
/// 保存节点
/// </summary>
private void SaveNodeWindow()
{
recordStop.isOn = true;//暂停记录
nodeWindow.SetActive(true);
var node = new SecondNodeObject();
node.nodeID = -1;
node.ParentID = FirstNodeManager.selectNode.nodeID;
nodeWindow.GetComponent<SecondNodeWindow>().SetWindow(node);
}
/// <summary>
/// 继续记录
/// </summary>
public void ContinueRecord()
{
recordStop.isOn = false;
}
/// <summary>
/// 放弃本次记录
/// </summary>
public void DeleteRecord()
{
ReplaySetting.PlayStatus = PlayStatus.normal;
}
/// <summary>
/// 添加标签
/// </summary>
private void AddTagWindow()
{
if (ReplaySetting.PlayStatus == PlayStatus.isEditor)
{
//在编辑状态下
recordStop.isOn = true;//暂停记录
}
else if (ReplaySetting.PlayStatus == PlayStatus.isReplay)
{
//在回放状态下
replayManager.playOrPause.isOn = false;//暂停回放
}
tagWindow.SetActive(true);
var tag = new TagData();
tagWindow.GetComponent<TagEditorWindow>().SetWindow(tag);
}
/// <summary>
/// 暂停,继续纪录改变状态
/// </summary>
/// <param name="isOn"></param>
private void ChangeRecordStatus(bool isOn)
{
if (isOn)
{
RecordManager.Instance.recordStatus = RecordStatus.pause;
addTagButton.gameObject.SetActive(false);//暂停时,禁止添加标签
MessageDispatcher.SendMessage("RecordStatusPause");
}
else
{
RecordManager.Instance.RecordFrame();//记录开始或继续时记录状态数据
RecordManager.Instance.recordStatus = RecordStatus.normal;
addTagButton.gameObject.SetActive(true);
}
}
private void DeleteNodeWindow()
{
if (selectNode == null)
{
return;
}
ResourceLoadWindow.Instance.LoadTipWindow("确定删除该二级节点?", DeleteNode, null);
}
private void EditorNodeWindow()
{
if (selectNode == null)
{
return;
}
nodeWindow.SetActive(true);
nodeWindow.GetComponent<SecondNodeWindow>().SetWindow(selectNode);
}
/// <summary>
/// 新增二级节点
/// </summary>
/// <param name="name"></param>
public void AddNode(SecondNodeObject node)
{
if (node.nodeName == "")
{
ResourceLoadWindow.Instance.LoadTextHintWindow("节点名不能为空", 0.5f);
return;
}
//查看有没有相同名称
if (CheckSameName(node.nodeName, node.nodeID))
{
ResourceLoadWindow.Instance.LoadTextHintWindow("已有相同名称", 0.5f);
return;
}
node.nodeID = GetNextID();
list.Add(node);
AddNodeItem(node);//UI中添加
AddXml(node);//xml文件中添加
RecordManager.Instance.RecordFrame();//添加节点最后帧数据
RecordManager.Instance.jsonData.sumTime = RecordManager.Instance.RecordTimer;//总时间
RecordManager.Instance.SaveRecord(node.nodeName);
nodeWindow.SetActive(false);
ReplaySetting.PlayStatus = PlayStatus.normal;
}
/// <summary>
/// 修改二级节点xml顺序
/// </summary>
public void UpdateSecondNodeSort(Transform NodeParent)
{
SecondNodeItem[] thisNodes = NodeParent.GetComponentsInChildren<SecondNodeItem>();
if (thisNodes.Length<1)
{
return;
}
for (int i = 0; i < thisNodes.Length; i++)
{
UpdataNodeXml(thisNodes[i].obj);
}
for (int i = 0; i < thisNodes.Length; i++)
{
for (int j = 0; j < list.Count; j++)
{
if (list[j].nodeID == thisNodes[i].obj.nodeID)
{
list.Remove(list[j]);
}
}
}
for (int i = 0; i < thisNodes.Length; i++)
{
AddXml(thisNodes[i].obj);
DisasterManager.secondNodeObjects.Add(thisNodes[i].obj);
}
}
private void AddNodeItem(SecondNodeObject obj)
{
GameObject item = Instantiate(itemPrefab, itemParent) as GameObject;
item.GetComponent<Toggle>().group = itemParent.GetComponent<ToggleGroup>();
item.GetComponent<SecondNodeItem>().SetItem(obj);
}
private void AddXml(SecondNodeObject obj)
{
string filepath = DisasterManager.GetNodeListXML();
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode nodes = xmlDoc.SelectSingleNode("NodeList/SecondNode/Nodes");
XmlElement node = xmlDoc.CreateElement("Node");
node.SetAttribute("ID", obj.nodeID.ToString());
node.SetAttribute("ParentID", obj.ParentID.ToString());
node.SetAttribute("Name", obj.nodeName);
node.SetAttribute("Detail", obj.nodeDetail);
nodes.AppendChild(node);
xmlDoc.Save(filepath);
}
}
/// <summary>
/// 删除二级节点
/// </summary>
private void DeleteNode()
{
foreach (Transform item in itemParent)
{
var msg = item.GetComponent<SecondNodeItem>();
if (msg.obj == selectNode)
{
Destroy(item.gameObject);
break;
}
}
DeleteXml(selectNode);//xml文件中删除
list.Remove(selectNode);
}
/// <summary>
/// 修改二级节点xml文件
/// </summary>
private void UpdataNodeXml(SecondNodeObject obj)
{
string filepath = DisasterManager.GetNodeListXML();
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode nodes = xmlDoc.SelectSingleNode("NodeList/SecondNode/Nodes");
XmlNodeList nodeList = nodes.ChildNodes;
for (int i = 0; i < nodeList.Count; i++)
{
var node = nodeList[i] as XmlElement;
if (node.GetAttribute("ID") == obj.nodeID.ToString())
{
nodes.RemoveChild(node);
break;
}
}
xmlDoc.Save(filepath);
}
}
private void DeleteXml(SecondNodeObject obj)
{
//修改节点文件
string filepath = DisasterManager.GetNodeListXML();
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode nodes = xmlDoc.SelectSingleNode("NodeList/SecondNode/Nodes");
XmlNodeList nodeList = nodes.ChildNodes;
for (int i = 0; i < nodeList.Count; i++)
{
var node = nodeList[i] as XmlElement;
if (node.GetAttribute("ID") == obj.nodeID.ToString())
{
nodes.RemoveChild(node);
break;
}
}
xmlDoc.Save(filepath);
}
//删除数据文件
string dataFilePath = Application.dataPath + @"/Data/" + DisasterManager.disaster.Id + "/File/XmlData/" + obj.nodeName + ".xml";
if (File.Exists(dataFilePath))
{
File.Delete(dataFilePath);
}
}
/// <summary>
/// 编辑二级节点
/// </summary>
/// <param name="name"></param>
public void EditorNode(SecondNodeObject node)
{
if (node.nodeName == "")
{
ResourceLoadWindow.Instance.LoadTextHintWindow("节点名不能为空", 0.5f);
return;
}
//查看有没有相同名称
if (CheckSameName(node.nodeName, node.nodeID))
{
ResourceLoadWindow.Instance.LoadTextHintWindow("已有相同名称", 0.5f);
return;
}
EditorNodeItem(node);//UI中修改
EditorXML(node);//xml文件中修改
nodeWindow.SetActive(false);
}
private void EditorNodeItem(SecondNodeObject obj)
{
foreach (Transform item in itemParent)
{
var msg = item.GetComponent<SecondNodeItem>();
if (msg.obj.nodeID == obj.nodeID)
{
msg.SetUI();
break;
}
}
}
private void EditorXML(SecondNodeObject obj)
{
string filepath = DisasterManager.GetNodeListXML();
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode nodes = xmlDoc.SelectSingleNode("NodeList/SecondNode/Nodes");
XmlNodeList nodeList = nodes.ChildNodes;
for (int i = 0; i < nodeList.Count; i++)
{
var node = nodeList[i] as XmlElement;
if (node.GetAttribute("ID") == obj.nodeID.ToString())
{
node.SetAttribute("Name", obj.nodeName.ToString());
node.SetAttribute("Detail", obj.nodeDetail.ToString());
break;
}
}
xmlDoc.Save(filepath);
}
//修改数据文件名称
var oldNodeName = nodeWindow.GetComponent<SecondNodeWindow>().oldNodeName;
if (oldNodeName != obj.nodeName)
{
string oldPath = Application.dataPath + @"/Data/" + DisasterManager.disaster.Id + "/File/XmlData/" + oldNodeName + ".xml";
string dataFilePath = Application.dataPath + @"/Data/" + DisasterManager.disaster.Id + "/File/XmlData/" + obj.nodeName + ".xml";
if (File.Exists(oldPath))
{
File.Move(oldPath,dataFilePath);
}
}
}
public void ShowTagList(GameObject node)
{
var nodeObject = node.GetComponent<SecondNodeItem>().obj;
string nodeName = nodeObject.nodeName;
//这里有问题,由于标签存储在每个节点的数据文件里,所以每次获取标签需要解读文件,这是数据存储设计导致的问题
string filePath = Application.dataPath + @"/Data/" + DisasterManager.disaster.Id + "/File/XmlData/" + nodeName + ".xml";
if (File.Exists(filePath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode firstNodes = xmlDoc.SelectSingleNode("Record");
var jsonData = JsonUtility.FromJson<JsonData>(firstNodes.InnerText);
tagListPanel.SetActive(true);
tagListPanel.transform.position = node.transform.position + new Vector3(0, -0.2f, 0);
tagListPanel.GetComponent<TagListPanel>().Draw(jsonData.tagDataList);
}
}
public void HideTagList()
{
tagListPanel.SetActive(false);
}
private void SetUI()
{
if (DisasterManager.editorRight)
{
addButton.gameObject.SetActive(true);
deleteButton.gameObject.SetActive(true);
editorButton.gameObject.SetActive(true);
saveButton.gameObject.SetActive(false);
addTagButton.gameObject.SetActive(false);
recordStop.gameObject.SetActive(false);
}
else
{
addButton.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
editorButton.gameObject.SetActive(false);
saveButton.gameObject.SetActive(false);
addTagButton.gameObject.SetActive(false);
recordStop.gameObject.SetActive(false);
}
}
/// <summary>
/// 根据游戏状态显示UI
/// </summary>
/// <param name="data"></param>
private void SetUIByPlayStatus(IMessage data)
{
if (DisasterManager.editorRight)
{
var playStatus = ReplaySetting.PlayStatus;
if (playStatus == PlayStatus.normal)
{
addButton.gameObject.SetActive(true);
deleteButton.gameObject.SetActive(true);
editorButton.gameObject.SetActive(true);
saveButton.gameObject.SetActive(false);
addTagButton.gameObject.SetActive(false);
recordStop.gameObject.SetActive(false);
}
else if (playStatus == PlayStatus.isEditor)
{
addButton.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
editorButton.gameObject.SetActive(false);
saveButton.gameObject.SetActive(true);
addTagButton.gameObject.SetActive(true);
recordStop.gameObject.SetActive(true);
}
else if (playStatus == PlayStatus.isReplay)
{
addButton.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
editorButton.gameObject.SetActive(false);
saveButton.gameObject.SetActive(false);
addTagButton.gameObject.SetActive(true);
recordStop.gameObject.SetActive(false);
}
}
else
{
addButton.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
editorButton.gameObject.SetActive(false);
saveButton.gameObject.SetActive(false);
addTagButton.gameObject.SetActive(false);
recordStop.gameObject.SetActive(false);
}
}
/// <summary>
/// 检查是否重名
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private bool CheckSameName(string name, int id)
{
foreach (var node in list)
{
if (node.nodeName == name)
{
if (node.nodeID == id)//ID相同代表同一个节点
{
continue;
}
return true;
}
}
return false;
}
/// <summary>
/// 从xml文件中获取自增主键
/// </summary>
/// <returns></returns>
public int GetNextID()
{
int result = 0;
string filepath = DisasterManager.GetNodeListXML();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode nextID = xmlDoc.SelectSingleNode("NodeList/SecondNode/NextID");
result = int.Parse(nextID.InnerText);
nextID.InnerText = (result + 1).ToString();
xmlDoc.Save(filepath);
return result;
}
}