SpriteFrameModuleBaseView.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. using System;
  2. using UnityEditor.UIElements;
  3. using UnityEngine.UIElements;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.Sprites
  6. {
  7. internal abstract partial class SpriteFrameModuleBase : SpriteEditorModuleBase
  8. {
  9. protected enum GizmoMode
  10. {
  11. BorderEditing,
  12. RectEditing
  13. }
  14. protected class Styles
  15. {
  16. public readonly GUIStyle dragdot = "U2D.dragDot";
  17. public readonly GUIStyle dragdotactive = "U2D.dragDotActive";
  18. public readonly GUIStyle createRect = "U2D.createRect";
  19. public readonly GUIStyle pivotdotactive = "U2D.pivotDotActive";
  20. public readonly GUIStyle pivotdot = "U2D.pivotDot";
  21. public readonly GUIStyle dragBorderdot = new GUIStyle();
  22. public readonly GUIStyle dragBorderDotActive = new GUIStyle();
  23. public readonly GUIStyle toolbar;
  24. public Styles()
  25. {
  26. toolbar = new GUIStyle(EditorStyles.inspectorBig);
  27. toolbar.margin.top = 0;
  28. toolbar.margin.bottom = 0;
  29. createRect.border = new RectOffset(3, 3, 3, 3);
  30. dragBorderdot.fixedHeight = 5f;
  31. dragBorderdot.fixedWidth = 5f;
  32. dragBorderdot.normal.background = EditorGUIUtility.whiteTexture;
  33. dragBorderDotActive.fixedHeight = dragBorderdot.fixedHeight;
  34. dragBorderDotActive.fixedWidth = dragBorderdot.fixedWidth;
  35. dragBorderDotActive.normal.background = EditorGUIUtility.whiteTexture;
  36. }
  37. }
  38. private static Styles s_Styles;
  39. protected static Styles styles
  40. {
  41. get
  42. {
  43. if (s_Styles == null)
  44. s_Styles = new Styles();
  45. return s_Styles;
  46. }
  47. }
  48. private const float kInspectorWidth = 330f;
  49. private const float kInspectorHeight = 170;
  50. private const float kPivotFieldPrecision = 0.0001f;
  51. private float m_Zoom = 1.0f;
  52. private GizmoMode m_GizmoMode;
  53. private VisualElement m_NameElement;
  54. private TextField m_NameField;
  55. private VisualElement m_PositionElement;
  56. private IntegerField m_PositionFieldX;
  57. private IntegerField m_PositionFieldY;
  58. private IntegerField m_PositionFieldW;
  59. private IntegerField m_PositionFieldH;
  60. private IntegerField m_BorderFieldL;
  61. private IntegerField m_BorderFieldT;
  62. private IntegerField m_BorderFieldR;
  63. private IntegerField m_BorderFieldB;
  64. private EnumField m_PivotField;
  65. private EnumField m_PivotUnitModeField;
  66. private VisualElement m_CustomPivotElement;
  67. private FloatField m_CustomPivotFieldX;
  68. private FloatField m_CustomPivotFieldY;
  69. private VisualElement m_SelectedFrameInspector;
  70. private bool ShouldShowRectScaling()
  71. {
  72. return hasSelected && m_GizmoMode == GizmoMode.RectEditing;
  73. }
  74. private static Rect inspectorRect
  75. {
  76. get
  77. {
  78. return new Rect(
  79. 0, 0,
  80. kInspectorWidth,
  81. kInspectorHeight);
  82. }
  83. }
  84. private void RemoveMainUI(VisualElement mainView)
  85. {
  86. if (mainView.Contains(m_SelectedFrameInspector))
  87. mainView.Remove(m_SelectedFrameInspector);
  88. mainView.UnregisterCallback<SpriteSelectionChangeEvent>(SelectionChange);
  89. }
  90. protected void UpdatePositionField(FocusOutEvent evt)
  91. {
  92. if (hasSelected)
  93. {
  94. m_PositionFieldX.SetValueWithoutNotify((int)selectedSpriteRect.x);
  95. m_PositionFieldY.SetValueWithoutNotify((int)selectedSpriteRect.y);
  96. m_PositionFieldW.SetValueWithoutNotify((int)selectedSpriteRect.width);
  97. m_PositionFieldH.SetValueWithoutNotify((int)selectedSpriteRect.height);
  98. }
  99. }
  100. private void UpdateBorderField(FocusOutEvent evt)
  101. {
  102. if (hasSelected)
  103. {
  104. m_BorderFieldL.SetValueWithoutNotify((int)selectedSpriteBorder.x);
  105. m_BorderFieldB.SetValueWithoutNotify((int)selectedSpriteBorder.y);
  106. m_BorderFieldR.SetValueWithoutNotify((int)selectedSpriteBorder.z);
  107. m_BorderFieldT.SetValueWithoutNotify((int)selectedSpriteBorder.w);
  108. }
  109. }
  110. void SetupIntegerField(IntegerField field, EventCallback<FocusOutEvent> onFocusOutEvent, EventCallback<ChangeEvent<int>> onChangeEvent)
  111. {
  112. field.RegisterCallback(onFocusOutEvent);
  113. field.RegisterValueChangedCallback(onChangeEvent);
  114. }
  115. void SetDragFieldLimit(IntegerField field, int value)
  116. {
  117. // The only way to know if value change is due to dragger or text input
  118. var t = field.Q("unity-text-input");
  119. if (!t.focusController.IsFocused(t))
  120. {
  121. // Value changed due to drag. We set back the field so to show the drag limit
  122. field.SetValueWithoutNotify(value);
  123. }
  124. }
  125. void OnPositionIntXChange(ChangeEvent<int> evt)
  126. {
  127. if (hasSelected)
  128. {
  129. var rect = selectedSpriteRect;
  130. rect.x = evt.newValue;
  131. selectedSpriteRect = rect;
  132. SetDragFieldLimit(m_PositionFieldX, (int)selectedSpriteRect.x);
  133. m_PositionFieldW.SetValueWithoutNotify((int)selectedSpriteRect.width);
  134. }
  135. }
  136. void OnPositionIntYChange(ChangeEvent<int> evt)
  137. {
  138. if (hasSelected)
  139. {
  140. var rect = selectedSpriteRect;
  141. rect.y = evt.newValue;
  142. selectedSpriteRect = rect;
  143. SetDragFieldLimit(m_PositionFieldY, (int)selectedSpriteRect.y);
  144. m_PositionFieldH.SetValueWithoutNotify((int)selectedSpriteRect.height);
  145. }
  146. }
  147. void OnPositionIntWChange(ChangeEvent<int> evt)
  148. {
  149. if (hasSelected)
  150. {
  151. var rect = selectedSpriteRect;
  152. rect.width = evt.newValue;
  153. selectedSpriteRect = rect;
  154. SetDragFieldLimit(m_PositionFieldW, (int)selectedSpriteRect.width);
  155. m_PositionFieldX.SetValueWithoutNotify((int)selectedSpriteRect.x);
  156. }
  157. }
  158. void OnPositionIntHChange(ChangeEvent<int> evt)
  159. {
  160. if (hasSelected)
  161. {
  162. var rect = selectedSpriteRect;
  163. rect.height = evt.newValue;
  164. selectedSpriteRect = rect;
  165. SetDragFieldLimit(m_PositionFieldH, (int)selectedSpriteRect.height);
  166. m_PositionFieldY.SetValueWithoutNotify((int)selectedSpriteRect.y);
  167. }
  168. }
  169. void OnBorderIntLChange(ChangeEvent<int> evt)
  170. {
  171. if (hasSelected)
  172. {
  173. var border = selectedSpriteBorder;
  174. border.x = evt.newValue;
  175. selectedSpriteBorder = border;
  176. SetDragFieldLimit(m_BorderFieldL, (int)selectedSpriteBorder.x);
  177. }
  178. }
  179. void OnBorderIntBChange(ChangeEvent<int> evt)
  180. {
  181. if (hasSelected)
  182. {
  183. var border = selectedSpriteBorder;
  184. border.y = evt.newValue;
  185. selectedSpriteBorder = border;
  186. SetDragFieldLimit(m_BorderFieldB, (int)selectedSpriteBorder.y);
  187. }
  188. }
  189. void OnBorderIntRChange(ChangeEvent<int> evt)
  190. {
  191. if (hasSelected)
  192. {
  193. var border = selectedSpriteBorder;
  194. border.z = (evt.newValue + border.x) <= selectedSpriteRect.width ? evt.newValue : selectedSpriteRect.width - border.x;
  195. selectedSpriteBorder = border;
  196. SetDragFieldLimit(m_BorderFieldR, (int)selectedSpriteBorder.z);
  197. }
  198. }
  199. void OnBorderIntTChange(ChangeEvent<int> evt)
  200. {
  201. if (hasSelected)
  202. {
  203. var border = selectedSpriteBorder;
  204. border.w = (evt.newValue + border.y) <= selectedSpriteRect.height ? evt.newValue : selectedSpriteRect.height - border.y;
  205. selectedSpriteBorder = border;
  206. SetDragFieldLimit(m_BorderFieldT, (int)selectedSpriteBorder.w);
  207. }
  208. }
  209. private void AddMainUI(VisualElement mainView)
  210. {
  211. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Packages/com.unity.2d.sprite/Editor/UI/SpriteEditor/SpriteFrameModuleInspector.uxml") as VisualTreeAsset;
  212. m_SelectedFrameInspector = visualTree.CloneTree().Q("spriteFrameModuleInspector");
  213. m_NameElement = m_SelectedFrameInspector.Q("name");
  214. m_NameField = m_SelectedFrameInspector.Q<TextField>("spriteName");
  215. m_NameField.RegisterValueChangedCallback((evt) =>
  216. {
  217. if (hasSelected)
  218. {
  219. selectedSpriteName = evt.newValue;
  220. }
  221. });
  222. m_NameField.RegisterCallback<FocusOutEvent>((focus) =>
  223. {
  224. if (hasSelected)
  225. {
  226. m_NameField.SetValueWithoutNotify(selectedSpriteName);
  227. }
  228. });
  229. m_PositionElement = m_SelectedFrameInspector.Q("position");
  230. m_PositionFieldX = m_PositionElement.Q<IntegerField>("positionX");
  231. SetupIntegerField(m_PositionFieldX, UpdatePositionField, OnPositionIntXChange);
  232. m_PositionFieldY = m_PositionElement.Q<IntegerField>("positionY");
  233. SetupIntegerField(m_PositionFieldY, UpdatePositionField, OnPositionIntYChange);
  234. m_PositionFieldW = m_PositionElement.Q<IntegerField>("positionW");
  235. SetupIntegerField(m_PositionFieldW, UpdatePositionField, OnPositionIntWChange);
  236. m_PositionFieldH = m_PositionElement.Q<IntegerField>("positionH");
  237. SetupIntegerField(m_PositionFieldH, UpdatePositionField, OnPositionIntHChange);
  238. var borderElement = m_SelectedFrameInspector.Q("border");
  239. m_BorderFieldL = borderElement.Q<IntegerField>("borderL");
  240. SetupIntegerField(m_BorderFieldL, UpdateBorderField, OnBorderIntLChange);
  241. m_BorderFieldT = borderElement.Q<IntegerField>("borderT");
  242. SetupIntegerField(m_BorderFieldT, UpdateBorderField, OnBorderIntTChange);
  243. m_BorderFieldR = borderElement.Q<IntegerField>("borderR");
  244. SetupIntegerField(m_BorderFieldR, UpdateBorderField, OnBorderIntRChange);
  245. m_BorderFieldB = borderElement.Q<IntegerField>("borderB");
  246. SetupIntegerField(m_BorderFieldB, UpdateBorderField, OnBorderIntBChange);
  247. m_PivotField = m_SelectedFrameInspector.Q<EnumField>("pivotField");
  248. m_PivotField.Init(SpriteAlignment.Center);
  249. m_PivotField.label = L10n.Tr("Pivot");
  250. m_PivotField.RegisterValueChangedCallback((evt) =>
  251. {
  252. if (hasSelected)
  253. {
  254. SpriteAlignment alignment = (SpriteAlignment)evt.newValue;
  255. SetSpritePivotAndAlignment(selectedSpritePivot, alignment);
  256. m_CustomPivotElement.SetEnabled(selectedSpriteAlignment == SpriteAlignment.Custom);
  257. Vector2 pivot = selectedSpritePivotInCurUnitMode;
  258. m_CustomPivotFieldX.SetValueWithoutNotify(pivot.x);
  259. m_CustomPivotFieldY.SetValueWithoutNotify(pivot.y);
  260. }
  261. });
  262. m_PivotUnitModeField = m_SelectedFrameInspector.Q<EnumField>("pivotUnitModeField");
  263. m_PivotUnitModeField.Init(PivotUnitMode.Normalized);
  264. m_PivotUnitModeField.label = L10n.Tr("Pivot Unit Mode");
  265. m_PivotUnitModeField.RegisterValueChangedCallback((evt) =>
  266. {
  267. if (hasSelected)
  268. {
  269. m_PivotUnitMode = (PivotUnitMode)evt.newValue;
  270. Vector2 pivot = selectedSpritePivotInCurUnitMode;
  271. m_CustomPivotFieldX.SetValueWithoutNotify(pivot.x);
  272. m_CustomPivotFieldY.SetValueWithoutNotify(pivot.y);
  273. }
  274. });
  275. m_CustomPivotElement = m_SelectedFrameInspector.Q("customPivot");
  276. m_CustomPivotFieldX = m_CustomPivotElement.Q<FloatField>("customPivotX");
  277. m_CustomPivotFieldX.RegisterValueChangedCallback((evt) =>
  278. {
  279. if (hasSelected)
  280. {
  281. float newValue = (float)evt.newValue;
  282. float pivotX = m_PivotUnitMode == PivotUnitMode.Pixels
  283. ? ConvertFromRectToNormalizedSpace(new Vector2(newValue, 0.0f), selectedSpriteRect).x
  284. : newValue;
  285. var pivot = selectedSpritePivot;
  286. pivot.x = pivotX;
  287. SetSpritePivotAndAlignment(pivot, selectedSpriteAlignment);
  288. }
  289. });
  290. m_CustomPivotFieldY = m_CustomPivotElement.Q<FloatField>("customPivotY");
  291. m_CustomPivotFieldY.RegisterValueChangedCallback((evt) =>
  292. {
  293. if (hasSelected)
  294. {
  295. float newValue = (float)evt.newValue;
  296. float pivotY = m_PivotUnitMode == PivotUnitMode.Pixels
  297. ? ConvertFromRectToNormalizedSpace(new Vector2(0.0f, newValue), selectedSpriteRect).y
  298. : newValue;
  299. var pivot = selectedSpritePivot;
  300. pivot.y = pivotY;
  301. SetSpritePivotAndAlignment(pivot, selectedSpriteAlignment);
  302. }
  303. });
  304. //// Force an update of all the fields.
  305. PopulateSpriteFrameInspectorField();
  306. mainView.RegisterCallback<SpriteSelectionChangeEvent>(SelectionChange);
  307. // Stop mouse events from reaching the main view.
  308. m_SelectedFrameInspector.pickingMode = PickingMode.Ignore;
  309. m_SelectedFrameInspector.RegisterCallback<MouseDownEvent>((e) => { e.StopPropagation(); });
  310. m_SelectedFrameInspector.RegisterCallback<MouseUpEvent>((e) => { e.StopPropagation(); });
  311. m_SelectedFrameInspector.AddToClassList("moduleWindow");
  312. m_SelectedFrameInspector.AddToClassList("bottomRightFloating");
  313. mainView.Add(m_SelectedFrameInspector);
  314. }
  315. private void SelectionChange(SpriteSelectionChangeEvent evt)
  316. {
  317. m_SelectedFrameInspector.style.display = hasSelected ? DisplayStyle.Flex : DisplayStyle.None;
  318. PopulateSpriteFrameInspectorField();
  319. }
  320. private void UIUndoCallback()
  321. {
  322. PopulateSpriteFrameInspectorField();
  323. }
  324. protected void PopulateSpriteFrameInspectorField()
  325. {
  326. m_SelectedFrameInspector.style.display = hasSelected ? DisplayStyle.Flex : DisplayStyle.None;
  327. if (!hasSelected)
  328. return;
  329. m_NameElement.SetEnabled(containsMultipleSprites);
  330. m_NameField.SetValueWithoutNotify(selectedSpriteName);
  331. m_PositionElement.SetEnabled(containsMultipleSprites);
  332. var spriteRect = selectedSpriteRect;
  333. m_PositionFieldX.SetValueWithoutNotify(Mathf.RoundToInt(spriteRect.x));
  334. m_PositionFieldY.SetValueWithoutNotify(Mathf.RoundToInt(spriteRect.y));
  335. m_PositionFieldW.SetValueWithoutNotify(Mathf.RoundToInt(spriteRect.width));
  336. m_PositionFieldH.SetValueWithoutNotify(Mathf.RoundToInt(spriteRect.height));
  337. var spriteBorder = selectedSpriteBorder;
  338. m_BorderFieldL.SetValueWithoutNotify(Mathf.RoundToInt(spriteBorder.x));
  339. m_BorderFieldT.SetValueWithoutNotify(Mathf.RoundToInt(spriteBorder.w));
  340. m_BorderFieldR.SetValueWithoutNotify(Mathf.RoundToInt(spriteBorder.z));
  341. m_BorderFieldB.SetValueWithoutNotify(Mathf.RoundToInt(spriteBorder.y));
  342. m_PivotField.SetValueWithoutNotify(selectedSpriteAlignment);
  343. m_PivotUnitModeField.SetValueWithoutNotify(m_PivotUnitMode);
  344. Vector2 pivot = selectedSpritePivotInCurUnitMode;
  345. m_CustomPivotFieldX.SetValueWithoutNotify(pivot.x);
  346. m_CustomPivotFieldY.SetValueWithoutNotify(pivot.y);
  347. m_CustomPivotElement.SetEnabled(hasSelected && selectedSpriteAlignment == SpriteAlignment.Custom);
  348. }
  349. private static Vector2 ApplySpriteAlignmentToPivot(Vector2 pivot, Rect rect, SpriteAlignment alignment)
  350. {
  351. if (alignment != SpriteAlignment.Custom)
  352. {
  353. Vector2[] snapPoints = GetSnapPointsArray(rect);
  354. Vector2 texturePos = snapPoints[(int)alignment];
  355. return ConvertFromTextureToNormalizedSpace(texturePos, rect);
  356. }
  357. return pivot;
  358. }
  359. private static Vector2 ConvertFromTextureToNormalizedSpace(Vector2 texturePos, Rect rect)
  360. {
  361. return new Vector2((texturePos.x - rect.xMin) / rect.width, (texturePos.y - rect.yMin) / rect.height);
  362. }
  363. private static Vector2 ConvertFromNormalizedToRectSpace(Vector2 normalizedPos, Rect rect)
  364. {
  365. Vector2 rectPos = new Vector2(rect.width * normalizedPos.x, rect.height * normalizedPos.y);
  366. // This is to combat the lack of precision formating on the UI controls.
  367. rectPos.x = Mathf.Round(rectPos.x / kPivotFieldPrecision) * kPivotFieldPrecision;
  368. rectPos.y = Mathf.Round(rectPos.y / kPivotFieldPrecision) * kPivotFieldPrecision;
  369. return rectPos;
  370. }
  371. private static Vector2 ConvertFromRectToNormalizedSpace(Vector2 rectPos, Rect rect)
  372. {
  373. return new Vector2(rectPos.x / rect.width, rectPos.y / rect.height);
  374. }
  375. private static Vector2[] GetSnapPointsArray(Rect rect)
  376. {
  377. Vector2[] snapPoints = new Vector2[9];
  378. snapPoints[(int)SpriteAlignment.TopLeft] = new Vector2(rect.xMin, rect.yMax);
  379. snapPoints[(int)SpriteAlignment.TopCenter] = new Vector2(rect.center.x, rect.yMax);
  380. snapPoints[(int)SpriteAlignment.TopRight] = new Vector2(rect.xMax, rect.yMax);
  381. snapPoints[(int)SpriteAlignment.LeftCenter] = new Vector2(rect.xMin, rect.center.y);
  382. snapPoints[(int)SpriteAlignment.Center] = new Vector2(rect.center.x, rect.center.y);
  383. snapPoints[(int)SpriteAlignment.RightCenter] = new Vector2(rect.xMax, rect.center.y);
  384. snapPoints[(int)SpriteAlignment.BottomLeft] = new Vector2(rect.xMin, rect.yMin);
  385. snapPoints[(int)SpriteAlignment.BottomCenter] = new Vector2(rect.center.x, rect.yMin);
  386. snapPoints[(int)SpriteAlignment.BottomRight] = new Vector2(rect.xMax, rect.yMin);
  387. return snapPoints;
  388. }
  389. protected void Repaint()
  390. {
  391. spriteEditor.RequestRepaint();
  392. }
  393. protected void HandleGizmoMode()
  394. {
  395. GizmoMode oldGizmoMode = m_GizmoMode;
  396. var evt = eventSystem.current;
  397. if (evt.control)
  398. m_GizmoMode = GizmoMode.BorderEditing;
  399. else
  400. m_GizmoMode = GizmoMode.RectEditing;
  401. if (oldGizmoMode != m_GizmoMode && (evt.type == EventType.KeyDown || evt.type == EventType.KeyUp) && (evt.keyCode == KeyCode.LeftControl || evt.keyCode == KeyCode.RightControl || evt.keyCode == KeyCode.LeftAlt || evt.keyCode == KeyCode.RightAlt))
  402. Repaint();
  403. }
  404. protected bool MouseOnTopOfInspector()
  405. {
  406. if (hasSelected == false)
  407. return false;
  408. var point = GUIClip.Unclip(eventSystem.current.mousePosition);
  409. point = m_SelectedFrameInspector.parent.LocalToWorld(point);
  410. var selectedElement = m_SelectedFrameInspector.panel.Pick(point);
  411. if (selectedElement != null
  412. && selectedElement.pickingMode != PickingMode.Ignore
  413. && selectedElement.FindCommonAncestor(m_SelectedFrameInspector) == m_SelectedFrameInspector)
  414. return true;
  415. return false;
  416. }
  417. protected void HandlePivotHandle()
  418. {
  419. if (!hasSelected)
  420. return;
  421. EditorGUI.BeginChangeCheck();
  422. SpriteAlignment alignment = selectedSpriteAlignment;
  423. Vector2 pivot = selectedSpritePivot;
  424. Rect rect = selectedSpriteRect;
  425. pivot = ApplySpriteAlignmentToPivot(pivot, rect, alignment);
  426. Vector2 pivotHandlePosition = SpriteEditorHandles.PivotSlider(rect, pivot, styles.pivotdot, styles.pivotdotactive);
  427. if (EditorGUI.EndChangeCheck())
  428. {
  429. // Pivot snapping only happen when ctrl is press. Same as scene view snapping move
  430. if (eventSystem.current.control)
  431. SnapPivotToSnapPoints(pivotHandlePosition, out pivot, out alignment);
  432. else if (m_PivotUnitMode == PivotUnitMode.Pixels)
  433. SnapPivotToPixels(pivotHandlePosition, out pivot, out alignment);
  434. else
  435. {
  436. pivot = pivotHandlePosition;
  437. alignment = SpriteAlignment.Custom;
  438. }
  439. SetSpritePivotAndAlignment(pivot, alignment);
  440. PopulateSpriteFrameInspectorField();
  441. }
  442. }
  443. protected void HandleBorderSidePointScalingSliders()
  444. {
  445. if (!hasSelected)
  446. return;
  447. GUIStyle dragDot = styles.dragBorderdot;
  448. GUIStyle dragDotActive = styles.dragBorderDotActive;
  449. var color = new Color(0f, 1f, 0f);
  450. Rect rect = selectedSpriteRect;
  451. Vector4 border = selectedSpriteBorder;
  452. float left = rect.xMin + border.x;
  453. float right = rect.xMax - border.z;
  454. float top = rect.yMax - border.w;
  455. float bottom = rect.yMin + border.y;
  456. EditorGUI.BeginChangeCheck();
  457. float horizontal = bottom - (bottom - top) / 2;
  458. float vertical = left - (left - right) / 2;
  459. float center = horizontal;
  460. HandleBorderPointSlider(ref left, ref center, MouseCursor.ResizeHorizontal, false, dragDot, dragDotActive, color);
  461. center = horizontal;
  462. HandleBorderPointSlider(ref right, ref center, MouseCursor.ResizeHorizontal, false, dragDot, dragDotActive, color);
  463. center = vertical;
  464. HandleBorderPointSlider(ref center, ref top, MouseCursor.ResizeVertical, false, dragDot, dragDotActive, color);
  465. center = vertical;
  466. HandleBorderPointSlider(ref center, ref bottom, MouseCursor.ResizeVertical, false, dragDot, dragDotActive, color);
  467. if (EditorGUI.EndChangeCheck())
  468. {
  469. border.x = left - rect.xMin;
  470. border.z = rect.xMax - right;
  471. border.w = rect.yMax - top;
  472. border.y = bottom - rect.yMin;
  473. selectedSpriteBorder = border;
  474. PopulateSpriteFrameInspectorField();
  475. }
  476. }
  477. protected void HandleBorderCornerScalingHandles()
  478. {
  479. if (!hasSelected)
  480. return;
  481. GUIStyle dragDot = styles.dragBorderdot;
  482. GUIStyle dragDotActive = styles.dragBorderDotActive;
  483. var color = new Color(0f, 1f, 0f);
  484. Rect rect = selectedSpriteRect;
  485. Vector4 border = selectedSpriteBorder;
  486. float left = rect.xMin + border.x;
  487. float right = rect.xMax - border.z;
  488. float top = rect.yMax - border.w;
  489. float bottom = rect.yMin + border.y;
  490. EditorGUI.BeginChangeCheck();
  491. // Handle corner points, but hide them if border values are below 1
  492. HandleBorderPointSlider(ref left, ref top, MouseCursor.ResizeUpLeft, border.x < 1 && border.w < 1, dragDot, dragDotActive, color);
  493. HandleBorderPointSlider(ref right, ref top, MouseCursor.ResizeUpRight, border.z < 1 && border.w < 1, dragDot, dragDotActive, color);
  494. HandleBorderPointSlider(ref left, ref bottom, MouseCursor.ResizeUpRight, border.x < 1 && border.y < 1, dragDot, dragDotActive, color);
  495. HandleBorderPointSlider(ref right, ref bottom, MouseCursor.ResizeUpLeft, border.z < 1 && border.y < 1, dragDot, dragDotActive, color);
  496. if (EditorGUI.EndChangeCheck())
  497. {
  498. border.x = left - rect.xMin;
  499. border.z = rect.xMax - right;
  500. border.w = rect.yMax - top;
  501. border.y = bottom - rect.yMin;
  502. selectedSpriteBorder = border;
  503. PopulateSpriteFrameInspectorField();
  504. }
  505. }
  506. protected void HandleBorderSideScalingHandles()
  507. {
  508. if (hasSelected == false)
  509. return;
  510. Rect rect = new Rect(selectedSpriteRect);
  511. Vector4 border = selectedSpriteBorder;
  512. float left = rect.xMin + border.x;
  513. float right = rect.xMax - border.z;
  514. float top = rect.yMax - border.w;
  515. float bottom = rect.yMin + border.y;
  516. Vector2 screenRectTopLeft = Handles.matrix.MultiplyPoint(new Vector3(rect.xMin, rect.yMin));
  517. Vector2 screenRectBottomRight = Handles.matrix.MultiplyPoint(new Vector3(rect.xMax, rect.yMax));
  518. float screenRectWidth = Mathf.Abs(screenRectBottomRight.x - screenRectTopLeft.x);
  519. float screenRectHeight = Mathf.Abs(screenRectBottomRight.y - screenRectTopLeft.y);
  520. EditorGUI.BeginChangeCheck();
  521. left = HandleBorderScaleSlider(left, rect.yMax, screenRectWidth, screenRectHeight, true);
  522. right = HandleBorderScaleSlider(right, rect.yMax, screenRectWidth, screenRectHeight, true);
  523. top = HandleBorderScaleSlider(rect.xMin, top, screenRectWidth, screenRectHeight, false);
  524. bottom = HandleBorderScaleSlider(rect.xMin, bottom, screenRectWidth, screenRectHeight, false);
  525. if (EditorGUI.EndChangeCheck())
  526. {
  527. border.x = left - rect.xMin;
  528. border.z = rect.xMax - right;
  529. border.w = rect.yMax - top;
  530. border.y = bottom - rect.yMin;
  531. selectedSpriteBorder = border;
  532. PopulateSpriteFrameInspectorField();
  533. }
  534. }
  535. protected void HandleBorderPointSlider(ref float x, ref float y, MouseCursor mouseCursor, bool isHidden, GUIStyle dragDot, GUIStyle dragDotActive, Color color)
  536. {
  537. var originalColor = GUI.color;
  538. if (isHidden)
  539. GUI.color = new Color(0, 0, 0, 0);
  540. else
  541. GUI.color = color;
  542. Vector2 point = SpriteEditorHandles.PointSlider(new Vector2(x, y), mouseCursor, dragDot, dragDotActive);
  543. x = point.x;
  544. y = point.y;
  545. GUI.color = originalColor;
  546. }
  547. protected float HandleBorderScaleSlider(float x, float y, float width, float height, bool isHorizontal)
  548. {
  549. float handleSize = styles.dragBorderdot.fixedWidth;
  550. Vector2 point = Handles.matrix.MultiplyPoint(new Vector2(x, y));
  551. float result;
  552. EditorGUI.BeginChangeCheck();
  553. if (isHorizontal)
  554. {
  555. Rect newRect = new Rect(point.x - handleSize * .5f, point.y, handleSize, height);
  556. result = SpriteEditorHandles.ScaleSlider(point, MouseCursor.ResizeHorizontal, newRect).x;
  557. }
  558. else
  559. {
  560. Rect newRect = new Rect(point.x, point.y - handleSize * .5f, width, handleSize);
  561. result = SpriteEditorHandles.ScaleSlider(point, MouseCursor.ResizeVertical, newRect).y;
  562. }
  563. if (EditorGUI.EndChangeCheck())
  564. return result;
  565. return isHorizontal ? x : y;
  566. }
  567. protected void DrawSpriteRectGizmos()
  568. {
  569. if (eventSystem.current.type != EventType.Repaint)
  570. return;
  571. SpriteEditorUtility.BeginLines(new Color(0f, 1f, 0f, 0.7f));
  572. var selectedGUID = selected != null ? selected.spriteID : new GUID();
  573. for (int i = 0; i < spriteCount; i++)
  574. {
  575. Vector4 border = GetSpriteBorderAt(i);
  576. if (m_GizmoMode != GizmoMode.BorderEditing && (m_RectsCache != null && m_RectsCache.spriteRects[i].spriteID != selectedGUID))
  577. {
  578. if (Mathf.Approximately(border.sqrMagnitude, 0))
  579. continue;
  580. }
  581. var rect = GetSpriteRectAt(i);
  582. SpriteEditorUtility.DrawLine(new Vector3(rect.xMin + border.x, rect.yMin), new Vector3(rect.xMin + border.x, rect.yMax));
  583. SpriteEditorUtility.DrawLine(new Vector3(rect.xMax - border.z, rect.yMin), new Vector3(rect.xMax - border.z, rect.yMax));
  584. SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMin + border.y), new Vector3(rect.xMax, rect.yMin + border.y));
  585. SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMax - border.w), new Vector3(rect.xMax, rect.yMax - border.w));
  586. }
  587. SpriteEditorUtility.EndLines();
  588. if (ShouldShowRectScaling())
  589. {
  590. Rect r = selectedSpriteRect;
  591. SpriteEditorUtility.BeginLines(new Color(0f, 0.1f, 0.3f, 0.25f));
  592. SpriteEditorUtility.DrawBox(new Rect(r.xMin + 1f / m_Zoom, r.yMin + 1f / m_Zoom, r.width, r.height));
  593. SpriteEditorUtility.EndLines();
  594. SpriteEditorUtility.BeginLines(new Color(0.25f, 0.5f, 1f, 0.75f));
  595. SpriteEditorUtility.DrawBox(r);
  596. SpriteEditorUtility.EndLines();
  597. }
  598. }
  599. // implements ISpriteEditorModule
  600. public override void DoMainGUI()
  601. {
  602. m_Zoom = Handles.matrix.GetColumn(0).magnitude;
  603. }
  604. public override void DoPostGUI()
  605. {
  606. }
  607. }
  608. }