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.

PathComponentEditor.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using UnityEditor.EditorTools;
  8. namespace UnityEditor.U2D.Path
  9. {
  10. public abstract class PathComponentEditor<T> : Editor where T : ScriptablePath
  11. {
  12. private static class Contents
  13. {
  14. public static readonly GUIContent snappingLabel = new GUIContent("Snapping", "Snap points using the snap settings");
  15. }
  16. private Editor m_CachedEditor = null;
  17. // Returns true on Changed.
  18. protected bool DoEditButton<U>(GUIContent icon, string label) where U : PathEditorTool<T>
  19. {
  20. const float kButtonWidth = 33;
  21. const float kButtonHeight = 23;
  22. const float k_SpaceBetweenLabelAndButton = 5;
  23. var buttonStyle = new GUIStyle("EditModeSingleButton");
  24. var rect = EditorGUILayout.GetControlRect(true, kButtonHeight, buttonStyle);
  25. var buttonRect = new Rect(rect.xMin + EditorGUIUtility.labelWidth, rect.yMin, kButtonWidth, kButtonHeight);
  26. var labelContent = new GUIContent(label);
  27. var labelSize = GUI.skin.label.CalcSize(labelContent);
  28. var labelRect = new Rect(
  29. buttonRect.xMax + k_SpaceBetweenLabelAndButton,
  30. rect.yMin + (rect.height - labelSize.y) * .5f,
  31. labelSize.x,
  32. rect.height);
  33. bool hasChanged = false;
  34. using (new EditorGUI.DisabledGroupScope(!EditorToolManager.IsAvailable<U>()))
  35. {
  36. using (var check = new EditorGUI.ChangeCheckScope())
  37. {
  38. var isActive = GUI.Toggle(buttonRect, EditorToolManager.IsActiveTool<U>(), icon, buttonStyle);
  39. GUI.Label(labelRect, label);
  40. if (check.changed)
  41. {
  42. if (isActive)
  43. ToolManager.SetActiveTool<U>();
  44. else
  45. ToolManager.RestorePreviousTool();
  46. hasChanged = true;
  47. }
  48. }
  49. }
  50. return hasChanged;
  51. }
  52. protected void DoPathInspector<U>() where U : PathEditorTool<T>
  53. {
  54. if (EditorToolManager.IsActiveTool<U>() && EditorToolManager.IsAvailable<U>())
  55. {
  56. var paths = EditorToolManager.GetEditorTool<U>().paths;
  57. CreateCachedEditor(paths, null, ref m_CachedEditor);
  58. if (m_CachedEditor == null) //Needed to avoid a nullref on exiting playmode
  59. return;
  60. using (var check = new EditorGUI.ChangeCheckScope())
  61. {
  62. m_CachedEditor.OnInspectorGUI();
  63. if (check.changed)
  64. EditorToolManager.GetEditorTool<U>().SetShapes();
  65. }
  66. }
  67. }
  68. protected void DoSnappingInspector<U>() where U : PathEditorTool<T>
  69. {
  70. if (EditorToolManager.IsActiveTool<U>() && EditorToolManager.IsAvailable<U>())
  71. {
  72. var tool = EditorToolManager.GetEditorTool<U>();
  73. tool.enableSnapping = EditorGUILayout.Toggle(Contents.snappingLabel, tool.enableSnapping);
  74. }
  75. }
  76. protected void DoOpenEndedInspector<U>(SerializedProperty isOpenEndedProperty) where U : PathEditorTool<T>
  77. {
  78. serializedObject.Update();
  79. using (var check = new EditorGUI.ChangeCheckScope())
  80. {
  81. EditorGUILayout.PropertyField(isOpenEndedProperty);
  82. if (check.changed)
  83. {
  84. if (EditorToolManager.IsActiveTool<U>() && EditorToolManager.IsAvailable<U>())
  85. {
  86. var paths = EditorToolManager.GetEditorTool<U>().paths;
  87. foreach (var path in paths)
  88. {
  89. path.undoObject.RegisterUndo("Set Open Ended");
  90. path.isOpenEnded = isOpenEndedProperty.boolValue;
  91. }
  92. }
  93. }
  94. }
  95. serializedObject.ApplyModifiedProperties();
  96. }
  97. }
  98. }