154 lines
4.5 KiB
C#
154 lines
4.5 KiB
C#
using GCSeries.Core.Input;
|
||
using QFramework;
|
||
using QFramework.Example;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using XMLTool;
|
||
|
||
public class DeviceController : MonoSingleton<DeviceController>
|
||
{
|
||
public Dictionary<string, DeviceData> deviceDict = new Dictionary<string, DeviceData>();
|
||
|
||
public class DeviceData
|
||
{
|
||
public DeviceItem item;
|
||
public Device device;
|
||
}
|
||
|
||
|
||
public override void OnSingletonInit()
|
||
{
|
||
base.OnSingletonInit();
|
||
TypeEventSystem.Global.Register<OnModuleStart>(OnStart).UnRegisterWhenGameObjectDestroyed(gameObject);
|
||
TypeEventSystem.Global.Register<OnModuleQuit>(OnQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
|
||
|
||
#if VR
|
||
UIRoot.Instance.transform.Find("ZStylus").GetComponent<ZPointer>()?.OnObjectEntered.AddListener(OnObjEnter);
|
||
UIRoot.Instance.transform.Find("ZMouse").GetComponent<ZPointer>()?.OnObjectEntered.AddListener(OnObjEnter);
|
||
#endif
|
||
}
|
||
|
||
|
||
private void OnObjEnter(ZPointer arg0, GameObject arg1)
|
||
{
|
||
var deviceItem = arg1.GetComponent<DeviceItem>();
|
||
var uitip = UIKit.GetPanel<UIDeviceTip>();
|
||
if (deviceItem != null && deviceItem.tipItem != null)
|
||
{
|
||
if (uitip != null)
|
||
{
|
||
UIKit.OpenPanelAsync<UIDeviceTip>(UILevel.PopUI).ToAction().Start(this, () =>
|
||
{
|
||
});
|
||
}
|
||
if (uitip != null)
|
||
{
|
||
uitip.Open(new UIDeviceTipData() { txt = deviceItem.tipItem.label });
|
||
uitip.Show();
|
||
return;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
uitip?.Hide();
|
||
}
|
||
|
||
}
|
||
|
||
private void OnQuit(OnModuleQuit quit)
|
||
{
|
||
foreach (var item in deviceDict)
|
||
{
|
||
item.Value.item?.Close();
|
||
item.Value.device = null;
|
||
}
|
||
deviceDict.Clear();
|
||
}
|
||
|
||
private void OnStart(OnModuleStart start)
|
||
{
|
||
foreach (var device in Global.Instance.curModule.Devices)
|
||
{
|
||
GameObject obj = null;
|
||
DeviceItem item = null;
|
||
if (string.IsNullOrEmpty(device.Path) == false)
|
||
{
|
||
obj = Utility.FindObj(device.Path);
|
||
item = obj?.GetOrAddComponent<DeviceItem>();
|
||
item?.Init(device);
|
||
}
|
||
deviceDict.Add(device.Name, new DeviceData() { device = device, item = item });
|
||
}
|
||
}
|
||
|
||
public Device GetDevice(string name)
|
||
{
|
||
if (deviceDict.ContainsKey(name))
|
||
{
|
||
return deviceDict[name]?.device;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public GameObject GetDeviceObj(string name)
|
||
{
|
||
return GetDeviceItem(name)?.gameObject;
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
#if !VR
|
||
var uitip = UIKit.GetPanel<UIDeviceTip>();
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
RaycastHit hit;
|
||
if (Physics.Raycast(ray, out hit))
|
||
{
|
||
var deviceItem = hit.transform.GetComponent<DeviceItem>();
|
||
if (deviceItem != null)
|
||
{
|
||
if (Input.GetMouseButtonUp(1))
|
||
{
|
||
if (deviceItem.device.rightMenu != null)
|
||
{
|
||
var data = new UIRightMenuData();
|
||
data.btns = deviceItem.device.rightMenu.items;
|
||
data.OnBtnClick = str =>
|
||
{
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>str<74><72>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
if (str.Equals("ʰȡ", StringComparison.Ordinal))
|
||
{
|
||
PlayerController.Instance.PickDevice(deviceItem.device.Name);
|
||
}
|
||
};
|
||
UIKit.OpenPanelAsync<UIRightMenu>(canvasLevel: UILevel.PopUI, uiData: data).ToAction().StartGlobal();
|
||
}
|
||
}
|
||
else if (uitip != null && deviceItem.tipItem != null && EventSystem.current.IsPointerOverGameObject() == false)
|
||
{
|
||
uitip.Open(new UIDeviceTipData() { txt = deviceItem.tipItem.label });
|
||
uitip.Show();
|
||
return;
|
||
}
|
||
}
|
||
|
||
}
|
||
uitip?.Hide();
|
||
#endif
|
||
}
|
||
|
||
|
||
|
||
public DeviceItem GetDeviceItem(string name)
|
||
{
|
||
if (deviceDict.ContainsKey(name))
|
||
{
|
||
return deviceDict[name]?.item;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
}
|