海淀天下城电子沙盘单机版
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.

350 lines
11 KiB

4 years ago
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
using UnityEngine.UI;
using AX.MessageSystem;
using System;
public class FirstNodeManager : MonoBehaviour
{
public Button addButton;
public Button deleteButton;
public Button editorButton;
public Transform itemParent;
public GameObject nodeWindow;
private GameObject itemPrefab;
private List<FirstNodeObject> list;
public static FirstNodeObject selectNode;
void Awake()
{
MessageDispatcher.AddListener("DataInitializationComplete", SetItemUI);
}
// Use this for initialization
void Start()
{
addButton.onClick.AddListener(AddNodeWindow);
deleteButton.onClick.AddListener(DeleteNodeWindow);
editorButton.onClick.AddListener(EditorNodeWindow);
itemPrefab = Resources.Load("UI/FirstNodeItem") as GameObject;
//list = DisasterManager.firstNodeObjects;
MessageDispatcher.AddListener("PlayStatusChanged", SetUIByPlayStatus);
SetUI();
}
private void SetItemUI(IMessage obj)
{
if (itemPrefab==null)
{
itemPrefab = Resources.Load("UI/FirstNodeItem") as GameObject;
}
list = DisasterManager.firstNodeObjects;
DrawItem();
}
private void OnDestroy()
{
MessageDispatcher.RemoveListener("PlayStatusChanged", SetUIByPlayStatus);
MessageDispatcher.RemoveListener("DataInitializationComplete", SetItemUI);
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// 初始化第一节点UI
/// </summary>
private void DrawItem()
{
selectNode = null;
foreach (Transform item in itemParent)
{
Destroy(item.gameObject);
}
for (int i = 0; i < list.Count; i++)
{
AddNodeItem(list[i], i);
}
}
private void AddNodeWindow()
{
nodeWindow.SetActive(true);
var node = new FirstNodeObject();
node.nodeID = -1;
nodeWindow.GetComponent<FirstNodeWindow>().SetWindow(node);
}
private void DeleteNodeWindow()
{
if (selectNode == null)
{
return;
}
ResourceLoadWindow.Instance.LoadTipWindow("确定删除该一级节点?", DeleteNode, null);
}
private void EditorNodeWindow()
{
if (selectNode == null)
{
return;
}
nodeWindow.SetActive(true);
nodeWindow.GetComponent<FirstNodeWindow>().SetWindow(selectNode);
}
/// <summary>
/// 新增一级节点
/// </summary>
/// <param name="name"></param>
public void AddNode(FirstNodeObject node)
{
if (node.nodeName == "")
{
ResourceLoadWindow.Instance.LoadTextHintWindow("节点名不能为空", 0.5f);
return;
}
//查看有没有相同名称
if (CheckSameName(node.nodeName, node.nodeID))
{
ResourceLoadWindow.Instance.LoadTextHintWindow("已有相同名称", 0.5f);
return;
}
if (list.Count >= 12)
{
ResourceLoadWindow.Instance.LoadTextHintWindow("不能再生成一级目录", 0.5f);
return;
}
node.nodeID = GetNextID();
list.Add(node);
AddNodeItem(node, list.Count - 1);//UI中添加
AddXml(node);//xml文件中添加
nodeWindow.SetActive(false);
}
private void AddNodeItem(FirstNodeObject obj, int index)
{
GameObject item = Instantiate(itemPrefab, itemParent) as GameObject;
item.GetComponent<Toggle>().group = itemParent.GetComponent<ToggleGroup>();
item.GetComponent<FirstNodeItem>().SetItem(obj, index);
}
private void AddXml(FirstNodeObject obj)
{
string filepath = DisasterManager.GetNodeListXML();
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode nodes = xmlDoc.SelectSingleNode("NodeList/FirstNode/Nodes");
XmlElement node = xmlDoc.CreateElement("Node");
node.SetAttribute("ID", obj.nodeID.ToString());
node.SetAttribute("Name", obj.nodeName);
nodes.AppendChild(node);
xmlDoc.Save(filepath);
}
}
/// <summary>
/// 删除一级节点
/// </summary>
private void DeleteNode()
{
int index = 0;
foreach (Transform item in itemParent)
{
var msg = item.GetComponent<FirstNodeItem>();
if (msg.obj == selectNode)
{
index = msg.index;
Destroy(item.gameObject);
break;
}
}
DeleteXml(selectNode);//xml文件中删除
list.Remove(selectNode);
selectNode = null;
MessageDispatcher.SendMessage("DeleteFirstNode", index);//删除后需改变其他节点显示索引值
}
private void DeleteXml(FirstNodeObject obj)
{
string filepath = DisasterManager.GetNodeListXML();
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode nodes = xmlDoc.SelectSingleNode("NodeList/FirstNode/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);
}
}
/// <summary>
/// 编辑一级节点
/// </summary>
/// <param name="name"></param>
public void EditorNode(FirstNodeObject 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(FirstNodeObject obj)
{
foreach (Transform item in itemParent)
{
var msg = item.GetComponent<FirstNodeItem>();
if (msg.obj.nodeID == obj.nodeID)
{
msg.SetUI();
break;
}
}
}
private void EditorXML(FirstNodeObject obj)
{
string filepath = DisasterManager.GetNodeListXML();
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNode nodes = xmlDoc.SelectSingleNode("NodeList/FirstNode/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());
break;
}
}
xmlDoc.Save(filepath);
}
}
private void SetUI()
{
if (DisasterManager.editorRight)
{
addButton.gameObject.SetActive(true);
deleteButton.gameObject.SetActive(true);
editorButton.gameObject.SetActive(true);
}
else
{
addButton.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
editorButton.gameObject.SetActive(false);
}
}
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);
}
else if (playStatus == PlayStatus.isEditor)
{
addButton.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
editorButton.gameObject.SetActive(false);
}
else if (playStatus == PlayStatus.isReplay)
{
addButton.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
editorButton.gameObject.SetActive(false);
}
}
else
{
addButton.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
editorButton.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/FirstNode/NextID");
result = int.Parse(nextID.InnerText);
nextID.InnerText = (result + 1).ToString();
xmlDoc.Save(filepath);
return result;
}
public static string ToChinese(int Num)
{
string Number = "";
switch (Num)
{
case 0: Number = "一"; break;
case 1: Number = "二"; break;
case 2: Number = "三"; break;
case 3: Number = "四"; break;
case 4: Number = "五"; break;
case 5: Number = "六"; break;
case 6: Number = "七"; break;
case 7: Number = "八"; break;
case 8: Number = "九"; break;
case 9: Number = "十"; break;
case 10: Number = "十一"; break;
case 11: Number = "十二"; break;
}
return Number;
}
}