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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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>Callback for painting the GUI for the GridBrush in the Scene view.</summary>
  35. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  36. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  37. /// <param name="position">Current selected location of the brush.</param>
  38. /// <param name="tool">Current GridBrushBase::ref::Tool selected.</param>
  39. /// <param name="executing">Whether is brush is being used.</param>
  40. /// <remarks>Implement this for any special behaviours when the GridBrush is used on the Scene View.</remarks>
  41. public virtual void OnPaintSceneGUI(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  42. {
  43. OnPaintSceneGUIInternal(gridLayout, brushTarget, position, tool, executing);
  44. }
  45. /// <summary>Callback for painting the inspector GUI for the GridBrush in the tilemap palette.</summary>
  46. /// <remarks>Implement this to have a custom editor in the tilemap palette for the GridBrush.</remarks>
  47. public virtual void OnPaintInspectorGUI()
  48. {
  49. OnInspectorGUI();
  50. }
  51. /// <summary>Callback for drawing the Inspector GUI when there is an active GridSelection made in a GridLayout.</summary>
  52. /// <remarks>Override this to show custom Inspector GUI for the current selection.</remarks>
  53. public virtual void OnSelectionInspectorGUI() {}
  54. /// <summary>Callback for painting custom gizmos when there is an active GridSelection made in a GridLayout.</summary>
  55. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  56. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  57. /// <remarks>Override this to show custom gizmos for the current selection.</remarks>
  58. public virtual void OnSelectionSceneGUI(GridLayout gridLayout, GameObject brushTarget) {}
  59. /// <summary>
  60. /// Callback for painting custom gizmos for the GridBrush for the brush target
  61. /// </summary>
  62. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  63. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  64. /// <remarks>Override this to show custom gizmos for the brush target.</remarks>
  65. public virtual void OnSceneGUI(GridLayout gridLayout, GameObject brushTarget) {}
  66. /// <summary>Callback when the mouse cursor leaves a paintable region.</summary>
  67. /// <remarks>Implement this for any custom behaviour when the mouse cursor leaves a paintable region.</remarks>
  68. public virtual void OnMouseLeave() {}
  69. /// <summary>Callback when the mouse cursor enters a paintable region.</summary>
  70. /// <remarks>Implement this for any custom behaviour when the mouse cursor enters a paintable region.</remarks>
  71. public virtual void OnMouseEnter() {}
  72. /// <summary>Callback when a GridBrushBase.Tool is activated.</summary>
  73. /// <param name="tool">Tool that is activated.</param>
  74. /// <remarks>Implement this for any special behaviours when a Tool is activated.</remarks>
  75. public virtual void OnToolActivated(GridBrushBase.Tool tool) {}
  76. /// <summary>Callback when a GridBrushBase.Tool is deactivated.</summary>
  77. /// <param name="tool">Tool that is deactivated.</param>
  78. /// <remarks>Implement this for any special behaviours when a Tool is deactivated.</remarks>
  79. public virtual void OnToolDeactivated(GridBrushBase.Tool tool) {}
  80. /// <summary>Callback for registering an Undo action before the GridBrushBase does the current GridBrushBase::ref::Tool action.</summary>
  81. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  82. /// <param name="tool">Current GridBrushBase::ref::Tool selected.</param>
  83. /// <remarks>Implement this for any special Undo behaviours when a brush is used.</remarks>
  84. public virtual void RegisterUndo(GameObject brushTarget, GridBrushBase.Tool tool) {}
  85. /// <summary>Returns all valid targets that the brush can edit.</summary>
  86. public virtual GameObject[] validTargets { get { return null; } }
  87. internal void OnEditStart(GridLayout gridLayout, GameObject brushTarget)
  88. {
  89. SetBufferSyncTile(brushTarget, true);
  90. }
  91. internal void OnEditEnd(GridLayout gridLayout, GameObject brushTarget)
  92. {
  93. SetBufferSyncTile(brushTarget, false);
  94. }
  95. private void SetBufferSyncTile(GameObject brushTarget, bool active)
  96. {
  97. if (brushTarget == null || !Tilemap.HasSyncTileCallback())
  98. return;
  99. var tilemaps = brushTarget.GetComponentsInChildren<Tilemap>();
  100. foreach (var tilemap in tilemaps)
  101. {
  102. tilemap.bufferSyncTile = active;
  103. }
  104. }
  105. internal static void OnSceneGUIInternal(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  106. {
  107. if (Event.current.type != EventType.Repaint)
  108. return;
  109. if (tool == GridBrushBase.Tool.Select
  110. || tool == GridBrushBase.Tool.Move
  111. || GridSelectionTool.IsActive())
  112. {
  113. if (GridSelection.active && !executing)
  114. {
  115. Color color = Styles.activeColor;
  116. GridEditorUtility.DrawGridMarquee(gridLayout, position, color);
  117. }
  118. }
  119. }
  120. internal static void OnPaintSceneGUIInternal(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  121. {
  122. if (Event.current.type != EventType.Repaint)
  123. return;
  124. Color color = Color.white;
  125. if (tool == GridBrushBase.Tool.Pick && executing)
  126. color = Color.cyan;
  127. if (tool == GridBrushBase.Tool.Paint && executing)
  128. color = Color.yellow;
  129. if (tool == GridBrushBase.Tool.Select
  130. || tool == GridBrushBase.Tool.Move
  131. || GridSelectionTool.IsActive())
  132. {
  133. if (executing)
  134. color = Styles.executingColor;
  135. else if (GridSelection.active)
  136. color = Styles.activeColor;
  137. }
  138. if (brushTarget != null)
  139. {
  140. var targetLayout = brushTarget.GetComponent<GridLayout>();
  141. if (targetLayout != null)
  142. gridLayout = targetLayout;
  143. }
  144. if (position.zMin != 0)
  145. {
  146. var zeroBounds = position;
  147. zeroBounds.z = 0;
  148. GridEditorUtility.DrawGridMarquee(gridLayout, zeroBounds, color);
  149. color = Color.blue;
  150. }
  151. GridEditorUtility.DrawGridMarquee(gridLayout, position, color);
  152. }
  153. }
  154. }