Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion sources/core/Stride.Core.Mathematics/Size2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -68,6 +67,35 @@ public Size2(int width, int height)
[DataMember(1)]
public int Height;

/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the Width or Height component, depending on the index.</value>
/// <param name="index">The index of the component to access. Use 0 for the Width component and 1 for the Height component.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 1].</exception>
public int this[int index]
{
readonly get
{
return index switch
{
0 => Width,
1 => Height,
_ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Size2 run from 0 to 1, inclusive."),
};
}
set
{
switch (index)
{
case 0: Width = value; break;
case 1: Height = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Size2 run from 0 to 1, inclusive.");
}
}
}

/// <summary>
/// Determines whether the specified <see cref="object"/> is equal to this instance.
/// </summary>
Expand Down
159 changes: 158 additions & 1 deletion sources/core/Stride.Core.Mathematics/Size2F.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Stride.Core.Mathematics;
Expand Down Expand Up @@ -56,6 +56,16 @@ public Size2F(float width, float height)
Height = height;
}

/// <summary>
/// Initializes a new instance of the <see cref="Size2F"/> struct.
/// </summary>
/// <param name="uniform">The width and height of the <see cref="Size2F"/>.</param>
public Size2F(float uniform)
{
Width = uniform;
Height = uniform;
}

/// <summary>
/// Width.
/// </summary>
Expand All @@ -68,6 +78,35 @@ public Size2F(float width, float height)
[DataMember(1)]
public float Height;

/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the Width or Height component, depending on the index.</value>
/// <param name="index">The index of the component to access. Use 0 for the Width component and 1 for the Height component.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 1].</exception>
public float this[int index]
{
readonly get
{
return index switch
{
0 => Width,
1 => Height,
_ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Size2F run from 0 to 1, inclusive."),
};
}
set
{
switch (index)
{
case 0: Width = value; break;
case 1: Height = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Size2F run from 0 to 1, inclusive.");
}
}
}

/// <summary>
/// Determines whether the specified <see cref="object"/> is equal to this instance.
/// </summary>
Expand Down Expand Up @@ -118,6 +157,124 @@ public override readonly int GetHashCode()
return !left.Equals(right);
}

/// <summary>
/// Implements the operator <c>/</c>, component wise.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Size2F operator /(Size2F left, Size2F right)
{
return new Size2F(left.Width / right.Width, left.Height / right.Height);
}

/// <summary>
/// Implements the operator <c>/</c>, component wise.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Size2F operator /(Size2F left, float right)
{
return new Size2F(left.Width / right, left.Height / right);
}

/// <summary>
/// Implements the operator <c>*</c>, component wise.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Size2F operator *(Size2F left, Size2F right)
{
return new Size2F(left.Width * right.Width, left.Height * right.Height);
}

/// <summary>
/// Implements the operator <c>*</c>, component wise.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Size2F operator *(Size2F left, float right)
{
return new Size2F(left.Width * right, left.Height * right);
}

/// <summary>
/// Implements the operator <c>+</c>, component wise.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Size2F operator +(Size2F left, Size2F right)
{
return new Size2F(left.Width + right.Width, left.Height + right.Height);
}

/// <summary>
/// Implements the operator <c>-</c>, component wise.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Size2F operator -(Size2F left, Size2F right)
{
return new Size2F(left.Width + right.Width, left.Height + right.Height);
}

/// <summary>
/// Returns a size containing the largest components of the specified sizes.
/// </summary>
/// <param name="left">The first source size.</param>
/// <param name="right">The second source size.</param>
/// <returns>A size containing the largest components of the source size.</returns>
public static Size2F Max(Size2F left, Size2F right)
{
return new Size2F(Math.Max(left.Width, right.Width), Math.Max(left.Height, right.Height));
}

/// <summary>
/// Returns a size containing the smallest components of the specified sizes.
/// </summary>
/// <param name="left">The first source size.</param>
/// <param name="right">The second source size.</param>
/// <returns>A size containing the smallest components of the source size.</returns>
public static Size2F Min(Size2F left, Size2F right)
{
return new Size2F(Math.Min(left.Width, right.Width), Math.Min(left.Height, right.Height));
}

/// <summary>
/// Casts from <see cref="Vector2"/> to <see cref="Size2F"/>.
/// </summary>
/// <param name="vector">Value to cast.</param>
public static explicit operator Size2F(Vector2 vector)
{
return Unsafe.BitCast<Vector2, Size2F>(vector);
}

/// <summary>
/// Casts from <see cref="Size2F"/> to <see cref="Vector2"/>.
/// </summary>
/// <param name="size">Value to cast.</param>
public static explicit operator Vector2(Size2F size)
{
return Unsafe.BitCast<Size2F, Vector2>(size);
}

/// <inheritdoc/>
public override readonly string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal interface IAdornerBase<out TVisual>

void Show();

void Update(Vector3 position);
void Update(Vector2 position);
}

/// <summary>
Expand Down Expand Up @@ -77,7 +77,7 @@ public void Show()
Visual.Visibility = Visibility.Visible;
}

public abstract void Update(Vector3 position);
public abstract void Update(Vector2 position);

protected void InitializeAttachedProperties()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Stride.Assets.Presentation.AssetEditors.UIEditor.Adorners
internal abstract class BorderAdorner : AdornerBase<Border>
{
private float borderThickness;
private Vector3 size;
private Size2F size;

protected BorderAdorner(UIEditorGameAdornerService service, UIElement gameSideElement)
: base(service, gameSideElement)
Expand Down Expand Up @@ -50,7 +50,7 @@ public float BorderThickness
}
}

public Vector3 Size
public Size2F Size
{
get { return size; }
set
Expand All @@ -64,13 +64,13 @@ public Vector3 Size

protected virtual void UpdateBorderThickness()
{
Visual.BorderThickness = Thickness.UniformCuboid(borderThickness);
Visual.BorderThickness = Thickness.Uniform(borderThickness);
}

protected virtual void UpdateSize()
{
// border thickness is added to the total size
Visual.Size = size + new Vector3(BorderThickness*2);
Visual.Size = size + new Size2F(BorderThickness*2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Unlit()
Visual.Opacity = 0.0f;
}

public override void Update(Vector3 position)
public override void Update(Vector2 position)
{
UpdateFromSettings();
Size = GameSideElement.RenderSize;
Expand All @@ -45,7 +45,7 @@ public override void Update(Vector3 position)
protected override void UpdateSize()
{
base.UpdateSize();
Visual.Margin = Thickness.UniformCuboid(-BorderThickness);
Visual.Margin = Thickness.Uniform(-BorderThickness);
}

private void UpdateFromSettings()
Expand Down
Loading