NodeDrag.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace XNode.Examples.RuntimeMathNodes {
  6. public class NodeDrag : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler {
  7. private Vector3 offset;
  8. private UGUIMathBaseNode node;
  9. private void Awake() {
  10. node = GetComponentInParent<UGUIMathBaseNode>();
  11. }
  12. public void OnDrag(PointerEventData eventData) {
  13. node.transform.localPosition = node.graph.scrollRect.content.InverseTransformPoint(eventData.position) - offset;
  14. }
  15. public void OnBeginDrag(PointerEventData eventData) {
  16. Vector2 pointer = node.graph.scrollRect.content.InverseTransformPoint(eventData.position);
  17. Vector2 pos = node.transform.localPosition;
  18. offset = pointer - pos;
  19. }
  20. public void OnEndDrag(PointerEventData eventData) {
  21. node.transform.localPosition = node.graph.scrollRect.content.InverseTransformPoint(eventData.position) - offset;
  22. Vector2 pos = node.transform.localPosition;
  23. pos.y = -pos.y;
  24. node.node.position = pos;
  25. }
  26. public void OnPointerClick(PointerEventData eventData) {
  27. if (eventData.button != PointerEventData.InputButton.Right)
  28. return;
  29. node.graph.nodeContextMenu.selectedNode = node.node;
  30. node.graph.nodeContextMenu.OpenAt(eventData.position);
  31. }
  32. }
  33. }