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