Bitmap.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package h2d;
  2. /**
  3. Displays a single bitmap Tile on the screen.
  4. It is a most primitive Drawable and easiest to use, but vastly inferior to others in terms of performance when used for rendering of many tiles.
  5. When dealing with many images at once, it is recommended to use batched renderers, like `h2d.SpriteBatch` or `h2d.TileGroup`.
  6. **/
  7. class Bitmap extends Drawable {
  8. /**
  9. The tile to display. See `h2d.Tile` documentation for details.
  10. If the tile is null, a pink 5x5 bitmap will be displayed instead.
  11. **/
  12. public var tile(default,set) : Tile;
  13. /**
  14. If set, rescale the tile to match the given width, keeping the aspect ratio unless `height` is also set.
  15. Note that both `width` and `height` are `null` by default and in order to retrieve bitmap dimensions with
  16. scaling accurately, call `getSize` method or address `tile.width/height` to get unscaled dimensions.
  17. **/
  18. public var width(default,set) : Null<Float>;
  19. /**
  20. If set, rescale the tile to match the given height, keeping the aspect ratio unless `width` is also set.
  21. Note that both `width` and `height` are `null` by default and in order to retrieve bitmap dimensions with
  22. scaling accurately, call `getSize` method or address `tile.width/height` to get unscaled dimensions.
  23. **/
  24. public var height(default,set) : Null<Float>;
  25. /**
  26. Create a Bitmap with specified tile and parent object.
  27. @param tile A Tile that should be rendered by this Bitmap.
  28. @param parent An optional parent `h2d.Object` instance to which Bitmap adds itself if set.
  29. **/
  30. public function new( ?tile : Tile, ?parent : h2d.Object ) {
  31. super(parent);
  32. this.tile = tile;
  33. }
  34. #if flash
  35. override function set_tileWrap(b) {
  36. if( b && tile != null && tile.getTexture().flags.has(IsNPOT) )
  37. throw "Cannot set tileWrap on a non power-of-two texture";
  38. return tileWrap = b;
  39. }
  40. #end
  41. override function getBoundsRec( relativeTo : Object, out : h2d.col.Bounds, forSize : Bool ) {
  42. super.getBoundsRec(relativeTo, out, forSize);
  43. if( tile != null ) {
  44. if( width == null && height == null )
  45. addBounds(relativeTo, out, tile.dx, tile.dy, tile.width, tile.height);
  46. else
  47. addBounds(relativeTo, out, tile.dx, tile.dy, width != null ? width : tile.width * height / tile.height, height != null ? height : tile.height * width / tile.width);
  48. }
  49. }
  50. function set_width(w) {
  51. if( width == w ) return w;
  52. width = w;
  53. onContentChanged();
  54. return w;
  55. }
  56. function set_height(h) {
  57. if( height == h ) return h;
  58. height = h;
  59. onContentChanged();
  60. return h;
  61. }
  62. function set_tile(t) {
  63. if( tile == t ) return t;
  64. tile = t;
  65. onContentChanged();
  66. return t;
  67. }
  68. override function draw( ctx : RenderContext ) {
  69. if( width == null && height == null ) {
  70. emitTile(ctx,tile);
  71. return;
  72. }
  73. if( tile == null ) tile = h2d.Tile.fromColor(0xFF00FF);
  74. var ow = tile.width;
  75. var oh = tile.height;
  76. @:privateAccess {
  77. tile.width = width != null ? width : ow * height / oh;
  78. tile.height = height != null ? height : oh * width / ow;
  79. }
  80. emitTile(ctx,tile);
  81. @:privateAccess {
  82. tile.width = ow;
  83. tile.height = oh;
  84. }
  85. }
  86. }