Skip to content

Commit

Permalink
Merge pull request #97 from jingwood/use-System.Numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
jingwood authored Nov 6, 2022
2 parents 2dad1fc + d6fde2f commit 1e3f692
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 137 deletions.
8 changes: 4 additions & 4 deletions src/D2DLibExport/D2DDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ public D2DGeometry CreatePieGeometry(D2DPoint origin, D2DSize size, float startA
var eangle = endAngle * Math.PI / 180f;
var angleDiff = endAngle - startAngle;

var startPoint = new D2DPoint((float)(origin.x + halfSize.width * Math.Cos(sangle)),
(float)(origin.y + halfSize.height * Math.Sin(sangle)));
var startPoint = new D2DPoint((float)(origin.X + halfSize.width * Math.Cos(sangle)),
(float)(origin.Y + halfSize.height * Math.Sin(sangle)));

var endPoint = new D2DPoint((float)(origin.x + halfSize.width * Math.Cos(eangle)),
(float)(origin.y + halfSize.height * Math.Sin(eangle)));
var endPoint = new D2DPoint((float)(origin.X + halfSize.width * Math.Cos(eangle)),
(float)(origin.Y + halfSize.height * Math.Sin(eangle)));

path.AddLines(new D2DPoint[] { origin, startPoint });

Expand Down
16 changes: 8 additions & 8 deletions src/D2DLibExport/D2DGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public void DrawEllipse(FLOAT x, FLOAT y, FLOAT width, FLOAT height, D2DColor co
FLOAT weight = 1, D2DDashStyle dashStyle = D2DDashStyle.Solid)
{
var ellipse = new D2DEllipse(x, y, width / 2f, height / 2f);
ellipse.origin.x += ellipse.radiusX;
ellipse.origin.y += ellipse.radiusY;
ellipse.origin.X += ellipse.radiusX;
ellipse.origin.Y += ellipse.radiusY;

this.DrawEllipse(ellipse, color, weight, dashStyle);
}
Expand Down Expand Up @@ -142,8 +142,8 @@ public void FillEllipse(D2DPoint p, FLOAT radial, D2DColor color)
public void FillEllipse(D2DPoint p, FLOAT w, FLOAT h, D2DColor color)
{
D2DEllipse ellipse = new D2DEllipse(p, w / 2, h / 2);
ellipse.origin.x += ellipse.radiusX;
ellipse.origin.y += ellipse.radiusY;
ellipse.origin.X += ellipse.radiusX;
ellipse.origin.Y += ellipse.radiusY;

this.FillEllipse(ellipse, color);
}
Expand Down Expand Up @@ -248,14 +248,14 @@ public void PopLayer()
D2D.PopLayer(this.Handle);
}

public void SetTransform(D2DMatrix3x2 mat)
public void SetTransform(Matrix3x2 mat)
{
D2D.SetTransform(this.Handle, ref mat);
}

public D2DMatrix3x2 GetTransform()
public Matrix3x2 GetTransform()
{
D2DMatrix3x2 mat;
Matrix3x2 mat;
D2D.GetTransform(this.Handle, out mat);
return mat;
}
Expand Down Expand Up @@ -443,7 +443,7 @@ public void DrawStrokedText(string text, D2DPoint location,
D2DFontStyle fontStyle = D2DFontStyle.Normal,
D2DFontStretch fontStretch = D2DFontStretch.Normal)
{
this.DrawStrokedText(text, location.x, location.y, strokeColor, strokeWidth, fillColor,
this.DrawStrokedText(text, location.X, location.Y, strokeColor, strokeWidth, fillColor,
fontName, fontSize, fontWeight, fontStyle, fontStretch);
}

Expand Down
6 changes: 3 additions & 3 deletions src/D2DLibExport/D2DLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public static extern HANDLE PushLayer(HANDLE ctx, HANDLE layerHandle, D2DRect co
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SkewTransform([In] HANDLE ctx, [In] FLOAT angleX, [In] FLOAT angleY, [Optional] D2DPoint center);
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetTransform([In] HANDLE context, [In] ref D2DMatrix3x2 mat);
public static extern void SetTransform([In] HANDLE context, [In] ref Matrix3x2 mat);
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void GetTransform([In] HANDLE context, [Out] out D2DMatrix3x2 mat);
public static extern void GetTransform([In] HANDLE context, [Out] out Matrix3x2 mat);
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void ResetTransform([In] HANDLE context);

Expand Down Expand Up @@ -300,7 +300,7 @@ public static extern bool PathStrokeContainsPoint(HANDLE pathCtx, D2DPoint point
public static extern void GetGeometryBounds(HANDLE pathCtx, ref D2DRect rect);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void GetGeometryTransformedBounds(HANDLE pathCtx, ref D2DMatrix3x2 mat3x2, ref D2DRect rect);
public static extern void GetGeometryTransformedBounds(HANDLE pathCtx, ref Matrix3x2 mat3x2, ref D2DRect rect);

#endregion // Geometry

Expand Down
1 change: 1 addition & 0 deletions src/D2DLibExport/D2DLibExport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.7.1" Condition="'$(TargetFramework)' == 'netstandard2.0'" PrivateAssets="all" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/D2DLibExport/D2DPathGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void AddBeziers(D2DBezierSegment[] bezierSegments)
//}

public void AddArc(D2DPoint endPoint, D2DSize size, FLOAT sweepAngle,
D2DArcSize arcSize = D2DArcSize.Small,
D2DSweepDirection sweepDirection = D2DSweepDirection.Clockwise)
D2DArcSize arcSize = D2DArcSize.Small,
D2DSweepDirection sweepDirection = D2DSweepDirection.Clockwise)
{
D2D.AddPathArc(this.Handle, endPoint, size, sweepAngle, arcSize, sweepDirection);
}
Expand Down
120 changes: 22 additions & 98 deletions src/D2DLibExport/D2DStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static D2DColor Randomly()
public static readonly D2DColor HotPink = D2DColor.FromGDIColor(System.Drawing.Color.HotPink);
public static readonly D2DColor LightPink = D2DColor.FromGDIColor(System.Drawing.Color.LightPink);
}
#endregion
#endregion // D2DColor

#region Rect
[Serializable]
Expand All @@ -201,8 +201,9 @@ public D2DRect(float left, float top, float width, float height)
}

public D2DRect(D2DPoint origin, D2DSize size)
: this(origin.x - size.width * 0.5f, origin.y - size.height * 0.5f, size.width, size.height)
{ }
: this(origin.X - size.width * 0.5f, origin.Y - size.height * 0.5f, size.width, size.height)
{
}

public D2DPoint Location
{
Expand All @@ -211,10 +212,10 @@ public D2DPoint Location
{
FLOAT width = this.right - this.left;
FLOAT height = this.bottom - this.top;
this.left = value.x;
this.right = value.x + width;
this.top = value.y;
this.bottom = value.y + height;
this.left = value.X;
this.right = value.X + width;
this.top = value.Y;
this.bottom = value.Y + height;
}
}

Expand Down Expand Up @@ -293,7 +294,7 @@ public static explicit operator System.Drawing.Rectangle(D2DRect rect)
return System.Drawing.Rectangle.Round(rect);
}
}
#endregion Rect
#endregion // Rect

#region Rounded Rect

Expand All @@ -307,75 +308,6 @@ public struct D2DRoundedRect
}
#endregion Rounded Rect

#region Point
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct D2DPoint
{
public FLOAT x;
public FLOAT y;

public D2DPoint(FLOAT x, FLOAT y)
{
this.x = x;
this.y = y;
}

public void Offset(FLOAT offx, FLOAT offy)
{
this.x += offx;
this.y += offy;
}

public static readonly D2DPoint Zero = new D2DPoint(0, 0);
public static readonly D2DPoint One = new D2DPoint(1, 1);

public override bool Equals(object obj)
{
if (!(obj is D2DPoint)) return false;

var p2 = (D2DPoint)obj;

return x == p2.x && y == p2.y;
}

public static bool operator ==(D2DPoint p1, D2DPoint p2)
{
return p1.x == p2.x && p1.y == p2.y;
}

public static bool operator !=(D2DPoint p1, D2DPoint p2)
{
return p1.x != p2.x || p1.y != p2.y;
}

public static implicit operator D2DPoint(System.Drawing.Point p)
{
return new D2DPoint(p.X, p.Y);
}

public static implicit operator D2DPoint(System.Drawing.PointF p)
{
return new D2DPoint(p.X, p.Y);
}

public static implicit operator System.Drawing.PointF(D2DPoint p)
{
return new System.Drawing.PointF(p.x, p.y);
}

public static explicit operator System.Drawing.Point(D2DPoint p)
{
return System.Drawing.Point.Round(p);
}

public override int GetHashCode()
{
return (int)((this.x * 0xff) + this.y);
}
}
#endregion

#region Size
[Serializable]
[StructLayout(LayoutKind.Sequential)]
Expand All @@ -392,6 +324,16 @@ public D2DSize(FLOAT width, FLOAT height)

public static readonly D2DSize Empty = new D2DSize(0, 0);

public static implicit operator D2DSize(Vector2 v)
{
return new D2DSize(v.X, v.Y);
}

public static implicit operator Vector2(D2DSize v)
{
return new Vector2(v.width, v.height);
}

public static implicit operator D2DSize(System.Drawing.Size wsize)
{
return new D2DSize(wsize.Width, wsize.Height);
Expand Down Expand Up @@ -446,8 +388,8 @@ public D2DEllipse(FLOAT x, FLOAT y, FLOAT rx, FLOAT ry)
{
}

public FLOAT X { get { return origin.x; } set { origin.x = value; } }
public FLOAT Y { get { return origin.y; } set { origin.y = value; } }
public FLOAT X { get { return origin.X; } set { origin.X = value; } }
public FLOAT Y { get { return origin.Y; } set { origin.Y = value; } }
}
#endregion

Expand All @@ -474,24 +416,6 @@ public D2DBezierSegment(FLOAT x1, FLOAT y1, FLOAT x2, FLOAT y2, FLOAT x3, FLOAT
this.point3 = new D2DPoint(x3, y3);
}
}
#endregion

#region Matrix
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct D2DMatrix3x2
{
public FLOAT a1, b1;
public FLOAT a2, b2;
public FLOAT a3, b3;

public D2DMatrix3x2(float a1, float b1, float a2, float b2, float a3, float b3)
{
this.a1 = a1; this.b1 = b1;
this.a2 = a2; this.b2 = b2;
this.a3 = a3; this.b3 = b3;
}
}
#endregion // BezierSegment

#endregion // Matrix
}
2 changes: 2 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<Using Include="System.IntPtr" Alias="HANDLE" />
<Using Include="System.Int64" Alias="HRESULT" />
<Using Include="System.Int32" Alias="BOOL" />
<Using Include="System.Numerics" />
<using Include="System.Numerics.Vector2" Alias="D2DPoint" />
</ItemGroup>

<ItemGroup>
Expand Down
21 changes: 2 additions & 19 deletions src/Examples/Demos/HitTestByInverseTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected override void OnRender(D2DGraphics g)
base.OnRender(g);

// set the transform before draw rect
g.SetTransform(mat.toD2DMatrix3x2());
g.SetTransform(mat);

g.FillRectangle(rect, isHitted ? D2DColor.LightYellow : D2DColor.LightGray);
g.DrawRectangle(rect, isHitted ? D2DColor.Red : D2DColor.Blue, 2);
Expand Down Expand Up @@ -165,22 +165,5 @@ public static implicit operator D2DRect(Rect2D r)
return new D2DRect(r.X, r.Y, r.Width, r.Height);
}
}
#endregion /* Rect2D */

public static class NumericExtension
{
public static D2DMatrix3x2 toD2DMatrix3x2(this Matrix3x2 mat)
{
D2DMatrix3x2 d2dmat;

d2dmat.a1 = mat.M11;
d2dmat.b1 = mat.M12;
d2dmat.a2 = mat.M21;
d2dmat.b2 = mat.M22;
d2dmat.a3 = mat.M31;
d2dmat.b3 = mat.M32;

return d2dmat;
}
}
#endregion // Rect2D
}
7 changes: 4 additions & 3 deletions src/Examples/Demos/Whiteboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected override void OnMouseWheel(MouseEventArgs e)

private void drawPen(Point currentPoint)
{
var diff = new Point(currentPoint.X - this.lastPoint.X, currentPoint.Y - this.lastPoint.Y);
var diff = new Vector2(currentPoint.X - this.lastPoint.X, currentPoint.Y - this.lastPoint.Y);

memg.BeginRender();
D2DEllipse ellipse = new D2DEllipse(D2DPoint.Zero, this.penSize);
Expand Down Expand Up @@ -236,9 +236,10 @@ private void drawPen(Point currentPoint)
this.lastPoint = currentPoint;
}


private readonly float[] eraserPenDashes = new[] { 2f, 2f };

private float eraserDashOffset = 0.0f;

private void drawCursor(D2DGraphics g, Point p)
{
if (this.penColor == D2DColor.White)
Expand All @@ -256,7 +257,7 @@ private void drawCursor(D2DGraphics g, Point p)
else
{
// else draw pen
g.DrawEllipse(p, this.penSize, this.penColor, 2.0f);
g.DrawEllipse(new Vector2(p.X, p.Y), this.penSize, this.penColor, 2.0f);
}
}
}
Expand Down

0 comments on commit 1e3f692

Please sign in to comment.