diff --git a/Source/Engine/Core/Types/LayersMask.cs b/Source/Engine/Core/Types/LayersMask.cs new file mode 100644 index 000000000..a4a48bfae --- /dev/null +++ b/Source/Engine/Core/Types/LayersMask.cs @@ -0,0 +1,162 @@ +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. + +using System; +using System.Runtime.CompilerServices; + +namespace FlaxEngine +{ + partial struct LayersMask : IComparable, IComparable + { + /// + /// Initializes a new instance of the struct. + /// + /// The bit mask. + public LayersMask(int mask) => Mask = (uint)mask; + + /// + /// Initializes a new instance of the struct. + /// + /// The bit mask. + public LayersMask(uint mask) => Mask = mask; + + /// + /// 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. + public bool HasLayer(int layerIndex) + { + return (Mask & (1 << layerIndex)) != 0; + } + + /// + /// Determines whether the specified layer is set in the mask. + /// + /// Name of the layer (from layers settings). + /// true if the specified layer is set; otherwise, false. + public bool HasLayer(string layerName) + { + return HasLayer(Level.GetLayerIndex(layerName)); + } + + /// + /// Adds two masks. + /// + /// The left mask. + /// The right mask. + /// The sum of the two masks. + public static LayersMask operator +(LayersMask left, LayersMask right) => new LayersMask(left.Mask | right.Mask); + + /// + /// Removes one mask from another. + /// + /// The left mask. + /// The right mask. + /// The left mask without right mask. + public static LayersMask operator -(LayersMask left, LayersMask right) => new LayersMask(left.Mask & ~right.Mask); + + /// + /// Performance bitwise OR of the masks. + /// + /// The left mask. + /// The right mask. + /// The sum of the two masks. + public static LayersMask operator |(LayersMask left, LayersMask right) => new LayersMask(left.Mask | right.Mask); + + /// + /// Performance bitwise AND of the masks. + /// + /// The left mask. + /// The right mask. + /// The conjunction of the two masks. + public static LayersMask operator &(LayersMask left, LayersMask right) => new LayersMask(left.Mask & right.Mask); + + /// + /// Performance bitwise XOR of the masks. + /// + /// The left mask. + /// The right mask. + /// The difference of the two masks. + public static LayersMask operator ^(LayersMask left, LayersMask right) => new LayersMask(left.Mask ^ right.Mask); + + /// + /// Performance bitwise NOT of the mask. + /// + /// The mask. + /// The negated mask. + public static LayersMask operator ~(LayersMask left) => new LayersMask(~left.Mask); + + /// + /// Performs an implicit conversion from to . + /// + /// The mask. + /// The mask value. + public static implicit operator uint(LayersMask mask) => mask.Mask; + + /// + /// Tests for equality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has the same value as ; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(LayersMask left, LayersMask right) + { + return left.Mask == right.Mask; + } + + /// + /// Tests for inequality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has a different value than ; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(LayersMask left, LayersMask right) + { + return left.Mask != right.Mask; + } + + /// + public override int GetHashCode() + { + return Mask.GetHashCode(); + } + + /// + public override string ToString() + { + return Mask.ToString(); + } + + /// + /// Tests for equality between two objects. + /// + /// The other value to compare. + /// True if both values are equal, otherwise false. + public bool Equals(LayersMask other) + { + return Mask == other.Mask; + } + + /// + public override bool Equals(object obj) + { + return obj is LayersMask other && Equals(other); + } + + /// + public int CompareTo(object obj) + { + if (obj is LayersMask other) + return Mask.CompareTo(other.Mask); + return 0; + } + + /// + public int CompareTo(LayersMask other) + { + return Mask.CompareTo(other.Mask); + } + } +} diff --git a/Source/Engine/Core/Config/LayersMask.h b/Source/Engine/Core/Types/LayersMask.h similarity index 55% rename from Source/Engine/Core/Config/LayersMask.h rename to Source/Engine/Core/Types/LayersMask.h index b2539e78a..bd8e90335 100644 --- a/Source/Engine/Core/Config/LayersMask.h +++ b/Source/Engine/Core/Types/LayersMask.h @@ -17,6 +17,17 @@ DECLARE_SCRIPTING_TYPE_MINIMAL(LayersMask); /// API_FIELD() uint32 Mask = MAX_uint32; +public: + + FORCE_INLINE LayersMask() + { + } + + FORCE_INLINE LayersMask(uint32 mask) + : Mask(mask) + { + } + FORCE_INLINE bool HasLayer(int32 layerIndex) const { return (Mask & (1 << layerIndex)) != 0; @@ -24,8 +35,50 @@ DECLARE_SCRIPTING_TYPE_MINIMAL(LayersMask); bool HasLayer(const StringView& layerName) const; - bool operator==(const LayersMask& other) const; - bool operator!=(const LayersMask& other) const; + operator uint32() const + { + return Mask; + } + + bool operator==(const LayersMask& other) const + { + return Mask == other.Mask; + } + + bool operator!=(const LayersMask& other) const + { + return Mask != other.Mask; + } + + LayersMask operator+(const LayersMask& other) const + { + return Mask | other.Mask; + } + + LayersMask operator-(const LayersMask& other) const + { + return Mask & ~other.Mask; + } + + LayersMask operator&(const LayersMask& other) const + { + return Mask && other.Mask; + } + + LayersMask operator|(const LayersMask& other) const + { + return Mask | other.Mask; + } + + LayersMask operator^(const LayersMask& other) const + { + return Mask ^ other.Mask; + } + + LayersMask operator-() const + { + return ~Mask; + } }; // @formatter:off diff --git a/Source/Engine/Graphics/RenderView.h b/Source/Engine/Graphics/RenderView.h index f478576f2..22acbfb52 100644 --- a/Source/Engine/Graphics/RenderView.h +++ b/Source/Engine/Graphics/RenderView.h @@ -5,7 +5,7 @@ #include "Engine/Core/Math/BoundingFrustum.h" #include "Engine/Core/Math/Matrix.h" #include "Engine/Core/Math/Vector3.h" -#include "Engine/Core/Config/LayersMask.h" +#include "Engine/Core/Types/LayersMask.h" #include "Engine/Level/Types.h" #include "Enums.h" diff --git a/Source/Engine/Level/Actors/Camera.h b/Source/Engine/Level/Actors/Camera.h index 4d05b53fb..7f40617dd 100644 --- a/Source/Engine/Level/Actors/Camera.h +++ b/Source/Engine/Level/Actors/Camera.h @@ -7,7 +7,7 @@ #include "Engine/Core/Math/BoundingFrustum.h" #include "Engine/Core/Math/Viewport.h" #include "Engine/Core/Math/Ray.h" -#include "Engine/Core/Config/LayersMask.h" +#include "Engine/Core/Types/LayersMask.h" #if USE_EDITOR #include "Engine/Content/AssetReference.h" #include "Engine/Graphics/Models/ModelInstanceEntry.h" diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index 602093363..2746ce65e 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -42,16 +42,6 @@ bool LayersMask::HasLayer(const StringView& layerName) const return HasLayer(Level::GetLayerIndex(layerName)); } -bool LayersMask::operator==(const LayersMask& other) const -{ - return Mask == other.Mask; -} - -bool LayersMask::operator!=(const LayersMask& other) const -{ - return Mask != other.Mask; -} - enum class SceneEventType { OnSceneSaving = 0, diff --git a/Source/Engine/Level/Level.h b/Source/Engine/Level/Level.h index 8beed4a24..9fe9cb39b 100644 --- a/Source/Engine/Level/Level.h +++ b/Source/Engine/Level/Level.h @@ -462,7 +462,7 @@ public: /// /// Gets the zero-based index of the layer. /// - static int32 GetLayerIndex(const StringView& layer); + API_FUNCTION() static int32 GetLayerIndex(const StringView& layer); private: