Esempio n. 1
0
        private void DoNoiseSettingsObjectField()
        {
            bool  profileInUse     = m_activeNoiseSettingsProfile != null;
            float buttonWidth      = 60;
            float indentOffset     = EditorGUI.indentLevel * 15f;
            Rect  lineRect         = GUILayoutUtility.GetRect(1, EditorGUIUtility.singleLineHeight);
            Rect  labelRect        = new Rect(lineRect.x, lineRect.y, EditorGUIUtility.labelWidth - indentOffset, lineRect.height);
            Rect  fieldRect        = new Rect(labelRect.xMax, lineRect.y, lineRect.width - labelRect.width - buttonWidth * (profileInUse ? 3 : 2), lineRect.height);
            Rect  resetButtonRect  = new Rect(fieldRect.xMax, lineRect.y, buttonWidth, lineRect.height);
            Rect  saveButtonRect   = new Rect(resetButtonRect.xMax, lineRect.y, buttonWidth, lineRect.height);
            Rect  saveAsButtonRect = new Rect(profileInUse ? saveButtonRect.xMax : resetButtonRect.xMax, lineRect.y, buttonWidth, lineRect.height);

            EditorGUI.PrefixLabel(labelRect, Styles.noiseSettingsProfile);

            NoiseSettings settingsProfile = m_activeNoiseSettingsProfile;

            settingsProfile = (NoiseSettings)EditorGUI.ObjectField(fieldRect, settingsProfile, typeof(NoiseSettings), false);

            if (m_activeNoiseSettingsProfile != null)
            {
                if (GUI.Button(resetButtonRect, Styles.revert))
                {
                    Undo.RecordObject(noiseSettings, "Noise Settings - Revert");

                    noiseSettings.Copy(m_activeNoiseSettingsProfile);
                }
            }
            else
            {
                if (GUI.Button(resetButtonRect, Styles.reset))
                {
                    Undo.RecordObject(noiseSettings, "Noise Settings - Reset");

                    noiseSettings.Reset();
                }
            }

            if (profileInUse && GUI.Button(saveButtonRect, Styles.apply))
            {
                m_activeNoiseSettingsProfile.CopySerialized(noiseSettings);
            }

            if (GUI.Button(saveAsButtonRect, Styles.saveAs))
            {
                string path = EditorUtility.SaveFilePanel("Save Noise Settings",
                                                          Application.dataPath,
                                                          "New Noise Settings.asset",
                                                          "asset");
                // saving to project's asset folder
                if (path.StartsWith(Application.dataPath))
                {
                    // TODO(wyatt): need to check if this works with different locales/languages. folder might not be
                    //              called "Assets" in non-English Editor builds
                    string assetPath = path.Substring(Application.dataPath.Length - 6);
                    // settingsProfile = NoiseSettings.CreateAsset(assetPath, noiseSettings);
                    settingsProfile = NoiseSettingsFactory.CreateAsset(assetPath);
                    settingsProfile.CopySerialized(noiseSettings);
                }
                // saving asset somewhere else. why? dunno!
                else if (!string.IsNullOrEmpty(path))
                {
                    Debug.LogError("Invalid path specified for creation of new Noise Settings asset. Must be a valid path within the current Unity project's Assets folder/data path.");
                }
            }

            // check if the profile in the object field changed
            bool changed = settingsProfile != m_activeNoiseSettingsProfile;

            if (changed)
            {
                if (settingsProfile == null)
                {
                    noiseSettings.Copy(noiseSettingsIfNull);
                }
                else
                {
                    if (m_activeNoiseSettingsProfile == null)
                    {
                        noiseSettingsIfNull.Copy(noiseSettings);
                    }

                    noiseSettings.Copy(settingsProfile);
                }

                noiseSettingsGUI.Init(noiseSettings);
                m_activeNoiseSettingsProfile = settingsProfile;
            }

            GUILayout.Space(12);
        }
Esempio n. 2
0
        public NoiseEditorView(NoiseSettings _noiseUpdateTarget_ = null, NoiseSettings _sourceAsset_ = null)
        {
            // create temp noisesettings asset and the IMGUI view for this window
            m_noiseUpdateTarget = _noiseUpdateTarget_ == null?ScriptableObject.CreateInstance <NoiseSettings>() : _noiseUpdateTarget_;

            m_serializedNoiseProfile = new SerializedObject(m_noiseUpdateTarget);
            m_noiseGUI = new NoiseSettingsGUI();
            m_noiseGUI.Init(m_noiseUpdateTarget);

            m_noiseSourceAsset = _sourceAsset_;

            var stylesheet = EditorGUIUtility.isProSkin ?
                             Resources.Load <StyleSheet>("Styles/Noise_Dark") :
                             Resources.Load <StyleSheet>("Styles/Noise_Light");

            var settingsScrollView = new ScrollView()
            {
                name = Styles.settingsScrollViewName
            };

            ///////////////////////////////////////////////////////////////////////////////
            // settings buttons
            ///////////////////////////////////////////////////////////////////////////////

            var noiseGUIContainer = new IMGUIContainer()
            {
                name = Styles.noiseGUIContainerName
            };

            noiseGUIContainer.onGUIHandler = () =>
            {
                EditorGUI.BeginChangeCheck();
                {
                    m_noiseGUI.OnGUI(NoiseSettingsGUIFlags.All & (~NoiseSettingsGUIFlags.Preview));
                }
                bool changed = EditorGUI.EndChangeCheck();

                if (changed)
                {
                    INTERNAL_OnSettingsChanged();
                }
            };
            settingsScrollView.Add(noiseGUIContainer);

            ///////////////////////////////////////////////////////////////////////////////
            // settings buttons
            ///////////////////////////////////////////////////////////////////////////////

            filePanelContainer = new VisualElement()
            {
                name  = Styles.saveButtonsContainer,
                style =
                {
                    flexDirection = FlexDirection.Row
                }
            };
            filePanelContainer.AddToClassList(Styles.filePanelContainer);

            saveAsButton = new Button(SaveAsCallback)
            {
                name    = Styles.saveAsButtonName,
                text    = "Save As",
                tooltip = Styles.saveasTooltip
            };
            saveAsButton.AddToClassList(Styles.filePanelButton);

            revertButton = new Button(ResetRevertCallback)
            {
                name    = Styles.resetButtonName,
                text    = "Reset",
                tooltip = Styles.resetTooltip
            };
            revertButton.AddToClassList(Styles.filePanelButton);

            applyButton = new Button(() => { Undo.RecordObject(m_noiseSourceAsset, "NoiseWindow - Apply Settings"); m_noiseSourceAsset.CopySerialized(m_noiseUpdateTarget); })
            {
                name    = Styles.applyButtonName,
                text    = "Apply",
                tooltip = Styles.applyTooltip
            };
            applyButton.AddToClassList(Styles.filePanelButton);
            applyButton.AddToClassList(Styles.filePanelButton);

            ///////////////////////////////////////////////////////////////////////////////
            // noise settings object field
            ///////////////////////////////////////////////////////////////////////////////

            var objectFieldContainer = new VisualElement()
            {
                name = Styles.objectFieldContainer
            };

            objectFieldContainer.AddToClassList(Styles.objectFieldContainer);

            objectField = new ObjectField()
            {
                name = Styles.noiseAssetFieldName,
                allowSceneObjects = false,
                objectType        = typeof(NoiseSettings),
                label             = Styles.noiseAssetFieldLabel,
                tooltip           = Styles.noiseAssetFieldTooltip //,
                                                                  // viewDataKey = Styles.noiseAssetFieldName
            };
            objectField.AddToClassList(Styles.noiseAssetFieldName);
            objectField.RegisterCallback <ChangeEvent <UnityEngine.Object> >(OnSourceProfileChanged);

            objectFieldContainer.Add(objectField);

            ///////////////////////////////////////////////////////////////////////////////
            // export settings
            ///////////////////////////////////////////////////////////////////////////////

            var flexArea = new VisualElement()
            {
                name = Styles.flexArea
            };

            flexArea.AddToClassList(Styles.flexArea);

            var exportContainer = new VisualElement()
            {
                name = Styles.exportContainer
            };

            exportContainer.AddToClassList(Styles.exportContainer);

            var exportHeader = new Foldout()
            {
                name        = Styles.exportHeader,
                text        = "Export Settings",
                tooltip     = Styles.exportTooltip,
                viewDataKey = Styles.exportHeader
            };

            exportHeader.RegisterCallback <ChangeEvent <bool> >(
                (evt) =>
            {
                if (evt.newValue)
                {
                    m_exportContainer.Add(m_exportSettings);
                    m_exportContainer.Add(m_exportButton);
                }
                else
                {
                    m_exportContainer.Remove(m_exportSettings);
                    m_exportContainer.Remove(m_exportButton);
                }
            }
                );
            exportHeader.AddToClassList(Styles.foldoutContainer);

            var exportSettings = CreateExportSettingsView();

            var exportButton = new Button(
                () =>
            {
                if (m_exportType.value == ExportTextureType.Texture2D)
                {
                    Export2D();
                }
                else if (m_exportType.value == ExportTextureType.Texture3D)
                {
                    Export3D();
                }
            }
                )
            {
                name = Styles.exportButton,
                text = "Export To Texture"
            };

            exportButton.AddToClassList(Styles.exportButton);

            m_exportButton = exportButton;
            exportContainer.Add(exportHeader);
            // exportContainer.Add( exportSettings );
            // exportContainer.Add( exportButton );

            m_exportContainer  = exportContainer;
            exportHeader.value = false;

            // container for the settings panel
            var settingsContainer = new VisualElement()
            {
                name = Styles.settingsContainerName
            };

            settingsContainer.AddToClassList(Styles.settingsContainerName);
            settingsContainer.Add(objectFieldContainer);
            settingsContainer.Add(filePanelContainer);
            settingsContainer.Add(settingsScrollView);
            settingsContainer.Add(flexArea);   // add this so the export stuff stays at the bottom of the settings container
            settingsContainer.Add(exportContainer);
            settingsContainer.Bind(m_serializedNoiseProfile);

            ///////////////////////////////////////////////////////////////////////////////
            // settings buttons
            ///////////////////////////////////////////////////////////////////////////////

            var previewContainer = new VisualElement()
            {
                name = Styles.noisePreviewContainerName
            };

            previewContainer.AddToClassList(Styles.noisePreviewContainerName);

            var previewLabel = new Label()
            {
                name    = Styles.noisePreviewLabelName,
                text    = Styles.previewLabel,
                tooltip = Styles.previewLabelTooltip
            };

            previewLabel.AddToClassList(Styles.noisePreviewLabelName);
            previewContainer.Add(previewLabel);

            m_noiseFieldView = new NoiseFieldView(m_serializedNoiseProfile)
            {
                name = Styles.noisePreviewTextureName
            };
            m_noiseFieldView.onGUIHandler += () =>
            {
                INTERNAL_OnSettingsChanged();
            };
            m_noiseFieldView.AddToClassList(Styles.noisePreviewTextureName);
            previewContainer.Add(m_noiseFieldView);

            ///////////////////////////////////////////////////////////////////////////////
            // wrap it all up
            ///////////////////////////////////////////////////////////////////////////////

            styleSheets.Add(stylesheet);
            AddToClassList(Styles.noiseWindowName);
            Add(settingsContainer);
            Add(previewContainer);

            this.Bind(m_serializedNoiseProfile);

            m_settingsContainer = settingsContainer;

            INTERNAL_OnSourceProfileChanged(_sourceAsset_);

            this.viewDataKey = Styles.noiseWindowName;
        }