Skip to content

Commit

Permalink
Merge pull request #106 from Shallowmallow/GraphicsPath
Browse files Browse the repository at this point in the history
GraphicsPath
  • Loading branch information
ianharrigan authored Feb 10, 2024
2 parents de8cc1f + c3bbacf commit dd07500
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/hx/widgets/GraphicsContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
76 changes: 76 additions & 0 deletions src/hx/widgets/GraphicsPath.hx
Original file line number Diff line number Diff line change
@@ -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<GraphicPathCalls> = [];

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);
}
}
3 changes: 3 additions & 0 deletions src/wx/widgets/GraphicsContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

}
29 changes: 29 additions & 0 deletions src/wx/widgets/GraphicsPath.hx
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit dd07500

Please sign in to comment.