2025-05-09 17:09:37 +08:00

300 lines
7.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using QFramework;
using QFramework.Example;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using XMLTool;
using static XMLTool.Body3D;
public class Body3DController : MonoSingleton<Body3DController>
{
public enum Status
{
Normal = 1 << 0,
Active = 1 << 1,
Drag = 1 << 2,
}
Body3DController() { }
Dictionary<GameObject, Body3DObjItem> objs = new Dictionary<GameObject, Body3DObjItem>();
bool selectIsGroup = false;
public List<GameObject> isOnList = new List<GameObject>();
public bool allowDrag = false;
public Status status = Status.Normal;
private Vector2 mouseDownPosition; // ¼Ç¼Êó±ê°´ÏÂʱµÄλÖÃ
Stack<GameObject> activeObjs = new Stack<GameObject>();
public Stack<GameObject> moveObjs = new Stack<GameObject>();
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);
UIBody3DInfoData data = new UIBody3DInfoData();
data.body = selected.obj.GetComponent<Body3DObjItem>().body;
UIKit.OpenPanelAsync<UIBody3DInfo>(UILevel.PopUI, data).ToAction().Start(this);
}
}
else
{
if (isOnList.Contains(selected.obj))
{
isOnList.Remove(selected.obj);
selected.obj.GetComponent<ObjectToggle>().Set(false);
}
if (isOnList.Count <= 0)
{
UIKit.HidePanel<UIBody3DInfo>();
}
}
}
public void SetStatus(Status status, bool isAdd)
{
if (isAdd)
{
this.status |= status;
}
else
{
// ɾ³ý״̬
this.status &= ~status;
}
}
public bool CheckStatus(Status status)
{
return (this.status & status) == status;
}
public void AddActiveObj(GameObject obj)
{
activeObjs.Push(obj);
}
public GameObject PopActiveObj()
{
if (activeObjs.Count > 0)
{
return activeObjs.Pop();
}
else
{
return null;
}
}
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;
}
}
public void Active(bool isActive)
{
foreach (var item in objs)
{
if (isOnList.Contains(item.Key) == false)
{
item.Key.SetActive(isActive);
}
}
}
public void Active(GameObject obj, bool isActive, bool isOther)
{
if (isOther)
{
foreach (var item in objs)
{
if (item.Key != obj)
{
item.Key.SetActive(isActive);
}
}
}
else
{
obj.SetActive(isActive);
}
}
public void Transparent(GameObject obj, bool isTransparent, bool isOther)
{
if (isOther)
{
foreach (var item in objs)
{
if (item.Key != obj)
{
var mesh = item.Key.GetComponent<MeshRenderer>();
if (mesh != null)
{
Utility.SetSurfaceType(mesh.material, isTransparent);
}
else
{
Debug.LogError($"ÎïÌå:{item.Key.name} ûÓÐMeshRenderer×é¼þ");
}
}
}
}
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)
{
if (body.subBody != null && body.subBody.Count > 0)
{
foreach (var item in body.subBody)
{
Parser(item.Value);
}
if (body.isLineModeItem)
{
GameObject obj = Utility.FindObj(body.Path);
obj.GetOrAddComponent<LineModeItem>().Init(body);
}
}
else
{
GameObject obj = Utility.FindObj(body.Path);
//if (obj != null) { Debug.Log("????" + body.Path + "????"); };
var bodyObjItem = obj.GetOrAddComponent<Body3DObjItem>();
bodyObjItem.Init(body);
var lineModeItem = obj.GetOrAddComponent<LineModeItem>().Init(body);
objs.Add(obj, bodyObjItem);
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
// ¼Ç¼Êó±ê°´ÏÂʱµÄλÖÃ
mouseDownPosition = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
// »ñÈ¡Êó±ȩ̂ÆðʱµÄλÖÃ
Vector2 mouseUpPosition = Input.mousePosition;
// ¼ÆËãÊó±ê°´ÏºÍ̧ÆðλÖÃÖ®¼äµÄ¾àÀë
float distance = Vector2.Distance(mouseDownPosition, mouseUpPosition);
// ÅжÏÒÆ¶¯¾àÀëÊÇ·ñСÓÚ 1 ÇÒδµã»÷ÔÚ UI ÉÏ
if (distance < 1f && EventSystem.current.IsPointerOverGameObject() == false)
{
// ´ÓÏà»ú·¢ÉäÒ»ÌõÉäÏßµ½µ±Ç°Êó±êλÖÃ
if (Camera.main != null)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//GameObject obj = hit.collider.gameObject;
// ÕâÀï¿ÉÒÔÌí¼Ó´¦ÀíÉäÏß»÷ÖÐÎïÌåµÄÂß¼­
}
else
{
if (!selectIsGroup)
{
ClearObjectToggle();
}
}
}
}
}
}
public void ClearObjectToggle()
{
for (int i = isOnList.Count - (1); i >= 0; i--)
{
isOnList[i].GetComponent<ObjectToggle>().Set(false);
}
}
public void Clear()
{
objs.Clear();
}
}