Jelajahi Sumber

added h2d.Bitmap.width/height to force resize of tile

Nicolas Cannasse 5 tahun lalu
induk
melakukan
ae2bd69ca3
2 mengubah file dengan 62 tambahan dan 2 penghapusan
  1. 52 2
      h2d/Bitmap.hx
  2. 10 0
      h2d/domkit/BaseComponents.hx

+ 52 - 2
h2d/Bitmap.hx

@@ -9,7 +9,17 @@ class Bitmap extends Drawable {
 		The tile to display see `h2d.Tile` documentation for details.
 		If the tile is null, a pink 5x5 bitmap will be displayed instead. Use remove() or visible=false to hide a h2d.Bitmap
 	**/
-	public var tile : Tile;
+	public var tile(default,set) : Tile;
+
+	/**
+	 * 	If set, rescale the tile to match the given width, keeping the aspect ratio unless height is also set.
+	**/
+	public var width(default,set) : Null<Float>;
+
+	/**
+	 * 	If set, rescale the tile to match the given height, keeping the aspect ratio unless width is also set.
+	**/
+	public var height(default,set) : Null<Float>;
 
 	/**
 		Create a Bitmap with specified tile and parent object.
@@ -27,11 +37,51 @@ class Bitmap extends Drawable {
 
 	override function getBoundsRec( relativeTo : Object, out : h2d.col.Bounds, forSize : Bool ) {
 		super.getBoundsRec(relativeTo, out, forSize);
-		if( tile != null ) addBounds(relativeTo, out, tile.dx, tile.dy, tile.width, tile.height);
+		if( tile != null ) {
+			if( width == null && height == null )
+				addBounds(relativeTo, out, tile.dx, tile.dy, tile.width, tile.height);
+			else
+				addBounds(relativeTo, out, tile.dx, tile.dy, width != null ? width : tile.width * height / tile.height, height != null ? height : tile.height * width / tile.width);
+		}
+	}
+
+	function set_width(w) {
+		if( width == w ) return w;
+		width = w;
+		onContentChanged();
+		return w;
+	}
+
+	function set_height(h) {
+		if( height == h ) return h;
+		height = h;
+		onContentChanged();
+		return h;
+	}
+
+	function set_tile(t) {
+		if( tile == t ) return t;
+		tile = t;
+		onContentChanged();
+		return t;
 	}
 
 	override function draw( ctx : RenderContext ) {
+		if( width == null && height == null ) {
+			emitTile(ctx,tile);
+			return;
+		}
+		var ow = tile.width;
+		var oh = tile.height;
+		@:privateAccess {
+			tile.width = width != null ? width : ow * height / oh;
+			tile.height = height != null ? height : oh * width / ow;
+		}
 		emitTile(ctx,tile);
+		@:privateAccess {
+			tile.width = ow;
+			tile.height = oh;
+		}
 	}
 
 }

+ 10 - 0
h2d/domkit/BaseComponents.hx

@@ -424,6 +424,8 @@ class MaskComp extends ObjectComp implements domkit.Component.ComponentDecl<h2d.
 class BitmapComp extends DrawableComp implements domkit.Component.ComponentDecl<h2d.Bitmap> {
 
 	@:p(tile) var src : h2d.Tile;
+	@:p(auto) var width : Null<Float>;
+	@:p(auto) var height : Null<Float>;
 
 	static function create( parent : h2d.Object ) {
 		return new h2d.Bitmap(h2d.Tile.fromColor(0xFF00FF,32,32,0.9),parent);
@@ -433,6 +435,14 @@ class BitmapComp extends DrawableComp implements domkit.Component.ComponentDecl<
 		o.tile = t == null ? h2d.Tile.fromColor(0xFF00FF,32,32,0.9) : t;
 	}
 
+	static function set_width( o : h2d.Bitmap, v : Null<Float> ) {
+		o.width = v;
+	}
+
+	static function set_height( o : h2d.Bitmap, v : Null<Float> ) {
+		o.height = v;
+	}
+
 }
 
 @:uiComp("text")