2026-05-07 10:14:44 +02:00

232 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace Wing.Utils
{
public enum EMouseButton
{
None = -1,
Left = 0,
Right = 1,
Middle = 2,
}
public class TouchInteraction : MonoBehaviour
{
public delegate void InteractionEventHandler(ElementTag tag);
public event InteractionEventHandler SelectedEvent;
public event InteractionEventHandler HoverTargetChangedEvent;
public delegate void InteractionEventHandlerEx(GameObject go);
public event InteractionEventHandlerEx SelectedGoEvent;
public event InteractionEventHandlerEx HoverGoTargetChangedEvent;
private Camera m_camera = null;
private int m_layerMask = 0;
private ElementTag m_currentTag = null;
private ElementTag m_lastTag = null;
private GameObject m_currentGameObject = null;
private bool mMouseDown = false;
private Vector3 mMousePos;
public KeyCode CurrentCode
{
get;
private set;
}
public EMouseButton CurrentMouseCode
{
get;
private set;
}
public bool IsShift
{
get;
private set;
}
public void Set(Camera camera, int layerMask)
{
m_camera = camera;
m_layerMask = layerMask;
}
void Awake()
{
m_camera = Camera.main;
m_layerMask = int.MaxValue;
}
void OnGUI()
{
if (Event.current != null && Input.anyKey)
{
IsShift = Event.current.shift;
if (Event.current.isKey)
{
CurrentCode = Event.current.keyCode;
}
if(Event.current.isMouse)
{
CurrentMouseCode = (EMouseButton)Event.current.button;
}
}
else
{
IsShift = false;
}
}
private void checkClick()
{
if (Input.GetMouseButtonDown(0))
{
mMouseDown = true;
mMousePos = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
var df = Input.mousePosition - mMousePos;
var dx = Mathf.Abs(df.x);
var dy = Mathf.Abs(df.y);
if (mMouseDown && (dx < 2f && dy < 2f))
{
OnClick();
}
mMouseDown = false;
}
}
void Update()
{
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
{
return;
}
checkClick();
GameObject go = null;
if (Input.touches.Length > 0)
{
go = DetectiveGameObject(Input.touches[0].position);
}
else
{
go = DetectiveGameObject(Input.mousePosition);
}
if (go != m_currentGameObject)
{
if(HoverGoTargetChangedEvent != null)
{
HoverGoTargetChangedEvent(go);
}
ElementTag newTag = m_currentTag;
if (go != null)
{
newTag = go.GetComponent<ElementTag>();
}
else
{
newTag = null;
}
if(newTag != m_currentTag)
{
m_lastTag = m_currentTag;
m_currentTag = newTag;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
if (m_lastTag != m_currentTag)
{
if (HoverTargetChangedEvent != null)
{
HoverTargetChangedEvent(m_currentTag);
}
}
#endif
}
}
m_currentGameObject = go;
}
public ElementTag CurrentSelectedTag
{
get
{
return m_currentTag;
}
}
public ElementTag LastSelectedTag
{
get
{
return m_lastTag;
}
}
public void Destroy()
{
GameObject.Destroy(this);
}
private void OnClick()
{
if(SelectedGoEvent != null)
{
SelectedGoEvent(m_currentGameObject);
}
if (m_currentTag != null && SelectedEvent != null)
{
SelectedEvent(m_currentTag);
}
}
public GameObject DetectiveGameObject(Vector2 screenPoint)
{
Ray ray = m_camera.ScreenPointToRay(new Vector3(screenPoint.x, screenPoint.y, 0));
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000 , m_layerMask))
{
GameObject go = hitInfo.collider.gameObject;
return go;
}
return null;
}
public ElementTag DetectiveTag(Vector2 screenPoint)
{
Ray ray = m_camera.ScreenPointToRay(new Vector3(screenPoint.x, screenPoint.y, 0));
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000, m_layerMask))
{
GameObject go = hitInfo.collider.gameObject;
ElementTag tag = go.GetComponent<ElementTag>();
//MaterialSetter setter = new MaterialSetter(tag.Element.ObjInstance);
//setter.Set("Custom/Outline");
return tag;
}
return null;
}
}
}