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

Create separate classes for each type for Point, Size and Rectangle #12

Merged
merged 7 commits into from
Jun 29, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
bin/
obj/
/_packages
GeneXus.Drawing-DotNet.sln.DotSettings.user
27 changes: 6 additions & 21 deletions src/Common/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private Point(SKPoint point)
/// <summary>
/// Initializes a new instance of the <see cref='Point'/> class with the specified coordinates.
/// </summary>
public Point(float x, float y)
public Point(int x, int y)
: this(new SKPoint(x, y)) { }

/// <summary>
Expand Down Expand Up @@ -113,18 +113,18 @@ public Point(int dw)
/// <summary>
/// Gets the x-coordinate of this <see cref='Point'/>.
/// </summary>
public float X
public int X
{
readonly get => m_point.X;
readonly get => (int)m_point.X;
set => m_point.X = value;
}

/// <summary>
/// Gets the y-coordinate of this <see cref='Point'/>.
/// </summary>
public float Y
public int Y
{
readonly get => m_point.Y;
readonly get => (int)m_point.Y;
set => m_point.Y = value;
}

Expand All @@ -148,25 +148,10 @@ public float Y
/// </summary>
public static Point Subtract(Point pt, Size sz) => new(pt.m_point - sz.m_size);

/// <summary>
/// Converts a <see cref='Point'/> by performing a ceiling operation on all the coordinates.
/// </summary>
public static Point Ceiling(Point value) => new(unchecked((int)Math.Ceiling(value.X)), unchecked((int)Math.Ceiling(value.Y)));

/// <summary>
/// Converts a <see cref='Point'/> by performing a truncate operation on all the coordinates.
/// </summary>
public static Point Truncate(Point value) => new(unchecked((int)value.X), unchecked((int)value.Y));

/// <summary>
/// Converts a <see cref='Point'/> by performing a round operation on all the coordinates.
/// </summary>
public static Point Round(Point value) => new(unchecked((int)Math.Round(value.X)), unchecked((int)Math.Round(value.Y)));

/// <summary>
/// Translates this <see cref='Point'/> by the specified amount.
/// </summary>
public void Offset(float dx, float dy) => m_point.Offset(dx, dy);
public void Offset(int dx, int dy) => m_point.Offset(dx, dy);

/// <summary>
/// Translates this <see cref='Point'/> by the specified amount.
Expand Down
177 changes: 177 additions & 0 deletions src/Common/PointF.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using System;
using SkiaSharp;

namespace GeneXus.Drawing;

[Serializable]
public struct PointF : IEquatable<PointF>
{
internal SKPoint m_point;

private PointF(SKPoint point)
{
m_point = point;
}

/// <summary>
/// Initializes a new instance of the <see cref='PointF'/> class with the specified coordinates.
/// </summary>
public PointF(float x, float y)
: this(new SKPoint(x, y)) { }

/// <summary>
/// Initializes a new instance of the <see cref='PointF'/> class from a <see cref='SizeF'/> .
/// </summary>
public PointF(SizeF sz)
: this(sz.Width, sz.Height) { }

/// <summary>
/// Initializes a new instance of the <see cref='PointF'/> class using coordinates specified by an integer value.
/// </summary>
public PointF(int dw)
: this(unchecked((short)((dw >> 0) & 0xFFFF)), unchecked((short)((dw >> 16) & 0xFFFF))) { }

/// <summary>
/// Creates a human-readable string that represents this <see cref='PointF'/>.
/// </summary>
public override readonly string ToString() => $"{{X={X},Y={Y}}}";


#region Operators

/// <summary>
/// Creates a <see cref='SKPoint'/> with the coordinates of the specified <see cref='PointF'/> .
/// </summary>
public static explicit operator SKPoint(PointF point) => point.m_point;

/// <summary>
/// Creates a <see cref='SizeF'/> with the coordinates of the specified <see cref='PointF'/> .
/// </summary>
public static explicit operator SizeF(PointF p) => new(p.X, p.Y);

/// <summary>
/// Compares two <see cref='PointF'/> objects. The result specifies whether the values of the
/// <see cref='PointF.X'/> and <see cref='PointF.Y'/> properties of the two
/// <see cref='PointF'/> objects are equal.
/// </summary>
public static bool operator ==(PointF left, PointF right) => left.m_point == right.m_point;

/// <summary>
/// Compares two <see cref='PointF'/> objects. The result specifies whether the values of the
/// <see cref='PointF.X'/> or <see cref='PointF.Y'/> properties of the two
/// <see cref='PointF'/> objects are unequal.
/// </summary>
public static bool operator !=(PointF left, PointF right) => left.m_point != right.m_point;

/// <summary>
/// Translates a <see cref='PointF'/> by a given <see cref='SizeF'/> .
/// </summary>
public static PointF operator +(PointF pt, SizeF sz) => Add(pt, sz);

/// <summary>
/// Translates a <see cref='PointF'/> by the negative of a given <see cref='SizeF'/> .
/// </summary>
public static PointF operator -(PointF pt, SizeF sz) => Subtract(pt, sz);

#endregion


#region IEquatable

/// <summary>
/// Tests whether a <see cref='PointF'/> has the same coordinates
/// as this Point.
/// </summary>
public readonly bool Equals(PointF other) => m_point == other.m_point;

/// <summary>
/// Tests whether <paramref name="obj"/> is a <see cref='PointF'/> with the same coordinates
/// as this Point.
/// </summary>
public override readonly bool Equals(object obj) => m_point.Equals(obj);

/// <summary>
/// Returns a hash code.
/// </summary>
public override readonly int GetHashCode() => m_point.GetHashCode();

#endregion


#region Fields

/// <summary>
/// Creates a new instance of the <see cref='PointF'/> class with member data left uninitialized.
/// </summary>
public static readonly PointF Empty = default;

#endregion


#region Properties

/// <summary>
/// Gets the x-coordinate of this <see cref='PointF'/>.
/// </summary>
public float X
{
readonly get => m_point.X;
set => m_point.X = value;
}

/// <summary>
/// Gets the y-coordinate of this <see cref='PointF'/>.
/// </summary>
public float Y
{
readonly get => m_point.Y;
set => m_point.Y = value;
}

/// <summary>
/// Gets a value indicating whether this <see cref='PointF'/> is empty.
/// </summary>
public readonly bool IsEmpty => m_point.IsEmpty;

#endregion


#region Methods

/// <summary>
/// Translates a <see cref='PointF'/> by a given <see cref='SizeF'/> .
/// </summary>
public static PointF Add(PointF pt, SizeF sz) => new(pt.m_point + sz.m_size);

/// <summary>
/// Translates a <see cref='PointF'/> by the negative of a given <see cref='SizeF'/> .
/// </summary>
public static PointF Subtract(PointF pt, SizeF sz) => new(pt.m_point - sz.m_size);

/// <summary>
/// Converts a <see cref='PointF'/> by performing a ceiling operation on all the coordinates.
/// </summary>
public static PointF Ceiling(PointF value) => new(unchecked((int)Math.Ceiling(value.X)), unchecked((int)Math.Ceiling(value.Y)));

/// <summary>
/// Converts a <see cref='PointF'/> by performing a truncate operation on all the coordinates.
/// </summary>
public static PointF Truncate(PointF value) => new(unchecked((int)value.X), unchecked((int)value.Y));

/// <summary>
/// Converts a <see cref='PointF'/> by performing a round operation on all the coordinates.
/// </summary>
public static PointF Round(PointF value) => new(unchecked((int)Math.Round(value.X)), unchecked((int)Math.Round(value.Y)));

/// <summary>
/// Translates this <see cref='PointF'/> by the specified amount.
/// </summary>
public void Offset(float dx, float dy) => m_point.Offset(dx, dy);

/// <summary>
/// Translates this <see cref='PointF'/> by the specified amount.
/// </summary>
public void Offset(PointF p) => Offset(p.X, p.Y);

#endregion
}
40 changes: 20 additions & 20 deletions src/Common/Rectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal Rectangle(SKRect rect)
/// Initializes a new instance of the <see cref='Rectangle'/> struct with the
/// specified location and size.
/// </summary>
public Rectangle(float x, float y, float width, float height)
public Rectangle(int x, int y, int width, int height)
: this(new SKRect(x, y, width + x, height + y)) { }

/// <summary>
Expand All @@ -29,13 +29,13 @@ public Rectangle(Point location, Size size)
/// <summary>
/// Initializes a new instance of the <see cref='Rectangle'/> struct with the specified location (x, y) and size.
/// </summary>
public Rectangle(float x, float y, Size size)
public Rectangle(int x, int y, Size size)
: this(x, y, size.Width, size.Height) { }

/// <summary>
/// Initializes a new instance of the <see cref='Rectangle'/> struct with the specified location and size (width, height).
/// </summary>
public Rectangle(Point location, float width, float height)
public Rectangle(Point location, int width, int height)
: this(location.X, location.Y, width, height) { }

/// <summary>
Expand Down Expand Up @@ -99,49 +99,49 @@ public Rectangle(Point location, float width, float height)
/// Gets the x-coordinate of the upper-left corner of the rectangular region defined by this
/// <see cref='Rectangle'/> .
/// </summary>
public readonly float Left => m_rect.Left;
public readonly int Left => (int)m_rect.Left;

/// <summary>
/// Gets the x-coordinate of the lower-right corner of the rectangular region defined by this
/// <see cref='Rectangle'/>.
/// </summary>
public readonly float Right => m_rect.Right;
public readonly int Right => (int)m_rect.Right;

/// <summary>
/// Gets the y-coordinate of the upper-left corner of the rectangular region defined by this
/// <see cref='Rectangle'/>.
/// </summary>
public readonly float Top => m_rect.Top;
public readonly int Top => (int)m_rect.Top;

/// <summary>
/// Gets the y-coordinate of the lower-right corner of the rectangular region defined by this
/// <see cref='Rectangle'/>.
/// </summary>
public readonly float Bottom => m_rect.Bottom;
public readonly int Bottom => (int)m_rect.Bottom;

/// <summary>
/// Gets or sets the width of the rectangular region defined by this <see cref='Rectangle'/>.
/// </summary>
public float Width
public int Width
{
readonly get => m_rect.Width;
readonly get => (int)m_rect.Width;
set => m_rect.Right = m_rect.Left + value;
}

/// <summary>
/// Gets or sets the width of the rectangular region defined by this <see cref='Rectangle'/>.
/// </summary>
public float Height
public int Height
{
readonly get => m_rect.Height;
readonly get => (int)m_rect.Height;
set => m_rect.Bottom = m_rect.Top + value;
}

/// <summary>
/// Gets or sets the x-coordinate of the upper-left corner of the rectangular region defined by this
/// <see cref='Rectangle'/>.
/// </summary>
public float X
public int X
{
readonly get => Left;
set
Expand All @@ -155,7 +155,7 @@ public float X
/// Gets or sets the y-coordinate of the upper-left corner of the rectangular region defined by this
/// <see cref='Rectangle'/>.
/// </summary>
public float Y
public int Y
{
readonly get => Top;
set
Expand All @@ -170,7 +170,7 @@ public float Y
/// </summary>
public Size Size
{
get => new Size(Width, Height);
get => new(Width, Height);
set => (Width, Height) = (value.Width, value.Height);
}

Expand All @@ -180,7 +180,7 @@ public Size Size
/// </summary>
public Point Location
{
get => new Point(X, Y);
get => new(X, Y);
set => (X, Y) = (value.X, value.Y);
}

Expand All @@ -198,7 +198,7 @@ public Point Location
/// <summary>
/// Creates a new <see cref='Rectangle'/> with the specified location and size.
/// </summary>
public static Rectangle FromLTRB(float left, float top, float right, float bottom) => new(left, top, unchecked(right - left), unchecked(bottom - top));
public static Rectangle FromLTRB(int left, int top, int right, int bottom) => new(left, top, unchecked(right - left), unchecked(bottom - top));

#endregion

Expand All @@ -215,7 +215,7 @@ public Point Location
/// Determines if the specified point is contained within the rectangular region defined by this
/// <see cref='Rectangle'/> .
/// </summary>
public readonly bool Contains(float x, float y) => m_rect.Contains(x, y);
public readonly bool Contains(int x, int y) => m_rect.Contains(x, y);

/// <summary>
/// Determines if the specified point is contained within the rectangular region defined by this
Expand Down Expand Up @@ -260,7 +260,7 @@ public static Rectangle Union(Rectangle a, Rectangle b)
/// <summary>
/// Inflates this <see cref='Rectangle'/> by the specified amount.
/// </summary>
public void Inflate(float width, float height) => m_rect.Inflate(width, height);
public void Inflate(int width, int height) => m_rect.Inflate(width, height);

/// <summary>
/// Inflates this <see cref='Rectangle'/> by the specified amount.
Expand All @@ -270,7 +270,7 @@ public static Rectangle Union(Rectangle a, Rectangle b)
/// <summary>
/// Creates a <see cref='Rectangle'/> that is inflated by the specified amount.
/// </summary>
public static Rectangle Inflate(Rectangle rect, float x, float y)
public static Rectangle Inflate(Rectangle rect, int x, int y)
{
var ret = SKRect.Inflate(rect.m_rect, x, y);
return new Rectangle(ret);
Expand All @@ -279,7 +279,7 @@ public static Rectangle Inflate(Rectangle rect, float x, float y)
/// <summary>
/// Adjusts the location of this <see cref='Rectangle'/> by the specified amount.
/// </summary>
public void Offset(float x, float y) => m_rect.Offset(x, y);
public void Offset(int x, int y) => m_rect.Offset(x, y);

/// <summary>
/// Adjusts the location of this <see cref='Rectangle'/> by the specified amount.
Expand Down
Loading