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