public override void OnDisable()
 {
     if (m_window != null)
     {
         m_window.Close();
         m_window = null;
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Create a NoiseWindow that applies changes to a provided NoiseAsset and loads from a provided source Asset
        /// </summary>
        public static NoiseWindow Create(NoiseSettings noise, NoiseSettings sourceAsset = null)
        {
            NoiseWindow wnd = null;

            // check to see if a window with the same context exists already
            foreach (var w in s_openNoiseWindows)
            {
                if (w.noiseEditorView != null && w.noiseEditorView.noiseUpdateTarget == noise)
                {
                    wnd = w;

                    break;
                }
            }

            if (null == wnd)
            {
                wnd = ScriptableObject.CreateInstance <NoiseWindow>();
                wnd.titleContent = EditorGUIUtility.TrTextContent("Noise Editor");

                var view = new NoiseEditorView(noise, sourceAsset);
                wnd.rootVisualElement.Clear();
                wnd.rootVisualElement.Add(view);
                wnd.noiseEditorView = view;

                wnd.m_noiseAsset = noise;

                wnd.minSize = new Vector2(550, 300);

                wnd.rootVisualElement.Bind(new SerializedObject(wnd.m_noiseAsset));
                wnd.rootVisualElement.viewDataKey = "NoiseWindow";
            }

            wnd.Show();
            wnd.Focus();

            return(wnd);
        }
        public override void DoGUI(Rect rect)
        {
            if (m_noiseSettings == null)
            {
                m_noiseSettings = ScriptableObject.CreateInstance <NoiseSettings>();
            }

            GUIContent localLabel     = NoiseFilter.localLabel;
            GUIContent worldLabel     = NoiseFilter.worldLabel;
            GUIContent heightmapLabel = NoiseFilter.heightmapLabel;
            GUIContent editLabel      = NoiseFilter.editLabel;

            float editWith = GUI.skin.label.CalcSize(editLabel).x + 20f;
            Rect  editRect = new Rect(rect.xMax - editWith, rect.y, editWith, rect.height);

            Rect labelRect = rect;

            labelRect.width = GUI.skin.label.CalcSize(coordinateLabel).x;

            Rect worldRect = labelRect;

            worldRect.x     = labelRect.xMax;
            worldRect.width = GUI.skin.button.CalcSize(worldLabel).x + 10f;

            Rect localRect = worldRect;

            localRect.x     = worldRect.xMax;
            localRect.width = GUI.skin.button.CalcSize(localLabel).x + 10f;

            Rect heightmapRect = localRect;

            heightmapRect.x     = localRect.xMax + 10f;
            heightmapRect.width = GUI.skin.button.CalcSize(heightmapLabel).x + 10f;

            if (editRect.xMin < heightmapRect.xMax + 10f)
            {
                worldRect.x     -= labelRect.width;
                localRect.x     -= labelRect.width;
                heightmapRect.x -= labelRect.width;
                labelRect.width  = 0;
            }

            editRect.x = Mathf.Max(editRect.x, heightmapRect.xMax + 4f);

            if (editRect.xMax > rect.xMax)
            {
                worldLabel          = NoiseFilter.worldShortLabel;
                localLabel          = NoiseFilter.localShortLabel;
                heightmapLabel      = NoiseFilter.heightmapShortLabel;
                worldRect.width     = GUI.skin.label.CalcSize(worldLabel).x + 10f;
                localRect.width     = GUI.skin.label.CalcSize(localLabel).x + 10f;
                heightmapRect.width = GUI.skin.label.CalcSize(heightmapLabel).x + 10f;
                localRect.x         = worldRect.xMax;
                heightmapRect.x     = localRect.xMax + 10f;

                editRect.x = rect.xMax - editWith;
            }

            editRect.x = Mathf.Max(heightmapRect.xMax + 4f, editRect.x);

            if (editRect.xMax > rect.xMax)
            {
                editLabel      = editShortLabel;
                editRect.width = GUI.skin.label.CalcSize(editLabel).x + 10f;
            }

            GUI.Label(labelRect, coordinateLabel);

            if (TerrainToolGUIHelper.ToggleButton(worldRect, worldLabel, !m_isLocalSpace))
            {
                m_isLocalSpace = false;
            }

            if (TerrainToolGUIHelper.ToggleButton(localRect, localLabel, m_isLocalSpace))
            {
                m_isLocalSpace = true;
            }

            m_useHeightmap = TerrainToolGUIHelper.ToggleButton(heightmapRect, heightmapLabel, m_useHeightmap);

            m_noiseSettings.useTextureForPositions = m_useHeightmap;

            if (GUI.Button(editRect, editLabel))
            {
                NoiseWindow wnd = NoiseWindow.Create(m_noiseSettings, m_noiseSource);
                wnd.noiseEditorView.onSettingsChanged += (noise) =>
                {
                    m_noiseSettings.Copy(noise);
                };
                wnd.noiseEditorView.onSourceAssetChanged += (noise) =>
                {
                    m_noiseSource = noise;
                };
                wnd.onDisableCallback += () =>
                {
                    m_window = null;
                };

                m_window = wnd;
            }
        }