Drawer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditor.Experimental.Rendering.Universal.Path2D.GUIFramework;
  5. namespace UnityEditor.Experimental.Rendering.Universal.Path2D
  6. {
  7. internal class DefaultStyles
  8. {
  9. public readonly GUIStyle pointNormalStyle;
  10. public readonly GUIStyle pointHoveredStyle;
  11. public readonly GUIStyle pointSelectedStyle;
  12. public readonly GUIStyle pointPreviewStyle;
  13. public readonly GUIStyle pointRemovePreviewStyle;
  14. public readonly GUIStyle tangentNormalStyle;
  15. public readonly GUIStyle tangentHoveredStyle;
  16. public readonly GUIStyle selectionRectStyle;
  17. public DefaultStyles()
  18. {
  19. var pointNormal = Resources.Load<Texture2D>("Path/pointNormal");
  20. var pointHovered = Resources.Load<Texture2D>("Path/pointHovered");
  21. var pointSelected = Resources.Load<Texture2D>("Path/pointSelected");
  22. var pointPreview = Resources.Load<Texture2D>("Path/pointPreview");
  23. var pointRemovePreview = Resources.Load<Texture2D>("Path/pointRemovePreview");
  24. var tangentNormal = Resources.Load<Texture2D>("Path/tangentNormal");
  25. pointNormalStyle = CreateStyle(Resources.Load<Texture2D>("Path/pointNormal"), Vector2.one * 12f);
  26. pointHoveredStyle = CreateStyle(Resources.Load<Texture2D>("Path/pointHovered"), Vector2.one * 12f);
  27. pointSelectedStyle = CreateStyle(Resources.Load<Texture2D>("Path/pointSelected"), Vector2.one * 12f);
  28. pointPreviewStyle = CreateStyle(Resources.Load<Texture2D>("Path/pointPreview"), Vector2.one * 12f);
  29. pointRemovePreviewStyle = CreateStyle(Resources.Load<Texture2D>("Path/pointRemovePreview"), Vector2.one * 12f);
  30. tangentNormalStyle = CreateStyle(Resources.Load<Texture2D>("Path/tangentNormal"), Vector2.one * 8f);
  31. tangentHoveredStyle = CreateStyle(Resources.Load<Texture2D>("Path/pointHovered"), Vector2.one * 10f);
  32. selectionRectStyle = GUI.skin.FindStyle("selectionRect");
  33. }
  34. private GUIStyle CreateStyle(Texture2D texture, Vector2 size)
  35. {
  36. var guiStyle = new GUIStyle();
  37. guiStyle.normal.background = texture;
  38. guiStyle.fixedWidth = size.x;
  39. guiStyle.fixedHeight = size.y;
  40. return guiStyle;
  41. }
  42. }
  43. internal class Drawer : IDrawer
  44. {
  45. private IGUIState m_GUIState = new GUIState();
  46. private DefaultStyles m_Styles;
  47. protected DefaultStyles styles
  48. {
  49. get
  50. {
  51. if (m_Styles == null)
  52. m_Styles = new DefaultStyles();
  53. return m_Styles;
  54. }
  55. }
  56. public void DrawSelectionRect(Rect rect)
  57. {
  58. Handles.BeginGUI();
  59. styles.selectionRectStyle.Draw(rect, GUIContent.none, false, false, false, false);
  60. Handles.EndGUI();
  61. }
  62. public void DrawCreatePointPreview(Vector3 position)
  63. {
  64. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointPreviewStyle);
  65. }
  66. public void DrawRemovePointPreview(Vector3 position)
  67. {
  68. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointRemovePreviewStyle);
  69. }
  70. public void DrawPoint(Vector3 position)
  71. {
  72. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointNormalStyle);
  73. }
  74. public void DrawPointHovered(Vector3 position)
  75. {
  76. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointHoveredStyle);
  77. }
  78. public void DrawPointSelected(Vector3 position)
  79. {
  80. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointSelectedStyle);
  81. }
  82. public void DrawLine(Vector3 p1, Vector3 p2, float width, Color color)
  83. {
  84. Handles.color = color;
  85. Handles.DrawAAPolyLine(width, new Vector3[] { p1, p2 });
  86. }
  87. public void DrawDottedLine(Vector3 p1, Vector3 p2, float width, Color color)
  88. {
  89. Handles.color = color;
  90. Handles.DrawDottedLine(p1, p2, width);
  91. }
  92. public void DrawBezier(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, float width, Color color)
  93. {
  94. Handles.color = color;
  95. Handles.DrawBezier(p1, p4, p2, p3, color, null, width);
  96. }
  97. public void DrawTangent(Vector3 position, Vector3 tangent)
  98. {
  99. DrawLine(position, tangent, 3f, Color.yellow);
  100. DrawGUIStyleCap(0, tangent, Quaternion.identity, m_GUIState.GetHandleSize(tangent), styles.tangentNormalStyle);
  101. }
  102. private void DrawGUIStyleCap(int controlID, Vector3 position, Quaternion rotation, float size, GUIStyle guiStyle)
  103. {
  104. if (Camera.current && Vector3.Dot(position - Camera.current.transform.position, Camera.current.transform.forward) < 0f)
  105. return;
  106. Handles.BeginGUI();
  107. guiStyle.Draw(GetGUIStyleRect(guiStyle, position), GUIContent.none, controlID);
  108. Handles.EndGUI();
  109. }
  110. private Rect GetGUIStyleRect(GUIStyle style, Vector3 position)
  111. {
  112. Vector2 vector = HandleUtility.WorldToGUIPoint(position);
  113. float fixedWidth = style.fixedWidth;
  114. float fixedHeight = style.fixedHeight;
  115. return new Rect(vector.x - fixedWidth / 2f, vector.y - fixedHeight / 2f, fixedWidth, fixedHeight);
  116. }
  117. }
  118. }