VirtualFramework/Assets/Scripts/Item/Body3DObjItem.cs
shenjianxing c8642b1afb 修复bug
2025-04-18 15:03:20 +08:00

224 lines
6.8 KiB
C#

using QFramework;
using QFramework.Example;
using System;
using System.Collections;
using System.Collections.Generic;
using Turing.Core.TuringInput;
using Turing.Samples;
using UnityEditor;
using UnityEngine;
using XMLTool;
public class Body3DObjItem : MonoBehaviour
{
public Body3D.Body body;
public ObjectToggle objToggle;
IObjDrag objDrag;
// 记录上一次鼠标按下的时间
private float lastClickTime;
// 双击的时间间隔阈值
private const float doubleClickTimeThreshold = 0.3f;
Shader shader;
private void Awake()
{
shader = GetComponent<Renderer>()?.material.shader;
TypeEventSystem.Global.Register<OnChangeMat>(OnChangeMatEvent).UnRegisterWhenGameObjectDestroyed(this);
#if VR
#if Turing
UIRoot.Instance.transform.Find("ZStylus").GetComponent<TuringStylus>().OnButtonPressed.AddListener(OnButtonPressed);
UIRoot.Instance.transform.Find("ZStylus").GetComponent<TuringStylus>().OnButtonReleased.AddListener(OnButtonReleased);
UIRoot.Instance.transform.Find("ZStylus").GetComponent<TuringStylus>().OnObjectEntered.AddListener(OnEnter);
UIRoot.Instance.transform.Find("ZStylus").GetComponent<TuringStylus>().OnObjectExited.AddListener(OnExit);
UIRoot.Instance.transform.Find("ZMouse").GetComponent<TuringMouse>().OnButtonPressed.AddListener(OnButtonPressed);
UIRoot.Instance.transform.Find("ZMouse").GetComponent<TuringMouse>().OnButtonReleased.AddListener(OnButtonReleased);
UIRoot.Instance.transform.Find("ZMouse").GetComponent<TuringMouse>().OnObjectEntered.AddListener(OnEnter);
UIRoot.Instance.transform.Find("ZMouse").GetComponent<TuringMouse>().OnObjectExited.AddListener(OnExit);
gameObject.GetOrAddComponent<TuringDraggableEx>();
#endif
#endif
}
#if Turing
bool isEnter = false;
private void OnExit(TuringPointer arg0, GameObject arg1)
{
if (arg1 == gameObject)
{
isEnter = false;
}
}
private void OnEnter(TuringPointer arg0, GameObject arg1)
{
if (arg1 == gameObject)
{
isEnter = true;
}
}
private void OnButtonReleased(TuringPointer arg0, int arg1)
{
if (arg1 == 0 && isEnter)
{
}
}
private void OnButtonPressed(TuringPointer arg0, int arg1)
{
if (arg1 == 0 && isEnter)
{
OnMouseDown();
}
}
#endif
private void OnChangeMatEvent(OnChangeMat t)
{
if (t.shader != null)
{
GetComponent<Renderer>().material.shader = t.shader;
}
else
{
GetComponent<Renderer>().material.shader = this.shader;
}
}
public void Init(Body3D.Body body)
{
this.body = body;
if (body.subBody == null || body.subBody.Count == 0)
{
if (body.toggle != null)
{
objToggle = gameObject.GetOrAddComponent<ObjectToggle>();
ObjectColorToggle colorToggle = null;
if (body.toggle.color != null)
{
colorToggle = gameObject.GetOrAddComponent<ObjectColorToggle>();
if (string.IsNullOrEmpty(body.toggle.color.isOn) == false)
{
colorToggle.isOnColor = Utility.ToColor(body.toggle.color.isOn);
}
if (string.IsNullOrEmpty(body.toggle.color.isOff) == false)
{
colorToggle.isOffColor = Utility.ToColor(body.toggle.color.isOff);
}
}
objToggle.OnValueChanged.AddListener(isOn =>
{
if (Body3DController.Instance.CheckStatus(Body3DController.Status.Active))
{
if (isOn == true)
{
gameObject.SetActive(false);
Body3DController.Instance.AddActiveObj(gameObject);
}
}
else
{
if (colorToggle != null)
{
if (isOn)
{
colorToggle.SetColor(ObjectColorToggle.State.On);
}
else
{
colorToggle.SetColor(ObjectColorToggle.State.Off);
}
}
#if VR
#if Turing
objDrag = gameObject.GetOrAddComponent<TuringDraggableEx>();
#endif
#else
objDrag = gameObject.GetOrAddComponent<ObjDrag>();
#endif
objDrag.OnDragEnd.AddListener(obj =>
{
Body3DController.Instance.AddMoveObj(gameObject);
});
RefreshDrag();
if (isOn)
{
TypeEventSystem.Global.Register<OnBody3DDragChanged>(OnBody3DDragHandler);
}
else
{
TypeEventSystem.Global.UnRegister<OnBody3DDragChanged>(OnBody3DDragHandler);
}
}
TypeEventSystem.Global.Send<OnBody3DSelected>(new OnBody3DSelected() { isOn = isOn, obj = gameObject });
});
}
}
}
private void OnMouseDown()
{
// 计算当前时间与上一次点击时间的间隔
float currentTime = Time.time;
if (currentTime - lastClickTime < doubleClickTimeThreshold)
{
// 触发双击事件
OnDoubleClick();
}
lastClickTime = currentTime;
}
public void OnDoubleClick()
{
if (Body3DController.Instance.CheckStatus(Body3DController.Status.Drag))
{
var drag = gameObject.GetComponent<IObjDrag>();
if (drag != null)
{
drag.OnDoubleClick();
}
}
else if (Body3DController.Instance.CheckStatus(Body3DController.Status.Active) == false)
{
float distance = 1;
if (float.TryParse(body.FocusDistance, out distance))
{
Show3DCamera.instance.FocusObj(gameObject.transform.position, distance, 0.5f);
}
else
{
Show3DCamera.instance.FocusObj(gameObject.transform.position, moveTime: 0.5f);
}
}
}
private void OnBody3DDragHandler(OnBody3DDragChanged drag)
{
RefreshDrag();
}
public void RefreshDrag()
{
if (objToggle != null && objDrag != null)
{
objDrag.isOn = objToggle.isOn && Body3DController.Instance.CheckStatus(Body3DController.Status.Drag);
}
}
}