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

add brush set transform APIs and sample code #127

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions src/D2DLibExport/D2DBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
4 changes: 4 additions & 0 deletions src/D2DLibExport/D2DLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
121 changes: 121 additions & 0 deletions src/Examples/SampleCode/BitmapBrushWithTransform.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
2 changes: 1 addition & 1 deletion src/Examples/SampleCode/HelloWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/d2dlib/Brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<D2DBrushContext*>(brushHandler);

if (brushContext->brush != NULL) {
brushContext->brush->SetTransform(transform);
}
}


void ReleaseBrush(HANDLE brushHandle)
{
D2DBrushContext* brushContext = reinterpret_cast<D2DBrushContext*>(brushHandle);
Expand Down
2 changes: 2 additions & 0 deletions src/d2dlib/Brush.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}