116 lines
3.2 KiB
C#
Raw Normal View History

2025-03-25 13:23:47 +08:00
using QFramework;
2025-02-12 17:36:00 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2025-04-18 13:53:59 +08:00
using Turing.Core.TuringInput;
2025-02-12 17:36:00 +08:00
using UnityEngine;
using UnityEngine.Events;
2025-05-20 16:13:27 +08:00
using UnityEngine.EventSystems;
2025-02-12 17:36:00 +08:00
public class ObjectToggle : MonoBehaviour
{
public bool isOn = false;
//EventHandler OnValueChanged
public UnityEvent<bool> OnValueChanged = new UnityEvent<bool>();
private float mouseDownTime;
private bool isMouseDown;
2025-03-25 13:23:47 +08:00
bool isLock = false;
private void Awake()
{
TypeEventSystem.Global.Register<OnLock>(OnLockEvent).UnRegisterWhenDisabled(this);
2025-04-18 13:53:59 +08:00
#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);
#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)
{
OnMouseUp();
}
}
private void OnButtonPressed(TuringPointer arg0, int arg1)
{
if (arg1 == 0 && isEnter)
{
OnMouseDown();
}
2025-03-25 13:23:47 +08:00
}
2025-04-18 13:53:59 +08:00
#endif
2025-03-25 13:23:47 +08:00
private void OnLockEvent(OnLock islock)
{
this.isLock = islock.isLock;
}
2025-02-12 17:36:00 +08:00
private void OnMouseDown()
{
2025-03-25 13:23:47 +08:00
if (isLock == true)
{
return;
}
2025-05-20 16:13:27 +08:00
if (EventSystem.current.IsPointerOverGameObject() == true)
{
return;
}
2025-02-12 17:36:00 +08:00
// <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>µ<EFBFBD>ʱ<EFBFBD><CAB1>
mouseDownTime = Time.time;
isMouseDown = true;
}
private void OnMouseUp()
{
if (isMouseDown)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>º<EFBFBD>̧<EFBFBD><CCA7><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float clickDuration = Time.time - mouseDownTime;
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD> 0.2 <20><>
if (clickDuration < 0.2f)
{
if (isOn == false)
{
isOn = !isOn;
OnValueChanged?.Invoke(isOn);
}
2025-02-12 17:36:00 +08:00
}
isMouseDown = false;
}
}
public void Set(bool isOn)
{
this.isOn = isOn;
OnValueChanged?.Invoke(isOn);
}
2025-03-25 13:23:47 +08:00
2025-02-12 17:36:00 +08:00
}