Merge remote-tracking branch 'origin/master' into 1.1
This commit is contained in:
@@ -223,8 +223,6 @@ namespace FlaxEngine.GUI
|
||||
TextAlignment.Center,
|
||||
TextWrapping.WrapWords
|
||||
);
|
||||
|
||||
base.Draw();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -60,172 +60,6 @@ namespace FlaxEngine.Utilities
|
||||
}, removeEmptyLines ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random double.
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <param name="maxValue">The maximum value</param>
|
||||
/// <returns>A random double</returns>
|
||||
public static double NextDouble(this Random random, double maxValue)
|
||||
{
|
||||
return random.NextDouble() * maxValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random double.
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <param name="minValue">The minimum value</param>
|
||||
/// <param name="maxValue">The maximum value</param>
|
||||
/// <returns>A random double</returns>
|
||||
public static double NextDouble(this Random random, double minValue, double maxValue)
|
||||
{
|
||||
return random.NextDouble() * (maxValue - minValue) + minValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random float.
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <returns>A random float</returns>
|
||||
public static float NextFloat(this Random random)
|
||||
{
|
||||
return (float)random.NextDouble();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random float.
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <param name="maxValue">The maximum value</param>
|
||||
/// <returns>A random float</returns>
|
||||
public static float NextFloat(this Random random, float maxValue)
|
||||
{
|
||||
return (float)random.NextDouble() * maxValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random float.
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <param name="minValue">The minimum value</param>
|
||||
/// <param name="maxValue">The maximum value</param>
|
||||
/// <returns></returns>
|
||||
public static float NextFloat(this Random random, float minValue, float maxValue)
|
||||
{
|
||||
return (float)random.NextDouble() * (maxValue - minValue) + minValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random Color.
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <returns></returns>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random Vector2 with components in range [0;1].
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <returns></returns>
|
||||
public static Vector2 NextVector2(this Random random)
|
||||
{
|
||||
return new Vector2(NextFloat(random, 1.0f),
|
||||
NextFloat(random, 1.0f));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random Vector3 with components in range [0;1].
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <returns></returns>
|
||||
public static Vector3 NextVector3(this Random random)
|
||||
{
|
||||
return new Vector3(NextFloat(random, 1.0f),
|
||||
NextFloat(random, 1.0f),
|
||||
NextFloat(random, 1.0f));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random Vector4 with components in range [0;1].
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <returns></returns>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random Quaternion.
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <returns></returns>
|
||||
public static Quaternion NextQuaternion(this Random random)
|
||||
{
|
||||
return Quaternion.Euler(NextFloat(random, -180, 180),
|
||||
NextFloat(random, -180, 180),
|
||||
NextFloat(random, -180, 180));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random 64-bit signed integer value.
|
||||
/// </summary>
|
||||
/// <param name="random">The random.</param>
|
||||
/// <returns></returns>
|
||||
internal static long NextLong(this Random random)
|
||||
{
|
||||
var numArray = new byte[8];
|
||||
random.NextBytes(numArray);
|
||||
return (long)(BitConverter.ToUInt64(numArray, 0) & 9223372036854775807L);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random normalized 2D direction vector.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <returns>A random normalized 2D direction vector.</returns>
|
||||
public static Vector2 NextDirection2D(this Random random)
|
||||
{
|
||||
return Vector2.Normalize(new Vector2((float)random.NextDouble(), (float)random.NextDouble()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random normalized 3D direction vector.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <returns>A random normalized 3D direction vector.</returns>
|
||||
public static Vector3 NextDirection3D(this Random random)
|
||||
{
|
||||
return Vector3.Normalize(new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random point in a circle of a given radius.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="radius">Radius of circle. Default 1.0f.</param>
|
||||
/// <returns>A random point in a circle of a given radius.</returns>
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the elements of the specified collection to the end of the <see cref="ICollection{T}"/>.
|
||||
/// </summary>
|
||||
@@ -236,15 +70,9 @@ namespace FlaxEngine.Utilities
|
||||
public static void AddRange<T>(this ICollection<T> destination, IEnumerable<T> 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<T>(this Queue<T> queue, IEnumerable<T> 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<T>(this Stack<T> stack, IEnumerable<T> 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<T>(this IEnumerable<T> source, Action<T> 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<T>(this Random random, IList<T> 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<T>(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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random <see cref="bool"/>.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="weight">Normalized value that determines the chance to return true.</param>
|
||||
/// <returns>A <see cref="bool"/> thats either true or false.</returns>
|
||||
public static bool NextBool(this Random random, float weight = 0.5f) => random.NextDouble() < weight;
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random <see cref="byte"/> value between min and max.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>A random <see cref="byte"/> between min and max.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random <see cref="float"/> value between min and max.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>A random <see cref="float"/> between min and max.</returns>
|
||||
public static float NextFloat(this Random random, float min = 0.0f, float max = 1.0f)
|
||||
{
|
||||
return (float)random.NextDouble() * (max - min) + min;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random <see cref="float"/> value between 0 and max.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>A random <see cref="float"/> between min and max.</returns>
|
||||
public static float NextFloat(this Random random, float max)
|
||||
{
|
||||
return (float)random.NextDouble() * max;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random <see cref="Quaternion"/>.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="randomRoll">Should the roll value be randomized.</param>
|
||||
/// <returns>A random <see cref="Quaternion"/>.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random <see cref="Vector2"/> point in a circle of a given radius.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="radius">Radius of circle. Default 1.0f./>.</param>
|
||||
/// <returns>A random <see cref="Vector2"/>.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a uniformly distributed random unit length vector point on a unit sphere.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <returns>A random <see cref="Vector3"/>.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random <see cref="Vector2"/> with components in a given range.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>A random <see cref="Vector2"/>.</returns>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random <see cref="Vector3"/> with components in a given range.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>A random <see cref="Vector3"/>.</returns>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random <see cref="Vector4"/> with components in a given range.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>A random <see cref="Vector4"/>.</returns>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random <see cref="Color"/>.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="randomAlpha">Randomize the alpha value.</param>
|
||||
/// <returns>A nice random <see cref="Color"/>.</returns>
|
||||
public static Color NextColor(this Random random, bool randomAlpha = false)
|
||||
{
|
||||
return new Color(NextFloat(random), NextFloat(random), NextFloat(random), randomAlpha ? NextFloat(random) : 1.0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random <see cref="ColorHSV"/>.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="randomAlpha">Randomize the alpha value.</param>
|
||||
/// <returns>A nice random <see cref="ColorHSV"/>.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random <see cref="double"/>.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>A random <see cref="double"/>.</returns>
|
||||
public static double NextDouble(this Random random, double min = 0.0d, double max = 1.0d)
|
||||
{
|
||||
return random.NextDouble() * (max - min) + min;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random <see cref="double"/>.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>A random <see cref="double"/>.</returns>
|
||||
public static double NextDouble(this Random random, double max = 1.0d)
|
||||
{
|
||||
return random.NextDouble() * max;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random <see cref="long"/>.
|
||||
/// </summary>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <returns>A random <see cref="long"/>.</returns>
|
||||
internal static long NextLong(this Random random)
|
||||
{
|
||||
var numArray = new byte[8];
|
||||
random.NextBytes(numArray);
|
||||
return (long)(BitConverter.ToUInt64(numArray, 0) & long.MaxValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random value of the given enum.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum">The enum to get the value from.</typeparam>
|
||||
/// <param name="random">An instance of <see cref="Random"/>.</param>
|
||||
/// <returns>A random enum value.</returns>
|
||||
public static TEnum NextEnum<TEnum>(this Random random)
|
||||
{
|
||||
Array values = Enum.GetValues(typeof(TEnum));
|
||||
return (TEnum)values.GetValue(random.Next(values.Length));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user