283 lines
6.6 KiB
C#
Raw Normal View History

2025-02-12 17:36:00 +08:00
using QFramework;
using QFramework.Example;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using XMLTool;
2025-02-13 11:02:16 +08:00
using static XMLTool.Body3D;
2025-02-12 17:36:00 +08:00
public class Body3DController : MonoSingleton<Body3DController>
{
public enum Status
{
Normal = 1 << 0,
Active = 1 << 1,
2025-02-13 17:39:33 +08:00
Drag = 1 << 2,
}
2025-02-12 17:36:00 +08:00
Body3DController() { }
Dictionary<GameObject, Body3DOjbItem> objs = new Dictionary<GameObject, Body3DOjbItem>();
bool selectIsGroup = false;
public List<GameObject> isOnList = new List<GameObject>();
2025-02-13 11:02:16 +08:00
public bool allowDrag = false;
public Status status = Status.Normal;
private Vector2 mouseDownPosition; // <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EAB0B4>ʱ<EFBFBD><CAB1>λ<EFBFBD><CEBB>
2025-02-14 08:58:46 +08:00
Stack<GameObject> activeObjs = new Stack<GameObject>();
2025-02-19 13:07:05 +08:00
public Stack<GameObject> moveObjs = new Stack<GameObject>();
2025-02-12 17:36:00 +08:00
public override void OnSingletonInit()
{
base.OnSingletonInit();
TypeEventSystem.Global.Register<OnBody3DStart>((arg) =>
{
Refresh();
}).UnRegisterWhenGameObjectDestroyed(gameObject);
TypeEventSystem.Global.Register<OnModuleQuit>(OnClear);
TypeEventSystem.Global.Register<OnBody3DGroupTypeChanged>(OnToggleSelectType);
TypeEventSystem.Global.Register<OnBody3DSelected>(OnBody3DSelectedHandler);
}
private void OnBody3DSelectedHandler(OnBody3DSelected selected)
{
if (selected.isOn)
{
if (isOnList.Contains(selected.obj) == false)
{
if (selectIsGroup == false)
{
ClearObjectToggle();
}
isOnList.Add(selected.obj);
2025-02-13 11:02:16 +08:00
UIBody3DInfoData data = new UIBody3DInfoData();
2025-02-13 11:02:16 +08:00
data.body = selected.obj.GetComponent<Body3DOjbItem>().body;
UIKit.OpenPanelAsync<UIBody3DInfo>(UILevel.PopUI, data).ToAction().Start(this);
2025-02-12 17:36:00 +08:00
}
}
else
{
if (isOnList.Contains(selected.obj))
{
isOnList.Remove(selected.obj);
selected.obj.GetComponent<ObjectToggle>().Set(false);
}
2025-02-13 11:02:16 +08:00
if (isOnList.Count <= 0)
{
UIKit.HidePanel<UIBody3DInfo>();
2025-02-13 11:02:16 +08:00
}
2025-02-12 17:36:00 +08:00
}
}
2025-02-14 08:58:46 +08:00
public void SetStatus(Status status, bool isAdd)
{
if (isAdd)
{
this.status |= status;
}
else
{
2025-02-14 13:20:54 +08:00
// ɾ<><C9BE>״̬
this.status &= ~status;
}
}
2025-02-13 17:39:33 +08:00
public bool CheckStatus(Status status)
{
return (this.status & status) == status;
}
2025-02-14 08:58:46 +08:00
public void AddActiveObj(GameObject obj)
{
activeObjs.Push(obj);
}
public GameObject PopActiveObj()
{
if (activeObjs.Count > 0)
{
return activeObjs.Pop();
}
else
{
return null;
}
}
2025-02-14 13:20:54 +08:00
public void AddMoveObj(GameObject obj)
{
if (moveObjs.Contains(obj)==false)
{
moveObjs.Push(obj);
}
}
public GameObject PopMoveObj()
{
if (moveObjs.Count > 0)
{
return moveObjs.Pop();
}
else
{
return null;
}
}
2025-02-13 11:02:16 +08:00
public void Active(bool isActive)
{
foreach (var item in objs)
{
if (isOnList.Contains(item.Key) == false)
{
item.Key.SetActive(isActive);
}
}
}
2025-02-12 17:36:00 +08:00
public void Active(GameObject obj, bool isActive, bool isOther)
{
if (isOther)
{
foreach (var item in objs)
{
if (item.Key != obj)
{
2025-02-13 11:02:16 +08:00
item.Key.SetActive(isActive);
2025-02-12 17:36:00 +08:00
}
}
}
else
{
obj.SetActive(isActive);
}
}
public void Transparent(GameObject obj, bool isTransparent, bool isOther)
{
if (isOther)
{
foreach (var item in objs)
{
if (item.Key != obj)
{
Utility.SetSurfaceType(item.Key.GetComponent<MeshRenderer>().material, isTransparent);
}
}
}
else
{
Utility.SetSurfaceType(obj.GetComponent<MeshRenderer>().material, isTransparent);
}
}
private void OnToggleSelectType(OnBody3DGroupTypeChanged type)
{
selectIsGroup = type.isGroup;
}
private void OnClear(OnModuleQuit quit)
{
Clear();
}
public void Refresh()
{
Parser(Global.Instance.cur3DPart);
}
public void Parser(Body3D.Body body)
{
2025-02-13 11:02:16 +08:00
if (body.subBody != null && body.subBody.Count > 0)
2025-02-12 17:36:00 +08:00
{
foreach (var item in body.subBody)
{
Parser(item.Value);
}
}
2025-02-13 11:02:16 +08:00
else
{
GameObject obj = Utility.FindObj(body.Path);
var bodyObjItem = obj.GetOrAddComponent<Body3DOjbItem>();
bodyObjItem.Init(body);
objs.Add(obj, bodyObjItem);
}
2025-02-12 17:36:00 +08:00
}
2025-02-12 17:36:00 +08:00
private void Update()
{
if (Input.GetMouseButtonDown(0))
2025-02-12 17:36:00 +08:00
{
// <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EAB0B4>ʱ<EFBFBD><CAB1>λ<EFBFBD><CEBB>
mouseDownPosition = Input.mousePosition;
}
2025-02-12 17:36:00 +08:00
if (Input.GetMouseButtonUp(0))
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>̧<EFBFBD><CCA7>ʱ<EFBFBD><CAB1>λ<EFBFBD><CEBB>
Vector2 mouseUpPosition = Input.mousePosition;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>º<EFBFBD>̧<EFBFBD><CCA7>λ<EFBFBD><CEBB>֮<EFBFBD><D6AE><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
float distance = Vector2.Distance(mouseDownPosition, mouseUpPosition);
// <20>ж<EFBFBD><D0B6>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>С<EFBFBD><D0A1> 1 <20><>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UI <20><>
if (distance < 1f && EventSystem.current.IsPointerOverGameObject() == false)
2025-02-12 17:36:00 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
if (Camera.main != null)
2025-02-12 17:36:00 +08:00
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//GameObject obj = hit.collider.gameObject;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߻<EFBFBD><DFBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD>
}
else
{
if (!selectIsGroup)
{
ClearObjectToggle();
}
}
2025-02-12 17:36:00 +08:00
}
}
}
}
2025-02-12 17:36:00 +08:00
public void ClearObjectToggle()
{
for (int i = isOnList.Count - (1); i >= 0; i--)
{
isOnList[i].GetComponent<ObjectToggle>().Set(false);
}
}
public void Clear()
{
objs.Clear();
}
}