ScaleGrid.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package h2d;
  2. class ScaleGrid extends h2d.TileGroup {
  3. public var borderWidth : Int;
  4. public var borderHeight : Int;
  5. public var width(default,set) : Int;
  6. public var height(default,set) : Int;
  7. public var tileBorders(default,set) : Bool;
  8. public function new( tile, borderW, borderH, ?parent ) {
  9. super(tile,parent);
  10. borderWidth = borderW;
  11. borderHeight = borderH;
  12. width = tile.width;
  13. height = tile.height;
  14. }
  15. function set_tileBorders(b) {
  16. this.tileBorders = b;
  17. reset();
  18. return b;
  19. }
  20. function set_width(w) {
  21. this.width = w;
  22. reset();
  23. return w;
  24. }
  25. function set_height(h) {
  26. this.height = h;
  27. reset();
  28. return h;
  29. }
  30. override function draw( ctx : RenderContext ) {
  31. if( content.isEmpty() ) {
  32. var bw = borderWidth, bh = borderHeight;
  33. // 4 corners
  34. content.addColor(0, 0, curColor, tile.sub(0, 0, bw, bh));
  35. content.addColor(width - bw, 0, curColor, tile.sub(tile.width - bw, 0, bw, bh));
  36. content.addColor(0, height-bh, curColor, tile.sub(0, tile.height - bh, bw, bh));
  37. content.addColor(width - bw, height - bh, curColor, tile.sub(tile.width - bw, tile.height - bh, bw, bh));
  38. var sizeX = tile.width - bw * 2;
  39. var sizeY = tile.height - bh * 2;
  40. if( !tileBorders ) {
  41. var w = width - bw * 2;
  42. var h = height - bh * 2;
  43. var t = tile.sub(bw, 0, sizeX, bh);
  44. t.scaleToSize(w, bh);
  45. content.addColor(bw, 0, curColor, t);
  46. var t = tile.sub(bw, tile.height - bh, sizeX, bh);
  47. t.scaleToSize(w, bh);
  48. content.addColor(bw, h + bh, curColor, t);
  49. var t = tile.sub(0, bh, bw, sizeY);
  50. t.scaleToSize(bw, h);
  51. content.addColor(0, bh, curColor, t);
  52. var t = tile.sub(tile.width - bw, bh, bw, sizeY);
  53. t.scaleToSize(bw, h);
  54. content.addColor(w + bw, bh, curColor, t);
  55. } else {
  56. var rw = Std.int((width - bw * 2) / sizeX);
  57. for( x in 0...rw ) {
  58. content.addColor(bw + x * sizeX, 0, curColor, tile.sub(bw, 0, sizeX, bh));
  59. content.addColor(bw + x * sizeX, height - bh, curColor, tile.sub(bw, tile.height - bh, sizeX, bh));
  60. }
  61. var dx = width - bw * 2 - rw * sizeX;
  62. if( dx > 0 ) {
  63. content.addColor(bw + rw * sizeX, 0, curColor, tile.sub(bw, 0, dx, bh));
  64. content.addColor(bw + rw * sizeX, height - bh, curColor, tile.sub(bw, tile.height - bh, dx, bh));
  65. }
  66. var rh = Std.int((height - bh * 2) / sizeY);
  67. for( y in 0...rh ) {
  68. content.addColor(0, bh + y * sizeY, curColor, tile.sub(0, bh, bw, sizeY));
  69. content.addColor(width - bw, bh + y * sizeY, curColor, tile.sub(tile.width - bw, bh, bw, sizeY));
  70. }
  71. var dy = height - bh * 2 - rh * sizeY;
  72. if( dy > 0 ) {
  73. content.addColor(0, bh + rh * sizeY, curColor, tile.sub(0, bh, bw, dy));
  74. content.addColor(width - bw, bh + rh * sizeY, curColor, tile.sub(tile.width - bw, bh, bw, dy));
  75. }
  76. }
  77. var t = tile.sub(bw, bh, sizeX, sizeY);
  78. t.scaleToSize(width - bw * 2,height - bh * 2);
  79. content.addColor(bw, bh, curColor, t);
  80. }
  81. super.draw(ctx);
  82. }
  83. }