Exemple #1
0
        public override void OnGUI()
        {
            var controlId = GUIUtility.GetControlID(s_DropZoneHash, FocusType.Passive);
            var rect      = GUILayoutUtility.GetRect(0, float.MaxValue, 0, float.MaxValue, GUIStyle.none, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));

            // On layout event, the rect returned is a dummy (0, 0, 1, 1) and the request is stored
            // On repaint event, the rect has the coorect value
            // But, GUILayout.BeginArea register the size on the layout event only
            // So we need to give to GUILayout.BeginArea the rect calculated from the previous repaint event
            if (Event.current.type == EventType.Repaint)
            {
                m_Rect = rect;
            }
            if (m_Rect.x == 0 && m_Rect.y == 0 && m_Rect.width == 0 && m_Rect.height == 0)
            {
                Repaint();
            }
            GUILayout.BeginArea(m_Rect);
            base.OnGUI();
            GUILayout.EndArea();
            if (EditorGUIX.DropZone(controlId, m_Rect, DropCheck) && Dropped != null)
            {
                Dropped(new DropEventArgs(DragAndDrop.objectReferences, DragAndDrop.paths));
            }
        }
        public override void OnGUI()
        {
            var controlId  = EditorGUIUtility.GetControlID(kCanvasHash, FocusType.Passive);
            var dropZoneId = EditorGUIUtility.GetControlID(kDropZoneHash, FocusType.Passive);

            var texture = GetValue(kPreviewTexture);

            var hasTexture    = texture != null;
            var targetTexture = (Texture)texture ?? Texture2D.whiteTexture;

            Rect canvasRectViewport;

            if (hasTexture)
            {
                var width  = texture != null ? texture.width : 256;
                var height = texture != null ? texture.height : 256;

                var cameraPosition = GetValue(kCameraPosition);
                var zoom           = GetValue(kZoom);
                EditorGUIXLayout.Canvas(controlId, ref cameraPosition, ref zoom, null, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                canvasRectViewport = GUILayoutUtility.GetLastRect();
                SetValue(kCameraPosition, cameraPosition);
                SetValue(kZoom, zoom);

                GUI.BeginClip(canvasRectViewport);
                DelightingHelpers.PushSRGBWrite(false);
                GUI.DrawTexture(EditorGUIX.GetCanvasDestinationRect(cameraPosition, zoom, width, height), targetTexture, ScaleMode.ScaleToFit);
                DelightingHelpers.PopSRGBWrite();

                if (Event.current.type == EventType.Repaint)
                {
                    m_Separator.OnGUI();
                    if (GetValue(kOverrideReferenceZone))
                    {
                        m_Gizmo.OnGUI();
                    }
                }
                else
                {
                    if (GetValue(kOverrideReferenceZone))
                    {
                        m_Gizmo.OnGUI();
                    }
                    m_Separator.OnGUI();
                }
                GUI.EndClip();

                var fitCanvasToWindow = GetValue(kFitCanvasToWindow);
                if (fitCanvasToWindow && Event.current.type == EventType.Repaint)
                {
                    EditorGUIX.CancelCanvasZoom(controlId);
                    var widthRatio   = canvasRectViewport.width / (float)width;
                    var heightRatio  = canvasRectViewport.height / (float)height;
                    var widthOffset  = 0f;
                    var heightOffset = 0f;
                    if (widthRatio < heightRatio)
                    {
                        zoom         = widthRatio;
                        heightOffset = (canvasRectViewport.height - zoom * height) * 0.5f;
                    }
                    else
                    {
                        zoom        = heightRatio;
                        widthOffset = (canvasRectViewport.width - zoom * width) * 0.5f;
                    }
                    SetValue(kZoom, zoom);
                    SetValue(kCameraPosition, new Vector2(widthOffset, heightOffset));
                    SetValue(kFitCanvasToWindow, false);
                }
            }
            else
            {
                EditorGUIXLayout.DropZoneHint(Content.dropZoneLabel);
                canvasRectViewport = GUILayoutUtility.GetLastRect();
            }

            if (EditorGUIX.DropZone(dropZoneId, canvasRectViewport, CanAcceptCallback))
            {
                var objs = DragAndDrop.objectReferences;
                if (objs.Length > 0)
                {
                    var path = AssetDatabase.GetAssetPath(objs[0]);
                    if (AssetDatabase.IsValidFolder(path))
                    {
                        SetValue(kInputFolderPath, path);
                        ExecuteCommand(kCmdLoadInputFolder);
                    }
                }
            }
        }