Bladeren bron

added beginTileFill(), drawPie() in Graphics

ncannasse 10 jaren geleden
bovenliggende
commit
d6f17b2562
2 gewijzigde bestanden met toevoegingen van 82 en 9 verwijderingen
  1. 52 1
      h2d/Graphics.hx
  2. 30 8
      samples/draw/Draw.hx

+ 52 - 1
h2d/Graphics.hx

@@ -118,6 +118,13 @@ class Graphics extends Drawable {
 	var xMax : Float;
 	var yMax : Float;
 
+	var ma : Float = 1.;
+	var mb : Float = 0.;
+	var mc : Float = 0.;
+	var md : Float = 1.;
+	var mx : Float = 0.;
+	var my : Float = 0.;
+
 	public var tile : h2d.Tile;
 
 	public function new(?parent) {
@@ -257,6 +264,35 @@ class Graphics extends Drawable {
 		doFill = true;
 	}
 
+	public function beginTileFill( ?dx : Float, ?dy : Float, ?scaleX : Float, ?scaleY : Float, ?tile : h2d.Tile ) {
+		beginFill(0xFFFFFF);
+		if( dx == null ) dx = 0;
+		if( dy == null ) dy = 0;
+		if( tile != null ) {
+			if( this.tile != null && tile.getTexture() != this.tile.getTexture() ) {
+				var tex = this.tile.getTexture();
+				if( tex.width != 1 || tex.height != 1 )
+					throw "All tiles must be of the same texture";
+			}
+			this.tile = tile;
+		} else
+			tile = this.tile;
+		if( tile == null )
+			throw "Tile not specified";
+		if( scaleX == null ) scaleX = 1;
+		if( scaleY == null ) scaleY = 1;
+
+		var tex = tile.getTexture();
+		var pixWidth = 1 / tex.width;
+		var pixHeight = 1 / tex.height;
+		ma = pixWidth / scaleX;
+		mb = 0;
+		mc = 0;
+		md = pixHeight / scaleY;
+		mx = -dx * ma;
+		my = -dy * md;
+	}
+
 	public function lineStyle( size : Float = 0, color = 0, alpha = 1. ) {
 		flush();
 		this.lineSize = size;
@@ -296,6 +332,21 @@ class Graphics extends Drawable {
 		}
 	}
 
+	public function drawPie( cx : Float, cy : Float, ray : Float, angleStart:Float, angleLength:Float, nsegments = 0 ) {
+		flush();
+		addPoint(cx, cy);
+		if( nsegments == 0 )
+			nsegments = Math.ceil(ray * angleLength / 4);
+		if( nsegments < 3 ) nsegments = 3;
+		var angle = angleLength / (nsegments - 1);
+		for( i in 0...nsegments ) {
+			var a = i * angle + angleStart;
+			addPoint(cx + Math.cos(a) * ray, cy + Math.sin(a) * ray);
+		}
+		addPoint(cx, cy);
+		flush();
+	}
+
 	public function addHole() {
 		if( pts.length > 0 ) {
 			prev.push(pts);
@@ -305,7 +356,7 @@ class Graphics extends Drawable {
 	}
 
 	public inline function addPoint( x : Float, y : Float ) {
-		addPointFull(x, y, curR, curG, curB, curA);
+		addPointFull(x, y, curR, curG, curB, curA, x * ma + y * mc + mx, x * mb + y * md + my);
 	}
 
 	public function addPointFull( x : Float, y : Float, r : Float, g : Float, b : Float, a : Float, u : Float = 0., v : Float = 0. ) {

+ 30 - 8
samples/draw/Draw.hx

@@ -1,5 +1,5 @@
 class Draw extends hxd.App {
-	
+
 	override function init() {
 		var g = new h2d.Graphics(s2d);
 		g.beginFill(0xFF0000);
@@ -10,9 +10,31 @@ class Draw extends hxd.App {
 		g.lineStyle(1, 0xFF00FF);
 		g.drawCircle(100, 100, 30);
 		g.endFill();
-		
+
+		// check pie + draw texture
+
+		var g = new h2d.Graphics(s2d);
+		var bmp = new hxd.BitmapData(64, 64);
+		for( x in 0...64 )
+			for( y in 0...64 )
+				bmp.setPixel(x, y, 0xFF000000 | (x * 4) | ((y * 4) << 8));
+		var tile = h2d.Tile.fromBitmap(bmp);
+		bmp.dispose();
+		g.lineStyle();
+
+		g.beginTileFill(-32,-32,tile);
+		g.drawPie(0, 0, 32, Math.PI / 3, Math.PI);
+		g.endFill();
+
+		g.beginTileFill(100, -64, 2, 2, tile);
+		g.drawRect(100, -64, 128, 128);
+		g.endFill();
+
+		g.x = 200;
+		g.y = 100;
+
 		// check the size and alignment of scaled bitmaps
-		
+
 		var bmp = new hxd.BitmapData(256, 256);
 		bmp.clear(0xFFFF00FF);
 		bmp.fill(19, 21, 13, 15, 0xFF202020);
@@ -21,23 +43,23 @@ class Draw extends hxd.App {
 		bmp.fill(19+13, 21, 1, 15, 0xFF0000FF);
 		bmp.fill(19, 21 + 15, 13, 1, 0xFF00FFFF);
 		var tile = h2d.Tile.fromBitmap(bmp);
-		
+
 		bmp.dispose();
-		
+
 		var b = new h2d.Bitmap(tile.sub(19, 21, 13, 15), s2d);
 		b.x = 200;
 		b.y = 200;
 		b.scale(32);
-		
+
 		var b = new h2d.Bitmap(tile.sub(18, 20, 15, 17), s2d);
 		b.x = 300;
 		b.y = 300;
 		b.scale(13);
 
 	}
-	
+
 	static function main() {
 		new Draw();
 	}
-	
+
 }