MultipleEditablePathController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.Experimental.Rendering.Universal.Path2D
  5. {
  6. internal class MultipleEditablePathController : IEditablePathController
  7. {
  8. private IEditablePathController m_Controller = new EditablePathController();
  9. private List<IEditablePath> m_Paths = new List<IEditablePath>();
  10. private float m_ClosestDistance = float.MaxValue;
  11. private IEditablePath m_ClosestPath;
  12. public IEditablePath editablePath
  13. {
  14. get { return m_Controller.editablePath; }
  15. set { m_Controller.editablePath = value; }
  16. }
  17. public IEditablePath closestEditablePath { get; private set; }
  18. public ISnapping<Vector3> snapping
  19. {
  20. get { return m_Controller.snapping; }
  21. set { m_Controller.snapping = value; }
  22. }
  23. public bool enableSnapping
  24. {
  25. get { return m_Controller.enableSnapping; }
  26. set { m_Controller.enableSnapping = value; }
  27. }
  28. public void ClearPaths()
  29. {
  30. m_Paths.Clear();
  31. }
  32. public void AddPath(IEditablePath path)
  33. {
  34. if (!m_Paths.Contains(path))
  35. m_Paths.Add(path);
  36. }
  37. public void RemovePath(IEditablePath path)
  38. {
  39. m_Paths.Remove(path);
  40. }
  41. public void RegisterUndo(string name)
  42. {
  43. var current = editablePath;
  44. ForEach((s) =>
  45. {
  46. editablePath = s;
  47. m_Controller.RegisterUndo(name);
  48. });
  49. editablePath = current;
  50. }
  51. public void ClearSelection()
  52. {
  53. var current = editablePath;
  54. ForEach((s) =>
  55. {
  56. editablePath = s;
  57. m_Controller.ClearSelection();
  58. });
  59. editablePath = current;
  60. }
  61. public void SelectPoint(int index, bool select)
  62. {
  63. m_Controller.SelectPoint(index, select);
  64. }
  65. public void CreatePoint(int index, Vector3 position)
  66. {
  67. m_Controller.CreatePoint(index, position);
  68. }
  69. public void RemoveSelectedPoints()
  70. {
  71. var current = editablePath;
  72. ForEach((s) =>
  73. {
  74. editablePath = s;
  75. m_Controller.RemoveSelectedPoints();
  76. });
  77. editablePath = current;
  78. }
  79. public void MoveSelectedPoints(Vector3 delta)
  80. {
  81. var current = editablePath;
  82. ForEach((s) =>
  83. {
  84. editablePath = s;
  85. m_Controller.MoveSelectedPoints(delta);
  86. });
  87. editablePath = current;
  88. }
  89. public void MoveEdge(int index, Vector3 delta)
  90. {
  91. m_Controller.MoveEdge(index, delta);
  92. }
  93. public void SetLeftTangent(int index, Vector3 position, bool setToLinear, bool mirror, Vector3 cachedRightTangent)
  94. {
  95. m_Controller.SetLeftTangent(index, position, setToLinear, mirror, cachedRightTangent);
  96. }
  97. public void SetRightTangent(int index, Vector3 position, bool setToLinear, bool mirror, Vector3 cachedLeftTangent)
  98. {
  99. m_Controller.SetRightTangent(index, position, setToLinear, mirror, cachedLeftTangent);
  100. }
  101. public void ClearClosestPath()
  102. {
  103. m_ClosestDistance = float.MaxValue;
  104. closestEditablePath = null;
  105. }
  106. public void AddClosestPath(float distance)
  107. {
  108. if (distance <= m_ClosestDistance)
  109. {
  110. m_ClosestDistance = distance;
  111. closestEditablePath = editablePath;
  112. }
  113. }
  114. private void ForEach(Action<IEditablePath> action)
  115. {
  116. foreach(var path in m_Paths)
  117. {
  118. if (path == null)
  119. continue;
  120. action(path);
  121. }
  122. }
  123. }
  124. }