EnergyEfficiencyManagement/Assets/Zion/Scripts/UIView/EquipmentStructureView.cs

570 lines
19 KiB
C#

using Newtonsoft.Json;
using Paroxe.PdfRenderer;
using SK.Framework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Playables;
using UnityEngine.UI;
/// <summary>
/// 设备结构认知
/// </summary>
public class EquipmentStructureView : UIView
{
public Variables variables;
private List<PlayableDirector> devicesTimeline = new List<PlayableDirector>();
private PlayableDirector currentDevice;
private bool isDragging;
public Animator[] animators;
private List<GameObject> models = new List<GameObject>();
private EnhancedModelViewerOrbitCamera enhancedModelViewerOrbitCamera;
private ModelSpreadController modelSpreadController;
public List<Button> deviceBtns = new List<Button>();
[System.Serializable]
public class ButtonData
{
public Sprite normalSprite;
public Sprite selectedSprite;
}
public List<ButtonData> deviceBtnData = new List<ButtonData>();
// 运行时使用的字典
private Dictionary<Button, ButtonData> buttonDictionary = new Dictionary<Button, ButtonData>();
private Button _currentSelectedButton;
[SerializeField] private List<ButtonData> modeData = new List<ButtonData>();
[SerializeField] private List<ButtonData> funcData = new List<ButtonData>();
public List<Button> openBtn_KongYaJi = new List<Button>();
public List<Button> openBtn_Beng = new List<Button>();
public List<Button> closeBtn_KongYaJi = new List<Button>();
public List<Button> closeBtn_Beng = new List<Button>();
protected override void OnInit(IViewData data)
{
base.OnInit(data);
variables.Set<EquipmentStructureView>("my_EquipmentStructure", this);
Init();
}
void Init()
{
models.Clear();
GameObject trans = GameObject.Find("AllModels");
if (trans != null)
{
for (int i = 0; i < trans.transform.childCount; i++)
{
models.Add(trans.transform.GetChild(i).gameObject);
models[i].SetActive(false);
}
}
focusDevices = "空调";
models.RemoveAt(0);
///打包部分检测
if (!BuildManager.Instance.isConclutedAirCondition && BuildManager.Instance.isConclutedAirCompressor && !BuildManager.Instance.isConclutedPump)
{
models.RemoveAt(1);
models.RemoveAt(0);
variables.Get<RectTransform>("空调").gameObject.Deactivate();
variables.Get<RectTransform>("泵").gameObject.Deactivate();
variables.Get<RectTransform>("空压机").gameObject.Deactivate();
focusDevices = "空压机";
}
if (!BuildManager.Instance.isConclutedAirCondition && BuildManager.Instance.isConclutedPump && !BuildManager.Instance.isConclutedAirCompressor)
{
models.RemoveAt(2);
models.RemoveAt(0);
variables.Get<RectTransform>("空调").gameObject.Deactivate();
variables.Get<RectTransform>("空压机").gameObject.Deactivate();
variables.Get<RectTransform>("泵").gameObject.Deactivate();
focusDevices = "泵";
}
models[0].Activate();
enhancedModelViewerOrbitCamera = Camera.main.GetComponent<EnhancedModelViewerOrbitCamera>();
modelSpreadController = this.gameObject.AddComponent<ModelSpreadController>();
enhancedModelViewerOrbitCamera.enabled = true;
enhancedModelViewerOrbitCamera.SetTarget(models[0].transform);
modelSpreadController.SetTarget(models[0].transform);
InitLeftBtns();
SetSelectedButton(deviceBtns[0]);
SetHightlight(variables.Get<Button>("合并"), modeData[1]);
variables.Get<Button>("关闭界面").onClick.AddListener(delegate
{
Unload();
foreach (var item in models)
{
if (item.activeSelf)
item.Deactivate();
}
UIUtils.Instance.SetParticalActive(false);
enhancedModelViewerOrbitCamera.enabled = false;
Load<TypicalEnergyHomeView>();
});
variables.Get<Button>("原理动画").onClick.AddListener(delegate
{
variables.Get<Slider>("动画节点").gameObject.SetActive(!variables.Get<Slider>("动画节点").gameObject.activeSelf);
});
variables.Get<Button>("拆解").onClick.AddListener(delegate
{
/*
variables.Get<Button>("1倍").gameObject.SetActive(!variables.Get<Button>("1倍").gameObject.activeSelf);
variables.Get<Button>("0.5倍").gameObject.SetActive(!variables.Get<Button>("0.5倍").gameObject.activeSelf);
variables.Get<Button>("0.25倍").gameObject.SetActive(!variables.Get<Button>("0.25倍").gameObject.activeSelf);*/
SetNormal(variables.Get<Button>("合并"), modeData[1]);
SetHightlight(variables.Get<Button>("拆解"), modeData[0]);
modelSpreadController.Split();
});
variables.Get<Button>("合并").onClick.AddListener(delegate
{
SetHightlight(variables.Get<Button>("合并"), modeData[1]);
SetNormal(variables.Get<Button>("拆解"), modeData[0]);
modelSpreadController.Merge();
});
variables.Get<Button>("媒体资源").onClick.AddListener(delegate
{
variables.Get<Button>("操作手册").gameObject.SetActive(!variables.Get<Button>("操作手册").gameObject.activeSelf);
variables.Get<Button>("FAQ").gameObject.SetActive(!variables.Get<Button>("FAQ").gameObject.activeSelf);
variables.Get<Button>("维护视频").gameObject.SetActive(!variables.Get<Button>("维护视频").gameObject.activeSelf);
});
/**/
variables.Get<Button>("安装调试").onClick.AddListener(delegate
{
//Debug.Log($"{_currentSelectedButton.name}");
if (focusDevices.Equals("空压机"))
{
variables.Get<RectTransform>("空气压缩机安装调试").gameObject.Activate();
}
else if (focusDevices.Equals("泵"))
{
variables.Get<RectTransform>("水泵安装调试").gameObject.Activate();
}
/*
GameMangner_TSQ.instance.bengStartOrStop(false);
StartCoroutine(LateOpen());*/
});
variables.Get<Button>("1倍").onClick.AddListener(delegate
{
SetTimelineSpeed(1);
});
variables.Get<Button>("0.5倍").onClick.AddListener(delegate
{
SetTimelineSpeed(0.5f);
});
variables.Get<Button>("0.25倍").onClick.AddListener(delegate
{
SetTimelineSpeed(0.25f);
});
variables.Get<Button>("操作手册").onClick.AddListener(delegate
{
SetHightlight(variables.Get<Button>("操作手册"), funcData[0]);
SetNormal(variables.Get<Button>("FAQ"), funcData[1]);
SetNormal(variables.Get<Button>("维护视频"), funcData[2]);
//variables.Get<PDFViewer>("PDFViewer").Activate();
variables.Get<GameObject>("PDF背景").Activate();
// variables.Get<PDFViewer>("PDFViewer").LoadDocumentFromStreamingAssets("PDF", "空调操作手册.pdf");
GetFileJson();
});
variables.Get<Button>("FAQ").onClick.AddListener(delegate
{
SetNormal(variables.Get<Button>("操作手册"), funcData[0]);
SetHightlight(variables.Get<Button>("FAQ"), funcData[1]);
SetNormal(variables.Get<Button>("维护视频"), funcData[2]);
variables.Get<Transform>("FAQ物体").Activate();
GetFileJson();
});
variables.Get<Button>("维护视频").onClick.AddListener(delegate
{
SetNormal(variables.Get<Button>("操作手册"), funcData[0]);
SetNormal(variables.Get<Button>("FAQ"), funcData[1]);
SetHightlight(variables.Get<Button>("维护视频"), funcData[2]);
variables.Get<Transform>("mediaPlayer").gameObject.Activate();
variables.Get<RectTransform>("MediaPlayerUI").gameObject.Activate();
});
for (int i = 0; i < openBtn_KongYaJi.Count; i++)
{
int index = i;
openBtn_KongYaJi[index].onClick.AddListener(() =>
{
SendDeviceCommand(index, true);
});
}
for (int i = 0; i < openBtn_Beng.Count; i++)
{
int index = i;
openBtn_Beng[index].onClick.AddListener(() =>
{
SendDeviceCommand(index, true);
});
}
for (int i = 0; i < closeBtn_KongYaJi.Count; i++)
{
int index = i;
closeBtn_KongYaJi[index].onClick.AddListener(() =>
{
SendDeviceCommand(index, false);
});
}
for (int i = 0; i < closeBtn_Beng.Count; i++)
{
int index = i;
closeBtn_Beng[index].onClick.AddListener(() =>
{
SendDeviceCommand(index, false);
});
}
StartCoroutine(OpenCheck());
}
IEnumerator LateOpen()
{
yield return new WaitForSeconds(2f);
GameMangner_TSQ.instance.bengStartOrStop(true);
}
void InitLeftBtns()
{
// 初始化字典
for (int i = 0; i < deviceBtns.Count; i++)
{
buttonDictionary[deviceBtns[i]] = deviceBtnData[i];
SetNormal(deviceBtns[i], deviceBtnData[i]);
// 添加点击事件
int index = i; // 闭包需要
deviceBtns[i].onClick.AddListener(() => OnButtonSelected(deviceBtns[index], index));
}
// 设置默认选中
if (deviceBtns.Count > 0)
{
SetSelectedButton(deviceBtns[0]);
}
}
private string focusDevices = "";
private void OnButtonSelected(Button selectedButton, int index)
{
focusDevices = selectedButton.name;
variables.Get<RectTransform>("空气压缩机安装调试").gameObject.Deactivate();
variables.Get<RectTransform>("水泵安装调试").gameObject.Deactivate();
if (focusDevices.Equals("空调"))
{
variables.Get<Button>("安装调试").interactable = false;
}
else
{
variables.Get<Button>("安装调试").interactable = true;
}
if (_currentSelectedButton == selectedButton) return;
// 取消上一个按钮的选中
if (_currentSelectedButton != null && buttonDictionary.ContainsKey(_currentSelectedButton))
{
SetNormal(_currentSelectedButton, buttonDictionary[_currentSelectedButton]);
}
for (int i = 0; i < models.Count; i++)
{
if (i == index)
{
models[i].Activate();
enhancedModelViewerOrbitCamera.SetTarget(models[i].transform);
modelSpreadController.SetTarget(models[i].transform);
}
else
{
models[i].Deactivate();
// 设置新按钮的选中
SetSelectedButton(selectedButton);
}
}
}
public void SetSelectedButton(Button button)
{
if (!buttonDictionary.ContainsKey(button))
{
Debug.LogWarning($"按钮 {button.name} 不在管理列表中");
return;
}
_currentSelectedButton = button;
SetHightlight(button, buttonDictionary[button]);
}
/// <summary>
/// 获取对应设备的FAQ和PDF文件
/// </summary>
private void GetFileJson()
{
Root root = new Root();
variables.Get<Text>("FAQText文本").text = "";
switch (_currentSelectedButton.name)
{
case "空调":
if (variables.Get<PDFViewer>("PDFViewer").gameObject.activeInHierarchy)
variables.Get<PDFViewer>("PDFViewer").LoadDocumentFromStreamingAssets("PDF", "空调操作手册.pdf");
root = JsonConvert.DeserializeObject<Root>(LocalTextLoader.LoadText(Application.streamingAssetsPath + "/FAQ/空调FAQ.json"));
for (int i = 0; i < root.DeviceFAQ.Count; i++)
{
variables.Get<Text>("FAQText文本").text += "<color=#29B4FF>" + root.DeviceFAQ[i].Question + "</color>" + "\n\n" + "<color=#CCEDFF>" + root.DeviceFAQ[i].Answer + "</color>" + "\n\n";
}
break;
case "泵":
if (variables.Get<PDFViewer>("PDFViewer").gameObject.activeInHierarchy)
variables.Get<PDFViewer>("PDFViewer").LoadDocumentFromStreamingAssets("PDF", "泵操作手册.pdf");
root = JsonConvert.DeserializeObject<Root>(LocalTextLoader.LoadText(Application.streamingAssetsPath + "/FAQ/泵FAQ.json"));
for (int i = 0; i < root.DeviceFAQ.Count; i++)
{
variables.Get<Text>("FAQText文本").text += "<color=#29B4FF>" + root.DeviceFAQ[i].Question + "</color>" + "\n\n" + "<color=#CCEDFF>" + root.DeviceFAQ[i].Answer + "</color>" + "\n\n";
}
break;
case "空压机":
if (variables.Get<PDFViewer>("PDFViewer").gameObject.activeInHierarchy)
variables.Get<PDFViewer>("PDFViewer").LoadDocumentFromStreamingAssets("PDF", "空压机操作手册.pdf");
root = JsonConvert.DeserializeObject<Root>(LocalTextLoader.LoadText(Application.streamingAssetsPath + "/FAQ/空压机FAQ.json"));
for (int i = 0; i < root.DeviceFAQ.Count; i++)
{
variables.Get<Text>("FAQText文本").text += "<color=#29B4FF>" + root.DeviceFAQ[i].Question + "</color>" + "\n\n" + "<color=#CCEDFF>" + root.DeviceFAQ[i].Answer + "</color>" + "\n\n";
}
break;
}
LayoutRebuilder.ForceRebuildLayoutImmediate(variables.Get<Text>("FAQText文本").rectTransform);
}
IEnumerator OpenCheck()
{
yield return new WaitForSeconds(3f);
enhancedModelViewerOrbitCamera.SetEnableClickToFocus(true);
}
/// <summary>
/// 设备开关状态改变事件
/// </summary>
/// <param name="isOn"></param>
void OnToggleValueChanged(bool isOn, int index)
{
if (isOn)
{
Debug.Log($"设备:{index}");
if (Get<EquipmentChildStructureView>() != null)
{
Debug.Log("关闭子结构");
Get<EquipmentChildStructureView>().Unload();
}
for (int i = 0; i < models.Count; i++)
{
if (i == index)
{
models[i].Activate();
enhancedModelViewerOrbitCamera.SetTarget(models[i].transform);
//currentModel = models[i].transform;
modelSpreadController.SetTarget(models[i].transform);
}
else
{
models[i].Deactivate();
}
}
}
}
/// <summary>
/// 速度改变事件
/// </summary>
/// <param name="speed"></param>
void SetTimelineSpeed(float speed)
{
if (currentDevice == null) return;
currentDevice.Resume();
variables.Get<Slider>("动画节点").value = 0;
if (currentDevice.playableGraph.IsValid())
{
var rootPlayable = currentDevice.playableGraph.GetRootPlayable(0);
if (rootPlayable.IsValid())
rootPlayable.SetSpeed(speed);
}
variables.Get<Slider>("动画节点").minValue = 0f;
variables.Get<Slider>("动画节点").maxValue = (float)currentDevice.duration;
variables.Get<Slider>("动画节点").wholeNumbers = false;
variables.Get<Slider>("动画节点").onValueChanged.AddListener(delegate (float value)
{
currentDevice.time = value;
currentDevice.Evaluate();
});
currentDevice.Play();
}
void Update()
{
/*
// Timeline → Slider
if (!isDragging && currentDevice.state == PlayState.Playing)
{
variables.Get<Slider>("动画节点").value = (float)currentDevice.time;
}
*/
}
/// <summary>
/// 开始拖拽
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData)
{
isDragging = true;
currentDevice.Pause();
Freeze();
}
/// <summary>
/// 拖拽结束
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData)
{
isDragging = false;
currentDevice.time = variables.Get<Slider>("动画节点").value;
currentDevice.Evaluate();
currentDevice.Play();
Resume();
}
public void Freeze()
{
foreach (var animator in animators)
{
if (animator != null)
animator.speed = 0f;
}
}
public void Resume()
{
foreach (var animator in animators)
{
if (animator != null)
animator.speed = 1f;
}
}
/// <summary>
/// 退出子物体观察模式
/// </summary>
public void ExitChildObservationMode()
{
//enhancedModelViewerOrbitCamera.ExitChildObservationMode();
}
private void SendDeviceCommand(int deviceIndex, bool turnOn)
{
GameMangner_TSQ.instance.SendDeviceCommand(deviceIndex, turnOn);
}
public void CtrAll(bool isOn)
{
GameMangner_TSQ.instance.CtrAll(isOn);
}
#region ButtonData
/// <summary>
/// 设置默认
/// </summary>
/// <param name="target"></param>
/// <param name="data"></param>
private void SetNormal(Button target, ButtonData data)
{
Image img = target.GetComponent<Image>();
if (img != null)
{
img.sprite = data.normalSprite;
}
}
/// <summary>
/// 设置高亮
/// </summary>
/// <param name="target"></param>
/// <param name="data"></param>
private void SetHightlight(Button target, ButtonData data)
{
Image img = target.GetComponent<Image>();
if (img != null)
{
img.sprite = data.selectedSprite;
}
}
#endregion
}
public class DeviceFAQItem
{
/// <summary>
/// 问题
/// </summary>
public string Question { get; set; }
/// <summary>
/// 回答
/// </summary>
public string Answer { get; set; }
}
public class Root
{
/// <summary>
/// 问题回答集合
/// </summary>
public List<DeviceFAQItem> DeviceFAQ { get; set; }
}