UGUIContextMenu.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. namespace XNode.Examples.RuntimeMathNodes {
  7. public class UGUIContextMenu : MonoBehaviour, IPointerExitHandler {
  8. public Action<Type, Vector2> onClickSpawn;
  9. public CanvasGroup group;
  10. [HideInInspector] public Node selectedNode;
  11. private Vector2 pos;
  12. private void Start() {
  13. Close();
  14. }
  15. public void OpenAt(Vector2 pos) {
  16. transform.position = pos;
  17. group.alpha = 1;
  18. group.interactable = true;
  19. group.blocksRaycasts = true;
  20. transform.SetAsLastSibling();
  21. }
  22. public void Close() {
  23. group.alpha = 0;
  24. group.interactable = false;
  25. group.blocksRaycasts = false;
  26. }
  27. public void SpawnMathNode() {
  28. SpawnNode(typeof(XNode.Examples.MathNodes.MathNode));
  29. }
  30. public void SpawnDisplayNode() {
  31. SpawnNode(typeof(XNode.Examples.MathNodes.DisplayValue));
  32. }
  33. public void SpawnVectorNode() {
  34. SpawnNode(typeof(XNode.Examples.MathNodes.Vector));
  35. }
  36. private void SpawnNode(Type nodeType) {
  37. Vector2 pos = new Vector2(transform.localPosition.x, -transform.localPosition.y);
  38. onClickSpawn(nodeType, pos);
  39. }
  40. public void RemoveNode() {
  41. RuntimeMathGraph runtimeMathGraph = GetComponentInParent<RuntimeMathGraph>();
  42. runtimeMathGraph.graph.RemoveNode(selectedNode);
  43. runtimeMathGraph.Refresh();
  44. Close();
  45. }
  46. public void OnPointerExit(PointerEventData eventData) {
  47. Close();
  48. }
  49. }
  50. }