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; private void Awake() { #if VR #if Turing UIRoot.Instance.transform.Find("ZStylus").GetComponent().OnButtonPressed.AddListener(OnButtonPressed); UIRoot.Instance.transform.Find("ZStylus").GetComponent().OnButtonReleased.AddListener(OnButtonReleased); UIRoot.Instance.transform.Find("ZStylus").GetComponent().OnObjectEntered.AddListener(OnEnter); UIRoot.Instance.transform.Find("ZStylus").GetComponent().OnObjectExited.AddListener(OnExit); UIRoot.Instance.transform.Find("ZMouse").GetComponent().OnButtonPressed.AddListener(OnButtonPressed); UIRoot.Instance.transform.Find("ZMouse").GetComponent().OnButtonReleased.AddListener(OnButtonReleased); UIRoot.Instance.transform.Find("ZMouse").GetComponent().OnObjectEntered.AddListener(OnEnter); UIRoot.Instance.transform.Find("ZMouse").GetComponent().OnObjectExited.AddListener(OnExit); gameObject.GetOrAddComponent(); #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 public void Init(Body3D.Body body) { this.body = body; if (body.subBody == null || body.subBody.Count == 0) { if (body.toggle != null) { objToggle = gameObject.GetOrAddComponent(); ObjectColorToggle colorToggle = null; if (body.toggle.color != null) { colorToggle = gameObject.GetOrAddComponent(); 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(); #endif #else objDrag = gameObject.GetOrAddComponent(); #endif objDrag.OnDragEnd.AddListener(obj => { Body3DController.Instance.AddMoveObj(gameObject); }); RefreshDrag(); if (isOn) { TypeEventSystem.Global.Register(OnBody3DDragHandler); } else { TypeEventSystem.Global.UnRegister(OnBody3DDragHandler); } } TypeEventSystem.Global.Send(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(); 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); } } }