Tile.hx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. package h2d;
  2. /**
  3. A core 2D rendering component representing a region of an underlying `h3d.mat.Texture`.
  4. Tiles cannot be created directly, and instances are created with the following methods:
  5. * Via the Resource Management system: `hxd.res.Image.toTile`.
  6. * From pre-existing Texture: `Tile.fromTexture`.
  7. * From pre-existing `BitmapData` or `Pixels`: `Tile.fromBitmap` and `Tile.fromPixels` (as well as `Tile.autoCut`).
  8. * From solid color: `Tile.fromColor`.
  9. * From previously existing Tile instance via various methods, such as `Tile.sub`.
  10. **/
  11. @:allow(h2d)
  12. class Tile {
  13. var innerTex : h3d.mat.Texture;
  14. var u : Float;
  15. var v : Float;
  16. var u2 : Float;
  17. var v2 : Float;
  18. /**
  19. Visual offset of the Tile along the X axis during rendering.
  20. **/
  21. public var dx : Float;
  22. /**
  23. Visual offset of the Tile along the Y axis during rendering.
  24. **/
  25. public var dy : Float;
  26. /**
  27. Horizontal position of the Tile on the Texture.
  28. Cannot be modified directly, use `Tile.setPosition` instead.
  29. **/
  30. public var x(default,null) : Float;
  31. /**
  32. Vertical position of the Tile on the Texture.
  33. Cannot be modified directly, use `Tile.setPosition` instead.
  34. **/
  35. public var y(default,null) : Float;
  36. /**
  37. Width of the Tile.
  38. Not guaranteed to represent real width of the Tile on texture. (see `Tile.scaleToSize`)
  39. Cannot be modified directly, use `Tile.setSize` instead.
  40. **/
  41. public var width(default,null) : Float;
  42. /**
  43. Height of the Tile.
  44. Not guaranteed to represent real height of the Tile on texture. (see `Tile.scaleToSize`)
  45. Cannot be modified directly, use `Tile.setSize` instead.
  46. **/
  47. public var height(default,null) : Float;
  48. /**
  49. The flip state of the Tile.
  50. @see `Tile.flipX`
  51. **/
  52. public var xFlip(get,set) : Bool;
  53. /**
  54. The flip state of the Tile.
  55. @see `Tile.flipY`
  56. **/
  57. public var yFlip(get,set) : Bool;
  58. /**
  59. An integer horizontal position of the Tile on the Texture.
  60. Alias to `Math.floor(tile.x)`.
  61. **/
  62. public var ix(get,never) : Int;
  63. inline function get_ix() return Math.floor(x);
  64. /**
  65. An integer vertical position of the Tile on the Texture.
  66. Alias to `Math.floor(tile.y)`.
  67. **/
  68. public var iy(get,never) : Int;
  69. inline function get_iy() return Math.floor(y);
  70. /**
  71. An integer width of the Tile.
  72. Alias to `Math.ceil(tile.width + tile.x) - tile.ix`.
  73. **/
  74. public var iwidth(get,never) : Int;
  75. inline function get_iwidth() return Math.ceil(width + x) - ix;
  76. /**
  77. An integer height of the Tile.
  78. Alias to `Math.ceil(tile.height + tile.y) - tile.iy`.
  79. **/
  80. public var iheight(get,never) : Int;
  81. inline function get_iheight() return Math.ceil(height + y) - iy;
  82. function new(tex : h3d.mat.Texture, x : Float, y : Float, w : Float, h : Float, dx : Float=0, dy : Float=0) {
  83. this.innerTex = tex;
  84. this.x = x;
  85. this.y = y;
  86. this.width = w;
  87. this.height = h;
  88. this.dx = dx;
  89. this.dy = dy;
  90. if( tex != null ) setTexture(tex);
  91. }
  92. /**
  93. Returns an underlying Texture instance.
  94. **/
  95. public inline function getTexture():h3d.mat.Texture {
  96. return innerTex;
  97. }
  98. /**
  99. Checks if Tile or underlying Texture were disposed.
  100. **/
  101. public function isDisposed() {
  102. return innerTex == null || innerTex.isDisposed();
  103. }
  104. function setTexture(tex : h3d.mat.Texture) {
  105. this.innerTex = tex;
  106. if( tex != null ) {
  107. this.u = x / tex.width;
  108. this.v = y / tex.height;
  109. this.u2 = (x + width) / tex.width;
  110. this.v2 = (y + height) / tex.height;
  111. }
  112. }
  113. /**
  114. Changes this Tile underlying texture to one used in the specified Tile.
  115. If Tile was scaled, new uv will cover new width and height instead of the original unscaled one.
  116. @param t The Tile used as a source of the Texture instance.
  117. It's possible to switch texture by referring the Texture instance directly, by using access hacks:
  118. ```haxe
  119. @:privateAccess tile.setTexture(myTextureInstance);
  120. ```
  121. **/
  122. public inline function switchTexture( t : Tile ) {
  123. setTexture(t.innerTex);
  124. }
  125. /**
  126. Create a sub-region of this Tile with specified size and offset.
  127. @param x The offset on top of the current Tile offset along the X axis.
  128. @param y The offset on top of the current Tile offset along the Y axis.
  129. @param w The width of the new Tile region. Can exceed current tile size.
  130. @param h The height of the new Tile region. Can exceed the current tile size.
  131. @param dx An optional visual offset of the new Tile along the X axis.
  132. @param dy An optional visual offset of the new Tile along the Y axis.
  133. **/
  134. public function sub( x : Float, y : Float, w : Float, h : Float, dx = 0., dy = 0. ) : Tile {
  135. return new Tile(innerTex, this.x + x, this.y + y, w, h, dx, dy);
  136. }
  137. /**
  138. Returns a new Tile with shifting origin point (`dx` and `dy`) to the tile center.
  139. To modify this Tile origin point, use `Tile.setCenterRatio`.
  140. **/
  141. public function center():Tile {
  142. return sub(0, 0, width, height, -(width * .5), -(height * .5));
  143. }
  144. /**
  145. Sets `dx` / `dy` as origin point dictated by `px` / `py` with a default being center.
  146. **/
  147. public inline function setCenterRatio(?px:Float=0.5, ?py:Float=0.5) : Void {
  148. dx = -(px*width);
  149. dy = -(py*height);
  150. }
  151. /**
  152. Flips the Tile horizontally. Note that `dx` is flipped as well.
  153. **/
  154. public function flipX() : Void {
  155. var tmp = u; u = u2; u2 = tmp;
  156. dx = -dx - width;
  157. }
  158. /**
  159. Flips the Tile vertically. Note that `dy` is flipped as well.
  160. **/
  161. public function flipY() : Void {
  162. var tmp = v; v = v2; v2 = tmp;
  163. dy = -dy - height;
  164. }
  165. /**
  166. Set the Tile position in the texture to the specified coordinate.
  167. **/
  168. public function setPosition(x : Float, y : Float) : Void {
  169. this.x = x;
  170. this.y = y;
  171. var tex = innerTex;
  172. if( tex != null ) {
  173. u = x / tex.width;
  174. v = y / tex.height;
  175. u2 = (x + width) / tex.width;
  176. v2 = (y + height) / tex.height;
  177. }
  178. }
  179. /**
  180. Set the Tile size in the texture to the specified dimensions.
  181. **/
  182. public function setSize(w : Float, h : Float) : Void {
  183. this.width = w;
  184. this.height = h;
  185. var tex = innerTex;
  186. if( tex != null ) {
  187. u2 = (x + w) / tex.width;
  188. v2 = (y + h) / tex.height;
  189. }
  190. }
  191. /**
  192. Rescales the Tile to be of the set width and height, but without affecting the uv coordinates.
  193. Using this method allows to upscale/downscale Tiles, but creates a mismatch between the tile uv and width/height values.
  194. Due to that, using any methods that modify the uv value will cause the new uv to treat scaled width and height as true dimensions
  195. and can lead to unexpected results if not accounted for.
  196. **/
  197. public function scaleToSize( w : Float, h : Float ) : Void {
  198. this.width = w;
  199. this.height = h;
  200. }
  201. /**
  202. Scrolls the texture position by specified amount.
  203. **/
  204. public function scrollDiscrete( dx : Float, dy : Float ) : Void {
  205. var tex = innerTex;
  206. u += dx / tex.width;
  207. v -= dy / tex.height;
  208. u2 += dx / tex.width;
  209. v2 -= dy / tex.height;
  210. x = u * tex.width;
  211. y = v * tex.height;
  212. }
  213. /**
  214. Disposes of the Tile and its underlying Texture.
  215. Note that if Texture is used by other Tile instances, it will cause them to point at a disposed texture and can lead to errors.
  216. **/
  217. public function dispose() : Void {
  218. if( innerTex != null ) innerTex.dispose();
  219. innerTex = null;
  220. }
  221. /**
  222. Create a copy of this Tile instance.
  223. **/
  224. public function clone() : Tile {
  225. var t = new Tile(null, x, y, width, height, dx, dy);
  226. t.innerTex = innerTex;
  227. t.u = u;
  228. t.u2 = u2;
  229. t.v = v;
  230. t.v2 = v2;
  231. return t;
  232. }
  233. function get_xFlip() return u2 < u;
  234. function get_yFlip() return v2 < v;
  235. function set_xFlip(v) {
  236. if( v != xFlip ) flipX();
  237. return v;
  238. }
  239. function set_yFlip(v) {
  240. if( v != yFlip ) flipY();
  241. return v;
  242. }
  243. /**
  244. Split the Tile horizontally or vertically by the number of given frames.
  245. @param frames The amount of frames this Tile has to be split into.
  246. @param vertical Causes split to be done vertically instead of horizontal split.
  247. @param subpixel When enabled, retains the floating-point remainder if calculated frame size is not integral.
  248. **/
  249. public function split( frames : Int = 0, vertical = false, subpixel = false ) : Array<Tile> {
  250. var tl = [];
  251. if( vertical ) {
  252. if( frames == 0 )
  253. frames = Std.int(height / width);
  254. var stride = subpixel ? height / frames : Std.int(height / frames);
  255. for( i in 0...frames )
  256. tl.push(sub(0, i * stride, width, stride));
  257. } else {
  258. if( frames == 0 )
  259. frames = Std.int(width / height);
  260. var stride = subpixel ? width / frames : Std.int(width / frames);
  261. for( i in 0...frames )
  262. tl.push(sub(i * stride, 0, stride, height));
  263. }
  264. return tl;
  265. }
  266. /**
  267. Split the tile into a list of tiles of Size x Size pixels.
  268. @param size The width and height of the new Tiles.
  269. @param dx Optional visual offset of the new Tiles along the X axis.
  270. @param dy Optional visual offset of the new Tiles along the Y axis.
  271. @returns A one-dimensional array ordered in Y/X.
  272. **/
  273. public function gridFlatten( size : Float, dx = 0., dy = 0. ) : Array<Tile> {
  274. return [for( y in 0...Std.int(height / size) ) for( x in 0...Std.int(width / size) ) sub(x * size, y * size, size, size, dx, dy)];
  275. }
  276. /**
  277. Split the tile into a list of tiles of Size x Size pixels.
  278. @param size The width and height of the new Tiles.
  279. @param dx Optional visual offset of the new Tiles along the X axis.
  280. @param dy Optional visual offset of the new Tiles along the Y axis.
  281. @returns A two-dimensional array ordered in `[X][Y]`.
  282. **/
  283. public function grid( size : Float, dx = 0., dy = 0. ) : Array<Array<Tile>> {
  284. return [for( x in 0...Std.int(width / size) ) [for( y in 0...Std.int(height / size) ) sub(x * size, y * size, size, size, dx, dy)]];
  285. }
  286. @:dox(hide)
  287. public function toString() : String {
  288. return "Tile(" + x + "," + y + "," + width + "x" + height + (dx != 0 || dy != 0 ? "," + dx + ":" + dy:"") + ")";
  289. }
  290. function upload( bmp:hxd.BitmapData ) : Void {
  291. var w = innerTex.width;
  292. var h = innerTex.height;
  293. #if flash
  294. if( w != bmp.width || h != bmp.height ) {
  295. var bmp2 = new flash.display.BitmapData(w, h, true, 0);
  296. var p0 = new flash.geom.Point(0, 0);
  297. var bmp = bmp.toNative();
  298. bmp2.copyPixels(bmp, bmp.rect, p0, bmp, p0, true);
  299. innerTex.uploadBitmap(hxd.BitmapData.fromNative(bmp2));
  300. bmp2.dispose();
  301. } else
  302. #end
  303. innerTex.uploadBitmap(bmp);
  304. }
  305. /**
  306. Create a solid color Tile with specified width, height, color and alpha.
  307. @param color The RGB color of the Tile.
  308. @param width The width of the Tile in pixels.
  309. @param height The height of the Tile in pixels.
  310. @param alpha The transparency of the Tile.
  311. **/
  312. public static function fromColor( color : Int, ?width = 1, ?height = 1, ?alpha = 1. ) : Tile {
  313. var t = new Tile(h3d.mat.Texture.fromColor(color,alpha),0,0,1,1);
  314. // scale to size
  315. t.width = width;
  316. t.height = height;
  317. return t;
  318. }
  319. /**
  320. Creates a new Texture from provided BitmapData and returns a Tile representing it.
  321. **/
  322. public static function fromBitmap( bmp : hxd.BitmapData ) : Tile {
  323. var tex = h3d.mat.Texture.fromBitmap(bmp);
  324. return new Tile(tex, 0, 0, bmp.width, bmp.height);
  325. }
  326. /**
  327. Creates a new POT Texture from bmp and cuts it in a grid of tiles with maximum size of `[width, height]`.
  328. Algorithm will use bottom-right pixels as background color and cut out empty space from each Tile and
  329. will modify the origin point to retain the Tile position.
  330. Each row scan continues as long as there are no empty tiles.
  331. @param bmp The BitmapData which will be split into tiles.
  332. @param width The width of a single grid entry.
  333. @param height An optional height of a single grid entry. Width will be used if not provided.
  334. **/
  335. public static function autoCut( bmp : hxd.BitmapData, width : Int, ?height : Int ) : { main: Tile, tiles: Array<Array<Tile>> } {
  336. #if js
  337. bmp.lock();
  338. #end
  339. if( height == null ) height = width;
  340. var colorBG = bmp.getPixel(bmp.width - 1, bmp.height - 1);
  341. var tl = new Array();
  342. var w = 1, h = 1;
  343. while( w < bmp.width )
  344. w <<= 1;
  345. while( h < bmp.height )
  346. h <<= 1;
  347. var tex = new h3d.mat.Texture(w, h);
  348. for( y in 0...Std.int(bmp.height / height) ) {
  349. var a = [];
  350. tl[y] = a;
  351. for( x in 0...Std.int(bmp.width / width) ) {
  352. var sz = isEmpty(bmp, x * width, y * height, width, height, colorBG);
  353. if( sz == null )
  354. break;
  355. a.push(new Tile(tex,x*width+sz.dx, y*height+sz.dy, sz.w, sz.h, sz.dx, sz.dy));
  356. }
  357. }
  358. #if js
  359. bmp.unlock();
  360. #end
  361. var main = new Tile(tex, 0, 0, bmp.width, bmp.height);
  362. main.upload(bmp);
  363. return { main : main, tiles : tl };
  364. }
  365. /**
  366. Create new Tile from provided Texture instance.
  367. **/
  368. public static function fromTexture( t : h3d.mat.Texture ) : Tile {
  369. return new Tile(t, 0, 0, t.width, t.height);
  370. }
  371. /**
  372. Creates new POT Texture from Pixels and returns a Tile representing it.
  373. **/
  374. public static function fromPixels( pixels : hxd.Pixels ) : Tile {
  375. var pix2 = pixels.makeSquare(true);
  376. var t = h3d.mat.Texture.fromPixels(pix2);
  377. if( pix2 != pixels ) pix2.dispose();
  378. return new Tile(t, 0, 0, pixels.width, pixels.height);
  379. }
  380. static function isEmpty( b : hxd.BitmapData, px : Int, py : Int, width : Int, height : Int, bg : Int ) {
  381. var empty = true;
  382. var xmin = width, ymin = height, xmax = 0, ymax = 0;
  383. for( x in 0...width )
  384. for( y in 0...height ) {
  385. var color : Int = b.getPixel(x + px, y + py);
  386. if( color & 0xFF000000 == 0 ) {
  387. if( color != 0 ) b.setPixel(x + px, y + py, 0);
  388. continue;
  389. }
  390. if( color != bg ) {
  391. empty = false;
  392. if( x < xmin ) xmin = x;
  393. if( y < ymin ) ymin = y;
  394. if( x > xmax ) xmax = x;
  395. if( y > ymax ) ymax = y;
  396. }
  397. if( color == bg && color != 0 )
  398. b.setPixel(x + px, y + py, 0);
  399. }
  400. return empty ? null : { dx : xmin, dy : ymin, w : xmax - xmin + 1, h : ymax - ymin + 1 };
  401. }
  402. }