ScaleGrid.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package h2d;
  2. /**
  3. A simple 9-slice bitmap renderer.
  4. Enables rendering of the Tile as a stretchable surface with unscaled corners, stretched center and either stretched or tiled borders.
  5. Set `ScaleGrid.width` and `ScaleGrid.height` to resize the ScaleGrid.
  6. **/
  7. class ScaleGrid extends h2d.TileGroup {
  8. /**
  9. The width of the left border in pixels.
  10. **/
  11. public var borderLeft(default, set) : Int;
  12. /**
  13. The width of the right border in pixels.
  14. **/
  15. public var borderRight(default, set) : Int;
  16. /**
  17. The height of the top border in pixels.
  18. **/
  19. public var borderTop(default, set) : Int;
  20. /**
  21. The height of the bottom border in pixels.
  22. **/
  23. public var borderBottom(default, set) : Int;
  24. /**
  25. Set the width of left and right borders altogether.
  26. **/
  27. public var borderWidth(never,set) : Int;
  28. /**
  29. Set the height of top and bottom borders altogether.
  30. **/
  31. public var borderHeight(never,set) : Int;
  32. /**
  33. The width of the bitmap. Setting to values less than `borderLeft + borderRight` leads to undefined results.
  34. **/
  35. public var width(default,set) : Float;
  36. /**
  37. The height of the bitmap. Setting to values less than `borderTop + borderBottom` leads to undefined results.
  38. **/
  39. public var height(default,set) : Float;
  40. /**
  41. When enabled, borders will be tiled along the edges instead of stretching to match the desired dimensions.
  42. Center tile is always stretched.
  43. **/
  44. public var tileBorders(default, set) : Bool;
  45. public var tileCenter(default, set) : Bool;
  46. /**
  47. When enabled, the borders will ignore the final scale of the `h2d.ScaleGrid` to be rendered pixel perfect.
  48. This does not change the values of `borderLeft`, `borderRight`, `borderTop` or `borderBottom`.
  49. Center tile is always stretched.
  50. **/
  51. public var ignoreScale(default, set) : Bool;
  52. /**
  53. Scale factor applied to borders only. If combined with `ignoreScale`, becomes an absolute scale independent from the scene scale.
  54. **/
  55. public var borderScale(default, set) : Float = 1.0;
  56. var contentTile : h2d.Tile;
  57. var currentScaleX = 1.;
  58. var currentScaleY = 1.;
  59. /**
  60. Create a new ScaleGrid with specified parameters.
  61. @param tile The source tile which will be sliced.
  62. @param borderW The width of the left and right borders in pixels.
  63. @param borderH The height of the top and bottom borders in pixels.
  64. @param parent An optional parent `h2d.Object` instance to which ScaleGrid adds itself if set.
  65. **/
  66. public function new( tile, borderL, borderT, ?borderR, ?borderB, ?parent ) {
  67. super(tile,parent);
  68. borderLeft = borderL;
  69. borderRight = (borderR != null)? borderR : borderL;
  70. borderTop = borderT;
  71. borderBottom = (borderB != null)? borderB : borderT;
  72. width = tile.width;
  73. height = tile.height;
  74. }
  75. function set_tileBorders(b) {
  76. if( tileBorders == b ) return b;
  77. this.tileBorders = b;
  78. clear();
  79. return b;
  80. }
  81. function set_tileCenter(b) {
  82. if( tileCenter == b ) return b;
  83. this.tileCenter = b;
  84. clear();
  85. return b;
  86. }
  87. function set_ignoreScale(b) {
  88. if(ignoreScale == b) return b;
  89. this.ignoreScale = b;
  90. clear();
  91. return b;
  92. }
  93. function set_borderScale(s) {
  94. if(borderScale == s) return s;
  95. this.borderScale = s;
  96. clear();
  97. return s;
  98. }
  99. function set_width(w) {
  100. if( width == w ) return w;
  101. this.width = w;
  102. clear();
  103. return w;
  104. }
  105. function set_height(h) {
  106. if( height == h ) return h;
  107. this.height = h;
  108. clear();
  109. return h;
  110. }
  111. function set_borderWidth(w) {
  112. if( borderLeft == w && borderRight == w ) return w;
  113. @:bypassAccessor borderLeft = w;
  114. @:bypassAccessor borderRight = w;
  115. clear();
  116. return w;
  117. }
  118. function set_borderHeight(h) {
  119. if( borderTop == h && borderBottom == h ) return h;
  120. @:bypassAccessor borderTop = h;
  121. @:bypassAccessor borderBottom = h;
  122. clear();
  123. return h;
  124. }
  125. function set_borderTop(top) {
  126. if( borderTop == top ) return top;
  127. this.borderTop = top;
  128. clear();
  129. return top;
  130. }
  131. function set_borderBottom(bot) {
  132. if( borderBottom == bot ) return bot;
  133. this.borderBottom = bot;
  134. clear();
  135. return bot;
  136. }
  137. function set_borderLeft(left) {
  138. if( borderLeft == left ) return left;
  139. this.borderLeft = left;
  140. clear();
  141. return left;
  142. }
  143. function set_borderRight(right) {
  144. if( borderRight == right ) return right;
  145. this.borderRight = right;
  146. clear();
  147. return right;
  148. }
  149. override function getBoundsRec(relativeTo, out, forSize) {
  150. checkUpdate();
  151. super.getBoundsRec(relativeTo, out, forSize);
  152. }
  153. function checkUpdate() {
  154. var needUpdate = false;
  155. if(ignoreScale){
  156. var s = getAbsPos().getScale();
  157. if(currentScaleX != s.x || currentScaleY != s.y){
  158. needUpdate = true;
  159. currentScaleX = s.x;
  160. currentScaleY = s.y;
  161. }
  162. }
  163. if( content.isEmpty() || tile != contentTile ) {
  164. contentTile = tile;
  165. needUpdate = true;
  166. }
  167. if(needUpdate){
  168. clear();
  169. updateContent();
  170. }
  171. }
  172. function updateContent() {
  173. var bt = borderTop, bb = borderBottom, bl = borderLeft, br = borderRight;
  174. var unscaledBl : Float = bl * borderScale,
  175. unscaledBr : Float = br * borderScale,
  176. unscaledBt : Float = bt * borderScale,
  177. unscaledBb : Float = bb * borderScale;
  178. var invScaleX = 1.;
  179. var invScaleY = 1.;
  180. if(ignoreScale){
  181. var s = getAbsPos().getScale();
  182. if(s.x == 0. || s.y == 0.)
  183. return;
  184. invScaleX /= s.x;
  185. invScaleY /= s.y;
  186. unscaledBl *= invScaleX;
  187. unscaledBr *= invScaleX;
  188. unscaledBt *= invScaleY;
  189. unscaledBb *= invScaleY;
  190. }
  191. // 4 corners
  192. var t = tile.sub(0, 0, bl, bt);
  193. t.scaleToSize(unscaledBl, unscaledBt);
  194. content.addColor(0, 0, curColor, t);
  195. t = tile.sub(tile.width - br, 0, br, bt);
  196. t.scaleToSize(unscaledBr, unscaledBt);
  197. content.addColor(width - unscaledBr, 0, curColor, t);
  198. t = tile.sub(0, tile.height - bb, bl, bb);
  199. t.scaleToSize(unscaledBl, unscaledBb);
  200. content.addColor(0, height-unscaledBb, curColor, t);
  201. t = tile.sub(tile.width - br, tile.height - bb, br, bb);
  202. t.scaleToSize(unscaledBr, unscaledBb);
  203. content.addColor(width - unscaledBr, height - unscaledBb, curColor, t);
  204. var innerTileWidth = tile.width - (br + bl);
  205. var innerTileHeight = tile.height - (bb + bt);
  206. var innerWidth = width - (unscaledBl + unscaledBr);
  207. var innerHeight = height - (unscaledBt + unscaledBb);
  208. if( !tileBorders ) {
  209. // top
  210. var t = tile.sub(bl, 0, innerTileWidth, bt);
  211. t.scaleToSize(innerWidth, unscaledBt);
  212. content.addColor(unscaledBl, 0, curColor, t);
  213. // bottom
  214. var t = tile.sub(bl, tile.height - bb, innerTileWidth, bb);
  215. t.scaleToSize(innerWidth, unscaledBb);
  216. content.addColor(unscaledBl, innerHeight + unscaledBt, curColor, t);
  217. // left
  218. var t = tile.sub(0, bt, bl, innerTileHeight);
  219. t.scaleToSize(unscaledBl, innerHeight);
  220. content.addColor(0, unscaledBt, curColor, t);
  221. // right
  222. var t = tile.sub(tile.width - br, bt, br, innerTileHeight);
  223. t.scaleToSize(unscaledBr, innerHeight);
  224. content.addColor(innerWidth + unscaledBl, unscaledBt, curColor, t);
  225. } else {
  226. var unscaledInnerTileWidth = innerTileWidth * invScaleX;
  227. var unscaledInnerTileHeight = innerTileHeight * invScaleY;
  228. var rw = Std.int(innerWidth / unscaledInnerTileWidth);
  229. for( x in 0...rw ) {
  230. // top
  231. var t = tile.sub(bl, 0, innerTileWidth, bt);
  232. t.scaleToSize(unscaledInnerTileWidth, unscaledBt);
  233. content.addColor(unscaledBl + x * unscaledInnerTileWidth, 0, curColor, t);
  234. // bottom
  235. t = tile.sub(bl, tile.height - bb, innerTileWidth, bb);
  236. t.scaleToSize(unscaledInnerTileWidth, unscaledBb);
  237. content.addColor(unscaledBl + x * unscaledInnerTileWidth, height - unscaledBb, curColor, t);
  238. }
  239. var dx = innerWidth - rw * unscaledInnerTileWidth;
  240. if( dx > 0 ) {
  241. // top
  242. var t = tile.sub(bl, 0, dx / invScaleX, bt);
  243. t.scaleToSize(dx, unscaledBt);
  244. content.addColor(unscaledBl + rw * unscaledInnerTileWidth, 0, curColor, t);
  245. // bottom
  246. t = tile.sub(bl, tile.height - bb, dx / invScaleX, bb);
  247. t.scaleToSize(dx, unscaledBb);
  248. content.addColor(unscaledBl + rw * unscaledInnerTileWidth, height - unscaledBb, curColor, t);
  249. }
  250. var rh = Std.int(innerHeight / unscaledInnerTileHeight);
  251. for( y in 0...rh ) {
  252. // left
  253. var t = tile.sub(0, bt, bl, innerTileHeight);
  254. t.scaleToSize(unscaledBl, unscaledInnerTileHeight);
  255. content.addColor(0, unscaledBt + y * unscaledInnerTileHeight, curColor, t);
  256. // right
  257. t = tile.sub(tile.width - br, bt, br, innerTileHeight);
  258. t.scaleToSize(unscaledBr, unscaledInnerTileHeight);
  259. content.addColor(width - unscaledBr, unscaledBt + y * unscaledInnerTileHeight, curColor, t);
  260. }
  261. var dy = innerHeight - rh * unscaledInnerTileHeight;
  262. if( dy > 0 ) {
  263. // left
  264. var t = tile.sub(0, bt, bl, dy / invScaleY);
  265. t.scaleToSize(unscaledBl, dy);
  266. content.addColor(0, bt + rh * unscaledInnerTileHeight, curColor, t);
  267. // right
  268. t = tile.sub(tile.width - br, bt, br, dy / invScaleY);
  269. t.scaleToSize(unscaledBr, dy);
  270. content.addColor(width - unscaledBr, unscaledBt + rh * unscaledInnerTileHeight, curColor, t);
  271. }
  272. }
  273. if( !tileCenter ) {
  274. var t = tile.sub(bl, bt, innerTileWidth, innerTileHeight);
  275. t.scaleToSize(width - (unscaledBr + unscaledBl), height - (unscaledBt + unscaledBb));
  276. content.addColor(unscaledBl, unscaledBt, curColor, t);
  277. }
  278. else {
  279. var unscaledInnerTileWidth = innerTileWidth * invScaleX;
  280. var unscaledInnerTileHeight = innerTileHeight * invScaleY;
  281. var rw = Std.int( ( width - (unscaledBr + unscaledBl) ) / unscaledInnerTileWidth );
  282. var rh = Std.int(innerHeight / unscaledInnerTileHeight);
  283. for( y in 0...rh )
  284. for( x in 0...rw ) {
  285. var t = tile.sub(bl, bt, unscaledInnerTileWidth, unscaledInnerTileHeight);
  286. content.addColor(unscaledBl + x * unscaledInnerTileWidth, unscaledBt + y * unscaledInnerTileHeight, curColor, t);
  287. }
  288. var dx = innerWidth - rw * unscaledInnerTileWidth;
  289. if( dx > 0 ) {
  290. for( y in 0...rh ) {
  291. var t = tile.sub(bl, bt, dx, unscaledInnerTileHeight);
  292. content.addColor(unscaledBl + rw * unscaledInnerTileWidth, unscaledBt + y * unscaledInnerTileHeight, curColor, t);
  293. }
  294. }
  295. var dy = innerHeight - rh * unscaledInnerTileHeight;
  296. if( dy > 0 ) {
  297. for( x in 0...rw ) {
  298. var t = tile.sub(bl, bt, unscaledInnerTileWidth, dy);
  299. content.addColor(unscaledBl + x * unscaledInnerTileWidth, unscaledBt + rh * unscaledInnerTileHeight, curColor, t);
  300. }
  301. }
  302. if( dx > 0 && dy > 0 ) {
  303. var t = tile.sub(bl, bt, dx, dy);
  304. content.addColor(unscaledBl + rw * unscaledInnerTileWidth, unscaledBt + rh * unscaledInnerTileHeight, curColor, t);
  305. }
  306. }
  307. }
  308. override function sync( ctx : RenderContext ) {
  309. checkUpdate();
  310. super.sync(ctx);
  311. }
  312. }