暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SpriteFrameModuleBaseView.cs 31KB

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