ND_SimulationAutomaticControl/Assets/Scripts/Line/LineManager.cs

448 lines
18 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 DG.Tweening;
using HighlightPlus;
using Newtonsoft.Json;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.UI;
using XCharts.Runtime;
public class LineManager : SerializedMonoBehaviour
{
public static LineManager instance;
public Transform point1;
public Transform point2;
public GameObject Tips;
public TextMeshProUGUI TipTexts;
public List<GameObject> Models = new List<GameObject>();
LineData lineData = new LineData();
public GameObject Lights;
/// <summary>
/// 高亮板、机器
/// </summary>
public HighlightEffect LightPanel, Devices;
public List<Sprite> Experimentalresults = new List<Sprite>();
/// <summary>
/// 电脑屏幕显示的图形
/// </summary>
public Image ComputerPanel;
/// <summary>
/// 点位信息
/// </summary>
public List<HighlightEffect> Highlights = new List<HighlightEffect>();
/// <summary>
/// 保存未删除前的所有点位
/// </summary>
public List<HighlightEffect> Highlights_Backup = new List<HighlightEffect>();
/// <summary>
/// 存储所有高亮位信息
/// </summary>
[OdinSerialize]
[ShowInInspector]
public Dictionary<int, List<HighlightEffect>> highlightsdic = new Dictionary<int, List<HighlightEffect>>();
/// <summary>
/// 存储所有课程模型
/// </summary>
[OdinSerialize]
[ShowInInspector]
public Dictionary<int, List<GameObject>> Modeldic = new Dictionary<int, List<GameObject>>();
public int hightindext = 0;
/// <summary>
/// 接口名称
/// </summary>
public string LineName;
public float Addtime = 0;
public string Laboratorycourse = "";
private void Awake()
{
instance = this;
PlayerPrefs.SetString("LineData", "");
}
void Start()
{
Initialization();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="ExperimentNumber">实验序号</param>
private void Initialization(int ExperimentNumber = 0)
{
lineData = new LineData();
#region 线
//if (PlayerPrefs.GetString("LineData") != "")
//{
// lineData = JsonConvert.DeserializeObject<LineData>(PlayerPrefs.GetString("LineData"));
// for (int i = 0; i < lineData.Modelname.Count; i++)
// {
// for (int j = 0; j < Models.Count; j++)
// {
// if (lineData.Modelname[i].Contains(Models[j].name))
// {
// Models[j].SetActive(true);
// }
// if (lineData.Modelname[i].Equals(Models[j].name) && Models[j].name.Equals("diannaoxian"))
// {
// Models[j].transform.DOLocalMove(new Vector3(-0.56f, -0.293f, -0.163f), 0.5f);
// }
// }
// }
//}
#endregion
//重新选择初始化
ComputerPanel.gameObject.SetActive(false);
allline = 0;
Highlights_Backup.ForEach((x) => { x.highlighted = false; });
HighlightInit("");
for (int i = 0; i < Highlights.Count; i++)
{
if (i != 0)
{
Highlights[i].highlighted = false;
}
if (Highlights[i].name.Equals("闸") || Highlights[i].name.Equals("开关"))
{
Highlights[i].GetComponent<LineConnect>().isClick = false;
Highlights[i].transform.DORotateQuaternion(Quaternion.Euler(0, 0, 0), 0.1f);
Material material = Lights.GetComponent<MeshRenderer>().material;
material.DisableKeyword("_EMISSION");
material.color = new Color(0.59f, 0.18f, 0.12f);
}
}
isshow = false;
LightPanel.highlighted = true;
Devices.highlighted = true;
switch (ExperimentNumber)
{
case 511:
Laboratorycourse = "比例P环节K=1";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[0];
break;
case 512:
Laboratorycourse = "比例P环节K=2";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[1];
break;
case 521:
Laboratorycourse = "积分I环节T=1S";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[2];
break;
case 522:
Laboratorycourse = "积分I环节T=0.1S";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[3];
break;
case 531:
Laboratorycourse = "比例积分PI环节T=1S";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[2];
break;
case 532:
Laboratorycourse = "比例积分PI环节T=0.1S";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[5];
break;
case 541:
Laboratorycourse = "比例微分PD环节T=1S";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[5];
break;
case 542:
Laboratorycourse = "比例微分PD环节T=2S";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[5];
break;
case 551:
Laboratorycourse = "惯性环节T=1S";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[5];
break;
case 552:
Laboratorycourse = "惯性环节T=2S";
UI_TopTipPanel.instance.Init("提示:" + "当前实验是" + Laboratorycourse, ",请点击" + LineName + "。");
ComputerPanel.sprite = Experimentalresults[5];
break;
}
}
bool isshow;
void Update()
{
if (Input.GetMouseButtonDown(2))
{
point1 = null;
point2 = null;
PlayerPrefs.SetString("LineData", "");
}
if (allline >= Models.Count && !isshow)
{
isshow = true;
TipTexts.text = "导线已全部连接,请先打开电源开关再打开按钮开关!";
StartCoroutine(WaitHide(Tips));
}
if (Highlights.Count <= 0)
{
ComputerPanel.gameObject.SetActive(true);
TipTexts.text = "当前实验图形已显示,请上传实验报告。";
StartCoroutine(WaitHide(Tips));
UI_TopTitlePanel.instance.SubitBtn.gameObject.SetActive(true);
Highlights = Highlights_Backup.ToList();
Bootstrap.Instance.uiManager.HidePanel<UI_TopTipPanel>();
}
Addtime += 0.05f;
}
/// <summary>
/// 切换课程
/// </summary>
public void ChangeClass(int indext)
{
if (LineShowModel.Instance.currentState == DrawingState.Idle)
{
//先重置高亮,再重置模型,否则初始化无法正确显示高亮
Highlights = highlightsdic[indext].ToList();
Highlights_Backup = highlightsdic[indext].ToList();
Initialization(indext);
Models.ForEach((x) => { x.SetActive(false); });
Models = Modeldic[indext].ToList();
}
}
/// <summary>
/// 鼠标点击的物体
/// </summary>
/// <param name="point"></param>
public void ClickPoint(Transform point)
{
if (Highlights.Count != 20 && Highlights.Count % 2 != 0)
{
for (int i = 0; i < Highlights.Count; i++)
{
for (int j = 0; j < Highlights_Backup.Count; j++)
{
if (!Highlights_Backup[j].name.Equals(Highlights[i].name))
{
Highlights_Backup[j].GetComponent<BoxCollider>().enabled = false;
}
}
}
}
if (point.name.Equals("diannaoxian"))
{
if (!lineData.Modelname.Contains(point.name))
{
allline++;
lineData.Modelname.Add(point.name);
string json = JsonConvert.SerializeObject(lineData);
PlayerPrefs.SetString("LineData", json);
}
point.DOLocalMove(new Vector3(-0.56f, -0.293f, -0.163f), 0.5f);
}
if (point.name.Equals("开关"))
{
point.DORotateQuaternion(Quaternion.Euler(-17f, 0, 0), 0.5f);
}
if (point.name.Equals("闸"))
{
Material material = Lights.GetComponent<MeshRenderer>().material;
material.EnableKeyword("_EMISSION");
material.color = UnityEngine.Color.red;
point.DORotateQuaternion(Quaternion.Euler(-50f, 0, 0), 0.5f);
}
HighlightInit(point.name);
}
public IEnumerator Checkconnection(Transform transform, Transform transform2)
{
LineConnect line = transform.GetComponent<LineConnect>();
LineConnect line2 = transform2.GetComponent<LineConnect>();
if (line != null && line2 != null)
{
line.GetComponent<LineConnect>().isClick = false;
line2.GetComponent<LineConnect>().isClick = false;
if (!JudgmentLine(line, line2))
{
yield return null;
StartCoroutine(WaitHide(Tips));
}
}
}
public bool JudgmentLine(LineConnect line1, LineConnect line2)
{
Init();
#region
// if (line1.interfaceType == InterfaceType.负12V && line2.interfaceType != InterfaceType.负12V ||
// line2.interfaceType == InterfaceType.负12V && line1.interfaceType != InterfaceType.负12V)
// {
// TipTexts.text = "连接错误,-12V应该与-12V相连";
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// else if (line1.interfaceType == InterfaceType.正12V && line2.interfaceType != InterfaceType.正12V
// || line2.interfaceType == InterfaceType.正12V && line1.interfaceType != InterfaceType.正12V
// )
// {
// TipTexts.text = "连接错误12V应该与12V相连";
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// //else if (line1.interfaceType == InterfaceType.零V && line2.interfaceType != InterfaceType.GND
// // || line2.interfaceType == InterfaceType.GND && line1.interfaceType != InterfaceType.零V)
// //{
// // TipTexts.text = "连接错误0V应该与GND相连";
// // // WireDrawingSystem.instance.currentState = WireDrawingSystem.DrawingState.Idle;
// // //WireDrawingSystem.instance.HandleDelete();
// // //WireDrawingSystem.instance.ClearSnapPreview();
// // LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// // return false;
// //}
// else if (line1.interfaceType == InterfaceType.IN2_200K && line2.interfaceType != InterfaceType.OUT1
// || line2.interfaceType == InterfaceType.IN2_200K && line1.interfaceType != InterfaceType.OUT1)
// {
// TipTexts.text = "连接错误200K应该与OUT1相连";
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// else if (line1.interfaceType == InterfaceType.OUT1 && (line2.interfaceType != InterfaceType.IN4 && line2.interfaceType != InterfaceType.IN2_200K)
// || line2.interfaceType == InterfaceType.OUT1 && line1.interfaceType != InterfaceType.IN4)
// {
// TipTexts.text = "连接错误OUT1应该与IN4相连";
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// else if (line1.interfaceType == InterfaceType.AD1 && line2.interfaceType != InterfaceType.OUT2
//|| line2.interfaceType == InterfaceType.AD1 && line1.interfaceType != InterfaceType.OUT2)
// {
// TipTexts.text = "连接错误AD1应该与OUT2相连";
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// else if ((line1.interfaceType == InterfaceType.零V && (line2.interfaceType != InterfaceType.GND && line2.interfaceType != InterfaceType.III)
// || line2.interfaceType == InterfaceType.GND && line1.interfaceType != InterfaceType.零V)
// )
// {
// TipTexts.text = "连接错误0V应该与GND相连";
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// else if (line1.interfaceType == InterfaceType.OUT && line2.interfaceType != InterfaceType.IN1
// || line2.interfaceType == InterfaceType.OUT && line1.interfaceType != InterfaceType.IN1)
// {
// TipTexts.text = "连接错误OUT应该与IN1相连";
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// else if (line1.interfaceType == InterfaceType.AD2 && line2.interfaceType != InterfaceType.IN1
//|| line2.interfaceType == InterfaceType.AD2 && line1.interfaceType != InterfaceType.IN1)
// {
// TipTexts.text = "连接错误AD2应该与IN1相连";
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// else if (line1.IsNotLine == true || line2.IsNotLine == true)
// {
// TipTexts.text = "连接错误,不需要此连线";
// // WireDrawingSystem.instance.currentState = WireDrawingSystem.DrawingState.Idle;
// //WireDrawingSystem.instance.HandleDelete();
// //WireDrawingSystem.instance.ClearSnapPreview();
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
// return false;
// }
// else
// {
#endregion
for (int i = 0; i < Models.Count; i++)
{
string[] splitname = Models[i].name.Split('连');
if (splitname[0] == line1.interfaceType.ToString() && splitname[1] == line2.interfaceType.ToString() ||
splitname[0] == line2.interfaceType.ToString() && splitname[1] == line1.interfaceType.ToString())
{
if (!lineData.Modelname.Contains(Models[i].name))
{
lineData.Modelname.Add(Models[i].name);
string json = JsonConvert.SerializeObject(lineData);
PlayerPrefs.SetString("LineData", json);
}
if (Models[i].activeInHierarchy)
{
continue;
}
Models[i].SetActive(true);
allline++;
break;
}
}
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
return true;
//}
}
int allline = 0;
public void Init()
{
point2 = null;
point1 = null;
}
IEnumerator WaitHide(GameObject game)
{
game.SetActive(true);
yield return new WaitForSeconds(1.5f);
game.SetActive(false);
}
/// <summary>
/// 高亮初始化
/// </summary>
private void HighlightInit(string point)
{
for (int i = 0; i < Highlights.Count; i++)
{
if (Highlights[i].name.Equals(point))
{
hightindext = i;
Highlights[i].highlighted = false;
Highlights.Remove(Highlights[i]);
break;
}
}
if (hightindext < Highlights.Count)
{
LineName = Highlights[hightindext].GetComponent<LineConnect>().interfaceType.ToString();
Highlights[hightindext].highlighted = true;
Highlights[hightindext].GetComponent<BoxCollider>().enabled = true;
}
LightPanel.highlighted = false;
Devices.highlighted = false;
Bootstrap.Instance.uiManager.ShowPanel<UI_TopTipPanel>(this, E_UI_Layer.Top,
(panel) =>
{
panel.Init("提示:" + "当前实验是比例P环节K=1", ",请点击" + LineName + "。");
});
}
}
public class LineData
{
public List<string> Modelname = new List<string>();
}