BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Editor / OVROverlayEditor.cs

OVROverlayEditor.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVROverlayEditor.cs 21.2 KiB Download Raw
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEditor;
5
6[CustomEditor(typeof(OVROverlay))]
7public class OVROverlayEditor : Editor
8{
9
10 /// <summary>
11 /// Common Video Types, to ease source and dest rect creation
12 /// </summary>
13 public enum StereoType
14 {
15 Custom = 0,
16 Mono = 1,
17 Stereo = 2,
18 StereoLeftRight = 3,
19 StereoTopBottom = 4,
20 }
21
22 public enum DisplayType
23 {
24 Custom = 0,
25 Full = 1,
26 Half = 2
27 }
28
29 private bool sourceRectsVisible = false;
30 private bool destRectsVisible = false;
31
32 private Material _SrcRectMaterial;
33 protected Material SrcRectMaterial
34 {
35 get
36 {
37 if (_SrcRectMaterial == null)
38 {
39 string[] shaders = AssetDatabase.FindAssets("OVROverlaySrcRectEditor");
40
41 if (shaders.Length > 0)
42 {
43 Shader shader = (Shader)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(shaders[0]), typeof(Shader));
44
45 if (shader != null)
46 {
47 _SrcRectMaterial = new Material(shader);
48 }
49 }
50 }
51 return _SrcRectMaterial;
52 }
53 }
54
55 private Material _DestRectMaterial;
56 protected Material DestRectMaterial
57 {
58 get
59 {
60 if (_DestRectMaterial == null)
61 {
62 string[] shaders = AssetDatabase.FindAssets("OVROverlayDestRectEditor");
63
64 if (shaders.Length > 0)
65 {
66 Shader shader = (Shader)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(shaders[0]), typeof(Shader));
67
68 if (shader != null)
69 {
70 _DestRectMaterial = new Material(shader);
71 }
72 }
73 }
74 return _DestRectMaterial;
75 }
76 }
77
78 private TextureRect _DraggingRect;
79 private Side _DraggingSide;
80
81 enum TextureRect
82 {
83 None,
84 SrcLeft,
85 SrcRight,
86 DestLeft,
87 DestRight
88 }
89
90 enum Side
91 {
92 Left,
93 Right,
94 Top,
95 Bottom
96 }
97
98 public override void OnInspectorGUI()
99 {
100 OVROverlay overlay = (OVROverlay)target;
101 if (overlay == null)
102 {
103 return;
104 }
105
106 EditorGUILayout.LabelField("Display Order", EditorStyles.boldLabel);
107 overlay.currentOverlayType = (OVROverlay.OverlayType)EditorGUILayout.EnumPopup(new GUIContent("Current Overlay Type", "Whether this overlay should layer behind the scene or in front of it"), overlay.currentOverlayType);
108 overlay.compositionDepth = EditorGUILayout.IntField(new GUIContent("Composition Depth", "Depth value used to sort OVROverlays in the scene, smaller value appears in front"), overlay.compositionDepth);
109 overlay.noDepthBufferTesting = EditorGUILayout.Toggle(new GUIContent("No Depth Buffer Testing", "The noDepthBufferTesting will stop layer's depth buffer compositing even if the engine has \"Shared Depth Buffer\" enabled"), overlay.noDepthBufferTesting);
110 EditorGUILayout.Space();
111
112 EditorGUILayout.LabelField(new GUIContent("Overlay Shape", "The shape of this overlay"), EditorStyles.boldLabel);
113 overlay.currentOverlayShape = (OVROverlay.OverlayShape)EditorGUILayout.EnumPopup(new GUIContent("Overlay Shape", "The shape of this overlay"), overlay.currentOverlayShape);
114 EditorGUILayout.Space();
115
116 EditorGUILayout.Separator();
117 EditorGUILayout.LabelField("Textures", EditorStyles.boldLabel);
118
119#if UNITY_ANDROID
120 bool lastIsExternalSurface = overlay.isExternalSurface;
121 overlay.isExternalSurface = EditorGUILayout.Toggle(new GUIContent("Is External Surface", "On Android, retrieve an Android Surface object to render to (e.g., video playback)"), overlay.isExternalSurface);
122
123 if (lastIsExternalSurface)
124 {
125 overlay.externalSurfaceWidth = EditorGUILayout.IntField("External Surface Width", overlay.externalSurfaceWidth);
126 overlay.externalSurfaceHeight = EditorGUILayout.IntField("External Surface Height", overlay.externalSurfaceHeight);
127 overlay.isProtectedContent = EditorGUILayout.Toggle(new GUIContent("Is Protected Content", "The external surface has L1 widevine protection."), overlay.isProtectedContent);
128 }
129 else
130#endif
131 {
132 if (overlay.textures == null)
133 {
134 overlay.textures = new Texture[2];
135 }
136 if (overlay.textures.Length < 2)
137 {
138 Texture[] tmp = new Texture[2];
139 for (int i = 0; i < overlay.textures.Length; i++)
140 {
141 tmp[i] = overlay.textures[i];
142 }
143 overlay.textures = tmp;
144 }
145
146 var labelControlRect = EditorGUILayout.GetControlRect();
147 EditorGUI.LabelField(new Rect(labelControlRect.x, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Left Texture", "Texture used for the left eye"));
148 EditorGUI.LabelField(new Rect(labelControlRect.x + labelControlRect.width / 2, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Right Texture", "Texture used for the right eye"));
149
150
151 var textureControlRect = EditorGUILayout.GetControlRect(GUILayout.Height(64));
152
153 overlay.textures[0] = (Texture)EditorGUI.ObjectField(new Rect(textureControlRect.x, textureControlRect.y, 64, textureControlRect.height), overlay.textures[0], typeof(Texture), true);
154 Texture right = (Texture)EditorGUI.ObjectField(new Rect(textureControlRect.x + textureControlRect.width / 2, textureControlRect.y, 64, textureControlRect.height), overlay.textures[1] != null ? overlay.textures[1] : overlay.textures[0], typeof(Texture), true);
155 if (right == overlay.textures[0])
156 {
157 overlay.textures[1] = null;
158 }
159 else
160 {
161 overlay.textures[1] = right;
162 }
163
164 overlay.isDynamic = EditorGUILayout.Toggle(new GUIContent("Dynamic Texture", "This texture will be updated dynamically at runtime (e.g., Video)"), overlay.isDynamic);
165#if !UNITY_ANDROID
166 overlay.isProtectedContent = EditorGUILayout.Toggle(new GUIContent("Is Protected Content", "The texture has copy protection, e.g., HDCP"), overlay.isProtectedContent);
167#endif
168 }
169 if (overlay.currentOverlayShape == OVROverlay.OverlayShape.Cylinder || overlay.currentOverlayShape == OVROverlay.OverlayShape.Equirect || overlay.currentOverlayShape == OVROverlay.OverlayShape.Quad) {
170
171 EditorGUILayout.Separator();
172 EditorGUILayout.Space();
173 EditorGUILayout.LabelField("Texture Rects", EditorStyles.boldLabel);
174
175 bool lastOverrideTextureRectMatrix = overlay.overrideTextureRectMatrix;
176 overlay.overrideTextureRectMatrix = !EditorGUILayout.Toggle(new GUIContent("Use Default Rects", overlay.textures[1] == null ? "If you need to use a single texture as a stereo image, uncheck this box" : "Uncheck this box if you need to clip you textures or layer"), !overlay.overrideTextureRectMatrix);
177
178 if (lastOverrideTextureRectMatrix)
179 {
180 sourceRectsVisible = EditorGUILayout.Foldout(sourceRectsVisible, new GUIContent("Source Rects", "What portion of the source texture will ultimately be shown in each eye."));
181
182 if (sourceRectsVisible)
183 {
184 var mat = SrcRectMaterial;
185
186 if (mat != null)
187 {
188 Rect drawRect = EditorGUILayout.GetControlRect(GUILayout.Height(128 + 8));
189 mat.SetVector("_SrcRectLeft", new Vector4(Mathf.Max(0.0f, overlay.srcRectLeft.x), Mathf.Max(0.0f, overlay.srcRectLeft.y), Mathf.Min(1.0f - overlay.srcRectLeft.x, overlay.srcRectLeft.width), Mathf.Min(1.0f - overlay.srcRectLeft.y, overlay.srcRectLeft.height)));
190 mat.SetVector("_SrcRectRight", new Vector4(Mathf.Max(0.0f, overlay.srcRectRight.x), Mathf.Max(0.0f, overlay.srcRectRight.y), Mathf.Min(1.0f - overlay.srcRectRight.x, overlay.srcRectRight.width), Mathf.Min(1.0f - overlay.srcRectRight.y, overlay.srcRectRight.height)));
191 // center our draw rect
192 var drawRectCentered = new Rect(drawRect.x + drawRect.width / 2 - 128 - 4, drawRect.y, 256 + 8, drawRect.height);
193 EditorGUI.DrawPreviewTexture(drawRectCentered, overlay.textures[0] ?? Texture2D.blackTexture, mat);
194
195 var drawRectInset = new Rect(drawRectCentered.x + 4, drawRectCentered.y + 4, drawRectCentered.width - 8, drawRectCentered.height - 8);
196 UpdateRectDragging(drawRectInset, drawRectInset, TextureRect.SrcLeft, TextureRect.SrcRight, ref overlay.srcRectLeft, ref overlay.srcRectRight);
197 CreateCursorRects(drawRectInset, overlay.srcRectLeft);
198 CreateCursorRects(drawRectInset, overlay.srcRectRight);
199 }
200
201 var labelControlRect = EditorGUILayout.GetControlRect();
202 EditorGUI.LabelField(new Rect(labelControlRect.x, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Left Source Rect", "The rect in the source image that will be displayed on the left eye layer"));
203 EditorGUI.LabelField(new Rect(labelControlRect.x + labelControlRect.width / 2, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Right Source Rect", "The rect in the source image that will be displayed on the right eye layer"));
204
205 var rectControlRect = EditorGUILayout.GetControlRect(GUILayout.Height(34));
206
207 overlay.srcRectLeft = Clamp01(EditorGUI.RectField(new Rect(rectControlRect.x, rectControlRect.y, rectControlRect.width / 2 - 20, rectControlRect.height), overlay.srcRectLeft));
208 overlay.srcRectRight = Clamp01(EditorGUI.RectField(new Rect(rectControlRect.x + rectControlRect.width / 2, rectControlRect.y, rectControlRect.width / 2 - 20, rectControlRect.height), overlay.srcRectRight));
209
210
211 EditorGUILayout.BeginHorizontal();
212 if (overlay.textures[1] != null)
213 {
214 if (GUILayout.Button(new GUIContent("Reset To Default", "Reset Source Rects to default")))
215 {
216 SetRectsByVideoType(overlay, StereoType.Stereo, DisplayType.Custom);
217 }
218 }
219 else
220 {
221 if (GUILayout.Button(new GUIContent("Monoscopic", "Display the full Texture in both eyes")))
222 {
223 SetRectsByVideoType(overlay, StereoType.Mono, DisplayType.Custom);
224 }
225 if (GUILayout.Button(new GUIContent("Stereo Left/Right", "The left half of the texture is displayed in the left eye, and the right half in the right eye")))
226 {
227 SetRectsByVideoType(overlay, StereoType.StereoLeftRight, DisplayType.Custom);
228 }
229 if (GUILayout.Button(new GUIContent("Stereo Top/Bottom", "The top half of the texture is displayed in the left eye, and the bottom half in the right eye")))
230 {
231 SetRectsByVideoType(overlay, StereoType.StereoTopBottom, DisplayType.Custom);
232 }
233 }
234 EditorGUILayout.EndHorizontal();
235
236 }
237 destRectsVisible = EditorGUILayout.Foldout(destRectsVisible, new GUIContent("Destination Rects", "What portion of the destination texture that the source will be rendered into."));
238 if (destRectsVisible)
239 {
240
241 var mat = DestRectMaterial;
242
243 if (mat != null)
244 {
245 Rect drawRect = EditorGUILayout.GetControlRect(GUILayout.Height(128 + 8));
246 mat.SetVector("_SrcRectLeft", new Vector4(Mathf.Max(0.0f, overlay.srcRectLeft.x), Mathf.Max(0.0f, overlay.srcRectLeft.y), Mathf.Min(1.0f - overlay.srcRectLeft.x, overlay.srcRectLeft.width), Mathf.Min(1.0f - overlay.srcRectLeft.y, overlay.srcRectLeft.height)));
247 mat.SetVector("_SrcRectRight", new Vector4(Mathf.Max(0.0f, overlay.srcRectRight.x), Mathf.Max(0.0f, overlay.srcRectRight.y), Mathf.Min(1.0f - overlay.srcRectRight.x, overlay.srcRectRight.width), Mathf.Min(1.0f - overlay.srcRectRight.y, overlay.srcRectRight.height)));
248 mat.SetVector("_DestRectLeft", new Vector4(Mathf.Max(0.0f, overlay.destRectLeft.x), Mathf.Max(0.0f, overlay.destRectLeft.y), Mathf.Min(1.0f - overlay.destRectLeft.x, overlay.destRectLeft.width), Mathf.Min(1.0f - overlay.destRectLeft.y, overlay.destRectLeft.height)));
249 mat.SetVector("_DestRectRight", new Vector4(Mathf.Max(0.0f, overlay.destRectRight.x), Mathf.Max(0.0f, overlay.destRectRight.y), Mathf.Min(1.0f - overlay.destRectRight.x, overlay.destRectRight.width), Mathf.Min(1.0f - overlay.destRectRight.y, overlay.destRectRight.height)));
250 mat.SetColor("_BackgroundColor", EditorGUIUtility.isProSkin ? (Color)new Color32(56, 56, 56, 255) : (Color)new Color32(194, 194, 194, 255));
251
252 var drawRectCentered = new Rect(drawRect.x + drawRect.width / 2 - 128 - 16 - 4, drawRect.y, 256 + 32 + 8, drawRect.height);
253 // center our draw rect
254 EditorGUI.DrawPreviewTexture(drawRectCentered, overlay.textures[0] ?? Texture2D.blackTexture, mat);
255
256 var drawRectInsetLeft = new Rect(drawRectCentered.x + 4, drawRectCentered.y + 4, drawRectCentered.width / 2 - 20, drawRectCentered.height - 8);
257 var drawRectInsetRight = new Rect(drawRectCentered.x + drawRectCentered.width / 2 + 16, drawRectCentered.y + 4, drawRectCentered.width / 2 - 20, drawRectCentered.height - 8);
258 UpdateRectDragging(drawRectInsetLeft, drawRectInsetRight, TextureRect.DestLeft, TextureRect.DestRight, ref overlay.destRectLeft, ref overlay.destRectRight);
259
260 CreateCursorRects(drawRectInsetLeft, overlay.destRectLeft);
261 CreateCursorRects(drawRectInsetRight, overlay.destRectRight);
262
263 }
264
265 var labelControlRect = EditorGUILayout.GetControlRect();
266 EditorGUI.LabelField(new Rect(labelControlRect.x, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Left Destination Rect", "The rect in the destination layer the left eye will display to"));
267 EditorGUI.LabelField(new Rect(labelControlRect.x + labelControlRect.width / 2, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Right Destination Rect", "The rect in the destination layer the right eye will display to"));
268
269 var rectControlRect = EditorGUILayout.GetControlRect(GUILayout.Height(34));
270
271 overlay.destRectLeft = Clamp01(EditorGUI.RectField(new Rect(rectControlRect.x, rectControlRect.y, rectControlRect.width / 2 - 20, rectControlRect.height), overlay.destRectLeft));
272 overlay.destRectRight = Clamp01(EditorGUI.RectField(new Rect(rectControlRect.x + rectControlRect.width / 2, rectControlRect.y, rectControlRect.width / 2 - 20, rectControlRect.height), overlay.destRectRight));
273
274
275 if (overlay.currentOverlayShape == OVROverlay.OverlayShape.Equirect)
276 {
277 EditorGUILayout.BeginHorizontal();
278 if (GUILayout.Button(new GUIContent("360 Video", "Display the full 360 layer")))
279 {
280 SetRectsByVideoType(overlay, StereoType.Custom, DisplayType.Full);
281 }
282 if (GUILayout.Button(new GUIContent("180 Video", "Display the front 180 layer")))
283 {
284 SetRectsByVideoType(overlay, StereoType.Custom, DisplayType.Half);
285 }
286 EditorGUILayout.EndHorizontal();
287 }
288 else
289 {
290 if (GUILayout.Button(new GUIContent("Reset To Default", "Reset Source Rects to default")))
291 {
292 SetRectsByVideoType(overlay, StereoType.Custom, DisplayType.Full);
293 }
294 }
295 }
296 }
297 }
298 EditorGUILayout.Separator();
299 EditorGUILayout.LabelField("Color Scale", EditorStyles.boldLabel);
300 EditorGUILayout.Space();
301 overlay.overridePerLayerColorScaleAndOffset = EditorGUILayout.Toggle(new GUIContent("Override Color Scale", "Manually set color scale and offset of this layer, regardless of what the global values are from OVRManager.SetColorScaleAndOffset()."), overlay.overridePerLayerColorScaleAndOffset);
302 if (overlay.overridePerLayerColorScaleAndOffset)
303 {
304 Vector4 colorScale = EditorGUILayout.Vector4Field(new GUIContent("Color Scale", "Scale that the color values for this overlay will be multiplied by."), overlay.colorScale);
305 Vector4 colorOffset = EditorGUILayout.Vector4Field(new GUIContent("Color Offset", "Offset that the color values for this overlay will be added to."), overlay.colorOffset);
306 overlay.SetPerLayerColorScaleAndOffset(colorScale, colorOffset);
307 }
308
309 EditorUtility.SetDirty(overlay);
310 }
311
312 private Rect Clamp01(Rect rect)
313 {
314 rect.x = Mathf.Clamp01(rect.x);
315 rect.y = Mathf.Clamp01(rect.y);
316 rect.width = Mathf.Clamp01(rect.width);
317 rect.height = Mathf.Clamp01(rect.height);
318 return rect;
319 }
320
321 private bool IsUnitRect(Rect rect)
322 {
323 return IsRect(rect, 0, 0, 1, 1);
324 }
325
326 private bool IsRect(Rect rect, float x, float y, float w, float h)
327 {
328 return rect.x == x && rect.y == y && rect.width == w && rect.height == h;
329 }
330
331 private StereoType GetStereoType(OVROverlay overlay)
332 {
333 if (overlay.textures[0] != null && overlay.textures[1] != null)
334 {
335 if (IsUnitRect(overlay.srcRectLeft) && IsUnitRect(overlay.srcRectRight))
336 {
337 return StereoType.Stereo;
338 }
339 else
340 {
341 return StereoType.Custom;
342 }
343 }
344 else if (overlay.textures[0] != null)
345 {
346 if (IsUnitRect(overlay.srcRectLeft) && IsUnitRect(overlay.srcRectRight))
347 {
348 return StereoType.Mono;
349 }
350 else if (IsRect(overlay.srcRectLeft, 0, 0, 0.5f, 1f) && IsRect(overlay.srcRectRight, 0.5f, 0, 0.5f, 1f))
351 {
352 return StereoType.StereoLeftRight;
353 }
354 else if (IsRect(overlay.srcRectLeft, 0, 0, 1f, 0.5f) && IsRect(overlay.srcRectRight, 0f, 0.5f, 1f, 0.5f))
355 {
356 return StereoType.StereoTopBottom;
357 }
358 else
359 {
360 return StereoType.Custom;
361 }
362 }
363 else
364 {
365 return StereoType.Mono;
366 }
367 }
368
369 private void SetRectsByVideoType(OVROverlay overlay, StereoType stereoType, DisplayType displayType)
370 {
371 Rect srcRectLeft, srcRectRight, destRectLeft, destRectRight;
372
373 switch (displayType)
374 {
375 case DisplayType.Full:
376 destRectLeft = destRectRight = new Rect(0, 0, 1, 1);
377 break;
378
379 case DisplayType.Half:
380 destRectLeft = destRectRight = new Rect(0.25f, 0, 0.5f, 1);
381 break;
382 default:
383 destRectLeft = overlay.destRectLeft;
384 destRectRight = overlay.destRectRight;
385 break;
386 }
387
388 switch (stereoType)
389 {
390 case StereoType.Mono:
391 case StereoType.Stereo:
392 srcRectLeft = srcRectRight = new Rect(0, 0, 1, 1);
393 break;
394
395 case StereoType.StereoTopBottom:
396 srcRectLeft = new Rect(0, 0, 1, 0.5f);
397 srcRectRight = new Rect(0, 0.5f, 1, 0.5f);
398 break;
399
400 case StereoType.StereoLeftRight:
401 srcRectLeft = new Rect(0, 0, 0.5f, 1);
402 srcRectRight = new Rect(0.5f, 0, 0.5f, 1);
403 break;
404 default:
405 srcRectLeft = overlay.srcRectLeft;
406 srcRectRight = overlay.srcRectRight;
407 break;
408 }
409 overlay.SetSrcDestRects(srcRectLeft, srcRectRight, destRectLeft, destRectRight);
410 }
411
412 private void GetCursorPoints(Rect drawRect, Rect selectRect, out Vector2 leftPos, out Vector2 rightPos, out Vector2 topPos, out Vector2 bottomPos)
413 {
414 leftPos = new Vector2(drawRect.x + selectRect.x * drawRect.width, drawRect.y + (selectRect.y + selectRect.height / 2) * drawRect.height);
415 rightPos = new Vector2(drawRect.x + (selectRect.x + selectRect.width) * drawRect.width, drawRect.y + (selectRect.y + selectRect.height / 2) * drawRect.height);
416 topPos = new Vector2(drawRect.x + (selectRect.x + selectRect.width / 2) * drawRect.width, drawRect.y + (selectRect.y) * drawRect.height);
417 bottomPos = new Vector2(drawRect.x + (selectRect.x + selectRect.width / 2) * drawRect.width, drawRect.y + (selectRect.y + selectRect.height) * drawRect.height);
418 }
419
420 private void CreateCursorRects(Rect drawRect, Rect selectRect)
421 {
422 Vector2 leftPos, rightPos, topPos, bottomPos;
423 GetCursorPoints(drawRect, selectRect, out leftPos, out rightPos, out topPos, out bottomPos);
424
425 EditorGUIUtility.AddCursorRect(new Rect(leftPos - 5 * Vector2.one, 10 * Vector2.one), MouseCursor.ResizeHorizontal);
426 EditorGUIUtility.AddCursorRect(new Rect(rightPos - 5 * Vector2.one, 10 * Vector2.one), MouseCursor.ResizeHorizontal);
427 EditorGUIUtility.AddCursorRect(new Rect(topPos - 5 * Vector2.one, 10 * Vector2.one), MouseCursor.ResizeVertical);
428 EditorGUIUtility.AddCursorRect(new Rect(bottomPos - 5 * Vector2.one, 10 * Vector2.one), MouseCursor.ResizeVertical);
429 }
430
431 private bool IsOverRectControls(Rect drawRect, Vector2 mousePos, Rect selectRect, ref Side side)
432 {
433 Vector2 leftPos, rightPos, topPos, bottomPos;
434 GetCursorPoints(drawRect, selectRect, out leftPos, out rightPos, out topPos, out bottomPos);
435
436 if ((leftPos - mousePos).sqrMagnitude <= 25)
437 {
438 side = Side.Left;
439 return true;
440 }
441 if ((rightPos - mousePos).sqrMagnitude <= 25)
442 {
443 side = Side.Right;
444 return true;
445 }
446 if ((topPos - mousePos).sqrMagnitude <= 25)
447 {
448 side = Side.Top;
449 return true;
450 }
451 if ((bottomPos - mousePos).sqrMagnitude <= 25)
452 {
453 side = Side.Bottom;
454 return true;
455 }
456 return false;
457 }
458
459 private void UpdateRectDragging(Rect drawingRectLeft, Rect drawingRectRight, TextureRect rectLeftType, TextureRect rectRightType, ref Rect rectLeft, ref Rect rectRight)
460 {
461 if (!Event.current.isMouse || Event.current.button != 0)
462 {
463 return;
464 }
465
466 if (Event.current.type == EventType.MouseUp)
467 {
468 _DraggingRect = TextureRect.None;
469 return;
470 }
471
472 Vector2 mousePos = Event.current.mousePosition;
473 if (_DraggingRect == TextureRect.None && Event.current.type == EventType.MouseDown)
474 {
475 if (IsOverRectControls(drawingRectLeft, mousePos, rectLeft, ref _DraggingSide))
476 {
477 _DraggingRect = rectLeftType;
478 }
479 if (_DraggingRect == TextureRect.None || Event.current.shift)
480 {
481 if (IsOverRectControls(drawingRectRight, mousePos, rectRight, ref _DraggingSide))
482 {
483 _DraggingRect = rectRightType;
484 }
485 }
486 }
487
488 if (_DraggingRect == rectLeftType)
489 {
490 SetRectSideValue(drawingRectLeft, mousePos, _DraggingSide, ref rectLeft);
491 }
492 if (_DraggingRect == rectRightType)
493 {
494 SetRectSideValue(drawingRectRight, mousePos, _DraggingSide, ref rectRight);
495 }
496 }
497
498 private void SetRectSideValue(Rect drawingRect, Vector2 mousePos, Side side, ref Rect rect)
499 {
500 // quantize to 1/32
501 float x = Mathf.Clamp01(Mathf.Round(((mousePos.x - drawingRect.x) / drawingRect.width) * 32) / 32.0f);
502 float y = Mathf.Clamp01(Mathf.Round(((mousePos.y - drawingRect.y) / drawingRect.height) * 32) / 32.0f);
503
504 switch (side)
505 {
506 case Side.Left:
507 float xMax = rect.xMax;
508 rect.x = Mathf.Min(x, xMax);
509 rect.width = xMax - rect.x;
510 break;
511 case Side.Right:
512 rect.width = Mathf.Max(0, x - rect.x);
513 break;
514 case Side.Top:
515 float yMax = rect.yMax;
516 rect.y = Mathf.Min(y, yMax);
517 rect.height = yMax - rect.y;
518 break;
519 case Side.Bottom:
520 rect.height = Mathf.Max(0, y - rect.y);
521 break;
522 }
523 }
524}