SpritePolygonModeModuleView.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor.UIElements;
  4. using UnityEngine.UIElements;
  5. using UIElementButton = UnityEngine.UIElements.Button;
  6. namespace UnityEditor.U2D.Sprites
  7. {
  8. internal partial class SpritePolygonModeModule : SpriteFrameModuleBase
  9. {
  10. private static class SpritePolygonModeStyles
  11. {
  12. public static readonly GUIContent changeShapeLabel = EditorGUIUtility.TrTextContent("Change Shape");
  13. }
  14. private VisualElement m_PolygonShapeView;
  15. private UIElementButton m_ChangeButton;
  16. private VisualElement m_WarningMessage;
  17. // overrides for SpriteFrameModuleViewBase
  18. private void AddMainUI(VisualElement element)
  19. {
  20. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Packages/com.unity.2d.sprite/Editor/UI/SpriteEditor/PolygonChangeShapeWindow.uxml") as VisualTreeAsset;
  21. m_PolygonShapeView = visualTree.CloneTree().Q<VisualElement>("polygonShapeWindow");
  22. m_PolygonShapeView.RegisterCallback<MouseDownEvent>((e) => { e.StopPropagation(); });
  23. m_PolygonShapeView.RegisterCallback<MouseUpEvent>((e) => { e.StopPropagation(); });
  24. SetupPolygonChangeShapeWindowElements(m_PolygonShapeView);
  25. element.Add(m_PolygonShapeView);
  26. }
  27. public override void DoMainGUI()
  28. {
  29. base.DoMainGUI();
  30. DrawGizmos();
  31. HandleGizmoMode();
  32. HandleBorderCornerScalingHandles();
  33. HandleBorderSidePointScalingSliders();
  34. HandleBorderSideScalingHandles();
  35. HandlePivotHandle();
  36. if (!MouseOnTopOfInspector())
  37. spriteEditor.HandleSpriteSelection();
  38. }
  39. public override void DoToolbarGUI(Rect toolbarRect)
  40. {
  41. using (new EditorGUI.DisabledScope(spriteEditor.editingDisabled))
  42. {
  43. GUIStyle skin = EditorStyles.toolbarPopup;
  44. Rect drawArea = toolbarRect;
  45. drawArea.width = skin.CalcSize(SpritePolygonModeStyles.changeShapeLabel).x;
  46. SpriteUtilityWindow.DrawToolBarWidget(ref drawArea, ref toolbarRect, (adjustedDrawArea) =>
  47. {
  48. showChangeShapeWindow = GUI.Toggle(adjustedDrawArea, showChangeShapeWindow, SpritePolygonModeStyles.changeShapeLabel, EditorStyles.toolbarButton);
  49. });
  50. }
  51. }
  52. private void DrawGizmos()
  53. {
  54. if (eventSystem.current.type != EventType.Repaint)
  55. return;
  56. for (int i = 0; i < spriteCount; i++)
  57. {
  58. List<Vector2[]> outline = GetSpriteOutlineAt(i);
  59. Vector2 offset = GetSpriteRectAt(i).size * 0.5f;
  60. if (outline.Count > 0)
  61. {
  62. SpriteEditorUtility.BeginLines(new Color(0.75f, 0.75f, 0.75f, 0.75f));
  63. for (int j = 0; j < outline.Count; ++j)
  64. {
  65. for (int k = 0, last = outline[j].Length - 1; k < outline[j].Length; last = k, ++k)
  66. SpriteEditorUtility.DrawLine(outline[j][last] + offset, outline[j][k] + offset);
  67. }
  68. SpriteEditorUtility.EndLines();
  69. }
  70. }
  71. DrawSpriteRectGizmos();
  72. }
  73. private void ViewUpdateSideCountField()
  74. {
  75. var sidesField = m_PolygonShapeView.Q<IntegerField>("labelIntegerField");
  76. sidesField.value = polygonSides;
  77. }
  78. private void SetupPolygonChangeShapeWindowElements(VisualElement moduleView)
  79. {
  80. var sidesField = moduleView.Q<IntegerField>("labelIntegerField");
  81. sidesField.SetValueWithoutNotify(polygonSides);
  82. sidesField.RegisterValueChangedCallback((evt) =>
  83. {
  84. polygonSides = (int)evt.newValue;
  85. ShowHideWarningMessage();
  86. });
  87. m_ChangeButton = moduleView.Q<UIElementButton>("changeButton");
  88. m_ChangeButton.RegisterCallback<MouseUpEvent>((e) =>
  89. {
  90. if (isSidesValid)
  91. {
  92. GeneratePolygonOutline();
  93. showChangeShapeWindow = false;
  94. }
  95. });
  96. m_WarningMessage = moduleView.Q("warning");
  97. ShowHideWarningMessage();
  98. }
  99. void ShowHideWarningMessage()
  100. {
  101. m_WarningMessage.style.display = !isSidesValid ? DisplayStyle.Flex : DisplayStyle.None;
  102. m_WarningMessage.style.position = m_WarningMessage.style.display == DisplayStyle.Flex ? Position.Relative : Position.Absolute;
  103. m_ChangeButton.style.display = isSidesValid ? DisplayStyle.Flex : DisplayStyle.None;;
  104. m_ChangeButton.style.position = m_ChangeButton.style.display == DisplayStyle.Flex ? Position.Relative : Position.Absolute;
  105. }
  106. private bool isSidesValid
  107. {
  108. get
  109. {
  110. return polygonSides == 0 || (polygonSides >= 3 && polygonSides <= 128);
  111. }
  112. }
  113. public bool showChangeShapeWindow
  114. {
  115. get { return m_PolygonShapeView.style.display == DisplayStyle.Flex; }
  116. set
  117. {
  118. var displayValue = value ? DisplayStyle.Flex : DisplayStyle.None;
  119. if (m_PolygonShapeView.style.display == displayValue)
  120. return;
  121. m_PolygonShapeView.style.display = displayValue;
  122. }
  123. }
  124. }
  125. }