157 lines
4.6 KiB
C#
Raw Permalink Normal View History

2025-03-20 13:21:32 +08:00
using GCSeries.Core.Input;
2024-12-14 18:27:59 +08:00
using System;
2024-12-26 19:35:31 +08:00
using System.Collections.Generic;
2024-12-14 18:27:59 +08:00
using System.IO;
using UnityEngine;
using UnityEngine.EventSystems;
namespace QFramework
{
public class ObjClickCondition : ICondition
{
private static SimpleObjectPool<ObjClickCondition> mSimpleObjectPool =
new SimpleObjectPool<ObjClickCondition>(() => new ObjClickCondition(), null, 10);
private ObjClickCondition() { }
public GameObject obj = null;
string path;
2024-12-26 19:35:31 +08:00
string deviceName;
2025-01-02 14:45:55 +08:00
bool isRight;
2025-03-20 13:21:32 +08:00
2025-03-31 16:07:26 +08:00
bool isClick = false;
2024-12-26 19:35:31 +08:00
public static ObjClickCondition Allocate(string path, Dictionary<string, string> datas)
2024-12-14 18:27:59 +08:00
{
var conditionAction = mSimpleObjectPool.Allocate();
conditionAction.ActionID = ActionKit.ID_GENERATOR++;
conditionAction.Deinited = false;
conditionAction.Reset();
conditionAction.path = path;
2024-12-26 19:35:31 +08:00
conditionAction.deviceName = datas.ContainsKey("deviceName") ? datas["deviceName"] : null;
2025-01-02 14:45:55 +08:00
conditionAction.isRight = true;
if (datas.ContainsKey("isRight"))
{
bool.TryParse(datas["isRight"], out conditionAction.isRight);
}
2025-03-31 16:07:26 +08:00
conditionAction.isClick = false;
2024-12-14 18:27:59 +08:00
return conditionAction;
}
public bool Check()
{
if (obj == null)
{
2024-12-26 19:35:31 +08:00
if (string.IsNullOrEmpty(deviceName))
{
obj = Utility.FindObj(path);
}
else
{
obj = DeviceController.Instance.GetDeviceObj(deviceName);
2025-01-13 17:18:29 +08:00
if (obj == null)
{
Debug.LogError($"û<><C3BB><EFBFBD>ҵ<EFBFBD> path:{path} deviceName:{deviceName}");
}
2024-12-26 19:35:31 +08:00
}
2024-12-14 18:27:59 +08:00
}
2025-03-20 13:21:32 +08:00
#if VR
#else
2025-01-13 17:18:29 +08:00
if (Input.GetMouseButtonUp(0) && EventSystem.current.IsPointerOverGameObject() == false)
2024-12-14 18:27:59 +08:00
{
Vector3 mousePos = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(mousePos);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
2025-01-02 14:45:55 +08:00
if (isRight)
{
return obj == hit.collider.gameObject;
}
else
{
return obj != hit.collider.gameObject;
}
2024-12-14 18:27:59 +08:00
}
}
2025-03-20 13:21:32 +08:00
#endif
2025-01-13 17:18:29 +08:00
2025-03-31 16:07:26 +08:00
return isClick;
2024-12-14 18:27:59 +08:00
}
public bool Paused { get; set; }
public bool Deinited { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
2025-03-20 13:21:32 +08:00
#if VR
UIRoot.Instance.transform.Find("ZMouse").GetComponent<ZPointer>().OnClick.AddListener(OnClick);
UIRoot.Instance.transform.Find("ZStylus").GetComponent<ZPointer>().OnClick.AddListener(OnClick);
#endif
}
#if VR
public void OnClick(ZPointer pointer, int index, GameObject obj)
{
2025-03-31 16:07:26 +08:00
if (isRight)
2025-03-20 13:21:32 +08:00
{
2025-03-31 16:07:26 +08:00
if (this.obj != null && obj == this.obj)
{
isClick = true;
}
}
else
{
if (this.obj != null)
{
var item = obj.GetComponent<DeviceItem>();
if (item != null && obj != this.obj)
{
isClick = true;
}
}
2025-03-20 13:21:32 +08:00
}
2025-03-31 16:07:26 +08:00
2024-12-14 18:27:59 +08:00
}
2025-03-20 13:21:32 +08:00
#endif
2024-12-14 18:27:59 +08:00
public void OnExecute(float dt)
{
if (Check())
{
this.Finish();
}
}
public void OnFinish()
{
}
public void Deinit()
{
if (!Deinited)
{
Deinited = true;
obj = null;
path = null;
mSimpleObjectPool.Recycle(this);
2025-03-31 16:07:26 +08:00
#if VR
UIRoot.Instance.transform.Find("ZMouse").GetComponent<ZPointer>().OnClick.RemoveListener(OnClick);
UIRoot.Instance.transform.Find("ZStylus").GetComponent<ZPointer>().OnClick.RemoveListener(OnClick);
#endif
2024-12-14 18:27:59 +08:00
}
}
public void Reset()
{
Paused = false;
Status = ActionStatus.NotStart;
}
}
2024-12-26 19:35:31 +08:00
//public static class ObjClickConditionExtension
//{
// public static ISequence ObjClickCondition(this ISequence self, string uipath)
// {
// return self.Append(QFramework.ObjClickCondition.Allocate(uipath));
// }
//}
2024-12-14 18:27:59 +08:00
}