ScaleGrid.hx 11 KB

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