diff --git a/src/hx/widgets/GraphicsContext.hx b/src/hx/widgets/GraphicsContext.hx index 59ed7ae6..2a0db8ab 100644 --- a/src/hx/widgets/GraphicsContext.hx +++ b/src/hx/widgets/GraphicsContext.hx @@ -2,6 +2,7 @@ package hx.widgets; import cpp.Pointer; import wx.widgets.GraphicsContext in WxGraphicsContext; +import wx.widgets.GraphicsPath in WxGraphicsPath; import wx.widgets.Font in WxFont; import wx.widgets.Colour in WxColour; import wx.widgets.WxString; @@ -18,6 +19,45 @@ class GraphicsContext extends GraphicsObject { } } + public function createPath():GraphicsPath { + var graphicsPath = new GraphicsPath(); + return graphicsPath; + } + + @:access(hx.widgets.GraphicsPath) + public function strokePath(path:GraphicsPath) { + var nativePath = graphicscontextRef.ptr.createPath(); + for (call in path.calls) { + switch (call) { + case moveToPoint(x, y): + nativePath.moveToPoint(x, y); + case addArcToPoint(x1, y1, x2, y2, r): + nativePath.addArcToPoint(x1, y1, x2, y2, r); + case addCircle(x, y, r): + nativePath.addCircle(x, y, r); + case addCurveToPoint(cx1, cy1, cx2, cy2, x, y): + nativePath.addCurveToPoint(cx1, cy1, cx2, cy2, x, y); + case addQuadCurveToPoint(cx, cy, x, y): + nativePath.addQuadCurveToPoint(cx, cy, x, y); + case addEllipse(x, y, w, h): + nativePath.addEllipse(x, y, w, h); + case addLineToPoint(x, y): + nativePath.addLineToPoint(x, y); + case addRectangle(x, y, w, h): + nativePath.addRectangle(x, y, w, h); + case addRoundedRectangle(x, y, w, h, r): + nativePath.addRoundedRectangle(x, y, w, h, r); + case addArc(x, y, r, startAngle, endAngle, clockwise): + nativePath.addArc(x, y, r, startAngle, endAngle, clockwise); + case closeSubpath: + nativePath.closeSubpath(); + } + + } + graphicscontextRef.ptr.strokePath(nativePath); + } + + public function strokeLine(x1:Float, y1:Float, x2:Float, y2:Float) { graphicscontextRef.ptr.strokeLine(x1, y1, x2, y2); } @@ -41,6 +81,10 @@ class GraphicsContext extends GraphicsObject { graphicscontextRef.ptr.drawText(str, x, y); } + public function drawRectangle(x:Int, y:Int, width:Int, height:Int) { + graphicscontextRef.ptr.drawRectangle(x, y, width, height); + } + public function drawRoundedRectangle(x:Float, y:Float, width:Float, height:Float, radius:Float) { graphicscontextRef.ptr.drawRoundedRectangle(x, y, width, height, radius); } diff --git a/src/hx/widgets/GraphicsPath.hx b/src/hx/widgets/GraphicsPath.hx new file mode 100644 index 00000000..e544dc8c --- /dev/null +++ b/src/hx/widgets/GraphicsPath.hx @@ -0,0 +1,76 @@ +package hx.widgets; + +import cpp.Reference; +import cpp.Pointer; +import cpp.RawPointer; +import wx.widgets.GraphicsPath in WxGraphicsPath; + +private enum GraphicPathCalls { + moveToPoint(x:Float, y:Float); + addArcToPoint(x1:Float, y1:Float, x2:Float, y2:Float, r:Float); + addCircle(x:Float, y:Float, r:Float); + addCurveToPoint(cx1:Float, cy1:Float, cx2:Float, cy2:Float, x:Float, y:Float); + addQuadCurveToPoint(cx:Float, cy:Float, x:Float, y:Float); + addEllipse(x:Float, y:Float, w:Float, h:Float); + addLineToPoint(x:Float, y:Float); + addRectangle(x:Float,y:Float,w:Float,h:Float); + addRoundedRectangle(x:Float,y:Float,w:Float,h:Float,r:Float); + addArc(x:Float, y:Float, r:Float, startAngle:Float, endAngle:Float, clockwise:Bool); + closeSubpath; +} + +class GraphicsPath extends Object { + // GraphicsPath is created by createGraphicsPath in GraphicsContext + // The issue is that wx creates a path class, but on the stack, which means its freed when it goes out of scope (ie, in the function call), + // which means we cant pass it around in the hxWidgets helper objects... ... so the shitty workaround is to cache the calls the we want to make to the native path object, + // then when we allocate the native path on the stack we "replay" those calls natively while it is valid mem + + private var calls:Array = []; + + public function new() { + } + + public function moveToPoint(x:Float, y:Float) { + calls.push(GraphicPathCalls.moveToPoint(x, y)); + } + + public function addArcToPoint(x1:Float, y1:Float, x2:Float, y2:Float, r:Float) { + calls.push(GraphicPathCalls.addArcToPoint(x1, y1, x2, y2, r)); + } + + public function addCircle(x:Float, y:Float, r:Float) { + calls.push(GraphicPathCalls.addCircle(x, y, r)); + } + + public function addCurveToPoint(cx1:Float, cy1:Float, cx2:Float, cy2:Float, x:Float, y:Float) { + calls.push(GraphicPathCalls.addCurveToPoint(cx1, cy1, cx2, cy2, x, y)); + } + + public function addQuadCurveToPoint(cx:Float, cy:Float, x:Float, y:Float) { + calls.push(GraphicPathCalls.addQuadCurveToPoint(cx, cy, x, y)); + } + + public function addEllipse(x:Float, y:Float, w:Float, h:Float) { + calls.push(GraphicPathCalls.addEllipse(x, y, w, h)); + } + + public function addLineToPoint(x:Float, y:Float) { + calls.push(GraphicPathCalls.addLineToPoint(x, y)); + } + + public function addRectangle(x:Float,y:Float,w:Float,h:Float) { + calls.push(GraphicPathCalls.addRectangle(x, y, w, h)); + } + + public function addRoundedRectangle(x:Float,y:Float,w:Float,h:Float,r:Float) { + calls.push(GraphicPathCalls.addRoundedRectangle(x ,y ,w ,h ,r)); + } + + public function addArc(x:Float, y:Float, r:Float, startAngle:Float, endAngle:Float, clockwise:Bool) { + calls.push(GraphicPathCalls.addArc(x, y, r, startAngle, endAngle, clockwise)); + } + + public function closeSubpath() { + calls.push(GraphicPathCalls.closeSubpath); + } +} \ No newline at end of file diff --git a/src/wx/widgets/GraphicsContext.hx b/src/wx/widgets/GraphicsContext.hx index 989fdf93..0441174c 100644 --- a/src/wx/widgets/GraphicsContext.hx +++ b/src/wx/widgets/GraphicsContext.hx @@ -24,6 +24,8 @@ extern class GraphicsContext extends GraphicsObject { ////////////////////////////////////////////////////////////////////////////////////////////////////////// // Instance functions ////////////////////////////////////////////////////////////////////////////////////////////////////////// + @:native("CreatePath") public function createPath():GraphicsPath; + @:native("StrokePath") public function strokePath(path:GraphicsPath):Void; @:native("StrokeLine") public function strokeLine(x1:Float, y1:Float, x2:Float, y2:Float):Void; @:native("SetPen") public function setPen(pen:Pen):Void; @:native("SetBrush") public function setBrush(brush:Brush):Void; @@ -33,5 +35,6 @@ extern class GraphicsContext extends GraphicsObject { @:native("DrawBitmap") public function drawBitmap(bmp:Bitmap, x:Float, y:Float, width:Float, height:Float):Void; @:native("SetAntialiasMode") public function setAntialiasMode(mode:AntialiasMode):Bool; @:native("SetInterpolationQuality") public function setInterpolationQuality(mode:InterpolationQuality):Bool; + @:native("DrawRectangle") public function drawRectangle(x:Int, y:Int, width:Int, height:Int):Void; } diff --git a/src/wx/widgets/GraphicsPath.hx b/src/wx/widgets/GraphicsPath.hx new file mode 100644 index 00000000..b8826347 --- /dev/null +++ b/src/wx/widgets/GraphicsPath.hx @@ -0,0 +1,29 @@ +package wx.widgets; + +import cpp.Pointer; +import cpp.RawPointer; +@:include("wx/graphics.h") +@:unreflective +@:native("wxGraphicsPath") +@:structAccess +extern class GraphicsPath extends GraphicsObject { + + ////////////////////////////////////////////////////////////////////////////////////////////////////////// + // instance functions + ////////////////////////////////////////////////////////////////////////////////////////////////////////// + + //AddCurveToPoint (wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y) + + @:native("MoveToPoint") public function moveToPoint(x:Float, y:Float):Void; + @:native("AddArcToPoint") public function addArcToPoint(x1:Float, y1:Float, x2:Float, y2:Float, r:Float):Void; + @:native("AddCircle") public function addCircle(x:Float, y:Float, r:Float):Void; + @:native("AddCurveToPoint") public function addCurveToPoint(cx1:Float, cy1:Float, cx2:Float, cy2:Float, x:Float, y:Float):Void; + @:native("AddEllipse") public function addEllipse(x:Float, y:Float, w:Float, h:Float):Void; + @:native("AddLineToPoint") public function addLineToPoint(x:Float, y:Float):Void; + @:native("AddPath") public function addPath(path:GraphicsPath):Void; + @:native("AddQuadCurveToPoint") public function addQuadCurveToPoint (cx:Float,cy:Float,x:Float,y:Float):Void; + @:native("AddRectangle") public function addRectangle(x:Float,y:Float,w:Float,h:Float):Void; + @:native("AddRoundedRectangle") public function addRoundedRectangle(x:Float,y:Float,w:Float,h:Float,r:Float):Void; + @:native("CloseSubpath") public function closeSubpath():Void; + @:native("AddArc") public function addArc(x:Float, y:Float, r:Float, startAngle:Float, endAngle:Float, clockwise:Bool):Void; +}