154 lines
4.5 KiB
C#
Raw Normal View History

2025-03-20 15:33:31 +08:00
using GCSeries.Core.Input;
2024-12-14 18:27:59 +08:00
using QFramework;
2025-01-07 17:12:49 +08:00
using QFramework.Example;
using System;
2024-12-14 18:27:59 +08:00
using System.Collections.Generic;
using UnityEngine;
2025-01-07 17:12:49 +08:00
using UnityEngine.EventSystems;
2024-12-14 18:27:59 +08:00
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);
2025-03-20 15:33:31 +08:00
#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)
{
2025-03-27 13:14:48 +08:00
uitip.Open(new UIDeviceTipData() { txt = deviceItem.tipItem.label });
uitip.Show();
2025-03-20 15:33:31 +08:00
return;
}
}
else
{
2025-03-27 13:14:48 +08:00
uitip?.Hide();
2025-03-20 15:33:31 +08:00
}
2024-12-14 18:27:59 +08:00
}
private void OnQuit(OnModuleQuit quit)
{
2025-01-14 17:56:57 +08:00
foreach (var item in deviceDict)
{
2025-01-14 19:12:21 +08:00
item.Value.item?.Close();
item.Value.device = null;
2025-01-14 17:56:57 +08:00
}
2024-12-14 18:27:59 +08:00
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);
2025-01-13 11:24:03 +08:00
item = obj?.GetOrAddComponent<DeviceItem>();
item?.Init(device);
2024-12-14 18:27:59 +08:00
}
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;
}
2025-01-07 17:12:49 +08:00
private void Update()
{
2025-03-20 15:33:31 +08:00
#if !VR
2025-01-07 17:12:49 +08:00
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)
2025-01-07 17:12:49 +08:00
{
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;
}
2025-01-07 17:12:49 +08:00
}
2025-01-07 17:12:49 +08:00
}
2025-03-27 13:14:48 +08:00
uitip?.Hide();
2025-03-20 15:33:31 +08:00
#endif
2025-01-07 17:12:49 +08:00
}
2024-12-14 18:27:59 +08:00
public DeviceItem GetDeviceItem(string name)
{
if (deviceDict.ContainsKey(name))
{
return deviceDict[name]?.item;
}
return null;
}
}