暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AngleRangeGUI.cs 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEditor.Sprites;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace UnityEditor.U2D
  7. {
  8. internal class AngleRangeGUI
  9. {
  10. public static readonly int kLeftHandleHashCode = "LeftHandle".GetHashCode();
  11. public static readonly int kRightHandleHashCode = "RightHandle".GetHashCode();
  12. public const float kRangeWidth = 10f;
  13. public static void AngleRangeField(Rect rect, ref float start, ref float end, float angleOffset, float radius, bool snap, bool drawLine, bool drawCircle, Color rangeColor)
  14. {
  15. var leftId = GUIUtility.GetControlID(kLeftHandleHashCode, FocusType.Passive);
  16. var rightId = GUIUtility.GetControlID(kRightHandleHashCode, FocusType.Passive);
  17. AngleRangeField(rect, leftId, rightId, ref start, ref end, angleOffset, radius, snap, drawLine, drawCircle, rangeColor);
  18. }
  19. public static void AngleRangeField(Rect rect, int leftHandleID, int rightHandleID, ref float start, ref float end, float angleOffset, float radius, bool snap, bool drawLine, bool drawCircle, Color rangeColor)
  20. {
  21. Color activeColor = Handles.color;
  22. if (Event.current.type == EventType.Repaint)
  23. {
  24. float range = end - start;
  25. Color color = Handles.color;
  26. Handles.color = rangeColor;
  27. if (range < 0f)
  28. Handles.color = Color.red;
  29. SpriteShapeHandleUtility.DrawSolidArc(rect.center, Vector3.forward, Quaternion.AngleAxis(start + angleOffset, Vector3.forward) * Vector3.right, range, radius, kRangeWidth);
  30. Handles.color = color;
  31. }
  32. EditorGUI.BeginChangeCheck();
  33. float handleSize = 15f;
  34. start = AngleField(rect, leftHandleID, start, angleOffset, new Vector2(-3.5f, -7.5f), start + angleOffset + 90f, handleSize, radius, snap, drawLine, drawCircle, SpriteShapeHandleUtility.RangeLeftCap);
  35. if (EditorGUI.EndChangeCheck())
  36. start = Mathf.Clamp(start, end - 360f, end);
  37. EditorGUI.BeginChangeCheck();
  38. end = AngleField(rect, rightHandleID, end, angleOffset, new Vector2(3.5f, -7.5f), end + angleOffset + 90f, handleSize, radius, snap, drawLine, false, SpriteShapeHandleUtility.RangeRightCap);
  39. if (EditorGUI.EndChangeCheck())
  40. end = Mathf.Clamp(end, end, start + 360f);
  41. Handles.color = activeColor;
  42. }
  43. public static void AngleRangeField(Rect rect, SerializedProperty startProperty, SerializedProperty endProperty, float angleOffset, float radius, bool snap, bool drawLine, bool drawCircle, Color rangeColor)
  44. {
  45. var start = startProperty.floatValue;
  46. var end = endProperty.floatValue;
  47. EditorGUI.BeginChangeCheck();
  48. AngleRangeField(rect, ref start, ref end, angleOffset, radius, snap, drawLine, drawCircle, rangeColor);
  49. if (EditorGUI.EndChangeCheck())
  50. {
  51. startProperty.floatValue = start;
  52. endProperty.floatValue = end;
  53. }
  54. }
  55. public static void AngleField(int id, SerializedProperty property, float angleOffset, Vector2 handleOffset, float handleAngle, float handleSize, float radius, bool snap, bool drawLine, bool drawCircle, Handles.CapFunction drawCapFunction)
  56. {
  57. EditorGUI.BeginChangeCheck();
  58. float value = AngleField(id, property.floatValue, angleOffset, handleOffset, handleAngle, handleSize, radius, snap, drawLine, drawCircle, drawCapFunction);
  59. if (EditorGUI.EndChangeCheck())
  60. {
  61. property.floatValue = value;
  62. }
  63. }
  64. public static void AngleField(Rect r, int id, SerializedProperty property, float angleOffset, Vector2 handleOffset, float handleAngle, float handleSize, float radius, bool snap, bool drawLine, bool drawCircle, Handles.CapFunction drawCapFunction)
  65. {
  66. EditorGUI.BeginChangeCheck();
  67. float value = AngleField(r, id, property.floatValue, angleOffset, handleOffset, handleAngle, handleSize, radius, snap, drawLine, drawCircle, drawCapFunction);
  68. if (EditorGUI.EndChangeCheck())
  69. {
  70. property.floatValue = value;
  71. }
  72. }
  73. public static float AngleField(int id, float angle, float angleOffset, Vector2 handleOffset, float handleAngle, float radius, float handleSize, bool snap, bool drawLine, bool drawCircle, Handles.CapFunction drawCapFunction)
  74. {
  75. Rect rect = EditorGUILayout.GetControlRect(false, radius * 2f);
  76. return AngleField(rect, id, angle, angleOffset, handleOffset, handleAngle, radius, handleSize, snap, drawLine, drawCircle, drawCapFunction);
  77. }
  78. public static float AngleField(Rect rect, int id, float angle, float angleOffset, Vector2 handleOffset, float handleAngle, float handleSize, float radius, bool snap, bool drawLine, bool drawCircle, Handles.CapFunction drawCapFunction)
  79. {
  80. float offsetedAngle = angle + angleOffset;
  81. Vector2 pos = new Vector2(Mathf.Cos(offsetedAngle * Mathf.Deg2Rad), Mathf.Sin(offsetedAngle * Mathf.Deg2Rad)) * radius + rect.center;
  82. if (Event.current.type == EventType.Repaint)
  83. {
  84. if (drawCircle)
  85. Handles.DrawWireDisc(rect.center, Vector3.forward, radius);
  86. if (drawLine)
  87. Handles.DrawLine(rect.center, pos);
  88. }
  89. if (GUI.enabled)
  90. {
  91. EditorGUI.BeginChangeCheck();
  92. Quaternion rotation = Quaternion.AngleAxis(handleAngle, Vector3.forward);
  93. Vector2 posNew = SpriteShapeHandleUtility.Slider2D(id, pos, rotation * handleOffset, rotation, handleSize, drawCapFunction);
  94. if (EditorGUI.EndChangeCheck())
  95. {
  96. Vector2 v1 = pos - rect.center;
  97. Vector2 v2 = posNew - rect.center;
  98. float angleDirection = Mathf.Sign(Vector3.Dot(Vector3.forward, Vector3.Cross(v1, v2)));
  99. float angleIncrement = Vector2.Angle(v1, v2);
  100. angle += angleIncrement * angleDirection;
  101. if (snap)
  102. angle = Mathf.RoundToInt(angle);
  103. }
  104. }
  105. return angle;
  106. }
  107. }
  108. }