A game about forced loneliness, made by TACStudios
at master 237 lines 8.8 kB view raw
1using UnityEngine; 2 3namespace UnityEditor.U2D.Sprites 4{ 5 internal partial class SpriteFrameModule : SpriteFrameModuleBase 6 { 7 private static class SpriteFrameModuleStyles 8 { 9 public static readonly GUIContent sliceButtonLabel = EditorGUIUtility.TrTextContent("Slice"); 10 public static readonly GUIContent trimButtonLabel = EditorGUIUtility.TrTextContent("Trim", "Trims selected rectangle (T)"); 11 } 12 13 // overrides for SpriteFrameModuleBase 14 public override void DoMainGUI() 15 { 16 // Do nothing when extension is activated. 17 if (m_CurrentMode != null) 18 { 19 m_CurrentMode.DoMainGUI(); 20 return; 21 } 22 base.DoMainGUI(); 23 DrawSpriteRectGizmos(); 24 DrawPotentialSpriteRectGizmos(); 25 26 if (!spriteEditor.editingDisabled) 27 { 28 HandleGizmoMode(); 29 30 if (containsMultipleSprites) 31 HandleRectCornerScalingHandles(); 32 33 HandleBorderCornerScalingHandles(); 34 HandleBorderSidePointScalingSliders(); 35 36 if (containsMultipleSprites) 37 HandleRectSideScalingHandles(); 38 39 HandleBorderSideScalingHandles(); 40 HandlePivotHandle(); 41 42 if (containsMultipleSprites) 43 HandleDragging(); 44 45 spriteEditor.HandleSpriteSelection(); 46 47 if (containsMultipleSprites) 48 { 49 HandleCreate(); 50 HandleDelete(); 51 HandleDuplicate(); 52 } 53 spriteEditor.spriteRects = m_RectsCache.GetSpriteRects(); 54 } 55 } 56 57 private void DrawPotentialSpriteRectGizmos() 58 { 59 if (m_PotentialRects != null && m_PotentialRects.Count > 0) 60 DrawRectGizmos(m_PotentialRects, Color.red); 61 } 62 63 public override void DoToolbarGUI(Rect toolbarRect) 64 { 65 using (new EditorGUI.DisabledScope(!containsMultipleSprites || spriteEditor.editingDisabled || m_TextureDataProvider.GetReadableTexture2D() == null || m_CurrentMode != null)) 66 { 67 GUIStyle skin = EditorStyles.toolbarPopup; 68 69 Rect drawArea = toolbarRect; 70 71 drawArea.width = skin.CalcSize(SpriteFrameModuleStyles.sliceButtonLabel).x; 72 SpriteUtilityWindow.DrawToolBarWidget(ref drawArea, ref toolbarRect, (adjustedDrawArea) => 73 { 74 if (GUI.Button(adjustedDrawArea, SpriteFrameModuleStyles.sliceButtonLabel, skin)) 75 { 76 if (SpriteEditorMenu.ShowAtPosition(adjustedDrawArea, this, this)) 77 GUIUtility.ExitGUI(); 78 } 79 }); 80 81 using (new EditorGUI.DisabledScope(!hasSelected)) 82 { 83 drawArea.x += drawArea.width; 84 drawArea.width = skin.CalcSize(SpriteFrameModuleStyles.trimButtonLabel).x; 85 SpriteUtilityWindow.DrawToolBarWidget(ref drawArea, ref toolbarRect, (adjustedDrawArea) => 86 { 87 if (GUI.Button(adjustedDrawArea, SpriteFrameModuleStyles.trimButtonLabel, EditorStyles.toolbarButton)) 88 { 89 TrimAlpha(); 90 Repaint(); 91 } 92 }); 93 } 94 } 95 } 96 97 private void HandleRectCornerScalingHandles() 98 { 99 if (!hasSelected) 100 return; 101 102 GUIStyle dragDot = styles.dragdot; 103 GUIStyle dragDotActive = styles.dragdotactive; 104 var color = Color.white; 105 106 Rect rect = new Rect(selectedSpriteRect_Rect); 107 108 float left = rect.xMin; 109 float right = rect.xMax; 110 float top = rect.yMax; 111 float bottom = rect.yMin; 112 113 EditorGUI.BeginChangeCheck(); 114 115 HandleBorderPointSlider(ref left, ref top, MouseCursor.ResizeUpLeft, false, dragDot, dragDotActive, color); 116 HandleBorderPointSlider(ref right, ref top, MouseCursor.ResizeUpRight, false, dragDot, dragDotActive, color); 117 HandleBorderPointSlider(ref left, ref bottom, MouseCursor.ResizeUpRight, false, dragDot, dragDotActive, color); 118 HandleBorderPointSlider(ref right, ref bottom, MouseCursor.ResizeUpLeft, false, dragDot, dragDotActive, color); 119 120 if (EditorGUI.EndChangeCheck()) 121 { 122 rect.xMin = left; 123 rect.xMax = right; 124 rect.yMax = top; 125 rect.yMin = bottom; 126 ScaleSpriteRect(rect); 127 PopulateSpriteFrameInspectorField(); 128 } 129 } 130 131 private void HandleRectSideScalingHandles() 132 { 133 if (!hasSelected) 134 return; 135 136 Rect rect = new Rect(selectedSpriteRect_Rect); 137 138 float left = rect.xMin; 139 float right = rect.xMax; 140 float top = rect.yMax; 141 float bottom = rect.yMin; 142 143 Vector2 screenRectTopLeft = Handles.matrix.MultiplyPoint(new Vector3(rect.xMin, rect.yMin)); 144 Vector2 screenRectBottomRight = Handles.matrix.MultiplyPoint(new Vector3(rect.xMax, rect.yMax)); 145 146 float screenRectWidth = Mathf.Abs(screenRectBottomRight.x - screenRectTopLeft.x); 147 float screenRectHeight = Mathf.Abs(screenRectBottomRight.y - screenRectTopLeft.y); 148 149 EditorGUI.BeginChangeCheck(); 150 151 left = HandleBorderScaleSlider(left, rect.yMax, screenRectWidth, screenRectHeight, true); 152 right = HandleBorderScaleSlider(right, rect.yMax, screenRectWidth, screenRectHeight, true); 153 154 top = HandleBorderScaleSlider(rect.xMin, top, screenRectWidth, screenRectHeight, false); 155 bottom = HandleBorderScaleSlider(rect.xMin, bottom, screenRectWidth, screenRectHeight, false); 156 157 if (EditorGUI.EndChangeCheck()) 158 { 159 rect.xMin = left; 160 rect.xMax = right; 161 rect.yMax = top; 162 rect.yMin = bottom; 163 164 ScaleSpriteRect(rect); 165 PopulateSpriteFrameInspectorField(); 166 } 167 } 168 169 private void HandleDragging() 170 { 171 if (hasSelected && !MouseOnTopOfInspector()) 172 { 173 Rect textureBounds = new Rect(0, 0, textureActualWidth, textureActualHeight); 174 EditorGUI.BeginChangeCheck(); 175 176 Rect oldRect = selectedSpriteRect_Rect; 177 Rect newRect = SpriteEditorUtility.ClampedRect(SpriteEditorUtility.RoundedRect(SpriteEditorHandles.SliderRect(oldRect)), textureBounds, true); 178 179 if (EditorGUI.EndChangeCheck()) 180 { 181 selectedSpriteRect_Rect = newRect; 182 UpdatePositionField(null); 183 } 184 } 185 } 186 187 private void HandleCreate() 188 { 189 if (!MouseOnTopOfInspector() && !eventSystem.current.alt) 190 { 191 // Create new rects via dragging in empty space 192 EditorGUI.BeginChangeCheck(); 193 Rect newRect = SpriteEditorHandles.RectCreator(textureActualWidth, textureActualHeight, styles.createRect); 194 if (EditorGUI.EndChangeCheck() && newRect.width > 0f && newRect.height > 0f) 195 { 196 CreateSprite(newRect); 197 GUIUtility.keyboardControl = 0; 198 } 199 } 200 } 201 202 private void HandleDuplicate() 203 { 204 IEvent evt = eventSystem.current; 205 if ((evt.type == EventType.ValidateCommand || evt.type == EventType.ExecuteCommand) 206 && evt.commandName == EventCommandNames.Duplicate) 207 { 208 if (evt.type == EventType.ExecuteCommand) 209 DuplicateSprite(); 210 211 evt.Use(); 212 } 213 } 214 215 private void HandleDelete() 216 { 217 IEvent evt = eventSystem.current; 218 219 if ((evt.type == EventType.ValidateCommand || evt.type == EventType.ExecuteCommand) 220 && (evt.commandName == EventCommandNames.SoftDelete || evt.commandName == EventCommandNames.Delete)) 221 { 222 if (evt.type == EventType.ExecuteCommand && hasSelected) 223 DeleteSprite(); 224 225 evt.Use(); 226 } 227 } 228 229 public override void DoPostGUI() 230 { 231 if (m_CurrentMode != null) 232 m_CurrentMode.DoPostGUI(); 233 else 234 base.DoPostGUI(); 235 } 236 } 237}