399 lines
13 KiB
C#
399 lines
13 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
using static BaseConf;
|
||
//============================================================
|
||
//支持中文,文件使用UTF-8编码
|
||
//@author #AUTHOR#
|
||
//@create #CREATEDATE#
|
||
//@company #COMPANY#
|
||
//
|
||
//@description:
|
||
//============================================================
|
||
|
||
public class DragController : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
|
||
{
|
||
|
||
public DragTest1 oriObjectPrefab;
|
||
private DragTest1 targetObject;
|
||
public UPosItem currentUPosItem;
|
||
|
||
public Func<bool> DragCondition;
|
||
public Action<DragTest1> DragAction;
|
||
|
||
public Action<DragTest1> BeginDragAction;
|
||
public Action<DragTest1> EndDragAction;
|
||
|
||
//public DragTest1 currentDevice;
|
||
|
||
|
||
public UPosManger uPosManger;
|
||
public DeviceManager deviceManager;
|
||
|
||
public bool isSaveMode = false;
|
||
|
||
public Image _icon;
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
BeginDragAction = (target) =>
|
||
{
|
||
target.gameObject.SetActive(true);
|
||
};
|
||
DragAction = (target) =>
|
||
{
|
||
//target.transform.position = Camera.main.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 5));
|
||
OnRay(target.gameObject);
|
||
};
|
||
|
||
}
|
||
public void OnBeginDrag(PointerEventData eventData)
|
||
{
|
||
if (!isSaveMode)
|
||
targetObject = Instantiate(oriObjectPrefab);
|
||
else
|
||
{
|
||
targetObject = oriObjectPrefab;
|
||
targetObject.gameObject.SetActive(true);
|
||
}
|
||
if (targetObject.GetComponent<TransparentGlow>())
|
||
targetObject.GetComponent<TransparentGlow>().F1();
|
||
//currentDevice = targetObject.GetComponent<DragTest1>();
|
||
targetObject.transform.eulerAngles = oriObjectPrefab.transform.eulerAngles;
|
||
targetObject.transform.localScale = oriObjectPrefab.transform.lossyScale;
|
||
BeginDragAction?.Invoke(targetObject);
|
||
}
|
||
|
||
public void OnDrag(PointerEventData eventData)
|
||
{
|
||
if (DragCondition != null && DragCondition.Invoke())
|
||
{
|
||
DragAction?.Invoke(targetObject);
|
||
}
|
||
else if (DragCondition == null)
|
||
{
|
||
DragAction?.Invoke(targetObject);
|
||
}
|
||
}
|
||
|
||
public void OnEndDrag(PointerEventData eventData)
|
||
{
|
||
var v3 = targetObject.transform.position;
|
||
var d = targetObject.gameObject.GetComponent<DeviceQuery>();
|
||
bool isCard = MatchingType(d);
|
||
|
||
EndDragAction?.Invoke(targetObject);
|
||
if (!currentUPosItem || !uPosManger)
|
||
{
|
||
DestroyImmediate(targetObject.gameObject);
|
||
RecoverSet();
|
||
return;
|
||
}
|
||
else if (!isCard)
|
||
{
|
||
if (string.IsNullOrEmpty(d.deviceList.id))
|
||
{
|
||
DestroyImmediate(targetObject.gameObject);
|
||
RecoverSet();
|
||
return;
|
||
}
|
||
else
|
||
{
|
||
targetObject.gameObject.SetActive(false);
|
||
RecoverSet();
|
||
return;
|
||
}
|
||
}
|
||
else if (isCard)
|
||
{
|
||
CountUPos();
|
||
d.getRacksInfo();
|
||
if (currentUPosItem.conf2.module_type == BaseConf.ModuleType.设备 ||
|
||
currentUPosItem.conf2.module_type == BaseConf.ModuleType.板卡 ||
|
||
currentUPosItem.conf2.module_type == BaseConf.ModuleType.风扇 ||
|
||
currentUPosItem.conf2.module_type == BaseConf.ModuleType.电池)
|
||
{
|
||
if (string.IsNullOrEmpty(d.deviceList.id))
|
||
AddHight(d);
|
||
}
|
||
}
|
||
//模型错误微调
|
||
if (GameManager.Inst && targetObject.gameObject.layer != 13)
|
||
{
|
||
if (d && !string.IsNullOrEmpty(d.deviceList.modelNum))
|
||
{
|
||
GameManager.Inst.Model_error(d.deviceList.modelNum, targetObject.gameObject);
|
||
}
|
||
}
|
||
else if (targetObject.gameObject.layer == 13 && isCard)
|
||
{
|
||
targetObject.transform.position = v3;
|
||
ClickEvent click = new ClickEvent();
|
||
click.insertion(targetObject.transform, Vector3.zero);
|
||
}
|
||
|
||
|
||
RecoverSet();
|
||
|
||
}
|
||
|
||
|
||
public void AddHight(DeviceQuery d)
|
||
{
|
||
GameObject g = null;
|
||
if (!d.transform.Find("高亮提示"))
|
||
{
|
||
g = new GameObject("高亮提示");
|
||
g.transform.SetParent(targetObject.transform);
|
||
g.transform.localScale = Vector3.one;
|
||
g.transform.localPosition = Vector3.zero;
|
||
g.transform.localEulerAngles = Vector3.zero;
|
||
g.AddComponent<MeshFilter>().mesh = targetObject.GetComponent<MeshFilter>().mesh;
|
||
g.AddComponent<MeshRenderer>().materials = targetObject.GetComponent<MeshRenderer>().materials;
|
||
}
|
||
else
|
||
{
|
||
g = d.transform.Find("高亮提示").gameObject;
|
||
}
|
||
List<Material> m = new List<Material>();
|
||
for (int i = 0; i < g.GetComponent<MeshRenderer>().materials.Length; i++)
|
||
{
|
||
//g.GetComponent<MeshRenderer>().materials[i] = Resources.Load<Material>("Materials/1Tou");
|
||
m.Add(Resources.Load<Material>("Materials/1Tou"));
|
||
}
|
||
g.GetComponent<MeshRenderer>().materials = m.ToArray();
|
||
|
||
d.hight = g;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 匹配安装零件类型
|
||
/// </summary>
|
||
/// <param name="d"></param>
|
||
/// <returns></returns>
|
||
public bool MatchingType(DeviceQuery d)
|
||
{
|
||
if (targetObject.gameObject.layer == 8 || targetObject.gameObject.layer == 13 || targetObject.gameObject.layer == 19 || targetObject.gameObject.layer == 20)
|
||
{
|
||
if (currentUPosItem && currentUPosItem.conf2.isUse && d.conf2.isUse &&
|
||
currentUPosItem.conf2.module_type == d.conf2.module_type)
|
||
{
|
||
if (currentUPosItem.conf2.module_type == BaseConf.ModuleType.板卡 &&
|
||
d.conf2.type_card == currentUPosItem.conf2.type_card)
|
||
{
|
||
return true;
|
||
}
|
||
else if (currentUPosItem.conf2.module_type == BaseConf.ModuleType.设备)
|
||
{
|
||
return true;
|
||
}
|
||
else if (currentUPosItem.conf2.module_type == BaseConf.ModuleType.风扇)
|
||
{
|
||
return true;
|
||
}
|
||
else if (currentUPosItem.conf2.module_type == BaseConf.ModuleType.电池)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
else { return false; }
|
||
}
|
||
|
||
public void RecoverSet()
|
||
{
|
||
if (targetObject && targetObject.GetComponent<TransparentGlow>())
|
||
targetObject.GetComponent<TransparentGlow>().F2();
|
||
currentUPosItem = null;
|
||
targetObject = null;
|
||
}
|
||
|
||
public void DeleteTargetObject()
|
||
{
|
||
DestroyImmediate(targetObject.gameObject);
|
||
currentUPosItem = null;
|
||
targetObject = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 计算U位数量
|
||
/// </summary>
|
||
private void CountUPos()
|
||
{
|
||
if (targetObject == null) return;
|
||
if (currentUPosItem == null)
|
||
{
|
||
if (!isSaveMode)
|
||
Destroy(targetObject.gameObject);
|
||
else
|
||
targetObject.gameObject.SetActive(false);
|
||
return;
|
||
}
|
||
if (currentUPosItem.isOccupied)
|
||
{
|
||
if (!isSaveMode)
|
||
Destroy(targetObject.gameObject);
|
||
else
|
||
targetObject.gameObject.SetActive(false);
|
||
return;
|
||
}
|
||
int index = uPosManger.CountUPos(targetObject.volume, currentUPosItem.ID);
|
||
//Debug.Log("index-------------" + index);
|
||
if (!CountUPos(targetObject, index))
|
||
{
|
||
if (isSaveMode)
|
||
{
|
||
targetObject.gameObject.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
Destroy(targetObject.gameObject);
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
if (isSaveMode)
|
||
{
|
||
deviceManager.SwitchStagingPanel(this.gameObject);
|
||
}
|
||
else
|
||
{
|
||
//Debug.LogError(12);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成设备
|
||
/// </summary>
|
||
/// <param name="dt"></param>
|
||
/// <param name="index">机位号码,从0开始</param>
|
||
public bool CountUPos(DragTest1 dt, int index)
|
||
{
|
||
if (index >= 0)
|
||
{
|
||
var ewq = uPosManger.uPosItems;
|
||
dt.transform.SetParent(uPosManger.uPosItems[index].transform);
|
||
dt.transform.localPosition = new Vector3(0.25f, 0, -0.045f * dt.volume + 0.0225f);
|
||
uPosManger.SetCurrentUPosIsOccupied(index, dt.volume, true);
|
||
var q = dt.transform.GetComponent<DeviceQuery>();
|
||
if (q && q.deviceList.deviceType == "3")
|
||
{
|
||
q.deviceList.devicePosition = uPosManger.uPosItems[index].transform.name;
|
||
dt.transform.localPosition = Vector3.zero;
|
||
}
|
||
|
||
DeviceItem di;
|
||
if (dt.gameObject.GetComponent<DeviceItem>() == null)
|
||
di = dt.gameObject.AddComponent<DeviceItem>();
|
||
else
|
||
di = dt.GetComponent<DeviceItem>();
|
||
//if (di.gameObject.GetComponent<HighLight_VR>() == null)
|
||
//di.gameObject.AddComponent<HighLight_VR>();
|
||
|
||
di.Init(deviceManager, index, dt.volume, oriObjectPrefab);
|
||
deviceManager.SetDeviceItem(di);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
//Destroy(dt.gameObject);
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 触碰高亮U位
|
||
/// </summary>
|
||
/// <param name="isOnUPos"></param>
|
||
public void SetUPosHighLight(bool isOnUPos)
|
||
{
|
||
if (!uPosManger)
|
||
{
|
||
Debug.Log("No uPosManger");
|
||
return;
|
||
}
|
||
else if (!GameManager.Inst || GameManager.Inst.FindParent(uPosManger.gameObject, GameManager.Inst.IsDesiredParent) != GameManager.Inst.nowCabine)
|
||
{
|
||
Debug.Log("Wrong cabinet");
|
||
return;
|
||
}
|
||
if (isOnUPos)
|
||
{
|
||
|
||
int index = uPosManger.CountUPos(targetObject.volume, currentUPosItem.ID);
|
||
uPosManger.SetUPosInstruct(index, targetObject.volume, isOnUPos);
|
||
}
|
||
else
|
||
{
|
||
uPosManger.SetUPosInstruct(0, 0, isOnUPos);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
private void OnRay(GameObject target)
|
||
{
|
||
currentUPosItem = null;
|
||
target.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 2));
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
RaycastHit hitInfo;
|
||
if (Physics.Raycast(ray, out hitInfo, 100, 1 << 10))
|
||
{
|
||
if (hitInfo.collider != null && hitInfo.collider.GetComponent<UPosItem>())
|
||
{
|
||
currentUPosItem = hitInfo.collider.GetComponent<UPosItem>();
|
||
if (!EventSystem.current.IsPointerOverGameObject())
|
||
{
|
||
if (target.GetComponent<DeviceQuery>().conf2.module_type == ModuleType.设备)
|
||
SetUPosHighLight(true);
|
||
if (GameManager.Inst.nowCabine && GameManager.Inst.FindParent(currentUPosItem.gameObject, GameManager.Inst.IsDesiredParent) == GameManager.Inst.nowCabine)
|
||
{
|
||
}
|
||
else
|
||
{
|
||
currentUPosItem = null;
|
||
return;
|
||
}
|
||
if (!currentUPosItem.isOccupied && target.layer != 13)
|
||
{
|
||
uPosManger = hitInfo.transform.parent.GetComponent<UPosManger>();
|
||
target.transform.position = hitInfo.point;
|
||
|
||
}
|
||
else if (!currentUPosItem.isOccupied && target.layer == 13 && currentUPosItem.conf2.module_type == ModuleType.板卡)
|
||
{
|
||
uPosManger = hitInfo.transform.parent.GetComponent<UPosManger>();
|
||
target.transform.position = hitInfo.point;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("EventSystem");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("hitInfo");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("Physics");
|
||
SetUPosHighLight(false);
|
||
}
|
||
}
|
||
|
||
}
|