CreatePointAction.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor.Experimental.Rendering.Universal.Path2D.GUIFramework;
  4. namespace UnityEditor.Experimental.Rendering.Universal.Path2D
  5. {
  6. internal class CreatePointAction : ClickAction
  7. {
  8. private Control m_PointControl;
  9. public Func<IGUIState, Vector2, Vector3> guiToWorld;
  10. public Action<int, Vector3> onCreatePoint;
  11. public CreatePointAction(Control pointControl, Control edgeControl) : base(edgeControl, 0, false)
  12. {
  13. m_PointControl = pointControl;
  14. }
  15. protected override void OnTrigger(IGUIState guiState)
  16. {
  17. base.OnTrigger(guiState);
  18. var index = hoveredControl.layoutData.index;
  19. var position = GetMousePositionWorld(guiState);
  20. if (onCreatePoint != null)
  21. onCreatePoint(index, position);
  22. guiState.nearestControl = m_PointControl.ID;
  23. var data = m_PointControl.layoutData;
  24. data.index = index + 1;
  25. data.position = position;
  26. data.distance = 0f;
  27. m_PointControl.layoutData = data;
  28. guiState.changed = true;
  29. }
  30. private Vector3 GetMousePositionWorld(IGUIState guiState)
  31. {
  32. if (guiToWorld != null)
  33. return guiToWorld(guiState, guiState.mousePosition);
  34. return guiState.GUIToWorld(guiState.mousePosition, hoveredControl.layoutData.forward, hoveredControl.layoutData.position);
  35. }
  36. }
  37. }