A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.9KB

  1. import Utils from "/app/js/common/Utils.js";
  2. import NESTile from "/app/js/models/NESTile.js";
  3. import NESPalette from "/app/js/models/NESPalette.js";
  4. export default class NESBank {
  5. constructor(){
  6. this.__LP = []; // Left Patterns (Sprites)
  7. this.__RP = []; // Right Patterns (Backgrounds)
  8. for (var i=0; i < 256; i++){
  9. this.__LP.push(new NESTile());
  10. this.__RP.push(new NESTile());
  11. }
  12. this.__palette = null;
  13. }
  14. get json(){
  15. JSON.stringify({
  16. LP: this.__LP.map(x=>x.base64),
  17. RP: this.__RP.map(x=>x.base64)
  18. });
  19. }
  20. get chr(){
  21. var buff = new Uint8Array(8192);
  22. var offset = 0;
  23. this.__LP.forEach(function(i){
  24. buff.set(i.chr, offset);
  25. offset += 16;
  26. });
  27. this.__RP.forEach(function(i){
  28. buff.set(i.chr, offset);
  29. offset += 16;
  30. });
  31. return buff;
  32. }
  33. get palette(){return this.__palette;}
  34. set palette(p){
  35. if (p !== null && !(p instanceof NESPalette))
  36. throw new TypeError("Expected null or NESPalette object.");
  37. if (p !== this.__palette){
  38. this.__palette = p;
  39. }
  40. }
  41. get lp(){
  42. return new Proxy(this, {
  43. get: function(obj, prop){
  44. if (prop === "length")
  45. return obj.__LP.length;
  46. if (!Utils.isInt(prop))
  47. throw new TypeError("Expected integer index.");
  48. prop = parseInt(prop);
  49. if (prop < 0 || prop >= 256)
  50. throw new RangeError("Index out of bounds.");
  51. return this.__LP[prop];
  52. },
  53. set: function(obj, prop, value){
  54. if (!Utils.isInt(prop))
  55. throw new TypeError("Expected integer index.");
  56. if (!(value instanceof NESTile))
  57. throw new TypeError("Can only assign NESTile objects.");
  58. prop = parseInt(prop);
  59. if (prop < 0 || prop >= 256)
  60. throw new RangeError("Index out of bounds.");
  61. this.__LP[prop].copy(value);
  62. }
  63. });
  64. }
  65. get rp(){
  66. return new Proxy(this, {
  67. get: function(obj, prop){
  68. if (prop === "length")
  69. return obj.__RP.length;
  70. if (!Utils.isInt(prop))
  71. throw new TypeError("Expected integer index.");
  72. prop = parseInt(prop);
  73. if (prop < 0 || prop >= 256)
  74. throw new RangeError("Index out of bounds.");
  75. return this.__RP[prop];
  76. },
  77. set: function(obj, prop, value){
  78. if (!Utils.isInt(prop))
  79. throw new TypeError("Expected integer index.");
  80. if (!(value instanceof NESTile))
  81. throw new TypeError("Can only assign NESTile objects.");
  82. prop = parseInt(prop);
  83. if (prop < 0 || prop >= 256)
  84. throw new RangeError("Index out of bounds.");
  85. this.__RP[prop].copy(value);
  86. }
  87. });
  88. }
  89. copy(b){
  90. if (!(b instanceof NESBank))
  91. throw new TypeError("Expected NESBank object.");
  92. for (var i=0; i < 256; i++){
  93. this.lp[i] = b.lp[i];
  94. this.rp[i] = b.rp[i];
  95. }
  96. return this;
  97. }
  98. clone(){
  99. return (new NESBank()).copy(this);
  100. }
  101. }