From e17b96b2d64c4eb1a9690c169619d5e92647b4a9 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 7 Apr 2025 11:14:33 -0500 Subject: [PATCH 1/2] Add additional methods for layers. --- Source/Engine/Core/Types/LayersMask.cs | 42 ++++++++++++++++++++++++++ Source/Engine/Core/Types/LayersMask.h | 19 +++++++++++- Source/Engine/Level/Level.cpp | 34 ++++++++++++++++++++- Source/Engine/Level/Level.h | 7 +++++ 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Core/Types/LayersMask.cs b/Source/Engine/Core/Types/LayersMask.cs index 96b190f64..c9409321d 100644 --- a/Source/Engine/Core/Types/LayersMask.cs +++ b/Source/Engine/Core/Types/LayersMask.cs @@ -39,6 +39,48 @@ namespace FlaxEngine return HasLayer(Level.GetLayerIndex(layerName)); } + /// + /// Gets a layer mask based on a specific layer names. + /// + /// The names of the layers (from layers settings). + /// A layer mask with the mask set to the layers found. Returns a mask with 0 if not found. + public static LayersMask GetMask(params string[] layerNames) + { + LayersMask mask = new LayersMask(); + foreach (var layerName in layerNames) + { + // Ignore blank entries + if (layerName.Length == 0) + continue; + int index = Level.GetLayerIndex(layerName); + if (index != -1 && !mask.HasLayer(index)) + { + mask.Mask |= (uint)(1 << index); + } + } + return mask; + } + + /// + /// Gets the layer index based on the layer name. + /// + /// The name of the layer. + /// The index if found, otherwise, returns -1. + public static int GetLayerIndex(string layerName) + { + return Level.GetLayerIndex(layerName); + } + + /// + /// Gets the layer name based on the layer index. + /// + /// The index of the layer. + /// The name of the layer if found, otherwise, a blank string. + public static string GetLayerName(int layerIndex) + { + return Level.GetLayerName(layerIndex); + } + /// /// Adds two masks. /// diff --git a/Source/Engine/Core/Types/LayersMask.h b/Source/Engine/Core/Types/LayersMask.h index a3a594894..f3a83a6ee 100644 --- a/Source/Engine/Core/Types/LayersMask.h +++ b/Source/Engine/Core/Types/LayersMask.h @@ -27,12 +27,29 @@ public: { } + /// + /// Determines whether the specified layer index is set in the mask. + /// + /// Index of the layer (zero-based). + /// true if the specified layer is set; otherwise, false. FORCE_INLINE bool HasLayer(int32 layerIndex) const { return (Mask & (1 << layerIndex)) != 0; } - bool HasLayer(const StringView& layerName) const; + /// + /// Determines whether the specified layer name is set in the mask. + /// + /// Name of the layer (from layers settings). + /// true if the specified layer is set; otherwise, false. + bool HasLayer(const StringView& layerName); + + /// + /// Gets a layers mask from a specific layer name. + /// + /// The layer names. + /// A layers mask with the Mask set to the same Mask as the layer name passed in. Returns a LayersMask with a mask of 0 if no layer found. + static LayersMask GetMask(StringView layerNames[]); operator uint32() const { diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index 5d50c9339..1fa7687a6 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -59,11 +59,31 @@ void LargeWorlds::UpdateOrigin(Vector3& origin, const Vector3& position) } } -bool LayersMask::HasLayer(const StringView& layerName) const +bool LayersMask::HasLayer(const StringView& layerName) { return HasLayer(Level::GetLayerIndex(layerName)); } +LayersMask LayersMask::GetMask(StringView layerNames[]) +{ + LayersMask mask(0); + if (layerNames == nullptr) + return mask; + for (int i = 0; i < layerNames->Length(); i++) + { + StringView& layerName = layerNames[i]; + // Ignore blank entries + if (layerName.Length() == 0) + continue; + int index = Level::GetLayerIndex(layerName); + if (index != -1 && !mask.HasLayer(index)) + { + mask.Mask |= static_cast(1 << index); + } + } + return mask; +} + enum class SceneEventType { OnSceneSaving = 0, @@ -728,6 +748,18 @@ int32 Level::GetLayerIndex(const StringView& layer) return result; } +StringView Level::GetLayerName(const int32 layerIndex) +{ + for (int32 i = 0; i < 32; i++) + { + if (i == layerIndex) + { + return Layers[i]; + } + } + return TEXT(""); +} + void Level::callActorEvent(ActorEventType eventType, Actor* a, Actor* b) { PROFILE_CPU(); diff --git a/Source/Engine/Level/Level.h b/Source/Engine/Level/Level.h index d68336beb..dbbe20629 100644 --- a/Source/Engine/Level/Level.h +++ b/Source/Engine/Level/Level.h @@ -534,6 +534,13 @@ public: /// API_FUNCTION() static int32 GetLayerIndex(const StringView& layer); + /// + /// Gets the name of the layer based on the index. + /// + /// The index to find the layer string. 0 - 32. + /// The layer string. Returns a blank string if index not found. + API_FUNCTION() static StringView GetLayerName(const int32 layerIndex); + private: // Actor API enum class ActorEventType From df6f8fd8ae9c772e17cf211ffd1a3a11212c251f Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 24 Aug 2025 13:46:53 +0200 Subject: [PATCH 2/2] Codestyle adjustments #3343 --- Source/Engine/Core/Types/LayersMask.cs | 12 +++++------- Source/Engine/Core/Types/LayersMask.h | 8 ++++---- Source/Engine/Level/Level.cpp | 17 ++++++----------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Source/Engine/Core/Types/LayersMask.cs b/Source/Engine/Core/Types/LayersMask.cs index 2838e7219..7d5c57066 100644 --- a/Source/Engine/Core/Types/LayersMask.cs +++ b/Source/Engine/Core/Types/LayersMask.cs @@ -32,7 +32,7 @@ namespace FlaxEngine /// /// Determines whether the specified layer is set in the mask. /// - /// Name of the layer (from layers settings). + /// Name of the layer (from Layers settings). /// true if the specified layer is set; otherwise, false. public bool HasLayer(string layerName) { @@ -42,21 +42,19 @@ namespace FlaxEngine /// /// Gets a layer mask based on a specific layer names. /// - /// The names of the layers (from layers settings). - /// A layer mask with the mask set to the layers found. Returns a mask with 0 if not found. + /// The names of the layers (from Layers settings). + /// A layer mask with the mask set to the layers found. Returns a mask with 0 if not found. public static LayersMask GetMask(params string[] layerNames) { LayersMask mask = new LayersMask(); foreach (var layerName in layerNames) { // Ignore blank entries - if (layerName.Length == 0) + if (string.IsNullOrEmpty(layerName)) continue; int index = Level.GetLayerIndex(layerName); - if (index != -1 && !mask.HasLayer(index)) - { + if (index != -1) mask.Mask |= (uint)(1 << index); - } } return mask; } diff --git a/Source/Engine/Core/Types/LayersMask.h b/Source/Engine/Core/Types/LayersMask.h index 3f175773e..25edbc05b 100644 --- a/Source/Engine/Core/Types/LayersMask.h +++ b/Source/Engine/Core/Types/LayersMask.h @@ -40,16 +40,16 @@ public: /// /// Determines whether the specified layer name is set in the mask. /// - /// Name of the layer (from layers settings). + /// Name of the layer (from Layers settings). /// true if the specified layer is set; otherwise, false. - bool HasLayer(const StringView& layerName); + bool HasLayer(const StringView& layerName) const; /// /// Gets a layers mask from a specific layer name. /// - /// The layer names. + /// The names of the layers (from Layers settings). /// A layers mask with the Mask set to the same Mask as the layer name passed in. Returns a LayersMask with a mask of 0 if no layer found. - static LayersMask GetMask(StringView layerNames[]); + static LayersMask GetMask(Span layerNames); operator uint32() const { diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index 1ff7d2cad..3db39144d 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -60,27 +60,22 @@ void LargeWorlds::UpdateOrigin(Vector3& origin, const Vector3& position) } } -bool LayersMask::HasLayer(const StringView& layerName) +bool LayersMask::HasLayer(const StringView& layerName) const { return HasLayer(Level::GetLayerIndex(layerName)); } -LayersMask LayersMask::GetMask(StringView layerNames[]) +LayersMask LayersMask::GetMask(Span layerNames) { LayersMask mask(0); - if (layerNames == nullptr) - return mask; - for (int i = 0; i < layerNames->Length(); i++) + for (StringView& layerName : layerNames) { - StringView& layerName = layerNames[i]; // Ignore blank entries if (layerName.Length() == 0) continue; - int index = Level::GetLayerIndex(layerName); - if (index != -1 && !mask.HasLayer(index)) - { - mask.Mask |= static_cast(1 << index); - } + int32 index = Level::GetLayerIndex(layerName); + if (index != -1) + mask.Mask |= (uint32)(1 << index); } return mask; }