Exemple #1
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            _drag = CodeUtils.Drag2D(_drag, r);

            var previewWidth = EditorGUIUtility.currentViewWidth - 10;

            if (!_previewAll)
            {
                EditorGUILayout.BeginVertical(GUILayout.Width(previewWidth));

                GUILayout.Label(string.Format("Show: {0}, {1}", currentPreviewIndex, _currentObjectToPreview.name), EditorStyles.whiteBoldLabel);

                EditorGUILayout.BeginHorizontal(GUILayout.Width(previewWidth));

                if (GUILayout.Button("Next"))
                {
                    int newIndex = (_lastIndex + 1) % instance.AssociatedObjects.Count;
                    _currentObjectToPreview = instance.AssociatedObjects[newIndex];
                    _lastIndex = newIndex;
                }

                if (GUILayout.Button("Previous"))
                {
                    var prev     = ((_lastIndex - 1)) % instance.AssociatedObjects.Count;
                    int newIndex = prev >= 0 ? prev : instance.AssociatedObjects.Count + prev;
                    _currentObjectToPreview = instance.AssociatedObjects[newIndex];
                    _lastIndex = newIndex;
                }

                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginVertical();
            }

            if (Event.current.type == EventType.Repaint)
            {
                if (!instance.AssociatedObjects.Any())
                {
                    EditorGUI.DropShadowLabel(r, "Has no objects!");
                }
                else
                {
                    if (!_previewAll)
                    {
                        var resultTexture = RenderObjectPreview(r, _currentObjectToPreview, background);

                        RenderObjectOrError(r, _currentObjectToPreview.name, resultTexture);
                    }
                    else
                    {
                        var previewGrid = EstimatePreviewGrid(r, instance.AssociatedObjects.Count);

                        int rows = previewGrid.GetLength(0);
                        int cols = previewGrid.GetLength(1);

                        var objectIndex = 0;

                        for (int i = 0; i < rows; i++)
                        {
                            for (int j = 0; j < cols; j++)
                            {
                                var currentObject = instance.AssociatedObjects[objectIndex];

                                var resultTexture       = RenderObjectPreview(r, currentObject, background);
                                var currentPositionRect = previewGrid[i, j];
                                RenderObjectOrError(currentPositionRect, currentObject.name, resultTexture);
                                objectIndex++;
                            }
                        }
                    }
                }
            }
        }