public override void OnInspectorGUI() { base.OnInspectorGUI(); var showAdvancedControls = serializedObject.FindProperty("_showAdvancedControls").boolValue; // preamble - styles for toggle buttons. this code and the below was based off the useful info provided by user Lasse here: // https://gamedev.stackexchange.com/questions/98920/how-do-i-create-a-toggle-button-in-unity-inspector if (ToggleButtonStyleNormal == null) { ToggleButtonStyleNormal = "Button"; ToggleButtonStyleToggled = new GUIStyle(ToggleButtonStyleNormal); ToggleButtonStyleToggled.normal.background = ToggleButtonStyleToggled.active.background; } EditorGUILayout.Space(); var spDisabled = serializedObject.FindProperty("_powerDisabled"); EditorGUILayout.BeginHorizontal(); bool allEnabled = true; for (int i = 0; i < spDisabled.arraySize; i++) { if (spDisabled.GetArrayElementAtIndex(i).boolValue) { allEnabled = false; } } bool toggle = allEnabled; if (toggle != EditorGUILayout.Toggle(toggle, GUILayout.Width(13f))) { for (int i = 0; i < spDisabled.arraySize; i++) { spDisabled.GetArrayElementAtIndex(i).boolValue = toggle; } } EditorGUILayout.LabelField("Spectrum", EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); var spec = target as OceanWaveSpectrum; var spPower = serializedObject.FindProperty("_powerLog"); var spChopScales = serializedObject.FindProperty("_chopScales"); var spGravScales = serializedObject.FindProperty("_gravityScales"); for (int i = 0; i < spPower.arraySize; i++) { EditorGUILayout.BeginHorizontal(); var spDisabled_i = spDisabled.GetArrayElementAtIndex(i); spDisabled_i.boolValue = !EditorGUILayout.Toggle(!spDisabled_i.boolValue, GUILayout.Width(15f)); float smallWL = OceanWaveSpectrum.SmallWavelength(i); var spPower_i = spPower.GetArrayElementAtIndex(i); if (showAdvancedControls) { EditorGUILayout.LabelField(string.Format("{0}", smallWL), EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); EditorGUILayout.Slider(spPower_i, OceanWaveSpectrum.MIN_POWER_LOG, OceanWaveSpectrum.MAX_POWER_LOG, " Power"); } else { EditorGUILayout.LabelField(string.Format("{0}", smallWL), GUILayout.Width(30f)); spPower_i.floatValue = GUILayout.HorizontalSlider(spPower_i.floatValue, OceanWaveSpectrum.MIN_POWER_LOG, OceanWaveSpectrum.MAX_POWER_LOG); EditorGUILayout.EndHorizontal(); } if (showAdvancedControls) { EditorGUILayout.Slider(spChopScales.GetArrayElementAtIndex(i), 0f, 4f, " Chop Scale"); EditorGUILayout.Slider(spGravScales.GetArrayElementAtIndex(i), 0f, 4f, " Grav Scale"); } } EditorGUILayout.Space(); EditorGUILayout.LabelField("Empirical Spectra", EditorStyles.boldLabel); EditorGUILayout.BeginHorizontal(); float spd_kmh = _windSpeed * 3.6f; EditorGUILayout.LabelField("Wind speed (km/h)", GUILayout.Width(120f)); spd_kmh = EditorGUILayout.Slider(spd_kmh, 0f, 60f); _windSpeed = spd_kmh / 3.6f; EditorGUILayout.EndHorizontal(); // descriptions from this very useful paper: https://hal.archives-ouvertes.fr/file/index/docid/307938/filename/frechot_realistic_simulation_of_ocean_surface_using_wave_spectra.pdf if (GUILayout.Button(new GUIContent("Phillips", "Base of modern parametric wave spectra"), _applyPhillipsSpectrum ? ToggleButtonStyleToggled : ToggleButtonStyleNormal)) { _applyPhillipsSpectrum = !_applyPhillipsSpectrum; } if (_applyPhillipsSpectrum) { _applyJONSWAPSpectrum = _applyPiersonMoskowitzSpectrum = false; Undo.RecordObject(this, "Apply Phillips Spectrum"); spec.ApplyPhillipsSpectrum(_windSpeed); } if (GUILayout.Button(new GUIContent("Pierson-Moskowitz", "Fully developed sea with infinite fetch"), _applyPiersonMoskowitzSpectrum ? ToggleButtonStyleToggled : ToggleButtonStyleNormal)) { _applyPiersonMoskowitzSpectrum = !_applyPiersonMoskowitzSpectrum; } if (_applyPiersonMoskowitzSpectrum) { _applyPhillipsSpectrum = _applyJONSWAPSpectrum = false; Undo.RecordObject(this, "Apply Pierson-Moskowitz Spectrum"); spec.ApplyPiersonMoskowitzSpectrum(_windSpeed); } _fetch = EditorGUILayout.Slider("Fetch", _fetch, 0f, 1000000f); if (GUILayout.Button(new GUIContent("JONSWAP", "Fetch limited sea where waves continue to grow"), _applyJONSWAPSpectrum ? ToggleButtonStyleToggled : ToggleButtonStyleNormal)) { _applyJONSWAPSpectrum = !_applyJONSWAPSpectrum; } if (_applyJONSWAPSpectrum) { _applyPhillipsSpectrum = _applyPiersonMoskowitzSpectrum = false; Undo.RecordObject(this, "Apply JONSWAP Spectrum"); spec.ApplyJONSWAPSpectrum(_windSpeed, _fetch); } serializedObject.ApplyModifiedProperties(); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); var showAdvancedControls = serializedObject.FindProperty("_showAdvancedControls").boolValue; var spSpectrumModel = serializedObject.FindProperty("_model"); var spectrumModel = (OceanWaveSpectrum.SpectrumModel)serializedObject.FindProperty("_model").enumValueIndex; EditorGUILayout.Space(); var spDisabled = serializedObject.FindProperty("_powerDisabled"); UpgradeSpectrum(spDisabled, false); EditorGUILayout.BeginHorizontal(); bool allEnabled = true; for (int i = 0; i < spDisabled.arraySize; i++) { if (spDisabled.GetArrayElementAtIndex(i).boolValue) { allEnabled = false; } } bool toggle = allEnabled; if (toggle != EditorGUILayout.Toggle(toggle, GUILayout.Width(13f))) { for (int i = 0; i < spDisabled.arraySize; i++) { spDisabled.GetArrayElementAtIndex(i).boolValue = toggle; } } EditorGUILayout.LabelField("Spectrum", EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); var spec = target as OceanWaveSpectrum; var spPower = serializedObject.FindProperty("_powerLog"); UpgradeSpectrum(spPower, OceanWaveSpectrum.MIN_POWER_LOG); var spChopScales = serializedObject.FindProperty("_chopScales"); UpgradeSpectrum(spChopScales, 1f); var spGravScales = serializedObject.FindProperty("_gravityScales"); UpgradeSpectrum(spGravScales, 1f); // Disable sliders if authoring with model. var canEditSpectrum = spectrumModel != OceanWaveSpectrum.SpectrumModel.None; for (int i = 0; i < spPower.arraySize; i++) { EditorGUILayout.BeginHorizontal(); var spDisabled_i = spDisabled.GetArrayElementAtIndex(i); spDisabled_i.boolValue = !EditorGUILayout.Toggle(!spDisabled_i.boolValue, GUILayout.Width(15f)); float smallWL = OceanWaveSpectrum.SmallWavelength(i); var spPower_i = spPower.GetArrayElementAtIndex(i); var isPowerDisabled = spDisabled_i.boolValue; var powerValue = isPowerDisabled ? OceanWaveSpectrum.MIN_POWER_LOG : spPower_i.floatValue; if (showAdvancedControls) { EditorGUILayout.LabelField(string.Format("{0}", smallWL), EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); // Disable slider if authoring with model. GUI.enabled = !canEditSpectrum && !spDisabled_i.boolValue; powerValue = EditorGUILayout.Slider(" Power", powerValue, OceanWaveSpectrum.MIN_POWER_LOG, OceanWaveSpectrum.MAX_POWER_LOG); GUI.enabled = true; } else { EditorGUILayout.LabelField(string.Format("{0}", smallWL), GUILayout.Width(30f)); // Disable slider if authoring with model. GUI.enabled = !canEditSpectrum && !spDisabled_i.boolValue; powerValue = GUILayout.HorizontalSlider(powerValue, OceanWaveSpectrum.MIN_POWER_LOG, OceanWaveSpectrum.MAX_POWER_LOG); GUI.enabled = true; EditorGUILayout.EndHorizontal(); // This will create a tooltip for slider. GUI.Label(GUILayoutUtility.GetLastRect(), new GUIContent("", powerValue.ToString())); } // If the power is disabled, we are using the MIN_POWER_LOG value so we don't want to store it. if (!isPowerDisabled) { spPower_i.floatValue = powerValue; } if (showAdvancedControls) { EditorGUILayout.Slider(spChopScales.GetArrayElementAtIndex(i), 0f, 4f, " Chop Scale"); EditorGUILayout.Slider(spGravScales.GetArrayElementAtIndex(i), 0f, 4f, " Grav Scale"); } } EditorGUILayout.Space(); EditorGUILayout.LabelField("Empirical Spectra", EditorStyles.boldLabel); EditorGUILayout.BeginHorizontal(); spectrumModel = (OceanWaveSpectrum.SpectrumModel)EditorGUILayout.EnumPopup(spectrumModel); spSpectrumModel.enumValueIndex = (int)spectrumModel; EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.HelpBox(modelDescriptions[(int)spectrumModel], MessageType.Info); EditorGUILayout.Space(); if (spectrumModel == OceanWaveSpectrum.SpectrumModel.None) { Undo.RecordObject(spec, "Change Spectrum"); } else { // It doesn't seem to matter where this is called. Undo.RecordObject(spec, $"Apply {ObjectNames.NicifyVariableName(spectrumModel.ToString())} Spectrum"); var labelWidth = 170f; EditorGUILayout.BeginHorizontal(); float spd_kmh = spec._windSpeed * 3.6f; EditorGUILayout.LabelField("Wind speed (km/h)", GUILayout.Width(labelWidth)); spd_kmh = EditorGUILayout.Slider(spd_kmh, 0f, 120f); spec._windSpeed = spd_kmh / 3.6f; EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(s_labelSWM, GUILayout.Width(labelWidth)); spec._smallWavelengthMultiplier = EditorGUILayout.Slider(spec._smallWavelengthMultiplier, 0f, 10f); EditorGUILayout.EndHorizontal(); if (spectrumModel == OceanWaveSpectrum.SpectrumModel.JONSWAP) { spec._fetch = EditorGUILayout.Slider(s_labelFetch, spec._fetch, 0f, 1000000f); } // Descriptions from this very useful paper: // https://hal.archives-ouvertes.fr/file/index/docid/307938/filename/frechot_realistic_simulation_of_ocean_surface_using_wave_spectra.pdf switch (spectrumModel) { case OceanWaveSpectrum.SpectrumModel.Phillips: spec.ApplyPhillipsSpectrum(spec._windSpeed, spec._smallWavelengthMultiplier); break; case OceanWaveSpectrum.SpectrumModel.PiersonMoskowitz: spec.ApplyPiersonMoskowitzSpectrum(spec._windSpeed, spec._smallWavelengthMultiplier); break; case OceanWaveSpectrum.SpectrumModel.JONSWAP: spec.ApplyJONSWAPSpectrum(spec._windSpeed, spec._fetch, spec._smallWavelengthMultiplier); break; } } serializedObject.ApplyModifiedProperties(); if (GUI.changed) { // We need to call this otherwise any property which has HideInInspector won't save. EditorUtility.SetDirty(spec); } }