Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Remove depth from Stride UI #2605

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
29 changes: 29 additions & 0 deletions sources/core/Stride.Core.Mathematics/Size2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ public Size2(int width, int height)
/// </summary>
[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="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 1].</exception>
public int this[int index]
{
get
{
switch (index)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the switch pattern here. It has a nicer syntax.

{
case 0: return Width;
case 1: return Height;
default: 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.
Expand Down
158 changes: 158 additions & 0 deletions sources/core/Stride.Core.Mathematics/Size2F.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// THE SOFTWARE.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Stride.Core.Mathematics
Expand Down Expand Up @@ -54,6 +55,16 @@ public Size2F(float width, float height)
Width = width;
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.
Expand All @@ -67,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="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 1].</exception>
public float this[int index]
{
get
{
switch (index)
{
case 0: return Width;
case 1: return Height;
default: 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 @@ -122,6 +162,124 @@ public override int GetHashCode()
return !left.Equals(right);
}

/// <summary>
/// Implements the operator <c>/</c>, component wise.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description of a method shouldn't explain what they implement but what they do.

For instance: "Divides the first size by the second, 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 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ internal enum MarginEdge
{
Left,
Top,
Back,
Right,
Bottom,
Front
Bottom
}

internal sealed class MarginAdorner : AdornerBase<Canvas>
Expand All @@ -39,7 +37,6 @@ public MarginAdorner(UIEditorGameAdornerService service, UIElement gameSideEleme
{
BackgroundColor = Color.WhiteSmoke*0.5f,
Font = font,
DepthAlignment = DepthAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
Expand All @@ -65,56 +62,51 @@ public override void Enable()
// do nothing (margin adorners are not hitable)
}

public override void Update(Vector3 position)
public override void Update(Vector2 position)
{
UpdateFromSettings();

var margin = GameSideElement.Margin;
var offset = GameSideElement.RenderSize*0.5f;
var offset = (Vector2)GameSideElement.RenderSize * 0.5f;

Vector3 pinOrigin;
Vector3 size;
Vector3 textRelativePosition;
Vector2 pinOrigin;
Size2F size;
Vector2 textRelativePosition;
float value;
switch (MarginEdge)
{
case MarginEdge.Left:
size = new Vector3(Math.Abs(margin.Left), thickness, thickness);
size = new Size2F(Math.Abs(margin.Left), thickness);
value = margin.Left;
pinOrigin = new Vector3(margin.Left >= 0 ? 1.0f : 0.0f, 0.5f, 0.5f);
position += new Vector3(-offset.X, 0.0f, 0.0f);
textRelativePosition = new Vector3(margin.Left < 0 ? 1.0f : 0.0f, 0.5f, 0.5f);
pinOrigin = new Vector2(margin.Left >= 0 ? 1.0f : 0.0f, 0.5f);
position += new Vector2(-offset.X, 0.0f);
textRelativePosition = new Vector2(margin.Left < 0 ? 1.0f : 0.0f, 0.5f);
break;

case MarginEdge.Right:
size = new Vector3(Math.Abs(margin.Right), thickness, thickness);
size = new Size2F(Math.Abs(margin.Right), thickness);
value = margin.Right;
pinOrigin = new Vector3(margin.Right <= 0 ? 1.0f : 0.0f, 0.0f, 0.5f);
position += new Vector3(offset.X, 0.0f, 0.0f);
textRelativePosition = new Vector3(margin.Right > 0 ? 1.0f : 0.0f, 0.5f, 0.5f);
pinOrigin = new Vector2(margin.Right <= 0 ? 1.0f : 0.0f, 0.0f);
position += new Vector2(offset.X, 0.0f);
textRelativePosition = new Vector2(margin.Right > 0 ? 1.0f : 0.0f, 0.5f);
break;

case MarginEdge.Top:
size = new Vector3(thickness, Math.Abs(margin.Top), thickness);
size = new Size2F(thickness, Math.Abs(margin.Top));
value = margin.Top;
pinOrigin = new Vector3(0.5f, margin.Top >= 0 ? 1.0f : 0.0f, 0.5f);
position += new Vector3(0.0f, -offset.Y, 0.0f);
textRelativePosition = new Vector3(0.5f, margin.Top < 0 ? 1.0f : 0.0f, 0.5f);
pinOrigin = new Vector2(0.5f, margin.Top >= 0 ? 1.0f : 0.0f);
position += new Vector2(0.0f, -offset.Y);
textRelativePosition = new Vector2(0.5f, margin.Top < 0 ? 1.0f : 0.0f);
break;

case MarginEdge.Bottom:
size = new Vector3(thickness, Math.Abs(margin.Bottom), thickness);
size = new Size2F(thickness, Math.Abs(margin.Bottom));
value = margin.Bottom;
pinOrigin = new Vector3(0.5f, margin.Bottom <= 0 ? 1.0f : 0.0f, 0.5f);
position += new Vector3(0.0f, offset.Y, 0.0f);
textRelativePosition = new Vector3(0.5f, margin.Bottom > 0 ? 1.0f : 0.0f, 0.5f);
pinOrigin = new Vector2(0.5f, margin.Bottom <= 0 ? 1.0f : 0.0f);
position += new Vector2(0.0f, offset.Y);
textRelativePosition = new Vector2(0.5f, margin.Bottom > 0 ? 1.0f : 0.0f);
break;

case MarginEdge.Back:
case MarginEdge.Front:
// FIXME: to be reviewed: not supported yet
throw new NotSupportedException();


default:
throw new ArgumentOutOfRangeException();
}
Expand Down
Loading