A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

283 行
7.8KB

  1. import Utils from "/app/js/common/Utils.js";
  2. import ISurface from "/app/js/ifaces/ISurface.js";
  3. import NESTile from "/app/js/models/NESTile.js";
  4. import NESPalette from "/app/js/models/NESPalette.js";
  5. function LRIdx2TileIdxCo(index, lid){
  6. if (isNaN(lid) || lid < 0 || lid > 2){
  7. lid = 2;
  8. }
  9. var res = {
  10. lid: 0,
  11. index: 0,
  12. x: 0,
  13. y: 0
  14. };
  15. var w = (lid == 2) ? 256 : 128;
  16. var x = Math.floor(index % w);
  17. var y = Math.floor(index / w);
  18. if (x < 128){
  19. res.index = (Math.floor(y/8) * 16) + Math.floor(x / 8);
  20. if (lid !== 2)
  21. res.lid = lid;
  22. } else {
  23. res.index = (Math.floor(y/8) * 16) + Math.floor((x - 128) / 8);
  24. res.lid = 1;
  25. }
  26. res.x = x % 8;
  27. res.y = y % 8;
  28. return res;
  29. }
  30. export default class NESBank extends ISurface{
  31. constructor(){
  32. super();
  33. this.__LP = []; // Left Patterns (Sprites)
  34. this.__RP = []; // Right Patterns (Backgrounds)
  35. this.__AccessMode = 2; // 0 = Sprites only | 1 = BG only | 2 = Sprites and BG
  36. var handle_datachanged = function(side){
  37. if ((side == 0 && (this.__AccessMode == 0 || this.__AccessMode == 2)) ||
  38. (side == 1 && (this.__AccessMode == 1 || this.__AccessMode == 2))){
  39. this.emit("data_changed");
  40. }
  41. }
  42. for (var i=0; i < 256; i++){
  43. this.__LP.push(new NESTile());
  44. this.__LP[i].listen("data_changed", handle_datachanged.bind(this, 0));
  45. this.__RP.push(new NESTile());
  46. this.__RP[i].listen("data_changed", handle_datachanged.bind(this, 1));
  47. }
  48. this.__palette = null;
  49. }
  50. get access_mode(){return this.__AccessMode;}
  51. set access_mode(m){
  52. if (!Utils.isInt(m))
  53. throw new TypeError("Access mode expected to be integer.");
  54. switch(m){
  55. case NESBank.ACCESSMODE_SPRITE:
  56. this.__AccessMode = NESBank.ACCESSMODE_SPRITE; break;
  57. case NESBank.ACCESSMODE_BACKGROUND:
  58. this.__AccessMode = NESBank.ACCESSMODE_BACKGROUND; break;
  59. case NESBank.ACCESSMODE_FULL:
  60. this.__AccessMode = NESBank.ACCESSMODE_FULL; break;
  61. }
  62. }
  63. get json(){
  64. JSON.stringify({
  65. LP: this.__LP.map(x=>x.base64),
  66. RP: this.__RP.map(x=>x.base64)
  67. });
  68. }
  69. get chr(){
  70. var buff = new Uint8Array(8192);
  71. var offset = 0;
  72. this.__LP.forEach(function(i){
  73. buff.set(i.chr, offset);
  74. offset += 16;
  75. });
  76. this.__RP.forEach(function(i){
  77. buff.set(i.chr, offset);
  78. offset += 16;
  79. });
  80. return buff;
  81. }
  82. set chr(buff){
  83. if (!(buff instanceof Uint8Array))
  84. throw new TypeError("Expected Uint8Array buffer.");
  85. if (buff.length !== 8192)
  86. throw new RangeError("Data buffer has invalid byte length.");
  87. var offset = 0;
  88. this.__LP.forEach((i) => {
  89. i.chr = buff.slice(offset, offset+15);
  90. offset += 16;
  91. });
  92. this.__RP.forEach((i) => {
  93. i.chr = buff.slice(offset, offset+15);
  94. offset += 16;
  95. });
  96. }
  97. get palette(){return this.__palette;}
  98. set palette(p){
  99. if (p !== null && !(p instanceof NESPalette))
  100. throw new TypeError("Expected null or NESPalette object.");
  101. if (p !== this.__palette){
  102. this.__palette = p;
  103. }
  104. }
  105. get width(){return (this.__AccessMode == NESBank.ACCESSMODE_FULL) ? 256 : 128;}
  106. get height(){return 128;}
  107. get length(){return this.width * this.height;}
  108. get coloridx(){
  109. return new Proxy(this, {
  110. get:function(obj, prop){
  111. var len = obj.length * 8;
  112. if (prop === "length")
  113. return len;
  114. if (!Utils.isInt(prop))
  115. throw new TypeError("Expected integer index.");
  116. prop = parseInt(prop);
  117. if (prop < 0 || prop >= len)
  118. return NESPalette.Default[4];
  119. var res = LRIdx2TileIdxCo(prop, this.__AccessMode);
  120. var list = (res.lid === 0) ? obj.__LP : obj.__RP;
  121. return list[res.index].getPixelIndex(res.x, res.y);
  122. },
  123. set:function(obj, prop, value){
  124. if (!Utils.isInt(prop))
  125. throw new TypeError("Expected integer index.");
  126. if (!Utils.isInt(value))
  127. throw new TypeError("Color expected to be integer.");
  128. prop = parseInt(prop);
  129. value = parseInt(value);
  130. if (prop < 0 || prop >= len)
  131. throw new RangeError("Index out of bounds.");
  132. if (value < 0 || value >= 4)
  133. throw new RangeError("Color index out of bounds.");
  134. var res = LRIdx2TileIdxCo(prop, this.__AccessMode);
  135. var list = (res.lid === 0) ? obj.__LP : obj.__RP;
  136. list[res.index].setPixelIndex(res.x, res.y, value);
  137. return true;
  138. }
  139. });
  140. }
  141. get lp(){
  142. return new Proxy(this, {
  143. get: function(obj, prop){
  144. if (prop === "length")
  145. return obj.__LP.length;
  146. if (!Utils.isInt(prop))
  147. throw new TypeError("Expected integer index.");
  148. prop = parseInt(prop);
  149. if (prop < 0 || prop >= 256)
  150. throw new RangeError("Index out of bounds.");
  151. return obj.__LP[prop];
  152. },
  153. set: function(obj, prop, value){
  154. if (!Utils.isInt(prop))
  155. throw new TypeError("Expected integer index.");
  156. if (!(value instanceof NESTile))
  157. throw new TypeError("Can only assign NESTile objects.");
  158. prop = parseInt(prop);
  159. if (prop < 0 || prop >= 256)
  160. throw new RangeError("Index out of bounds.");
  161. obj.__LP[prop].copy(value);
  162. return true;
  163. }
  164. });
  165. }
  166. get rp(){
  167. return new Proxy(this, {
  168. get: function(obj, prop){
  169. if (prop === "length")
  170. return obj.__RP.length;
  171. if (!Utils.isInt(prop))
  172. throw new TypeError("Expected integer index.");
  173. prop = parseInt(prop);
  174. if (prop < 0 || prop >= 256)
  175. throw new RangeError("Index out of bounds.");
  176. return obj.__RP[prop];
  177. },
  178. set: function(obj, prop, value){
  179. if (!Utils.isInt(prop))
  180. throw new TypeError("Expected integer index.");
  181. if (!(value instanceof NESTile))
  182. throw new TypeError("Can only assign NESTile objects.");
  183. prop = parseInt(prop);
  184. if (prop < 0 || prop >= 256)
  185. throw new RangeError("Index out of bounds.");
  186. obj.__RP[prop].copy(value);
  187. return true;
  188. }
  189. });
  190. }
  191. copy(b){
  192. if (!(b instanceof NESBank))
  193. throw new TypeError("Expected NESBank object.");
  194. for (var i=0; i < 256; i++){
  195. this.lp[i] = b.lp[i];
  196. this.rp[i] = b.rp[i];
  197. }
  198. return this;
  199. }
  200. clone(){
  201. return (new NESBank()).copy(this);
  202. }
  203. getColor(x,y){
  204. if (x < 0 || x >= this.width || y < 0 || y >= this.height)
  205. return this.__default_pi[4];
  206. var res = LRIdx2TileIdxCo((y*this.width)+x, this.__AccessMode);
  207. var list = (res.lid === 0) ? this.__LP : this.__RP;
  208. var pi = list[res.index].paletteIndex;
  209. var ci = list[res.index].getPixelIndex(res.x, res.y);
  210. if (this.__palette !== null){
  211. return this.__palette.get_palette_color(pi, ci);
  212. }
  213. return NESPalette.Default[ci];
  214. }
  215. getColorIndex(x, y){
  216. if (x < 0 || x >= this.width || y < 0 || y >= this.height)
  217. return {pi: -1, ci:-1};
  218. var res = LRIdx2TileIdxCo((y*this.width)+x, this.__AccessMode);
  219. var list = (res.lid === 0) ? this.__LP : this.__RP;
  220. return {
  221. pi: list[res.index].paletteIndex,
  222. ci: list[res.index].getPixelIndex(res.x, res.y)
  223. };
  224. }
  225. setColorIndex(x, y, ci, pi){
  226. if (x < 0 || x >= this.width || y < 0 || y > this.height)
  227. throw new RangeError("Coordinates out of bounds.");
  228. if (!Utils.isInt(pi))
  229. pi = -1;
  230. if (!Utils.isInt(ci))
  231. ci = 0;
  232. if (pi < 0){
  233. this.coloridx[(y*this.width)+x] = ci;
  234. } else {
  235. var res = LRIdx2TileIdxCo((y*this.width)+x, this.__AccessMode);
  236. var list = (res.lid === 0) ? this.__LP : this.__RP;
  237. list[res.index].paletteIndex = pi;
  238. list[res.index].setPixelIndex(res.x, res.y, ci);
  239. }
  240. return this;
  241. }
  242. }
  243. NESBank.ACCESSMODE_SPRITE = 0;
  244. NESBank.ACCESSMODE_BACKGROUND = 1;
  245. NESBank.ACCESSMODE_FULL = 2;