using SC.XR.Unity.Module_InputSystem;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Ximmerse.XR.Tag;
///
/// 机柜Tag管理
///
public class BladeServerManager : MonoSingleton
{
///
/// 所有的 UIItem
///
public List listTagTracking = new List();
// public List nowTagTracking = new List();
///
/// 当前识别到的Tag
///
public Dictionary dicNowTagTracking = new Dictionary();
///
/// 当前使用的Tag
///
public TagTracking foundTag;
public string foundID;
///
/// 当前Tag对应的UIItem
///
public DataItem foundItem;
///
/// 当前Tag 对应的机柜
///
public MachineObj foundMachineObj;
///
/// 检测角度
///
public float foundAngle;
///
/// 显示移动的距离
///
public float showDis;
///
/// 隐藏的距离
///
public float fadeDis;
///
/// 编辑Tag的移动框
///
// public Transform selectPanel;
///
/// 模拟测试用的
///
public TagTracking TestTracking;
public GameObject deviceDetails;
///
/// 同步位置的Obj
///
private Transform tagObj;
///
/// 是否是编辑状态
///
private bool state = false;
private int oldrow = -1;
//public delegate void TrackingFound(DataItem tagobj);
/////
///// 物体识别到显示
/////
//public TrackingFound OnTrackFound;
//public delegate void TrackingLast(DataItem tagobj);
/////
///// 物体识别到隐藏
/////
//public TrackingLast OnTrackLast;
private bool isSync;
public bool isDaoHang;
private void Start()
{
tagObj = new GameObject("TagObj").transform;
tagObj.position = Vector3.zero;
tagObj.eulerAngles = Vector3.zero;
isSync = false;
showDis = 0.55f;
fadeDis = 1.3f;
//ManipulationHandler manipulation = selectPanel.GetComponent();
//manipulation.PointerDown.AddListener(( msg )=> { SelectPointerDown(); });
//manipulation.PointerUp.AddListener((msg) => { SelectPointerUp(); });
}
float dis;
float angle;
Quaternion rot;
float roty;
Transform point;
private void Update()
{
#if UNITY_EDITOR
if(Input.GetKeyDown(KeyCode.K))
{
OnTrackingFound(TestTracking);
}
#endif
// Debug.Log(state);
// if(isDaoHang)
TagSynchronization();
}
///
/// 同步Tag
///
private void TagSynchronization()
{
if (foundItem != null && foundTag != null && !state)
{
dis = Vector3.Distance(new Vector3(foundTag.transform.position.x,0, foundTag.transform.position.z) ,new Vector3(OpenXRCamera.Instance.head.transform.position.x,0, OpenXRCamera.Instance.head.transform.position.z) );
angle = Vector3.Angle(OpenXRCamera.Instance.head.GetComponent().transform.forward, foundTag.transform.position - OpenXRCamera.Instance.head.transform.position);
// Debug.Log("Dis " + dis + " Angle" + angle + " showDis " + showDis + " fadeDis " + fadeDis);
if (Mathf.Abs(dis) < showDis && isDaoHang)
{
if (!foundItem.gameObject.activeSelf&& (BD_ShowDevicecDetailsObj.Instance == null||(BD_ShowDevicecDetailsObj.Instance != null && (!BD_ShowDevicecDetailsObj.Instance.gameObject.activeSelf||
!BD_ShowDevicecDetailsObj.Instance.transform.parent.gameObject.activeSelf))))
{
Debug.Log("DGJ ==== >> ShowFoundItem ");
foundItem.gameObject.SetActive(true);
if (BD_ShowDevicecDetailsObj.Instance != null && BD_ShowDevicecDetailsObj.Instance.gameObject.activeSelf)
BD_ShowDevicecDetailsObj.Instance.gameObject.SetActive(false);
// ScenesManager.Instance.showOffice(ScenesManager.SceneType.DeviceDetails);
// OnTrackFound(foundItem);
}
point = foundTag.transform.GetChild(0);
point.localPosition = foundItem.offsetPos;
switch (Mathf.Abs(foundItem.eulerAxle))
{
case 1:
point.localEulerAngles = new Vector3(0, 0, 90 * (foundItem.eulerAxle / Mathf.Abs(foundItem.eulerAxle)));
break;
case 2:
point.localEulerAngles = new Vector3(0, foundItem.eulerAxle > 0 ? 0 : 180, 0);
break;
case 3:
point.localEulerAngles = new Vector3(90 * (foundItem.eulerAxle / Mathf.Abs(foundItem.eulerAxle)), 0, 0);
break;
default:
break;
}
if (isSync)
{
Debug.Log("Dis " + dis + " Angle" + angle + " showDis " + showDis + " fadeDis " + fadeDis);
Debug.Log("FoundItem " + foundItem.transform.position + " point" + point.transform.position + " CA " + new Vector3(point.transform.position.x - foundItem.transform.position.x, 0, point.position.z - foundItem.transform.position.z));
//foundItem.transform.Translate(new Vector3(0, point.eulerAngles.y, 0), Time.deltaTime * 10);
foundItem.transform.eulerAngles = new Vector3(0, point.eulerAngles.y, 0);
foundItem.transform.position = point.transform.position;
UICallManager.Instance.OnTrackfound(foundItem);
isSync = false;
//MachineRoomManager.Instance.transform.eulerAngles = new Vector3(MachineRoomManager.Instance.transform.eulerAngles.x, point.eulerAngles.y+180, MachineRoomManager.Instance.transform.eulerAngles.z);
//MachineRoomManager.Instance.transform.position += new Vector3(point.transform.position.x - foundItem.transform.position.x, point.position.y - foundItem.transform.position.y, point.position.z - foundItem.transform.position.z);
//float offy = OpenXRCamera.Instance.transform.position.y - foundItem.transform.position.y;
//offy = offy * 0.05f;
//MachineRoomManager.Instance.transform.position += new Vector3(0, offy, 0);
}
}
// 隐藏的距离判断改由每个DataItem 自己监测
//else if (Mathf.Abs(dis) > fadeDis)
//{
// //OnTrackLast(foundItem);
// // foundItem.gameObject.SetActive(false);
// foundItem.Hide();
// UICallManager.Instance.OnTracklast(foundItem);
// foundItem = null;
// // foundTag = null;
// dicNowTagTracking.Clear();
// dicNowTagTracking = new Dictionary();
//}
}
else
{
isSync = true;
}
}
public void OnTrackingFound(TagTracking tracking)
{
#region 组合Tag识别
//if (!dicNowTagTracking.ContainsKey(tracking.TrackId))
//{
// //if(dicNowTagTracking.Count==1)
// //{
// // TagTracking firstTracking = dicNowTagTracking.FirstOrDefault().Value;
// // Debug.Log(firstTracking.TrackId +" "+tracking.TrackId+" "+ Vector3.Distance(firstTracking.transform.position, tracking.transform.position) + " Distance");
// // //if (Vector3.Distance(firstTracking.transform.position, tracking.transform.position) < 0.1f) // 判断两个Tag是否为并列使用
// // //{
// // // dicNowTagTracking.Add(tracking.TrackId, tracking);
// // // tagObj.SetParent(tracking.transform);
// // // tagObj.localEulerAngles = Vector3.zero;
// // // tagObj.localPosition = Vector3.zero;
// // // tagObj.SetParent(firstTracking.transform);
// // // if (tagObj.localPosition.x > 0)
// // // {
// // // foundTag = firstTracking;
// // // foundID = tracking.TrackId.ToString() + firstTracking.TrackId.ToString();
// // // }
// // // else
// // // {
// // // foundTag = tracking;
// // // foundID = firstTracking.TrackId.ToString() + tracking.TrackId.ToString();
// // // }
// // //}
// // //else
// // //{
// // // // 初始化
// // // StartTracking(tracking);
// // //}
// //}
// //else
// //{
// // // 初始化
// // StartTracking(tracking);
// //}
// #endregion
// if (dicNowTagTracking == null || dicNowTagTracking.Count == 0)
// {
// // 初始化
// StartTracking(tracking);
// }
//}
#endregion
foundTag = tracking;
foundID = tracking.TrackId.ToString();
Debug.Log("FoundID "+ foundID);
// UILogManager.Instance.SendLogText(2, foundID);
int row = SynPlayerTransform.Instance.GetRowCabinets();
bool orientation = SynPlayerTransform.Instance.GetOrientation();
if (orientation)
{
row += 1;
}
else
{
row += 0;
}
if (foundItem==null||foundItem.tagId!= tracking.TrackId || oldrow == -1 || oldrow != row)
{
oldrow = row;
QueryFoundItem(int.Parse(foundID));
isSync = true;
WayFindingManager.Instance.CloseWayFind();
// ScenesManager.Instance.showOffice(ScenesManager.SceneType.DeviceDetails);
}
Debug.Log(" FoundItem " + foundItem.ID);
UILogManager.Instance.SendLogText(1, tracking.TrackId.ToString() + " " + foundID);
}
public void OnTrackingLost(TagTracking tracking)
{
//if (tracking.TrackId == foundTag.TrackId)
// foundItem = null;
}
private void StartTracking(TagTracking tracking)
{
if(MachineRoomManager.Instance.isDataInit)
{
dicNowTagTracking.Clear();
dicNowTagTracking = new Dictionary();
dicNowTagTracking.Add(tracking.TrackId, tracking);
foundTag = tracking;
foundID = tracking.TrackId.ToString();
}
}
private void QueryFoundItem(int foundID)
{
Debug.Log("DGJ ===> QueryFoundItem " + listTagTracking.Count);
if (MachineRoomManager.Instance.isDataInit)
{
int row = SynPlayerTransform.Instance.GetRowCabinets();
bool orientation = SynPlayerTransform.Instance.GetOrientation();
if(orientation)
{
row += 1;
}
else
{
row += 0;
}
Debug.Log(row * MachineRoomManager.Instance.data.list + " " + (row + 1) * MachineRoomManager.Instance.data.list);
for (int i = row*MachineRoomManager.Instance.data.list; i < (row+1)*MachineRoomManager.Instance.data.list; i++)
{
Debug.Log("DGJ ===> " + listTagTracking[i].tagId);
if (listTagTracking[i].tagId == foundID)
{
if (foundItem != null)
{
//foundItem.gameObject.SetActive(false);
// foundItem.Hide();
if (state)
{
foundItem.isSelect = false;
foundItem.SetSelect(false);
// foundItem.SaveEdiotrData(selectPanel);
}
}
foundItem = listTagTracking[i];
// foundItem.gameObject.SetActive(true);
Debug.Log(" QueryFoundItem " + foundID);
if (state)
{
//selectPanel.eulerAngles = foundItem.transform.eulerAngles;
//selectPanel.position = foundItem.transform.position;
//selectPanel.gameObject.SetActive(true);
foundItem.isSelect = true;
foundItem.SetSelect(true);
}
break;
}
}
#region old
//for (int i = 0; i < listTagTracking.Count; i++)
//{
// Debug.Log("DGJ ===> " + listTagTracking[i].tagId);
// if (listTagTracking[i].tagId == foundID)
// {
// if (foundItem != null)
// {
// //foundItem.gameObject.SetActive(false);
// // foundItem.Hide();
// if (state)
// {
// foundItem.isSelect = false;
// foundItem.SetSelect(false);
// // foundItem.SaveEdiotrData(selectPanel);
// }
// }
// foundItem = listTagTracking[i];
// // foundItem.gameObject.SetActive(true);
// Debug.Log(" QueryFoundItem " + foundID);
// if (state)
// {
// //selectPanel.eulerAngles = foundItem.transform.eulerAngles;
// //selectPanel.position = foundItem.transform.position;
// //selectPanel.gameObject.SetActive(true);
// foundItem.isSelect = true;
// foundItem.SetSelect(true);
// }
// break;
// }
//}
// 找到x-tag 对应的机柜
//for (int i = 0; i < MachineRoomManager.Instance.listObjes.Count; i++)
//{
// if(MachineRoomManager.Instance.listObjes[i].deviceParameters!=null&&MachineRoomManager.Instance.listObjes[i].deviceParameters.deviceId == foundItem.ID.ToString())
// {
// // 找到后,
// foundMachineObj = MachineRoomManager.Instance.listObjes[i];
// }
//}
#endregion
if (foundItem != null && foundItem.tagId != foundID)
{
//foundItem.gameObject.SetActive(false);
// foundItem.Hide();
foundItem.isSelect = false;
foundItem.SetSelect(false);
foundItem = null;
}
}
}
public void EditorTag( Text text)
{
if(foundItem!=null)
{
text.text = state ? "打开" : "关闭";
text.text += "机柜Tag编辑";
state = !state;
if(state)
{
//selectPanel.eulerAngles = foundItem.transform.eulerAngles;
//selectPanel.position = foundItem.transform.position;
//selectPanel.gameObject.SetActive(true);
foundItem.isSelect = true;
foundItem.SetSelect(true);
}
else
{
foundItem.SaveEdiotrData(foundTag.transform);
//selectPanel.gameObject.SetActive(false);
foundItem.isSelect = false;
foundItem.SetSelect(false);
}
}
if(!state)
{
//for (int i = 0; i < listTagTracking.Count; i++)
//{
// listTagTracking[i].SaveData();
//}
foundItem.SaveData();
// Servermanager.Instance.Save();
MachineRoomManager.Instance.SaveAllData();
}
}
public void SelectPointerEnter()
{
//if(foundItem!=null)
// foundItem.SelectObj(selectPanel);
}
public void SelectPointerDown()
{
if (foundItem != null&& foundTag!=null)
{
foundItem.isSelect = true;
foundItem.SetSelect( true);
}
}
public void SelectPointerUp()
{
if (foundItem != null && foundTag != null)
{
foundItem.isSelect = false;
foundItem.SetSelect(false);
foundItem.SaveEdiotrData(foundTag.transform);
}
}
private void SaveData()
{
/*
*
*
*/
}
}