Example #1
0
        /// <summary>
        /// Renders an interactive Noise Preview along with tooltip icons and an optional Export button that opens a new ExportNoiseWindow.
        /// A background image is also rendered behind the preview that takes up the entire width of the EditorWindow currently being drawn.
        /// </summary>
        /// <param name = "minSize"> Minimum size for the Preview </param>
        /// <param name = "showExportButton"> Whether or not to render the Export button </param>
        public void DrawPreviewTexture(float minSize, ref bool isLocked)
        {
            // Draw label with tooltip
            //GUILayout.Label( Styles.noisePreview );

            float padding     = 4f;
            float iconWidth   = 40f;
            int   size        = (int)Mathf.Min(minSize, EditorGUIUtility.currentViewWidth);
            Rect  currentRect = rect;

            currentRect.y += EditorGUIUtility.singleLineHeight * 2f;
            Rect totalRect = new Rect(currentRect.x, currentRect.y, currentRect.width, size); //GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, size + padding * 2); // extra pixels for highlight border

            Color prev = GUI.color;

            GUI.color = new Color(.1f, .1f, .1f, 1f);
            GUI.DrawTexture(totalRect, Texture2D.whiteTexture, ScaleMode.StretchToFill, false);
            GUI.color = Color.white;

            // draw info icon
            // if(totalRect.Contains(Event.current.mousePosition))
            {
                Rect infoIconRect = new Rect(totalRect.x + padding, totalRect.y + padding, iconWidth, iconWidth);
                GUI.Label(infoIconRect, Styles.infoIcon);
                // GUI.Label( infoIconRect, Styles.noiseTooltip );
            }

            float buttonWidth    = GUI.skin.button.CalcSize(Styles.buttonUnlocked).x;
            float buttonHeight   = EditorGUIUtility.singleLineHeight;
            Rect  lockButtonRect = new Rect(totalRect.x + padding, totalRect.y + padding * 2f + GUI.skin.button.CalcSize(Styles.infoIcon).y, buttonWidth, buttonHeight);


            if (isLocked)
            {
                if (GUI.Button(lockButtonRect, Styles.buttonLocked))
                {
                    isLocked = false;
                }
            }
            else
            {
                if (GUI.Button(lockButtonRect, Styles.buttonUnlocked))
                {
                    isLocked = true;
                }
            }

            m_noisePreviewIsLocked = isLocked;
            // draw export button
            //Rect exportRect = new Rect( totalRect.xMax - buttonWidth - padding, totalRect.yMax - buttonHeight - padding, buttonWidth, buttonHeight );
            //if(GUI.Button(exportRect, Styles.export))
            //{
            //    serializedNoise.ApplyModifiedProperties();
            //    serializedNoise.Update();

            //    ExportNoiseWindow.ShowWindow( serializedNoise.targetObject as NoiseSettings );
            //}

            float safeSpace   = Mathf.Max(iconWidth * 2, buttonWidth * 2) + padding * 4;
            float minWidth    = Mathf.Min(size, totalRect.width - safeSpace);
            Rect  previewRect = new Rect(totalRect.x + totalRect.width / 2 - minWidth / 2, totalRect.y + totalRect.height / 2 - minWidth / 2, minWidth, minWidth);

            EditorGUIUtility.AddCursorRect(previewRect, MouseCursor.Pan);

            if (previewRect.Contains(Event.current.mousePosition) && !isLocked)
            {
                serializedNoise.Update();

                HandlePreviewTextureInput(previewRect);

                serializedNoise.ApplyModifiedProperties();
            }

            if (Event.current.type == EventType.Repaint)
            {
                // create preview RT here and keep until the next Repaint
                if (m_previewRT != null)
                {
                    RenderTexture.ReleaseTemporary(m_previewRT);
                }

                NoiseSettings noiseSettings = serializedNoise.targetObject as NoiseSettings;

                m_previewRT = RenderTexture.GetTemporary(512, 512, 0, RenderTextureFormat.ARGB32);
                RenderTexture tempRT = RenderTexture.GetTemporary(512, 512, 0, RenderTextureFormat.RFloat);

                RenderTexture prevActive = RenderTexture.active;

                NoiseUtils.Blit2D(noiseSettings, tempRT);

                NoiseUtils.BlitPreview2D(tempRT, m_previewRT);

                RenderTexture.active = prevActive;

                GUI.DrawTexture(previewRect, m_previewRT, ScaleMode.ScaleToFit, false);

                RenderTexture.ReleaseTemporary(tempRT);
            }

            GUI.color = prev;
        }