diff --git a/src/D2DLibExport/D2DBrush.cs b/src/D2DLibExport/D2DBrush.cs index df29ed866..841f8e65a 100644 --- a/src/D2DLibExport/D2DBrush.cs +++ b/src/D2DLibExport/D2DBrush.cs @@ -33,5 +33,10 @@ public override void Dispose() if (this.Handle != IntPtr.Zero) D2D.ReleaseBrush(this.Handle); this.handle = IntPtr.Zero; } + + public void SetTransform(Matrix3x2 transform) + { + D2D.BrushSetTransform(this.Handle, transform); + } } } diff --git a/src/D2DLibExport/D2DLib.cs b/src/D2DLibExport/D2DLib.cs index a85b4b91d..f988a7881 100644 --- a/src/D2DLibExport/D2DLib.cs +++ b/src/D2DLibExport/D2DLib.cs @@ -387,6 +387,10 @@ public static extern HANDLE CreateBitmapBrush(HANDLE ctx, HANDLE bitmap, D2DExtendMode extendModeX, D2DExtendMode extendModeY, D2DBitmapInterpolationMode interpolationMode = D2DBitmapInterpolationMode.Linear); + + [DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)] + public static extern void BrushSetTransform(HANDLE brushHandler, Matrix3x2 transform); + [DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern void ReleaseBrush(HANDLE brushCtx); diff --git a/src/Examples/SampleCode/BitmapBrushWithTransform.cs b/src/Examples/SampleCode/BitmapBrushWithTransform.cs new file mode 100644 index 000000000..9e6b856e9 --- /dev/null +++ b/src/Examples/SampleCode/BitmapBrushWithTransform.cs @@ -0,0 +1,121 @@ +/* +* MIT License +* +* Copyright (c) 2009-2021 Jingwood, unvell.com. All right reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + + +using System.ComponentModel; +using System.Drawing; +using unvell.D2DLib.Examples.Properties; + +namespace unvell.D2DLib.Examples.SampleCode +{ + public partial class BitmapBrushWithTransform : ExampleForm + { + public BitmapBrushWithTransform() + { + Text = "BitmapBrushWithTransform - D2DLib Sample Code"; + } + + D2DBitmap bitmap; + D2DBitmapBrush brush; + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + bitmap = Device.LoadBitmap(Resources.IMGP6873); + brush = Device.CreateBitmapBrush(bitmap, D2DExtendMode.Wrap, D2DExtendMode.Wrap); + + AnimationDraw = true; + x = startX; + y = startY; + } + + private float startX = 300, startY = 300; + private float x = 0, y = 0; + private float width = 300, height = 200; + private int direction = 0; + private int speed = 5; + + protected override void OnFrame() + { + switch (direction) + { + case 0: + x+=speed; + if (x > bitmap.Width - width-startX) + { + direction++; + } + break; + + case 1: + y += speed; + if (y > bitmap.Height - height-startY) + { + direction++; + } + break; + case 2: + x -= speed; + if (x < 0) + { + direction++; + } + break; + case 3: + y -= speed; + if (y < 0) + { + direction = 0; + } + break; + } + } + + protected override void OnRender(D2DGraphics g) + { + + brush.SetTransform(Matrix3x2.CreateTranslation(-x, -y)); + + using (var pen = Device.CreatePen(D2DColor.Transparent)) + { + g.DrawRoundedRectangle(new D2DRoundedRect + { + rect = new D2DRect(startY, startY, width, height), + radiusX = 10, + radiusY = 10 + }, pen, brush); + } + } + + protected override void OnFormClosed(FormClosedEventArgs e) + { + base.OnFormClosed(e); + + this.bitmap?.Dispose(); + this.brush?.Dispose(); + } + } + +} diff --git a/src/Examples/SampleCode/HelloWorld.cs b/src/Examples/SampleCode/HelloWorld.cs index fe879eedf..f3a7f3d08 100644 --- a/src/Examples/SampleCode/HelloWorld.cs +++ b/src/Examples/SampleCode/HelloWorld.cs @@ -36,7 +36,7 @@ protected override void OnRender(D2DGraphics g) g.DrawPolygon(new D2DPoint[] { new D2DPoint(100, 100), new D2DPoint(150, 150), new D2DPoint(100, 150) }, D2DColor.Black, 0, D2DDashStyle.Solid, D2DColor.Red); - g.DrawText("Text drawed using Direct2D API (d2dlib)", D2DColor.Black, "Arial", 24, 140, 110); + g.DrawText("Text rendered using Direct2D API (D2DLib)", D2DColor.Black, "Arial", 24, 140, 110); } } diff --git a/src/d2dlib/Brush.cpp b/src/d2dlib/Brush.cpp index abab055ee..0bab4a417 100644 --- a/src/d2dlib/Brush.cpp +++ b/src/d2dlib/Brush.cpp @@ -154,6 +154,16 @@ HANDLE CreateRadialGradientBrush(HANDLE ctx, D2D1_POINT_2F origin, D2D1_POINT_2F return (HANDLE)brushContext; } +void BrushSetTransform(HANDLE brushHandler, D2D1_MATRIX_3X2_F* transform) +{ + D2DBrushContext* brushContext = reinterpret_cast(brushHandler); + + if (brushContext->brush != NULL) { + brushContext->brush->SetTransform(transform); + } +} + + void ReleaseBrush(HANDLE brushHandle) { D2DBrushContext* brushContext = reinterpret_cast(brushHandle); diff --git a/src/d2dlib/Brush.h b/src/d2dlib/Brush.h index b217022b5..12baded44 100644 --- a/src/d2dlib/Brush.h +++ b/src/d2dlib/Brush.h @@ -43,5 +43,7 @@ extern "C" D2D1_EXTEND_MODE extendModeX, D2D1_EXTEND_MODE extendModeY, D2D1_BITMAP_INTERPOLATION_MODE interpolationMode); + D2DLIB_API void BrushSetTransform(HANDLE brushHandler, D2D1_MATRIX_3X2_F* transform); + D2DLIB_API void ReleaseBrush(HANDLE brushHandle); }