ScaleGrid.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package h2d;
  2. class ScaleGrid extends h2d.TileGroup {
  3. public var borderWidth(default,set) : Int;
  4. public var borderHeight(default,set) : 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. clear();
  18. return b;
  19. }
  20. function set_width(w) {
  21. this.width = w;
  22. clear();
  23. return w;
  24. }
  25. function set_height(h) {
  26. this.height = h;
  27. clear();
  28. return h;
  29. }
  30. function set_borderWidth(w) {
  31. this.borderWidth = w;
  32. clear();
  33. return w;
  34. }
  35. function set_borderHeight(h) {
  36. this.borderHeight = h;
  37. clear();
  38. return h;
  39. }
  40. override function getBoundsRec(relativeTo, out, forSize) {
  41. if( content.isEmpty() ) updateContent();
  42. super.getBoundsRec(relativeTo, out, forSize);
  43. }
  44. function updateContent() {
  45. var bw = borderWidth, bh = borderHeight;
  46. // 4 corners
  47. content.addColor(0, 0, curColor, tile.sub(0, 0, bw, bh));
  48. content.addColor(width - bw, 0, curColor, tile.sub(tile.width - bw, 0, bw, bh));
  49. content.addColor(0, height-bh, curColor, tile.sub(0, tile.height - bh, bw, bh));
  50. content.addColor(width - bw, height - bh, curColor, tile.sub(tile.width - bw, tile.height - bh, bw, bh));
  51. var sizeX = tile.width - bw * 2;
  52. var sizeY = tile.height - bh * 2;
  53. if( !tileBorders ) {
  54. var w = width - bw * 2;
  55. var h = height - bh * 2;
  56. var t = tile.sub(bw, 0, sizeX, bh);
  57. t.scaleToSize(w, bh);
  58. content.addColor(bw, 0, curColor, t);
  59. var t = tile.sub(bw, tile.height - bh, sizeX, bh);
  60. t.scaleToSize(w, bh);
  61. content.addColor(bw, h + bh, curColor, t);
  62. var t = tile.sub(0, bh, bw, sizeY);
  63. t.scaleToSize(bw, h);
  64. content.addColor(0, bh, curColor, t);
  65. var t = tile.sub(tile.width - bw, bh, bw, sizeY);
  66. t.scaleToSize(bw, h);
  67. content.addColor(w + bw, bh, curColor, t);
  68. } else {
  69. var rw = Std.int((width - bw * 2) / sizeX);
  70. for( x in 0...rw ) {
  71. content.addColor(bw + x * sizeX, 0, curColor, tile.sub(bw, 0, sizeX, bh));
  72. content.addColor(bw + x * sizeX, height - bh, curColor, tile.sub(bw, tile.height - bh, sizeX, bh));
  73. }
  74. var dx = width - bw * 2 - rw * sizeX;
  75. if( dx > 0 ) {
  76. content.addColor(bw + rw * sizeX, 0, curColor, tile.sub(bw, 0, dx, bh));
  77. content.addColor(bw + rw * sizeX, height - bh, curColor, tile.sub(bw, tile.height - bh, dx, bh));
  78. }
  79. var rh = Std.int((height - bh * 2) / sizeY);
  80. for( y in 0...rh ) {
  81. content.addColor(0, bh + y * sizeY, curColor, tile.sub(0, bh, bw, sizeY));
  82. content.addColor(width - bw, bh + y * sizeY, curColor, tile.sub(tile.width - bw, bh, bw, sizeY));
  83. }
  84. var dy = height - bh * 2 - rh * sizeY;
  85. if( dy > 0 ) {
  86. content.addColor(0, bh + rh * sizeY, curColor, tile.sub(0, bh, bw, dy));
  87. content.addColor(width - bw, bh + rh * sizeY, curColor, tile.sub(tile.width - bw, bh, bw, dy));
  88. }
  89. }
  90. var t = tile.sub(bw, bh, sizeX, sizeY);
  91. t.scaleToSize(width - bw * 2,height - bh * 2);
  92. content.addColor(bw, bh, curColor, t);
  93. }
  94. override function sync( ctx : RenderContext ) {
  95. if( content.isEmpty() ) {
  96. content.dispose();
  97. updateContent();
  98. }
  99. super.sync(ctx);
  100. }
  101. }