|
|
using System; |
|
|
using System.Collections; |
|
|
using System.Collections.Generic; |
|
|
using UnityEngine; |
|
|
using UnityEngine.EventSystems; |
|
|
using UnityEngine.UI; |
|
|
using AX.MessageSystem; |
|
|
[RequireComponent(typeof(Image))] |
|
|
public class DragVertice : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler, IEndDragHandler |
|
|
{ |
|
|
public PolygonController PolygonController; |
|
|
RaycastHit hit; |
|
|
public Canvas Canvas; |
|
|
private int index; |
|
|
private Image image; |
|
|
private Color originalColor;//ԭʼ<EFBFBD><EFBFBD>ɫ |
|
|
public static GameObject selectedObj; |
|
|
private GameObject addPanel; |
|
|
private Toggle addToggle; |
|
|
private Button delButton; |
|
|
public bool IsOnDrag; |
|
|
private void Start() |
|
|
{ |
|
|
if (!Canvas) |
|
|
Canvas = GameObject.Find("Canvas").GetComponent<Canvas>(); |
|
|
image = GetComponent<Image>(); |
|
|
originalColor = image.color; |
|
|
addPanel = transform.parent.parent.Find("VerticeAddPanel").gameObject; |
|
|
addToggle = addPanel.transform.Find("Add").GetComponent<Toggle>(); |
|
|
delButton = addPanel.transform.Find("DelButton").GetComponent<Button>(); |
|
|
delButton.onClick.AddListener(DelVertice); |
|
|
MessageDispatcher.AddListener("SelectedVertice", ChangeColor); |
|
|
} |
|
|
|
|
|
private void OnDestroy() |
|
|
{ |
|
|
MessageDispatcher.RemoveListener("SelectedVertice", ChangeColor); |
|
|
} |
|
|
private void Update() |
|
|
{ |
|
|
if (Input.GetButtonDown("Fire1") && addToggle.isOn) |
|
|
{ |
|
|
if (selectedObj == this.gameObject) |
|
|
{ |
|
|
if (!EventSystem.current.IsPointerOverGameObject()) |
|
|
{ |
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); |
|
|
var rayCast = getRayCastAll(ray); |
|
|
if (rayCast) |
|
|
{ |
|
|
PolygonController.AddPolygon(index + 1, hit.point, Input.mousePosition); |
|
|
/*if (PolygonController.GetComponent<CloneGameObjInfo>().gameObjType == CloneObjType.EvacuationExit) |
|
|
{ |
|
|
MessageDispatcher.SendMessage("SaveEvacuationExit", PolygonController.gameObject); |
|
|
}*/ |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (Input.GetButtonUp("Fire1") && addToggle.isOn) |
|
|
{ |
|
|
if (selectedObj == this.gameObject) |
|
|
{ |
|
|
if (!EventSystem.current.IsPointerOverGameObject()) |
|
|
{ |
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); |
|
|
var rayCast = getRayCastAll(ray); |
|
|
if (rayCast) |
|
|
{ |
|
|
PolygonController.AddPolygon(index + 1, hit.point, Input.mousePosition); |
|
|
/*if (PolygonController.GetComponent<CloneGameObjInfo>().gameObjType == CloneObjType.EvacuationExit) |
|
|
{ |
|
|
MessageDispatcher.SendMessage("SaveEvacuationExit", PolygonController.gameObject); |
|
|
}*/ |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (Input.GetButtonDown("Fire2") && addToggle.isOn) |
|
|
{ |
|
|
addToggle.isOn = false; |
|
|
} |
|
|
} |
|
|
private void ChangeColor(IMessage obj) |
|
|
{ |
|
|
if (selectedObj != this.gameObject) |
|
|
{ |
|
|
image.color = originalColor; |
|
|
} |
|
|
} |
|
|
|
|
|
public void OnDrag(PointerEventData eventData) |
|
|
{ |
|
|
if (!PolygonController.Editable) |
|
|
return; |
|
|
|
|
|
Vector2 anchoredPosition; |
|
|
|
|
|
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(Canvas.transform as RectTransform, eventData.position, null, out anchoredPosition)) |
|
|
{ |
|
|
if (index >= 0 && PolygonController.ChangePosition(index, eventData.position)) |
|
|
{ |
|
|
IsOnDrag = true; |
|
|
(transform as RectTransform).anchoredPosition = anchoredPosition; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
public void OnPointerDown(PointerEventData eventData) |
|
|
{ |
|
|
index = PolygonController.FindIndex(eventData.position); |
|
|
selectedObj = this.gameObject; |
|
|
image.color = Color.red; |
|
|
addPanel.SetActive(true); |
|
|
MessageDispatcher.SendMessage("SelectedVertice"); |
|
|
} |
|
|
|
|
|
public void OnPointerUp(PointerEventData eventData) |
|
|
{ |
|
|
if (IsOnDrag) |
|
|
{ |
|
|
IsOnDrag = false; |
|
|
} |
|
|
} |
|
|
private void OnDisable() |
|
|
{ |
|
|
if (selectedObj == this.gameObject) |
|
|
{ |
|
|
selectedObj = null; |
|
|
image.color = originalColor; |
|
|
addPanel.SetActive(false); |
|
|
} |
|
|
} |
|
|
private void DelVertice() |
|
|
{ |
|
|
if (selectedObj == this.gameObject) |
|
|
{ |
|
|
if (index < 0) |
|
|
{ |
|
|
Debug.Log($"-><color=#CD2200>{"index小于0"}</color>"); |
|
|
return; |
|
|
} |
|
|
PolygonController.DelPolygon(index); |
|
|
if (PolygonController.worldPositions.Count < 0) |
|
|
{ |
|
|
return; |
|
|
} |
|
|
if (index == 0) |
|
|
{ |
|
|
PolygonController.transform.Find("info").transform.position = PolygonController.worldPositions[0]; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
private bool getRayCastAll(Ray ray) |
|
|
{ |
|
|
bool rayCast = false; |
|
|
//Ray ray = Camera.main.ScreenPointToRay(point); |
|
|
RaycastHit[] hits; |
|
|
//int notRendering = LayerMask.NameToLayer("NotRendering"); |
|
|
int ignoreRaycast = LayerMask.NameToLayer("NotRendering"); |
|
|
hits = Physics.RaycastAll(ray, Mathf.Infinity, ~(1 << ignoreRaycast)); |
|
|
System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance)); |
|
|
for (var i = 0; i < hits.Length; i++) |
|
|
{ |
|
|
var hitpoint = hits[i]; |
|
|
var renderer = hitpoint.collider.GetComponent<Renderer>(); |
|
|
if (renderer) |
|
|
{ |
|
|
if (renderer.enabled) |
|
|
{ |
|
|
rayCast = true; |
|
|
hit = hitpoint; |
|
|
//Debug.Log("----->"+hit.collider.name); |
|
|
break; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
var renderers = hitpoint.transform.GetComponentsInChildren<Renderer>(); |
|
|
for (int k = 0; k < renderers.Length; k++) |
|
|
{ |
|
|
if (renderers[k].enabled) |
|
|
{ |
|
|
rayCast = true; |
|
|
hit = hitpoint; |
|
|
//Debug.Log("----->"+hit.collider.name); |
|
|
break; |
|
|
} |
|
|
} |
|
|
if (rayCast) |
|
|
{ |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
return rayCast; |
|
|
} |
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData) |
|
|
{ |
|
|
if (!PolygonController.Editable) |
|
|
return; |
|
|
|
|
|
Vector2 anchoredPosition; |
|
|
|
|
|
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(Canvas.transform as RectTransform, eventData.position, null, out anchoredPosition)) |
|
|
{ |
|
|
if (index >= 0 && PolygonController.ChangePosition(index, eventData.position, true)) |
|
|
{ |
|
|
IsOnDrag = true; |
|
|
(transform as RectTransform).anchoredPosition = anchoredPosition; |
|
|
} |
|
|
} |
|
|
} |
|
|
}
|
|
|
|