No Description
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.

Drawer.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditor.U2D.Common.Path.GUIFramework;
  5. namespace UnityEditor.U2D.Common.Path
  6. {
  7. internal class Drawer : IDrawer
  8. {
  9. internal class Styles
  10. {
  11. public readonly GUIStyle pointNormalStyle;
  12. public readonly GUIStyle tangentNormalStyle;
  13. public readonly GUIStyle tangentHoveredStyle;
  14. public Styles()
  15. {
  16. var pointNormal = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.common/Path/Editor/Handles/Path/pointNormal.png");
  17. pointNormalStyle = CreateStyle(pointNormal, Vector2.one * 12f);
  18. tangentNormalStyle = CreateStyle(pointNormal, Vector2.one * 8f);
  19. tangentHoveredStyle = CreateStyle(pointNormal, Vector2.one * 10f);
  20. }
  21. private GUIStyle CreateStyle(Texture2D texture, Vector2 size)
  22. {
  23. var guiStyle = new GUIStyle();
  24. guiStyle.normal.background = texture;
  25. guiStyle.fixedWidth = size.x;
  26. guiStyle.fixedHeight = size.y;
  27. return guiStyle;
  28. }
  29. }
  30. private IGUIState m_GUIState = new GUIState();
  31. private Styles m_Styles;
  32. private Styles styles
  33. {
  34. get
  35. {
  36. if (m_Styles == null)
  37. m_Styles = new Styles();
  38. return m_Styles;
  39. }
  40. }
  41. public void DrawCreatePointPreview(Vector3 position, Color color)
  42. {
  43. Color saved = GUI.color;
  44. GUI.color = color;
  45. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointNormalStyle);
  46. GUI.color = saved;
  47. }
  48. public void DrawPoint(Vector3 position, Color color)
  49. {
  50. Color saved = GUI.color;
  51. GUI.color = color;
  52. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointNormalStyle);
  53. GUI.color = saved;
  54. }
  55. public void DrawPointHovered(Vector3 position, Color color)
  56. {
  57. Color saved = GUI.color;
  58. GUI.color = color;
  59. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointNormalStyle);
  60. GUI.color = saved;
  61. }
  62. public void DrawPointSelected(Vector3 position, Color color)
  63. {
  64. Color saved = GUI.color;
  65. GUI.color = color;
  66. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointNormalStyle);
  67. GUI.color = saved;
  68. }
  69. public void DrawLine(Vector3 p1, Vector3 p2, float width, Color color)
  70. {
  71. Handles.color = color;
  72. Handles.DrawAAPolyLine(width, new Vector3[] { p1, p2 });
  73. }
  74. public void DrawBezier(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, float width, Color color)
  75. {
  76. Handles.color = color;
  77. Handles.DrawBezier(p1, p4, p2, p3, color, null, width);
  78. }
  79. public void DrawTangent(Vector3 position, Vector3 tangent, Color color)
  80. {
  81. DrawLine(position, tangent, 3f, color);
  82. Color saved = GUI.color;
  83. GUI.color = color;
  84. DrawGUIStyleCap(0, tangent, Quaternion.identity, m_GUIState.GetHandleSize(tangent), styles.tangentNormalStyle);
  85. GUI.color = saved;
  86. }
  87. private void DrawGUIStyleCap(int controlID, Vector3 position, Quaternion rotation, float size, GUIStyle guiStyle)
  88. {
  89. if (Camera.current && Vector3.Dot(position - Camera.current.transform.position, Camera.current.transform.forward) < 0f)
  90. return;
  91. Handles.BeginGUI();
  92. guiStyle.Draw(GetGUIStyleRect(guiStyle, position), GUIContent.none, controlID);
  93. Handles.EndGUI();
  94. }
  95. private Rect GetGUIStyleRect(GUIStyle style, Vector3 position)
  96. {
  97. Vector2 vector = HandleUtility.WorldToGUIPoint(position);
  98. float fixedWidth = style.fixedWidth;
  99. float fixedHeight = style.fixedHeight;
  100. return new Rect(vector.x - fixedWidth / 2f, vector.y - fixedHeight / 2f, fixedWidth, fixedHeight);
  101. }
  102. }
  103. }