Merge branch 'layer-methods' of https://github.com/Tryibion/FlaxEngine into Tryibion-layer-methods

This commit is contained in:
Wojtek Figat
2025-08-24 13:43:35 +02:00
4 changed files with 100 additions and 2 deletions

View File

@@ -39,6 +39,48 @@ namespace FlaxEngine
return HasLayer(Level.GetLayerIndex(layerName));
}
/// <summary>
/// Gets a layer mask based on a specific layer names.
/// </summary>
/// <param name="layerNames">The names of the layers (from layers settings).</param>
/// <returns>A layer mask with the mask set to the layers found. Returns a mask with 0 if not found. </returns>
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;
}
/// <summary>
/// Gets the layer index based on the layer name.
/// </summary>
/// <param name="layerName">The name of the layer.</param>
/// <returns>The index if found, otherwise, returns -1.</returns>
public static int GetLayerIndex(string layerName)
{
return Level.GetLayerIndex(layerName);
}
/// <summary>
/// Gets the layer name based on the layer index.
/// </summary>
/// <param name="layerIndex">The index of the layer.</param>
/// <returns>The name of the layer if found, otherwise, a blank string.</returns>
public static string GetLayerName(int layerIndex)
{
return Level.GetLayerName(layerIndex);
}
/// <summary>
/// Adds two masks.
/// </summary>

View File

@@ -27,12 +27,29 @@ public:
{
}
/// <summary>
/// Determines whether the specified layer index is set in the mask.
/// </summary>
/// <param name="layerIndex">Index of the layer (zero-based).</param>
/// <returns><c>true</c> if the specified layer is set; otherwise, <c>false</c>.</returns>
FORCE_INLINE bool HasLayer(int32 layerIndex) const
{
return (Mask & (1 << layerIndex)) != 0;
}
bool HasLayer(const StringView& layerName) const;
/// <summary>
/// Determines whether the specified layer name is set in the mask.
/// </summary>
/// <param name="layerName">Name of the layer (from layers settings).</param>
/// <returns><c>true</c> if the specified layer is set; otherwise, <c>false</c>.</returns>
bool HasLayer(const StringView& layerName);
/// <summary>
/// Gets a layers mask from a specific layer name.
/// </summary>
/// <param name="layerNames">The layer names.</param>
/// <returns>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.</returns>
static LayersMask GetMask(StringView layerNames[]);
operator uint32() const
{

View File

@@ -60,11 +60,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<uint32>(1 << index);
}
}
return mask;
}
enum class SceneEventType
{
OnSceneSaving = 0,
@@ -729,6 +749,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();

View File

@@ -534,6 +534,13 @@ public:
/// </summary>
API_FUNCTION() static int32 GetLayerIndex(const StringView& layer);
/// <summary>
/// Gets the name of the layer based on the index.
/// </summary>
/// <param name="layerIndex">The index to find the layer string. 0 - 32.</param>
/// <returns>The layer string. Returns a blank string if index not found.</returns>
API_FUNCTION() static StringView GetLayerName(const int32 layerIndex);
private:
// Actor API
enum class ActorEventType