From 72fe14048b2e9d83c6188f0c484554f4b4e32e50 Mon Sep 17 00:00:00 2001 From: Jingwood Date: Sat, 5 Nov 2022 22:51:00 +0900 Subject: [PATCH 1/5] use Vector2 instead of D2DPoint --- src/D2DLibExport/D2DDevice.cs | 8 +-- src/D2DLibExport/D2DGraphics.cs | 10 ++-- src/D2DLibExport/D2DLibExport.csproj | 1 + src/D2DLibExport/D2DStructs.cs | 81 +++------------------------- src/Directory.Build.props | 2 + src/Examples/Demos/Whiteboard.cs | 25 ++++----- 6 files changed, 31 insertions(+), 96 deletions(-) diff --git a/src/D2DLibExport/D2DDevice.cs b/src/D2DLibExport/D2DDevice.cs index 6414a512b..d5409d958 100644 --- a/src/D2DLibExport/D2DDevice.cs +++ b/src/D2DLibExport/D2DDevice.cs @@ -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 }); diff --git a/src/D2DLibExport/D2DGraphics.cs b/src/D2DLibExport/D2DGraphics.cs index a28b2ef59..50254721d 100644 --- a/src/D2DLibExport/D2DGraphics.cs +++ b/src/D2DLibExport/D2DGraphics.cs @@ -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); } @@ -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); } @@ -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); } diff --git a/src/D2DLibExport/D2DLibExport.csproj b/src/D2DLibExport/D2DLibExport.csproj index 6da8f11b9..da6c4faa0 100644 --- a/src/D2DLibExport/D2DLibExport.csproj +++ b/src/D2DLibExport/D2DLibExport.csproj @@ -10,6 +10,7 @@ + \ No newline at end of file diff --git a/src/D2DLibExport/D2DStructs.cs b/src/D2DLibExport/D2DStructs.cs index dcdd43a4d..ff5272d06 100644 --- a/src/D2DLibExport/D2DStructs.cs +++ b/src/D2DLibExport/D2DStructs.cs @@ -211,10 +211,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; } } @@ -307,75 +307,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)] @@ -446,8 +377,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 diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 0c4628cbd..6adb75dd7 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -33,6 +33,8 @@ + + diff --git a/src/Examples/Demos/Whiteboard.cs b/src/Examples/Demos/Whiteboard.cs index 58296a18c..d104e9c85 100644 --- a/src/Examples/Demos/Whiteboard.cs +++ b/src/Examples/Demos/Whiteboard.cs @@ -101,7 +101,7 @@ private void recreateMemoryGraphics() } private D2DBitmapGraphics memg; - private Point lastPoint, cursorPoint; + private Vector2 lastPoint, cursorPoint; private bool isDrawing; private Size penSize = new Size(5, 5); private D2DColor penColor = D2DColor.Blue; // use white as eraser, other else as normal pen @@ -143,9 +143,9 @@ protected override void OnMouseDown(MouseEventArgs e) } this.isDrawing = true; - this.lastPoint = e.Location; - this.cursorPoint = e.Location; - drawPen(e.Location); + this.lastPoint = new Vector2(e.X, e.Y); + this.cursorPoint = new Vector2(e.X, e.Y); + drawPen(new Vector2(e.Location.X, e.Location.Y)); this.showGettingStart = false; // when we're in the eraser, we want to invalidate since the eraser is animated @@ -156,14 +156,14 @@ protected override void OnMouseDown(MouseEventArgs e) protected override void OnMouseMove(MouseEventArgs e) { - this.cursorPoint = e.Location; + this.cursorPoint = new Vector2(e.X, e.Y); if (this.isDrawing) { - drawPen(e.Location); + drawPen(new Vector2(e.Location.X, e.Location.Y)); } else { - cursorPoint = e.Location; + cursorPoint = new Vector2(e.X, e.Y); } this.Invalidate(); } @@ -171,7 +171,7 @@ protected override void OnMouseMove(MouseEventArgs e) protected override void OnMouseUp(MouseEventArgs e) { this.isDrawing = false; - this.cursorPoint = e.Location; + this.cursorPoint = new Vector2(e.X, e.Y); } protected override void OnMouseEnter(EventArgs e) @@ -202,9 +202,9 @@ protected override void OnMouseWheel(MouseEventArgs e) this.Invalidate(); } - private void drawPen(Point currentPoint) + private void drawPen(Vector2 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); @@ -236,10 +236,11 @@ 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) + + private void drawCursor(D2DGraphics g, Vector2 p) { if (this.penColor == D2DColor.White) { From 35037de655a0f117924b77df8de48debcb1f40e7 Mon Sep 17 00:00:00 2001 From: Jingwood Date: Sat, 5 Nov 2022 22:53:29 +0900 Subject: [PATCH 2/5] use System.Numerics.Matrix3x2 instead of customized 3x2 struct --- src/D2DLibExport/D2DGraphics.cs | 6 +++--- src/D2DLibExport/D2DLib.cs | 6 +++--- src/D2DLibExport/D2DStructs.cs | 20 +----------------- .../Demos/HitTestByInverseTransform.cs | 21 ++----------------- 4 files changed, 9 insertions(+), 44 deletions(-) diff --git a/src/D2DLibExport/D2DGraphics.cs b/src/D2DLibExport/D2DGraphics.cs index 50254721d..b4dfa99f4 100644 --- a/src/D2DLibExport/D2DGraphics.cs +++ b/src/D2DLibExport/D2DGraphics.cs @@ -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; } diff --git a/src/D2DLibExport/D2DLib.cs b/src/D2DLibExport/D2DLib.cs index 0e4b47bee..d4bda2847 100644 --- a/src/D2DLibExport/D2DLib.cs +++ b/src/D2DLibExport/D2DLib.cs @@ -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); @@ -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 diff --git a/src/D2DLibExport/D2DStructs.cs b/src/D2DLibExport/D2DStructs.cs index ff5272d06..83e0af619 100644 --- a/src/D2DLibExport/D2DStructs.cs +++ b/src/D2DLibExport/D2DStructs.cs @@ -405,24 +405,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 } diff --git a/src/Examples/Demos/HitTestByInverseTransform.cs b/src/Examples/Demos/HitTestByInverseTransform.cs index d0a4a0f45..f31d540eb 100644 --- a/src/Examples/Demos/HitTestByInverseTransform.cs +++ b/src/Examples/Demos/HitTestByInverseTransform.cs @@ -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); @@ -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 } From 9504f9dce6ac5f65b377e21b24e11d19a4e302d4 Mon Sep 17 00:00:00 2001 From: Jingwood Date: Sat, 5 Nov 2022 22:53:58 +0900 Subject: [PATCH 3/5] make implicit conversion between D2DSize and Vector2 --- src/D2DLibExport/D2DStructs.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/D2DLibExport/D2DStructs.cs b/src/D2DLibExport/D2DStructs.cs index 83e0af619..10295630c 100644 --- a/src/D2DLibExport/D2DStructs.cs +++ b/src/D2DLibExport/D2DStructs.cs @@ -323,6 +323,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); From 2ee597f43d622b961d37ec6dc3aaa88d59d197c5 Mon Sep 17 00:00:00 2001 From: Jingwood Date: Sat, 5 Nov 2022 22:54:08 +0900 Subject: [PATCH 4/5] code format --- src/D2DLibExport/D2DPathGeometry.cs | 4 ++-- src/D2DLibExport/D2DStructs.cs | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/D2DLibExport/D2DPathGeometry.cs b/src/D2DLibExport/D2DPathGeometry.cs index a2f910cc6..7f89a217e 100644 --- a/src/D2DLibExport/D2DPathGeometry.cs +++ b/src/D2DLibExport/D2DPathGeometry.cs @@ -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); } diff --git a/src/D2DLibExport/D2DStructs.cs b/src/D2DLibExport/D2DStructs.cs index 10295630c..05532bd31 100644 --- a/src/D2DLibExport/D2DStructs.cs +++ b/src/D2DLibExport/D2DStructs.cs @@ -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] @@ -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 { @@ -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 From d6fde2f1e6b5a367901cde758edb620607e98a4e Mon Sep 17 00:00:00 2001 From: Jingwood Date: Sat, 5 Nov 2022 23:17:29 +0900 Subject: [PATCH 5/5] restore example code --- src/Examples/Demos/Whiteboard.cs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Examples/Demos/Whiteboard.cs b/src/Examples/Demos/Whiteboard.cs index d104e9c85..b4b54e7d9 100644 --- a/src/Examples/Demos/Whiteboard.cs +++ b/src/Examples/Demos/Whiteboard.cs @@ -101,7 +101,7 @@ private void recreateMemoryGraphics() } private D2DBitmapGraphics memg; - private Vector2 lastPoint, cursorPoint; + private Point lastPoint, cursorPoint; private bool isDrawing; private Size penSize = new Size(5, 5); private D2DColor penColor = D2DColor.Blue; // use white as eraser, other else as normal pen @@ -143,9 +143,9 @@ protected override void OnMouseDown(MouseEventArgs e) } this.isDrawing = true; - this.lastPoint = new Vector2(e.X, e.Y); - this.cursorPoint = new Vector2(e.X, e.Y); - drawPen(new Vector2(e.Location.X, e.Location.Y)); + this.lastPoint = e.Location; + this.cursorPoint = e.Location; + drawPen(e.Location); this.showGettingStart = false; // when we're in the eraser, we want to invalidate since the eraser is animated @@ -156,14 +156,14 @@ protected override void OnMouseDown(MouseEventArgs e) protected override void OnMouseMove(MouseEventArgs e) { - this.cursorPoint = new Vector2(e.X, e.Y); + this.cursorPoint = e.Location; if (this.isDrawing) { - drawPen(new Vector2(e.Location.X, e.Location.Y)); + drawPen(e.Location); } else { - cursorPoint = new Vector2(e.X, e.Y); + cursorPoint = e.Location; } this.Invalidate(); } @@ -171,7 +171,7 @@ protected override void OnMouseMove(MouseEventArgs e) protected override void OnMouseUp(MouseEventArgs e) { this.isDrawing = false; - this.cursorPoint = new Vector2(e.X, e.Y); + this.cursorPoint = e.Location; } protected override void OnMouseEnter(EventArgs e) @@ -202,7 +202,7 @@ protected override void OnMouseWheel(MouseEventArgs e) this.Invalidate(); } - private void drawPen(Vector2 currentPoint) + private void drawPen(Point currentPoint) { var diff = new Vector2(currentPoint.X - this.lastPoint.X, currentPoint.Y - this.lastPoint.Y); @@ -240,7 +240,7 @@ private void drawPen(Vector2 currentPoint) private float eraserDashOffset = 0.0f; - private void drawCursor(D2DGraphics g, Vector2 p) + private void drawCursor(D2DGraphics g, Point p) { if (this.penColor == D2DColor.White) { @@ -257,7 +257,7 @@ private void drawCursor(D2DGraphics g, Vector2 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); } } }