157 lines
4.6 KiB
C#
157 lines
4.6 KiB
C#
using GCSeries.Core.Input;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
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;
|
|
string deviceName;
|
|
bool isRight;
|
|
|
|
bool isClick = false;
|
|
public static ObjClickCondition Allocate(string path, Dictionary<string, string> datas)
|
|
{
|
|
var conditionAction = mSimpleObjectPool.Allocate();
|
|
conditionAction.ActionID = ActionKit.ID_GENERATOR++;
|
|
conditionAction.Deinited = false;
|
|
conditionAction.Reset();
|
|
conditionAction.path = path;
|
|
conditionAction.deviceName = datas.ContainsKey("deviceName") ? datas["deviceName"] : null;
|
|
conditionAction.isRight = true;
|
|
if (datas.ContainsKey("isRight"))
|
|
{
|
|
bool.TryParse(datas["isRight"], out conditionAction.isRight);
|
|
}
|
|
conditionAction.isClick = false;
|
|
return conditionAction;
|
|
}
|
|
public bool Check()
|
|
{
|
|
if (obj == null)
|
|
{
|
|
if (string.IsNullOrEmpty(deviceName))
|
|
{
|
|
obj = Utility.FindObj(path);
|
|
}
|
|
else
|
|
{
|
|
obj = DeviceController.Instance.GetDeviceObj(deviceName);
|
|
if (obj == null)
|
|
{
|
|
Debug.LogError($"ûÓÐÕÒµ½ path:{path} deviceName:{deviceName}");
|
|
}
|
|
}
|
|
}
|
|
#if VR
|
|
|
|
#else
|
|
if (Input.GetMouseButtonUp(0) && EventSystem.current.IsPointerOverGameObject() == false)
|
|
{
|
|
Vector3 mousePos = Input.mousePosition;
|
|
Ray ray = Camera.main.ScreenPointToRay(mousePos);
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(ray, out hit))
|
|
{
|
|
if (isRight)
|
|
{
|
|
return obj == hit.collider.gameObject;
|
|
}
|
|
else
|
|
{
|
|
return obj != hit.collider.gameObject;
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
return isClick;
|
|
}
|
|
public bool Paused { get; set; }
|
|
public bool Deinited { get; set; }
|
|
public ulong ActionID { get; set; }
|
|
public ActionStatus Status { get; set; }
|
|
public void OnStart()
|
|
{
|
|
#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)
|
|
{
|
|
if (isRight)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
#endif
|
|
|
|
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);
|
|
#if VR
|
|
UIRoot.Instance.transform.Find("ZMouse").GetComponent<ZPointer>().OnClick.RemoveListener(OnClick);
|
|
UIRoot.Instance.transform.Find("ZStylus").GetComponent<ZPointer>().OnClick.RemoveListener(OnClick);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
Paused = false;
|
|
Status = ActionStatus.NotStart;
|
|
}
|
|
}
|
|
|
|
//public static class ObjClickConditionExtension
|
|
//{
|
|
// public static ISequence ObjClickCondition(this ISequence self, string uipath)
|
|
// {
|
|
// return self.Append(QFramework.ObjClickCondition.Allocate(uipath));
|
|
// }
|
|
//}
|
|
} |