No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PathEditor.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using UnityEditor.U2D.Common.Path.GUIFramework;
  7. namespace UnityEditor.U2D.Common.Path
  8. {
  9. internal class PathEditor
  10. {
  11. const float kSnappingDistance = 15f;
  12. const string kDeleteCommandName = "Delete";
  13. const string kSoftDeleteCommandName = "SoftDelete";
  14. public IEditablePathController controller { get; set; }
  15. public bool linearTangentIsZero { get; set; }
  16. private IDrawer m_Drawer = new Drawer();
  17. private IDrawer m_DrawerOverride;
  18. private GUISystem m_GUISystem;
  19. public IDrawer drawerOverride { get; set; }
  20. private IDrawer drawer
  21. {
  22. get
  23. {
  24. if (drawerOverride != null)
  25. return drawerOverride;
  26. return m_Drawer;
  27. }
  28. }
  29. public PathEditor() : this(new GUISystem(new GUIState())) { }
  30. public PathEditor(GUISystem guiSystem)
  31. {
  32. m_GUISystem = guiSystem;
  33. var m_PointControl = new GenericControl("Point")
  34. {
  35. count = GetPointCount,
  36. distance = (guiState, i) =>
  37. {
  38. var position = GetPoint(i).position;
  39. return guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f);
  40. },
  41. position = (i) => { return GetPoint(i).position; },
  42. forward = (i) => { return GetForward(); },
  43. up = (i) => { return GetUp(); },
  44. right = (i) => { return GetRight(); },
  45. onRepaint = DrawPoint
  46. };
  47. var m_EdgeControl = new GenericControl("Edge")
  48. {
  49. count = GetEdgeCount,
  50. distance = DistanceToEdge,
  51. position = (i) => { return GetPoint(i).position; },
  52. forward = (i) => { return GetForward(); },
  53. up = (i) => { return GetUp(); },
  54. right = (i) => { return GetRight(); },
  55. onRepaint = DrawEdge
  56. };
  57. m_EdgeControl.onEndLayout = (guiState) => { controller.AddClosestPath(m_EdgeControl.layoutData.distance); };
  58. var m_LeftTangentControl = new GenericControl("LeftTangent")
  59. {
  60. count = () =>
  61. {
  62. if (GetShapeType() != ShapeType.Spline)
  63. return 0;
  64. return GetPointCount();
  65. },
  66. distance = (guiState, i) =>
  67. {
  68. if (linearTangentIsZero && GetPoint(i).tangentMode == TangentMode.Linear)
  69. return float.MaxValue;
  70. if (!IsSelected(i) || IsOpenEnded() && i == 0)
  71. return float.MaxValue;
  72. var position = GetLeftTangent(i);
  73. return guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f);
  74. },
  75. position = (i) => { return GetLeftTangent(i); },
  76. forward = (i) => { return GetForward(); },
  77. up = (i) => { return GetUp(); },
  78. right = (i) => { return GetRight(); },
  79. onRepaint = (guiState, control, i) =>
  80. {
  81. if (!IsSelected(i) || IsOpenEnded() && i == 0)
  82. return;
  83. var point = GetPoint(i);
  84. if (linearTangentIsZero && point.tangentMode == TangentMode.Linear)
  85. return;
  86. var position = point.position;
  87. var leftTangent = GetLeftTangent(i);
  88. drawer.DrawTangent(position, leftTangent, HandleSettings.tangentColor);
  89. }
  90. };
  91. var m_RightTangentControl = new GenericControl("RightTangent")
  92. {
  93. count = () =>
  94. {
  95. if (GetShapeType() != ShapeType.Spline)
  96. return 0;
  97. return GetPointCount();
  98. },
  99. distance = (guiState, i) =>
  100. {
  101. if (linearTangentIsZero && GetPoint(i).tangentMode == TangentMode.Linear)
  102. return float.MaxValue;
  103. if (!IsSelected(i) || IsOpenEnded() && i == GetPointCount()-1)
  104. return float.MaxValue;
  105. var position = GetRightTangent(i);
  106. return guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f);
  107. },
  108. position = (i) => { return GetRightTangent(i); },
  109. forward = (i) => { return GetForward(); },
  110. up = (i) => { return GetUp(); },
  111. right = (i) => { return GetRight(); },
  112. onRepaint = (guiState, control, i) =>
  113. {
  114. if (!IsSelected(i) || IsOpenEnded() && i == GetPointCount()-1)
  115. return;
  116. var point = GetPoint(i);
  117. if (linearTangentIsZero && point.tangentMode == TangentMode.Linear)
  118. return;
  119. var position = point.position;
  120. var rightTangent = GetRightTangent(i);
  121. drawer.DrawTangent(position, rightTangent, HandleSettings.tangentColor);
  122. }
  123. };
  124. var m_CreatePointAction = new CreatePointAction(m_PointControl, m_EdgeControl)
  125. {
  126. enable = (guiState, action) => !IsAltDown(guiState) && !guiState.isActionKeyDown && controller.closestEditablePath == controller.editablePath,
  127. enableRepaint = (guiState, action) => EnableCreatePointRepaint(guiState, m_PointControl, m_LeftTangentControl, m_RightTangentControl),
  128. repaintOnMouseMove = (guiState, action) => true,
  129. guiToWorld = GUIToWorld,
  130. onCreatePoint = (index, position) =>
  131. {
  132. controller.RegisterUndo("Create Point");
  133. controller.CreatePoint(index, position);
  134. },
  135. onPreRepaint = (guiState, action) =>
  136. {
  137. if (GetPointCount() > 0)
  138. {
  139. var position = ClosestPointInEdge(guiState, guiState.mousePosition, m_EdgeControl.layoutData.index);
  140. drawer.DrawCreatePointPreview(position, ControlPointSettings.controlPointColor);
  141. }
  142. }
  143. };
  144. Action<IGUIState> removePoints = (guiState) =>
  145. {
  146. controller.RegisterUndo("Remove Point");
  147. controller.RemoveSelectedPoints();
  148. guiState.changed = true;
  149. };
  150. var m_RemovePointAction1 = new CommandAction(kDeleteCommandName)
  151. {
  152. enable = (guiState, action) => { return GetSelectedPointCount() > 0; },
  153. onCommand = removePoints
  154. };
  155. var m_RemovePointAction2 = new CommandAction(kSoftDeleteCommandName)
  156. {
  157. enable = (guiState, action) => { return GetSelectedPointCount() > 0; },
  158. onCommand = removePoints
  159. };
  160. var dragged = false;
  161. var m_MovePointAction = new SliderAction(m_PointControl)
  162. {
  163. enable = (guiState, action) => !IsAltDown(guiState),
  164. onClick = (guiState, control) =>
  165. {
  166. dragged = false;
  167. var index = control.layoutData.index;
  168. if (!IsSelected(index))
  169. {
  170. controller.RegisterUndo("Selection");
  171. if (!guiState.isActionKeyDown)
  172. controller.ClearSelection();
  173. controller.SelectPoint(index, true);
  174. guiState.changed = true;
  175. }
  176. },
  177. onSliderChanged = (guiState, control, position) =>
  178. {
  179. var index = control.hotLayoutData.index;
  180. var delta = SnapIfNeeded(position) - GetPoint(index).position;
  181. if (!dragged)
  182. {
  183. controller.RegisterUndo("Move Point");
  184. dragged = true;
  185. }
  186. controller.MoveSelectedPoints(delta);
  187. }
  188. };
  189. var m_MoveEdgeAction = new SliderAction(m_EdgeControl)
  190. {
  191. enable = (guiState, action) => !IsAltDown(guiState) && guiState.isActionKeyDown,
  192. onSliderBegin = (guiState, control, position) =>
  193. {
  194. dragged = false;
  195. },
  196. onSliderChanged = (guiState, control, position) =>
  197. {
  198. var index = control.hotLayoutData.index;
  199. var delta = position - GetPoint(index).position;
  200. if (!dragged)
  201. {
  202. controller.RegisterUndo("Move Edge");
  203. dragged = true;
  204. }
  205. controller.MoveEdge(index, delta);
  206. }
  207. };
  208. var cachedRightTangent = Vector3.zero;
  209. var cachedLeftTangent = Vector3.zero;
  210. var cachedTangentMode = TangentMode.Linear;
  211. var m_MoveLeftTangentAction = new SliderAction(m_LeftTangentControl)
  212. {
  213. enable = (guiState, action) => !IsAltDown(guiState),
  214. onSliderBegin = (guiState, control, position) =>
  215. {
  216. dragged = false;
  217. var point = GetPoint(control.hotLayoutData.index);
  218. cachedRightTangent = point.rightTangent;
  219. cachedTangentMode = point.tangentMode;
  220. },
  221. onSliderChanged = (guiState, control, position) =>
  222. {
  223. var index = control.hotLayoutData.index;
  224. var setToLinear = m_PointControl.distance(guiState, index) <= DefaultControl.kPickDistance;
  225. if (!dragged)
  226. {
  227. controller.RegisterUndo("Move Tangent");
  228. dragged = true;
  229. }
  230. position = SnapIfNeeded(position);
  231. controller.SetLeftTangent(index, position, setToLinear, guiState.isShiftDown, cachedRightTangent, cachedTangentMode);
  232. }
  233. };
  234. var m_MoveRightTangentAction = new SliderAction(m_RightTangentControl)
  235. {
  236. enable = (guiState, action) => !IsAltDown(guiState),
  237. onSliderBegin = (guiState, control, position) =>
  238. {
  239. dragged = false;
  240. var point = GetPoint(control.hotLayoutData.index);
  241. cachedLeftTangent = point.leftTangent;
  242. cachedTangentMode = point.tangentMode;
  243. },
  244. onSliderChanged = (guiState, control, position) =>
  245. {
  246. var index = control.hotLayoutData.index;
  247. var setToLinear = m_PointControl.distance(guiState, index) <= DefaultControl.kPickDistance;
  248. if (!dragged)
  249. {
  250. controller.RegisterUndo("Move Tangent");
  251. dragged = true;
  252. }
  253. position = SnapIfNeeded(position);
  254. controller.SetRightTangent(index, position, setToLinear, guiState.isShiftDown, cachedLeftTangent, cachedTangentMode);
  255. }
  256. };
  257. m_GUISystem.AddControl(m_EdgeControl);
  258. m_GUISystem.AddControl(m_PointControl);
  259. m_GUISystem.AddControl(m_LeftTangentControl);
  260. m_GUISystem.AddControl(m_RightTangentControl);
  261. m_GUISystem.AddAction(m_CreatePointAction);
  262. m_GUISystem.AddAction(m_RemovePointAction1);
  263. m_GUISystem.AddAction(m_RemovePointAction2);
  264. m_GUISystem.AddAction(m_MovePointAction);
  265. m_GUISystem.AddAction(m_MoveEdgeAction);
  266. m_GUISystem.AddAction(m_MoveLeftTangentAction);
  267. m_GUISystem.AddAction(m_MoveRightTangentAction);
  268. }
  269. public void OnGUI()
  270. {
  271. m_GUISystem.OnGUI();
  272. }
  273. private bool IsAltDown(IGUIState guiState)
  274. {
  275. return guiState.hotControl == 0 && guiState.isAltDown;
  276. }
  277. private ControlPoint GetPoint(int index)
  278. {
  279. return controller.editablePath.GetPoint(index);
  280. }
  281. private int GetPointCount()
  282. {
  283. return controller.editablePath.pointCount;
  284. }
  285. private int GetEdgeCount()
  286. {
  287. if (controller.editablePath.isOpenEnded)
  288. return controller.editablePath.pointCount - 1;
  289. return controller.editablePath.pointCount;
  290. }
  291. private int GetSelectedPointCount()
  292. {
  293. return controller.editablePath.selection.Count;
  294. }
  295. private bool IsSelected(int index)
  296. {
  297. return controller.editablePath.selection.Contains(index);
  298. }
  299. private Vector3 GetForward()
  300. {
  301. return controller.editablePath.forward;
  302. }
  303. private Vector3 GetUp()
  304. {
  305. return controller.editablePath.up;
  306. }
  307. private Vector3 GetRight()
  308. {
  309. return controller.editablePath.right;
  310. }
  311. private Matrix4x4 GetLocalToWorldMatrix()
  312. {
  313. return controller.editablePath.localToWorldMatrix;
  314. }
  315. private ShapeType GetShapeType()
  316. {
  317. return controller.editablePath.shapeType;
  318. }
  319. private bool IsOpenEnded()
  320. {
  321. return controller.editablePath.isOpenEnded;
  322. }
  323. private Vector3 GetLeftTangent(int index)
  324. {
  325. if (linearTangentIsZero)
  326. return GetPoint(index).leftTangent;
  327. return controller.editablePath.CalculateLeftTangent(index);
  328. }
  329. private Vector3 GetRightTangent(int index)
  330. {
  331. if (linearTangentIsZero)
  332. return GetPoint(index).rightTangent;
  333. return controller.editablePath.CalculateRightTangent(index);
  334. }
  335. private int NextIndex(int index)
  336. {
  337. return EditablePathUtility.Mod(index + 1, GetPointCount());
  338. }
  339. private ControlPoint NextControlPoint(int index)
  340. {
  341. return GetPoint(NextIndex(index));
  342. }
  343. private int PrevIndex(int index)
  344. {
  345. return EditablePathUtility.Mod(index - 1, GetPointCount());
  346. }
  347. private ControlPoint PrevControlPoint(int index)
  348. {
  349. return GetPoint(PrevIndex(index));
  350. }
  351. private Vector3 ClosestPointInEdge(IGUIState guiState, Vector2 mousePosition, int index)
  352. {
  353. if (GetShapeType() == ShapeType.Polygon)
  354. {
  355. var p0 = GetPoint(index).position;
  356. var p1 = NextControlPoint(index).position;
  357. var mouseWorldPosition = GUIToWorld(guiState, mousePosition);
  358. var dir1 = (mouseWorldPosition - p0);
  359. var dir2 = (p1 - p0);
  360. return Mathf.Clamp01(Vector3.Dot(dir1, dir2.normalized) / dir2.magnitude) * dir2 + p0;
  361. }
  362. else if (GetShapeType() == ShapeType.Spline)
  363. {
  364. var nextIndex = NextIndex(index);
  365. float t;
  366. return BezierUtility.ClosestPointOnCurve(
  367. GUIToWorld(guiState, mousePosition),
  368. GetPoint(index).position,
  369. GetPoint(nextIndex).position,
  370. GetRightTangent(index),
  371. GetLeftTangent(nextIndex),
  372. out t);
  373. }
  374. return Vector3.zero;
  375. }
  376. private float DistanceToEdge(IGUIState guiState, int index)
  377. {
  378. if (GetShapeType() == ShapeType.Polygon)
  379. {
  380. return guiState.DistanceToSegment(GetPoint(index).position, NextControlPoint(index).position);
  381. }
  382. else if (GetShapeType() == ShapeType.Spline)
  383. {
  384. var closestPoint = ClosestPointInEdge(guiState, guiState.mousePosition, index);
  385. var closestPoint2 = HandleUtility.WorldToGUIPoint(closestPoint);
  386. return (closestPoint2 - guiState.mousePosition).magnitude;
  387. }
  388. return float.MaxValue;
  389. }
  390. private Vector3 GUIToWorld(IGUIState guiState, Vector2 position)
  391. {
  392. return guiState.GUIToWorld(position, GetForward(), GetLocalToWorldMatrix().MultiplyPoint3x4(Vector3.zero));
  393. }
  394. private void DrawPoint(IGUIState guiState, Control control, int index)
  395. {
  396. var position = GetPoint(index).position;
  397. var isTangent = (control.name == "LeftTangent" || control.name == "RightTangent");
  398. if (guiState.hotControl == control.actionID && control.hotLayoutData.index == index || IsSelected(index))
  399. drawer.DrawPointSelected(position, ControlPointSettings.controlPointSelectedColor);
  400. else if (guiState.hotControl == 0 && guiState.nearestControl == control.ID && !IsAltDown(guiState) && control.layoutData.index == index)
  401. drawer.DrawPointHovered(position, ControlPointSettings.controlPointSelectedColor);
  402. else
  403. drawer.DrawPoint(position, isTangent ? HandleSettings.tangentColor : ControlPointSettings.controlPointColor);
  404. }
  405. private void DrawEdge(IGUIState guiState, Control control, int index)
  406. {
  407. if (GetShapeType() == ShapeType.Polygon)
  408. {
  409. var nextIndex = NextIndex(index);
  410. var color = HandleSettings.splineColor;
  411. if(guiState.nearestControl == control.ID && control.layoutData.index == index && guiState.hotControl == 0 && !IsAltDown(guiState))
  412. color = HandleSettings.splineHoveredColor;
  413. drawer.DrawLine(GetPoint(index).position, GetPoint(nextIndex).position, 5f, color);
  414. }
  415. else if (GetShapeType() == ShapeType.Spline)
  416. {
  417. var nextIndex = NextIndex(index);
  418. var color = HandleSettings.splineColor;
  419. if(guiState.nearestControl == control.ID && control.layoutData.index == index && guiState.hotControl == 0 && !IsAltDown(guiState))
  420. color = HandleSettings.splineHoveredColor;
  421. drawer.DrawBezier(
  422. GetPoint(index).position,
  423. GetRightTangent(index),
  424. GetLeftTangent(nextIndex),
  425. GetPoint(nextIndex).position,
  426. 5f,
  427. color);
  428. }
  429. }
  430. private bool EnableCreatePointRepaint(IGUIState guiState, Control pointControl, Control leftTangentControl, Control rightTangentControl)
  431. {
  432. return guiState.nearestControl != pointControl.ID &&
  433. guiState.hotControl == 0 &&
  434. (guiState.nearestControl != leftTangentControl.ID) &&
  435. (guiState.nearestControl != rightTangentControl.ID);
  436. }
  437. private Vector3 SnapIfNeeded(Vector3 position)
  438. {
  439. if (!controller.enableSnapping || controller.snapping == null)
  440. return position;
  441. var guiPosition = HandleUtility.WorldToGUIPoint(position);
  442. var snappedGuiPosition = HandleUtility.WorldToGUIPoint(controller.snapping.Snap(position));
  443. var sqrDistance = (guiPosition - snappedGuiPosition).sqrMagnitude;
  444. if (sqrDistance < kSnappingDistance * kSnappingDistance)
  445. position = controller.snapping.Snap(position);
  446. return position;
  447. }
  448. }
  449. }