111 lines
3.0 KiB
C#
111 lines
3.0 KiB
C#
using QFramework;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Turing.Core.TuringInput;
|
||
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
|
||
public class ObjectToggle : MonoBehaviour
|
||
{
|
||
public bool isOn = false;
|
||
//EventHandler OnValueChanged
|
||
public UnityEvent<bool> OnValueChanged = new UnityEvent<bool>();
|
||
private float mouseDownTime;
|
||
private bool isMouseDown;
|
||
bool isLock = false;
|
||
private void Awake()
|
||
{
|
||
TypeEventSystem.Global.Register<OnLock>(OnLockEvent).UnRegisterWhenDisabled(this);
|
||
|
||
#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();
|
||
}
|
||
}
|
||
#endif
|
||
|
||
private void OnLockEvent(OnLock islock)
|
||
{
|
||
this.isLock = islock.isLock;
|
||
}
|
||
|
||
private void OnMouseDown()
|
||
{
|
||
if (isLock == true)
|
||
{
|
||
return;
|
||
}
|
||
// <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);
|
||
}
|
||
}
|
||
|
||
isMouseDown = false;
|
||
}
|
||
}
|
||
|
||
public void Set(bool isOn)
|
||
{
|
||
this.isOn = isOn;
|
||
OnValueChanged?.Invoke(isOn);
|
||
}
|
||
|
||
}
|