-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathGridBackground.cs
More file actions
221 lines (173 loc) · 8.62 KB
/
GridBackground.cs
File metadata and controls
221 lines (173 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.Experimental.GraphView
{
public class GridBackground : ImmediateModeElement
{
static CustomStyleProperty<float> s_SpacingProperty = new CustomStyleProperty<float>("--spacing");
static CustomStyleProperty<int> s_ThickLinesProperty = new CustomStyleProperty<int>("--thick-lines");
static CustomStyleProperty<Color> s_LineColorProperty = new CustomStyleProperty<Color>("--line-color");
static CustomStyleProperty<Color> s_ThickLineColorProperty = new CustomStyleProperty<Color>("--thick-line-color");
static CustomStyleProperty<Color> s_GridBackgroundColorProperty = new CustomStyleProperty<Color>("--grid-background-color");
static readonly float s_DefaultSpacing = 50f;
static readonly int s_DefaultThickLines = 10;
static readonly Color s_DefaultLineColor = new Color(0f, 0f, 0f, 0.18f);
static readonly Color s_DefaultThickLineColor = new Color(0f, 0f, 0f, 0.38f);
static readonly Color s_DefaultGridBackgroundColor = new Color(0.17f, 0.17f, 0.17f, 1.0f);
float m_Spacing = s_DefaultSpacing;
private float spacing => m_Spacing;
int m_ThickLines = s_DefaultThickLines;
private int thickLines => m_ThickLines;
Color m_LineColor = s_DefaultLineColor;
private Color lineColor => m_LineColor * playModeTintColor;
Color m_ThickLineColor = s_DefaultThickLineColor;
private Color thickLineColor => m_ThickLineColor * playModeTintColor;
Color m_GridBackgroundColor = s_DefaultGridBackgroundColor;
private Color gridBackgroundColor => m_GridBackgroundColor * playModeTintColor;
private VisualElement m_Container;
public GridBackground()
{
pickingMode = PickingMode.Ignore;
this.StretchToParentSize();
RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
}
private Vector3 Clip(Rect clipRect, Vector3 _in)
{
if (_in.x < clipRect.xMin)
_in.x = clipRect.xMin;
if (_in.x > clipRect.xMax)
_in.x = clipRect.xMax;
if (_in.y < clipRect.yMin)
_in.y = clipRect.yMin;
if (_in.y > clipRect.yMax)
_in.y = clipRect.yMax;
return _in;
}
private void OnCustomStyleResolved(CustomStyleResolvedEvent e)
{
float spacingValue = 0f;
int thicklinesValue = 0;
Color thicklineColorValue = Color.clear;
Color lineColorValue = Color.clear;
Color gridColorValue = Color.clear;
ICustomStyle customStyle = e.customStyle;
if (customStyle.TryGetValue(s_SpacingProperty, out spacingValue))
m_Spacing = spacingValue;
if (customStyle.TryGetValue(s_ThickLinesProperty, out thicklinesValue))
m_ThickLines = thickLines;
if (customStyle.TryGetValue(s_ThickLineColorProperty, out thicklineColorValue))
m_ThickLineColor = thicklineColorValue;
if (customStyle.TryGetValue(s_LineColorProperty, out lineColorValue))
m_LineColor = lineColorValue;
if (customStyle.TryGetValue(s_GridBackgroundColorProperty, out gridColorValue))
m_GridBackgroundColor = gridColorValue;
}
protected override void ImmediateRepaint()
{
VisualElement target = parent;
var graphView = target as GraphView;
if (graphView == null)
{
throw new InvalidOperationException("GridBackground can only be added to a GraphView");
}
m_Container = graphView.contentViewContainer;
Rect clientRect = graphView.layout;
// Since we're always stretch to parent size, we will use (0,0) as (x,y) coordinates
clientRect.x = 0;
clientRect.y = 0;
#pragma warning disable CS0618 // Type or member is obsolete
var containerScale = new Vector3(m_Container.transform.matrix.GetColumn(0).magnitude,
m_Container.transform.matrix.GetColumn(1).magnitude,
m_Container.transform.matrix.GetColumn(2).magnitude);
var containerTranslation = m_Container.transform.matrix.GetColumn(3);
#pragma warning restore CS0618 // Type or member is obsolete
var containerPosition = m_Container.layout;
// background
HandleUtility.ApplyWireMaterial();
GL.Begin(GL.QUADS);
GL.Color(gridBackgroundColor);
GL.Vertex(new Vector3(clientRect.x, clientRect.y));
GL.Vertex(new Vector3(clientRect.xMax, clientRect.y));
GL.Vertex(new Vector3(clientRect.xMax, clientRect.yMax));
GL.Vertex(new Vector3(clientRect.x, clientRect.yMax));
GL.End();
// vertical lines
Vector3 from = new Vector3(clientRect.x, clientRect.y, 0.0f);
Vector3 to = new Vector3(clientRect.x, clientRect.height, 0.0f);
var tx = Matrix4x4.TRS(containerTranslation, Quaternion.identity, Vector3.one);
from = tx.MultiplyPoint(from);
to = tx.MultiplyPoint(to);
from.x += (containerPosition.x * containerScale.x);
from.y += (containerPosition.y * containerScale.y);
to.x += (containerPosition.x * containerScale.x);
to.y += (containerPosition.y * containerScale.y);
float thickGridLineX = from.x;
float thickGridLineY = from.y;
// Update from/to to start at beginning of clientRect
from.x = (from.x % (spacing * (containerScale.x)) - (spacing * (containerScale.x)));
to.x = from.x;
from.y = clientRect.y;
to.y = clientRect.y + clientRect.height;
while (from.x < clientRect.width)
{
from.x += spacing * containerScale.x;
to.x += spacing * containerScale.x;
GL.Begin(GL.LINES);
GL.Color(lineColor);
GL.Vertex(Clip(clientRect, from));
GL.Vertex(Clip(clientRect, to));
GL.End();
}
float thickLineSpacing = (spacing * thickLines);
from.x = to.x = (thickGridLineX % (thickLineSpacing * (containerScale.x)) - (thickLineSpacing * (containerScale.x)));
while (from.x < clientRect.width + thickLineSpacing)
{
GL.Begin(GL.LINES);
GL.Color(thickLineColor);
GL.Vertex(Clip(clientRect, from));
GL.Vertex(Clip(clientRect, to));
GL.End();
from.x += (spacing * containerScale.x * thickLines);
to.x += (spacing * containerScale.x * thickLines);
}
// horizontal lines
from = new Vector3(clientRect.x, clientRect.y, 0.0f);
to = new Vector3(clientRect.x + clientRect.width, clientRect.y, 0.0f);
from.x += (containerPosition.x * containerScale.x);
from.y += (containerPosition.y * containerScale.y);
to.x += (containerPosition.x * containerScale.x);
to.y += (containerPosition.y * containerScale.y);
from = tx.MultiplyPoint(from);
to = tx.MultiplyPoint(to);
from.y = to.y = (from.y % (spacing * (containerScale.y)) - (spacing * (containerScale.y)));
from.x = clientRect.x;
to.x = clientRect.width;
while (from.y < clientRect.height)
{
from.y += spacing * containerScale.y;
to.y += spacing * containerScale.y;
GL.Begin(GL.LINES);
GL.Color(lineColor);
GL.Vertex(Clip(clientRect, from));
GL.Vertex(Clip(clientRect, to));
GL.End();
}
thickLineSpacing = spacing * thickLines;
from.y = to.y = (thickGridLineY % (thickLineSpacing * (containerScale.y)) - (thickLineSpacing * (containerScale.y)));
while (from.y < clientRect.height + thickLineSpacing)
{
GL.Begin(GL.LINES);
GL.Color(thickLineColor);
GL.Vertex(Clip(clientRect, from));
GL.Vertex(Clip(clientRect, to));
GL.End();
from.y += spacing * containerScale.y * thickLines;
to.y += spacing * containerScale.y * thickLines;
}
}
}
}