Scale node size to the next highest grid-aligned size.

This commit is contained in:
Menotdan
2023-09-18 10:37:23 -04:00
parent 83bf68a64f
commit b202573920
2 changed files with 30 additions and 4 deletions

View File

@@ -99,10 +99,22 @@ namespace FlaxEditor.Surface
/// </summary>
public CreateCustomNodeFunc Create;
private Float2 _size;
/// <summary>
/// Default initial size of the node.
/// </summary>
public Float2 Size;
public Float2 Size {
get
{
return _size;
}
set
{
_size = VisjectSurface.RoundToGrid(value, true);
Debug.Log(_size.ToString());
}
}
/// <summary>
/// Custom set of flags.

View File

@@ -178,11 +178,25 @@ namespace FlaxEditor.Surface
}
}
private Float2 RoundToGrid(Float2 point)
/// <summary>
/// Round a visject coordinate point to the grid.
/// </summary>
/// <param name="point">The point to be rounded.</param>
/// <param name="ceil">Round to ceiling instead?</param>
/// <returns></returns>
public static Float2 RoundToGrid(Float2 point, bool ceil = false)
{
Func<float, float> round = x =>
{
double pointGridUnits = Math.Abs((double)x) / GridSize;
pointGridUnits = ceil ? Math.Ceiling(pointGridUnits) : Math.Floor(pointGridUnits);
return (float)Math.CopySign(pointGridUnits * GridSize, x);
};
Float2 pointToRound = point;
pointToRound.X = (float)Math.CopySign(Math.Floor(Math.Abs((double)pointToRound.X) / GridSize) * GridSize, pointToRound.X);
pointToRound.Y = (float)Math.CopySign(Math.Floor(Math.Abs((double)pointToRound.Y) / GridSize) * GridSize, pointToRound.Y);
pointToRound.X = round(pointToRound.X);
pointToRound.Y = round(pointToRound.Y);
return pointToRound;
}