From a0aee15267b3fe19d11b65b0640088f297fc3eb4 Mon Sep 17 00:00:00 2001 From: xxSeys1 Date: Fri, 25 Oct 2024 15:30:54 +0200 Subject: [PATCH 1/8] add named terrain layers --- .../Editors/TerrainLayerEditor.cs | 45 +++++++++++++++++++ Source/Editor/Surface/Archetypes/Material.cs | 3 +- .../Tools/Terrain/Paint/SingleLayerMode.cs | 2 +- .../Core/Config/LayersAndTagsSettings.cs | 24 ++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Source/Editor/CustomEditors/Editors/TerrainLayerEditor.cs diff --git a/Source/Editor/CustomEditors/Editors/TerrainLayerEditor.cs b/Source/Editor/CustomEditors/Editors/TerrainLayerEditor.cs new file mode 100644 index 000000000..6e6f876b0 --- /dev/null +++ b/Source/Editor/CustomEditors/Editors/TerrainLayerEditor.cs @@ -0,0 +1,45 @@ +// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. + +using FlaxEditor.Content.Settings; +using FlaxEditor.CustomEditors.Elements; +using FlaxEditor.GUI; + +namespace FlaxEditor.CustomEditors.Editors +{ + /// + /// Custom editor for picking terrain layers. Instead of choosing bit mask or layer index it shows a combo box with simple layer picking by name. + /// + public sealed class TerrainLayerEditor : CustomEditor + { + private ComboBoxElement element; + + /// + public override DisplayStyle Style => DisplayStyle.Inline; + + /// + public override void Initialize(LayoutElementsContainer layout) + { + element = layout.ComboBox(); + element.ComboBox.SetItems(LayersAndTagsSettings.GetCurrentTerrainLayers()); + element.ComboBox.SelectedIndex = (int)Values[0]; + element.ComboBox.SelectedIndexChanged += OnSelectedIndexChanged; + } + + private void OnSelectedIndexChanged(ComboBox comboBox) + { + int value = comboBox.SelectedIndex; + if (value == -1) + value = 0; + + SetValue(value); + } + + /// + public override void Refresh() + { + base.Refresh(); + + element.ComboBox.SelectedIndex = (int)Values[0]; + } + } +} diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index f1da861c1..9da0ec067 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -1,6 +1,7 @@ // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System; +using FlaxEditor.Content.Settings; using FlaxEditor.Scripting; using FlaxEditor.Surface.Elements; using FlaxEditor.Windows.Assets; @@ -590,7 +591,7 @@ namespace FlaxEditor.Surface.Archetypes }, Elements = new[] { - NodeElementArchetype.Factory.ComboBox(0, 0, 70.0f, 0, FlaxEditor.Tools.Terrain.PaintTerrainGizmoMode.TerrainLayerNames), + NodeElementArchetype.Factory.ComboBox(0, 0, 70.0f, 0, LayersAndTagsSettings.GetCurrentTerrainLayers()), NodeElementArchetype.Factory.Output(0, "", typeof(float), 0), } }, diff --git a/Source/Editor/Tools/Terrain/Paint/SingleLayerMode.cs b/Source/Editor/Tools/Terrain/Paint/SingleLayerMode.cs index 5581eeb66..9cf550f5c 100644 --- a/Source/Editor/Tools/Terrain/Paint/SingleLayerMode.cs +++ b/Source/Editor/Tools/Terrain/Paint/SingleLayerMode.cs @@ -60,7 +60,7 @@ namespace FlaxEditor.Tools.Terrain.Paint /// /// The layer to paint with it. /// - [EditorOrder(10), Tooltip("The layer to paint with it. Terrain material can access per-layer blend weight to perform materials or textures blending.")] + [EditorOrder(10), Tooltip("The layer to paint on. Terrain material can access a per-layer blend weight to perform material or texture blending."), CustomEditorAlias("FlaxEditor.CustomEditors.Editors.TerrainLayerEditor")] public Layers Layer = Layers.Layer0; /// diff --git a/Source/Engine/Core/Config/LayersAndTagsSettings.cs b/Source/Engine/Core/Config/LayersAndTagsSettings.cs index 9346efee7..ddee29e88 100644 --- a/Source/Engine/Core/Config/LayersAndTagsSettings.cs +++ b/Source/Engine/Core/Config/LayersAndTagsSettings.cs @@ -1,6 +1,7 @@ // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using FlaxEngine; @@ -30,6 +31,29 @@ namespace FlaxEditor.Content.Settings return GetCurrentLayers(out int _); } + /// + /// The layers names. + /// + [EditorOrder(10), EditorDisplay("Terrain Layers", EditorDisplayAttribute.InlineStyle), Collection(CanResize = false, Display = CollectionAttribute.DisplayType.Inline)] + public string[] TerrainLayers = new string[8]; + + /// + /// Gets the current terrain layer names. Returns "Layer" + index for layers without a name. + /// + /// The layer names. + public static string[] GetCurrentTerrainLayers() + { + string[] layerNames = GameSettings.Load().TerrainLayers.ToArray(); + + for (int i = 0; i < layerNames.Length; i++) + { + if (string.IsNullOrEmpty(layerNames[i])) + layerNames[i] = $"Terrain Layer {i}"; + } + + return layerNames; + } + [LibraryImport("FlaxEngine", EntryPoint = "LayersAndTagsSettingsInternal_GetCurrentLayers", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(FlaxEngine.Interop.StringMarshaller))] [return: MarshalUsing(typeof(FlaxEngine.Interop.ArrayMarshaller<,>), CountElementName = "layerCount")] internal static partial string[] GetCurrentLayers(out int layerCount); From 68e531aebbd99570a8ad42e5fad1e1e7ad0652a7 Mon Sep 17 00:00:00 2001 From: xxSeys1 Date: Fri, 25 Oct 2024 16:09:30 +0200 Subject: [PATCH 2/8] change how layers with no name get displayed Makes the name shorter, fits into comboboxes more easily --- Source/Engine/Core/Config/LayersAndTagsSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Core/Config/LayersAndTagsSettings.cs b/Source/Engine/Core/Config/LayersAndTagsSettings.cs index ddee29e88..30f84f493 100644 --- a/Source/Engine/Core/Config/LayersAndTagsSettings.cs +++ b/Source/Engine/Core/Config/LayersAndTagsSettings.cs @@ -48,7 +48,7 @@ namespace FlaxEditor.Content.Settings for (int i = 0; i < layerNames.Length; i++) { if (string.IsNullOrEmpty(layerNames[i])) - layerNames[i] = $"Terrain Layer {i}"; + layerNames[i] = $"Layer {i}"; } return layerNames; From 1d7054265d1e5702cc1d2007eb6647925f2ca838 Mon Sep 17 00:00:00 2001 From: xxSeys1 Date: Fri, 25 Oct 2024 19:58:49 +0200 Subject: [PATCH 3/8] avoid null string issues --- Source/Engine/Core/Config/LayersAndTagsSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Core/Config/LayersAndTagsSettings.cs b/Source/Engine/Core/Config/LayersAndTagsSettings.cs index 30f84f493..840332efb 100644 --- a/Source/Engine/Core/Config/LayersAndTagsSettings.cs +++ b/Source/Engine/Core/Config/LayersAndTagsSettings.cs @@ -35,7 +35,7 @@ namespace FlaxEditor.Content.Settings /// The layers names. /// [EditorOrder(10), EditorDisplay("Terrain Layers", EditorDisplayAttribute.InlineStyle), Collection(CanResize = false, Display = CollectionAttribute.DisplayType.Inline)] - public string[] TerrainLayers = new string[8]; + public string[] TerrainLayers = Enumerable.Repeat(string.Empty, 8).ToArray(); /// /// Gets the current terrain layer names. Returns "Layer" + index for layers without a name. From c8146a8e99d02f5c1e0bda2023d62189f6c05f07 Mon Sep 17 00:00:00 2001 From: xxSeys1 Date: Sat, 16 Nov 2024 17:05:44 +0100 Subject: [PATCH 4/8] remove unused TerrainLayerNames --- .../Editor/Tools/Terrain/PaintTerrainGizmoMode.cs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/Source/Editor/Tools/Terrain/PaintTerrainGizmoMode.cs b/Source/Editor/Tools/Terrain/PaintTerrainGizmoMode.cs index 0be0fe41c..f9cf12fc1 100644 --- a/Source/Editor/Tools/Terrain/PaintTerrainGizmoMode.cs +++ b/Source/Editor/Tools/Terrain/PaintTerrainGizmoMode.cs @@ -21,21 +21,6 @@ namespace FlaxEditor.Tools.Terrain [HideInEditor] public class PaintTerrainGizmoMode : EditorGizmoMode { - /// - /// The terrain layer names. - /// - public static readonly string[] TerrainLayerNames = - { - "Layer 0", - "Layer 1", - "Layer 2", - "Layer 3", - "Layer 4", - "Layer 5", - "Layer 6", - "Layer 7", - }; - private struct SplatmapData { public IntPtr DataPtr; From 75d4a110de45b5494fd7ee3c1f49597797dd5605 Mon Sep 17 00:00:00 2001 From: xxSeys1 Date: Sat, 16 Nov 2024 17:08:10 +0100 Subject: [PATCH 5/8] remove usage of System.Linq --- Source/Engine/Core/Config/LayersAndTagsSettings.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Core/Config/LayersAndTagsSettings.cs b/Source/Engine/Core/Config/LayersAndTagsSettings.cs index 840332efb..911a131a7 100644 --- a/Source/Engine/Core/Config/LayersAndTagsSettings.cs +++ b/Source/Engine/Core/Config/LayersAndTagsSettings.cs @@ -1,7 +1,6 @@ // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System.Collections.Generic; -using System.Linq; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using FlaxEngine; @@ -35,7 +34,7 @@ namespace FlaxEditor.Content.Settings /// The layers names. /// [EditorOrder(10), EditorDisplay("Terrain Layers", EditorDisplayAttribute.InlineStyle), Collection(CanResize = false, Display = CollectionAttribute.DisplayType.Inline)] - public string[] TerrainLayers = Enumerable.Repeat(string.Empty, 8).ToArray(); + public string[] TerrainLayers = new string[8]; /// /// Gets the current terrain layer names. Returns "Layer" + index for layers without a name. @@ -43,7 +42,7 @@ namespace FlaxEditor.Content.Settings /// The layer names. public static string[] GetCurrentTerrainLayers() { - string[] layerNames = GameSettings.Load().TerrainLayers.ToArray(); + string[] layerNames = GameSettings.Load().TerrainLayers; for (int i = 0; i < layerNames.Length; i++) { From 498e94548ba11017f459fe34b2555f573d62a7f6 Mon Sep 17 00:00:00 2001 From: xxSeys1 Date: Sat, 16 Nov 2024 17:08:34 +0100 Subject: [PATCH 6/8] fix tests not passing --- Source/Engine/Core/Config/LayersAndTagsSettings.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/Engine/Core/Config/LayersAndTagsSettings.cs b/Source/Engine/Core/Config/LayersAndTagsSettings.cs index 911a131a7..92063b27d 100644 --- a/Source/Engine/Core/Config/LayersAndTagsSettings.cs +++ b/Source/Engine/Core/Config/LayersAndTagsSettings.cs @@ -42,6 +42,9 @@ namespace FlaxEditor.Content.Settings /// The layer names. public static string[] GetCurrentTerrainLayers() { + #if FLAX_TESTS + return Array.Empty(); + #else string[] layerNames = GameSettings.Load().TerrainLayers; for (int i = 0; i < layerNames.Length; i++) @@ -51,6 +54,7 @@ namespace FlaxEditor.Content.Settings } return layerNames; + #endif } [LibraryImport("FlaxEngine", EntryPoint = "LayersAndTagsSettingsInternal_GetCurrentLayers", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(FlaxEngine.Interop.StringMarshaller))] From 573b57ed4a512246bcfe2a47fa45511b7e0442ff Mon Sep 17 00:00:00 2001 From: xxSeys1 Date: Sat, 23 Nov 2024 17:16:02 +0100 Subject: [PATCH 7/8] add missing `System` --- Source/Engine/Core/Config/LayersAndTagsSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Core/Config/LayersAndTagsSettings.cs b/Source/Engine/Core/Config/LayersAndTagsSettings.cs index 92063b27d..886c34b8a 100644 --- a/Source/Engine/Core/Config/LayersAndTagsSettings.cs +++ b/Source/Engine/Core/Config/LayersAndTagsSettings.cs @@ -43,7 +43,7 @@ namespace FlaxEditor.Content.Settings public static string[] GetCurrentTerrainLayers() { #if FLAX_TESTS - return Array.Empty(); + return System.Array.Empty(); #else string[] layerNames = GameSettings.Load().TerrainLayers; From 3bd0858acf354d08c28ce711c128a7d708eb6aa4 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 26 Nov 2024 16:03:16 +0100 Subject: [PATCH 8/8] Fix typo and indent --- Source/Engine/Core/Config/LayersAndTagsSettings.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Core/Config/LayersAndTagsSettings.cs b/Source/Engine/Core/Config/LayersAndTagsSettings.cs index 886c34b8a..4086ec657 100644 --- a/Source/Engine/Core/Config/LayersAndTagsSettings.cs +++ b/Source/Engine/Core/Config/LayersAndTagsSettings.cs @@ -16,7 +16,7 @@ namespace FlaxEditor.Content.Settings public List Tags = new List(); /// - /// The layers names. + /// The layer names. /// [EditorOrder(10), EditorDisplay("Layers", EditorDisplayAttribute.InlineStyle), Collection(CanResize = false, Display = CollectionAttribute.DisplayType.Inline)] public string[] Layers = new string[32]; @@ -31,7 +31,7 @@ namespace FlaxEditor.Content.Settings } /// - /// The layers names. + /// The layer names. /// [EditorOrder(10), EditorDisplay("Terrain Layers", EditorDisplayAttribute.InlineStyle), Collection(CanResize = false, Display = CollectionAttribute.DisplayType.Inline)] public string[] TerrainLayers = new string[8]; @@ -42,19 +42,17 @@ namespace FlaxEditor.Content.Settings /// The layer names. public static string[] GetCurrentTerrainLayers() { - #if FLAX_TESTS +#if FLAX_TESTS return System.Array.Empty(); - #else +#else string[] layerNames = GameSettings.Load().TerrainLayers; - for (int i = 0; i < layerNames.Length; i++) { if (string.IsNullOrEmpty(layerNames[i])) layerNames[i] = $"Layer {i}"; } - return layerNames; - #endif +#endif } [LibraryImport("FlaxEngine", EntryPoint = "LayersAndTagsSettingsInternal_GetCurrentLayers", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(FlaxEngine.Interop.StringMarshaller))]