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.

GridBrushEditorBase.cs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEditor.EditorTools;
  2. using UnityEngine;
  3. using UnityEngine.Scripting.APIUpdating;
  4. using UnityEngine.Tilemaps;
  5. namespace UnityEditor.Tilemaps
  6. {
  7. /// <summary>Base class for Grid Brush Editor.</summary>
  8. [MovedFrom(true, "UnityEditor", "UnityEditor")]
  9. [CustomEditor(typeof(GridBrushBase))]
  10. public class GridBrushEditorBase : Editor
  11. {
  12. private static class Styles
  13. {
  14. public static readonly Color activeColor = new Color(1f, .5f, 0f);
  15. public static readonly Color executingColor = new Color(1f, .75f, 0.25f);
  16. }
  17. /// <summary>Returns a tooltip describing the usage of the brush and other helpful information.</summary>
  18. public virtual string tooltip
  19. {
  20. get { return null; }
  21. }
  22. /// <summary>Returns a texture used as an icon to identify this brush.</summary>
  23. public virtual Texture2D icon
  24. {
  25. get { return null; }
  26. }
  27. /// <summary>Checks if the Brush allows the changing of Z Position.</summary>
  28. /// <returns>Whether the Brush can change Z Position.</returns>
  29. public virtual bool canChangeZPosition
  30. {
  31. get { return true; }
  32. set {}
  33. }
  34. /// <summary>
  35. /// Whether the Brush is in a state that should be saved for selection.
  36. /// </summary>
  37. public virtual bool shouldSaveBrushForSelection => true;
  38. /// <summary>Callback for painting the GUI for the GridBrush in the Scene view.</summary>
  39. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  40. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  41. /// <param name="position">Current selected location of the brush.</param>
  42. /// <param name="tool">Current GridBrushBase::ref::Tool selected.</param>
  43. /// <param name="executing">Whether is brush is being used.</param>
  44. /// <remarks>Implement this for any special behaviours when the GridBrush is used on the Scene View.</remarks>
  45. public virtual void OnPaintSceneGUI(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  46. {
  47. OnPaintSceneGUIInternal(gridLayout, brushTarget, position, tool, executing);
  48. }
  49. /// <summary>Callback for painting the inspector GUI for the GridBrush in the tilemap palette.</summary>
  50. /// <remarks>Implement this to have a custom editor in the tilemap palette for the GridBrush.</remarks>
  51. public virtual void OnPaintInspectorGUI()
  52. {
  53. OnInspectorGUI();
  54. }
  55. /// <summary>Callback for drawing the Inspector GUI when there is an active GridSelection made in a GridLayout.</summary>
  56. /// <remarks>Override this to show custom Inspector GUI for the current selection.</remarks>
  57. public virtual void OnSelectionInspectorGUI() {}
  58. /// <summary>Callback for painting custom gizmos when there is an active GridSelection made in a GridLayout.</summary>
  59. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  60. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  61. /// <remarks>Override this to show custom gizmos for the current selection.</remarks>
  62. public virtual void OnSelectionSceneGUI(GridLayout gridLayout, GameObject brushTarget) {}
  63. /// <summary>
  64. /// Callback for painting custom gizmos for the GridBrush for the brush target
  65. /// </summary>
  66. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  67. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  68. /// <remarks>Override this to show custom gizmos for the brush target.</remarks>
  69. public virtual void OnSceneGUI(GridLayout gridLayout, GameObject brushTarget) {}
  70. /// <summary>Callback when the mouse cursor leaves a paintable region.</summary>
  71. /// <remarks>Implement this for any custom behaviour when the mouse cursor leaves a paintable region.</remarks>
  72. public virtual void OnMouseLeave() {}
  73. /// <summary>Callback when the mouse cursor enters a paintable region.</summary>
  74. /// <remarks>Implement this for any custom behaviour when the mouse cursor enters a paintable region.</remarks>
  75. public virtual void OnMouseEnter() {}
  76. /// <summary>Callback when a GridBrushBase.Tool is activated.</summary>
  77. /// <param name="tool">Tool that is activated.</param>
  78. /// <remarks>Implement this for any special behaviours when a Tool is activated.</remarks>
  79. public virtual void OnToolActivated(GridBrushBase.Tool tool) {}
  80. /// <summary>Callback when a GridBrushBase.Tool is deactivated.</summary>
  81. /// <param name="tool">Tool that is deactivated.</param>
  82. /// <remarks>Implement this for any special behaviours when a Tool is deactivated.</remarks>
  83. public virtual void OnToolDeactivated(GridBrushBase.Tool tool) {}
  84. /// <summary>Callback for registering an Undo action before the GridBrushBase does the current GridBrushBase::ref::Tool action.</summary>
  85. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  86. /// <param name="tool">Current GridBrushBase::ref::Tool selected.</param>
  87. /// <remarks>Implement this for any special Undo behaviours when a brush is used.</remarks>
  88. public virtual void RegisterUndo(GameObject brushTarget, GridBrushBase.Tool tool) {}
  89. /// <summary>Returns all valid targets that the brush can edit.</summary>
  90. public virtual GameObject[] validTargets { get { return null; } }
  91. internal void OnEditStart(GridLayout gridLayout, GameObject brushTarget)
  92. {
  93. SetBufferSyncTile(brushTarget, true);
  94. }
  95. internal void OnEditEnd(GridLayout gridLayout, GameObject brushTarget)
  96. {
  97. SetBufferSyncTile(brushTarget, false);
  98. }
  99. private void SetBufferSyncTile(GameObject brushTarget, bool active)
  100. {
  101. if (brushTarget == null || !Tilemap.HasSyncTileCallback())
  102. return;
  103. var tilemaps = brushTarget.GetComponentsInChildren<Tilemap>();
  104. foreach (var tilemap in tilemaps)
  105. {
  106. tilemap.bufferSyncTile = active;
  107. }
  108. }
  109. internal static void OnSceneGUIInternal(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  110. {
  111. if (Event.current.type != EventType.Repaint)
  112. return;
  113. if (tool == GridBrushBase.Tool.Select
  114. || tool == GridBrushBase.Tool.Move
  115. || GridSelectionTool.IsActive())
  116. {
  117. if (GridSelection.active && !executing)
  118. {
  119. Color color = Styles.activeColor;
  120. GridEditorUtility.DrawGridMarquee(gridLayout, position, color);
  121. }
  122. }
  123. }
  124. internal static void OnPaintSceneGUIInternal(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  125. {
  126. if (Event.current.type != EventType.Repaint)
  127. return;
  128. Color color = Color.white;
  129. if (tool == GridBrushBase.Tool.Pick && executing)
  130. color = Color.cyan;
  131. if (tool == GridBrushBase.Tool.Paint && executing)
  132. color = Color.yellow;
  133. if (tool == GridBrushBase.Tool.Select
  134. || tool == GridBrushBase.Tool.Move
  135. || GridSelectionTool.IsActive())
  136. {
  137. if (executing)
  138. color = Styles.executingColor;
  139. else if (GridSelection.active)
  140. color = Styles.activeColor;
  141. }
  142. if (brushTarget != null)
  143. {
  144. var targetLayout = brushTarget.GetComponent<GridLayout>();
  145. if (targetLayout != null)
  146. gridLayout = targetLayout;
  147. }
  148. if (position.zMin != 0)
  149. {
  150. var zeroBounds = position;
  151. zeroBounds.z = 0;
  152. GridEditorUtility.DrawGridMarquee(gridLayout, zeroBounds, color);
  153. color = Color.blue;
  154. }
  155. GridEditorUtility.DrawGridMarquee(gridLayout, position, color);
  156. }
  157. }
  158. }