diff --git a/Source/Engine/UI/GUI/Tooltip.cs b/Source/Engine/UI/GUI/Tooltip.cs index 24c0651ed..b4abfaf66 100644 --- a/Source/Engine/UI/GUI/Tooltip.cs +++ b/Source/Engine/UI/GUI/Tooltip.cs @@ -223,8 +223,6 @@ namespace FlaxEngine.GUI TextAlignment.Center, TextWrapping.WrapWords ); - - base.Draw(); } /// diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index e9030af77..b3f6d2053 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -60,172 +60,6 @@ namespace FlaxEngine.Utilities }, removeEmptyLines ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None); } - /// - /// Gets a random double. - /// - /// The random. - /// The maximum value - /// A random double - public static double NextDouble(this Random random, double maxValue) - { - return random.NextDouble() * maxValue; - } - - /// - /// Gets a random double. - /// - /// The random. - /// The minimum value - /// The maximum value - /// A random double - public static double NextDouble(this Random random, double minValue, double maxValue) - { - return random.NextDouble() * (maxValue - minValue) + minValue; - } - - /// - /// Gets a random float. - /// - /// The random. - /// A random float - public static float NextFloat(this Random random) - { - return (float)random.NextDouble(); - } - - /// - /// Gets a random float. - /// - /// The random. - /// The maximum value - /// A random float - public static float NextFloat(this Random random, float maxValue) - { - return (float)random.NextDouble() * maxValue; - } - - /// - /// Gets a random float. - /// - /// The random. - /// The minimum value - /// The maximum value - /// - public static float NextFloat(this Random random, float minValue, float maxValue) - { - return (float)random.NextDouble() * (maxValue - minValue) + minValue; - } - - /// - /// Gets a random Color. - /// - /// The random. - /// - public static Color NextColor(this Random random) - { - return new Color(NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f)); - } - - /// - /// Gets a random Vector2 with components in range [0;1]. - /// - /// The random. - /// - public static Vector2 NextVector2(this Random random) - { - return new Vector2(NextFloat(random, 1.0f), - NextFloat(random, 1.0f)); - } - - /// - /// Gets a random Vector3 with components in range [0;1]. - /// - /// The random. - /// - public static Vector3 NextVector3(this Random random) - { - return new Vector3(NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f)); - } - - /// - /// Gets a random Vector4 with components in range [0;1]. - /// - /// The random. - /// - public static Vector4 NextVector4(this Random random) - { - return new Vector4(NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f)); - } - - /// - /// Gets a random Quaternion. - /// - /// The random. - /// - public static Quaternion NextQuaternion(this Random random) - { - return Quaternion.Euler(NextFloat(random, -180, 180), - NextFloat(random, -180, 180), - NextFloat(random, -180, 180)); - } - - /// - /// Gets a random 64-bit signed integer value. - /// - /// The random. - /// - internal static long NextLong(this Random random) - { - var numArray = new byte[8]; - random.NextBytes(numArray); - return (long)(BitConverter.ToUInt64(numArray, 0) & 9223372036854775807L); - } - - /// - /// Generates a random normalized 2D direction vector. - /// - /// An instance of . - /// A random normalized 2D direction vector. - public static Vector2 NextDirection2D(this Random random) - { - return Vector2.Normalize(new Vector2((float)random.NextDouble(), (float)random.NextDouble())); - } - - /// - /// Generates a random normalized 3D direction vector. - /// - /// An instance of . - /// A random normalized 3D direction vector. - public static Vector3 NextDirection3D(this Random random) - { - return Vector3.Normalize(new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble())); - } - - /// - /// Generates a random point in a circle of a given radius. - /// - /// An instance of . - /// Radius of circle. Default 1.0f. - /// A random point in a circle of a given radius. - public static Vector2 PointInACircle(this Random random, float radius = 1.0f) - { - var randomRadius = (float)random.NextDouble() * radius; - - return new Vector2 - { - X = (float)Math.Cos(random.NextDouble()) * randomRadius, - Y = (float)Math.Sin(random.NextDouble()) * randomRadius, - }; - } - /// /// Adds the elements of the specified collection to the end of the . /// @@ -236,15 +70,9 @@ namespace FlaxEngine.Utilities public static void AddRange(this ICollection destination, IEnumerable collection) { if (destination == null) - { throw new ArgumentNullException(nameof(destination)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - foreach (var item in collection) { destination.Add(item); @@ -261,15 +89,9 @@ namespace FlaxEngine.Utilities public static void EnqueueRange(this Queue queue, IEnumerable collection) { if (queue == null) - { throw new ArgumentNullException(nameof(queue)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - foreach (var item in collection) { queue.Enqueue(item); @@ -286,15 +108,9 @@ namespace FlaxEngine.Utilities public static void PushRange(this Stack stack, IEnumerable collection) { if (stack == null) - { throw new ArgumentNullException(nameof(stack)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - foreach (var item in collection) { stack.Push(item); @@ -311,15 +127,9 @@ namespace FlaxEngine.Utilities public static void ForEach(this IEnumerable source, Action action) { if (source == null) - { throw new ArgumentNullException(nameof(source)); - } - if (action == null) - { throw new ArgumentNullException(nameof(action)); - } - foreach (var item in source) { action(item); @@ -338,15 +148,9 @@ namespace FlaxEngine.Utilities public static T Choose(this Random random, IList collection) { if (random == null) - { throw new ArgumentNullException(nameof(random)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - return collection[random.Next(collection.Count)]; } @@ -362,15 +166,9 @@ namespace FlaxEngine.Utilities public static T Choose(this Random random, params T[] collection) { if (random == null) - { throw new ArgumentNullException(nameof(random)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - return collection[random.Next(collection.Length)]; } @@ -404,5 +202,201 @@ namespace FlaxEngine.Utilities collection[n] = value; } } + + /// + /// Generates a random . + /// + /// An instance of . + /// Normalized value that determines the chance to return true. + /// A thats either true or false. + public static bool NextBool(this Random random, float weight = 0.5f) => random.NextDouble() < weight; + + /// + /// Generates a random value between min and max. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random between min and max. + public static byte NextByte(this Random random, byte min = 0, byte max = byte.MaxValue) + { + return (byte)random.Next(min, max == byte.MaxValue ? byte.MaxValue + 1 : max); + } + + /// + /// Generates a random value between min and max. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random between min and max. + public static float NextFloat(this Random random, float min = 0.0f, float max = 1.0f) + { + return (float)random.NextDouble() * (max - min) + min; + } + + /// + /// Generates a random value between 0 and max. + /// + /// An instance of . + /// The maximum value. + /// A random between min and max. + public static float NextFloat(this Random random, float max) + { + return (float)random.NextDouble() * max; + } + + /// + /// Generates a random . + /// + /// An instance of . + /// Should the roll value be randomized. + /// A random . + public static Quaternion NextQuaternion(this Random random, bool randomRoll = false) + { + return Quaternion.Euler(NextFloat(random, -180.0f, 180.0f), NextFloat(random, -180.0f, 180.0f), randomRoll ? NextFloat(random, -180.0f, 180.0f) : 0.0f); + } + + /// + /// Generates a random point in a circle of a given radius. + /// + /// An instance of . + /// Radius of circle. Default 1.0f./>. + /// A random . + public static Vector2 NextUnitVector2(this Random random, float radius = 1.0f) + { + var randomRadius = (float)random.NextDouble() * radius; + return new Vector2((float)Math.Cos(random.NextDouble()) * randomRadius, (float)Math.Sin(random.NextDouble()) * randomRadius); + } + + /// + /// Generates a uniformly distributed random unit length vector point on a unit sphere. + /// + /// An instance of . + /// A random . + public static Vector3 NextUnitVector3(this Random random) + { + Vector3 output; + float l; + + do + { + // Create random float with a mean of 0 and deviation of ±1 + output.X = NextFloat(random) * 2.0f - 1.0f; + output.Y = NextFloat(random) * 2.0f - 1.0f; + output.Z = NextFloat(random) * 2.0f - 1.0f; + + l = output.LengthSquared; + } while (l > 1 || l < Mathf.Epsilon); + + output.Normalize(); + + return output; + } + + /// + /// Gets a random with components in a given range. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random . + public static Vector2 NextVector2(this Random random, float min = 0.0f, float max = 1.0f) + { + return new Vector2(NextFloat(random, min, max), NextFloat(random, min, max)); + } + + /// + /// Gets a random with components in a given range. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random . + public static Vector3 NextVector3(this Random random, float min = 0.0f, float max = 1.0f) + { + return new Vector3(NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max)); + } + + /// + /// Gets a random with components in a given range. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random . + public static Vector4 NextVector4(this Random random, float min = 0.0f, float max = 1.0f) + { + return new Vector4(NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max)); + } + + /// + /// Generates a random . + /// + /// An instance of . + /// Randomize the alpha value. + /// A nice random . + public static Color NextColor(this Random random, bool randomAlpha = false) + { + return new Color(NextFloat(random), NextFloat(random), NextFloat(random), randomAlpha ? NextFloat(random) : 1.0f); + } + + /// + /// Generates a random . + /// + /// An instance of . + /// Randomize the alpha value. + /// A nice random . + public static ColorHSV NextColorHSV(this Random random, bool randomAlpha = false) + { + return new ColorHSV(NextFloat(random, 0.0f, 360.0f), 1.0f, 1.0f, randomAlpha ? NextFloat(random) : 1.0f); + } + + /// + /// Gets a random . + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random . + public static double NextDouble(this Random random, double min = 0.0d, double max = 1.0d) + { + return random.NextDouble() * (max - min) + min; + } + + /// + /// Gets a random . + /// + /// An instance of . + /// The maximum value. + /// A random . + public static double NextDouble(this Random random, double max = 1.0d) + { + return random.NextDouble() * max; + } + + /// + /// Gets a random . + /// + /// An instance of . + /// A random . + internal static long NextLong(this Random random) + { + var numArray = new byte[8]; + random.NextBytes(numArray); + return (long)(BitConverter.ToUInt64(numArray, 0) & long.MaxValue); + } + + /// + /// Returns a random value of the given enum. + /// + /// The enum to get the value from. + /// An instance of . + /// A random enum value. + public static TEnum NextEnum(this Random random) + { + Array values = Enum.GetValues(typeof(TEnum)); + return (TEnum)values.GetValue(random.Next(values.Length)); + } } }