A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

215 lines
5.6KB

  1. import Utils from "/app/js/common/Utils.js";
  2. import GlobalEvents from "/app/js/common/EventCaller.js";
  3. import Input from "/app/js/ui/Input.js";
  4. import NESPalette from "/app/js/models/NESPalette.js";
  5. //import NESTile from "/app/js/models/NESTile.js";
  6. //import NESBank from "/app/js/models/NESBank.js";
  7. import ISurface from "/app/js/ifaces/ISurface.js";
  8. const EL_CANVAS_ID = "painter";
  9. /* --------------------------------------------------------------------
  10. * Univeral data and functions.
  11. ------------------------------------------------------------------- */
  12. var canvas = null;
  13. var context = null;
  14. function ResizeCanvasImg(w, h){
  15. if (canvas !== null){
  16. canvas.width = w;
  17. canvas.height = h;
  18. }
  19. };
  20. // Handling window resize events...
  21. var HANDLE_Resize = Utils.debounce(function(e){
  22. if (canvas !== null){
  23. ResizeCanvasImg(
  24. canvas.clientWidth,
  25. canvas.clientHeight
  26. );
  27. GlobalEvents.emit("resize", canvas.clientWidth, canvas.clientHeight);
  28. }
  29. }, 250);
  30. window.addEventListener("resize", HANDLE_Resize);
  31. // Setting-up Input controls.
  32. var input = new Input();
  33. input.preventDefaults = true;
  34. // Mouse handling...
  35. /*input.listen("mousemove", handle_mouseevent);
  36. input.listen("mousedown", handle_mouseevent);
  37. input.listen("mouseup", handle_mouseevent);
  38. input.listen("mouseclick", handle_mouseclickevent);
  39. */
  40. /* --------------------------------------------------------------------
  41. * CTRLPainter
  42. * Actual controlling class.
  43. ------------------------------------------------------------------- */
  44. // For reference...
  45. // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke
  46. class CTRLPainter {
  47. constructor(){
  48. this.__scale = 1.0; // This is the scale the painter will display source information.
  49. this.__offset = [0.0, 0.0]; // This is the X,Y offset from origin to display source information.
  50. this.__onePaletteMode = true; // If true, ALL tiles will be drawing using the same palette.
  51. this.__brushSize = 1;
  52. this.__brushColor = 0;
  53. this.__brushPalette = 0;
  54. this.__gridEnabled = false;
  55. this.__gridSize = 1;
  56. this.__surface = null;
  57. var self = this;
  58. var handle_resize = (function(w,h){
  59. this.render();
  60. }).bind(this);
  61. GlobalEvents.listen("resize", handle_resize);
  62. var handle_change_surface = (function(surf){
  63. if (!(surf instanceof ISurface)){
  64. console.log("WARNING: Attempted to set painter to non-surface instance.");
  65. return;
  66. }
  67. this.__surface = surf;
  68. this.center_surface();
  69. this.render();
  70. }).bind(this);
  71. GlobalEvents.listen("change_surface", handle_change_surface);
  72. var handle_color_change = (function(pi, ci){
  73. this.__brushPalette = pi;
  74. this.__brushColor = ci;
  75. }).bind(this);
  76. GlobalEvents.listen("active_palette_color", handle_color_change);
  77. }
  78. get onePaletteMode(){return this.__onePaletteMode;}
  79. set onePaletteMode(e){
  80. this.__onePaletteMode = (e === true);
  81. this.render();
  82. }
  83. get scale(){
  84. return this.__scale;
  85. }
  86. set scale(s){
  87. if (typeof(s) !== 'number')
  88. throw new TypeError("Expected number value.");
  89. this.__scale = Math.max(0.1, Math.min(100.0, s));
  90. }
  91. get showGrid(){return this.__gridEnabled;}
  92. set showGrid(e){
  93. this.__gridEnabled = (e === true);
  94. }
  95. initialize(){
  96. if (canvas === null){
  97. canvas = document.getElementById(EL_CANVAS_ID);
  98. if (!canvas)
  99. throw new Error("Failed to obtain the canvas element.");
  100. context = canvas.getContext("2d");
  101. if (!context)
  102. throw new Error("Failed to obtain canvas context.");
  103. context.imageSmoothingEnabled = false;
  104. ResizeCanvasImg(canvas.clientWidth, canvas.clientHeight); // A forced "resize".
  105. input.mouseTargetElement = canvas;
  106. this.center_surface();
  107. }
  108. return this;
  109. }
  110. scale_up(amount=1){
  111. this.scale = this.scale + (amount*0.1);
  112. return this;
  113. }
  114. scale_down(amount=1){
  115. this.scale = this.scale - (amount*0.1);
  116. return this;
  117. }
  118. center_surface(){
  119. if (canvas === null || this.__surface === null)
  120. return;
  121. this.__offset[0] = Math.floor((canvas.clientWidth - this.__surface.width) * 0.5);
  122. this.__offset[1] = Math.floor((canvas.clientHeight - this.__surface.height) * 0.5);
  123. return this;
  124. }
  125. render(){
  126. if (context === null || this.__surface === null)
  127. return;
  128. // Get the contexts initial fillStyle. Don't want the render operation to override it.
  129. var fillStyle = context.fillStyle;
  130. var scalemult = 1.0/this.__scale;
  131. // Clearing the context surface...
  132. context.fillStyle = NESPalette.Default[4];
  133. context.fillRect(
  134. 0,0,
  135. Math.floor(canvas.clientWidth),
  136. Math.floor(canvas.clientHeight)
  137. );
  138. for (var j = 0; j < this.__surface.height; j++){
  139. var y = Math.floor((j*scalemult) + this.__offset[1]);
  140. for (var i = 0; i < this.__surface.width; i++){
  141. var x = Math.floor((i*scalemult) + this.__offset[0]);
  142. if (x >= 0 && x < canvas.clientWidth && y >= 0 && y < canvas.clientHeight){
  143. var color = "#666666";
  144. if (this.__onePaletteMode){
  145. var pinfo = this.__surface.getColorIndex(i, j);
  146. if (pinfo.ci >= 0)
  147. color = NESPalette.Default[pinfo.ci];
  148. } else {
  149. color = this.__surface.getColor(i, j);
  150. }
  151. context.fillStyle = color;
  152. context.fillRect(
  153. x,y,
  154. 1, 1
  155. );
  156. }
  157. }
  158. }
  159. if (this.__gridEnabled && this.__scale > 0.5){
  160. // TODO: render the grid!
  161. }
  162. // Return to the context's initial fillStyle.
  163. context.fillStyle = fillStyle;
  164. return this;
  165. }
  166. }
  167. const instance = new CTRLPainter();
  168. export default instance;