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文字以内のものにしてください。

456 行
13KB

  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 ISurface from "/app/js/ifaces/ISurface.js";
  6. const EL_CANVAS_ID = "painter";
  7. /* --------------------------------------------------------------------
  8. * Univeral data and functions.
  9. ------------------------------------------------------------------- */
  10. var canvas = null;
  11. var context = null;
  12. var ctximg = null;
  13. function OpenCanvasPixels(){
  14. if (context !== null){
  15. if (ctximg === null){
  16. ctximg = context.getImageData(0,0,Math.floor(canvas.clientWidth),Math.floor(canvas.clientHeight));
  17. }
  18. return (ctximg !== null)
  19. }
  20. return false;
  21. }
  22. function PutCanvasPixel(i,j,size,color){
  23. if (ctximg === null)
  24. return;
  25. i = Math.round(i);
  26. j = Math.round(j);
  27. size = Math.ceil(size);
  28. if (size <= 0){return;}
  29. var cw = Math.floor(canvas.clientWidth);
  30. var ch = Math.floor(canvas.clientHeight);
  31. var r = parseInt(color.substring(1, 3), 16);
  32. var g = parseInt(color.substring(3, 5), 16);
  33. var b = parseInt(color.substring(5, 7), 16);
  34. var idat = ctximg.data;
  35. for (var y=j; y < j+size; y++){
  36. for (var x=i; x < i+size; x++){
  37. if (x >= 0 && x < cw && y >= 0 && y < ch){
  38. var index = (y*cw*4) + (x*4);
  39. idat[index] = r;
  40. idat[index+1] = g;
  41. idat[index+2] = b;
  42. }
  43. }
  44. }
  45. }
  46. function CloseCanvasPixels(){
  47. if (ctximg !== null){
  48. context.putImageData(ctximg, 0, 0);
  49. ctximg = null;
  50. }
  51. }
  52. function ResizeCanvasImg(w, h){
  53. if (canvas !== null){
  54. canvas.width = w;
  55. canvas.height = h;
  56. }
  57. };
  58. // Handling window resize events...
  59. var HANDLE_Resize = Utils.debounce(function(e){
  60. if (canvas !== null){
  61. ResizeCanvasImg(
  62. canvas.clientWidth,
  63. canvas.clientHeight
  64. );
  65. GlobalEvents.emit("resize", canvas.clientWidth, canvas.clientHeight);
  66. }
  67. }, 250);
  68. window.addEventListener("resize", HANDLE_Resize);
  69. // Setting-up Input controls.
  70. var input = new Input();
  71. input.enableKeyboardInput(true);
  72. input.enableMouseInput(true);
  73. input.preventDefaults = true;
  74. // Mouse handling...
  75. /*input.listen("mousemove", handle_mouseevent);
  76. input.listen("mousedown", handle_mouseevent);
  77. input.listen("mouseup", handle_mouseevent);
  78. input.listen("mouseclick", handle_mouseclickevent);
  79. */
  80. /* --------------------------------------------------------------------
  81. * CTRLPainter
  82. * Actual controlling class.
  83. ------------------------------------------------------------------- */
  84. // For reference...
  85. // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke
  86. class CTRLPainter {
  87. constructor(){
  88. this.__scale = 1.0; // This is the scale the painter will display source information.
  89. this.__offset = [0.0, 0.0]; // This is the X,Y offset from origin to display source information.
  90. this.__onePaletteMode = true; // If true, ALL tiles will be drawing using the same palette.
  91. this.__brushSize = 1;
  92. this.__brushLastPos = [0.0, 0.0];
  93. this.__brushPos = [0.0, 0.0];
  94. this.__brushColor = 0;
  95. this.__brushPalette = 0;
  96. this.__gridEnabled = false;
  97. this.__gridSize = 1;
  98. this.__surface = null;
  99. this.__palette = null;
  100. // var self = this;
  101. var RenderD = Utils.throttle((function(){
  102. this.render();
  103. }).bind(this), 20);
  104. var LineToSurface = (function(x0, y0, x1, y1, ci, pi){
  105. var dx = x1 - x0;
  106. var ix = Math.sign(dx);
  107. dx = 2 * Math.abs(dx);
  108. var dy = y1 - y0;
  109. var iy = Math.sign(dy);
  110. dy = 2 * Math.abs(dy);
  111. if (dx > dy){
  112. var err = dy - (dx/2);
  113. var y = y0;
  114. Utils.range(x0, x1, 1).forEach((x) => {
  115. this.__surface.setColorIndex(x, y, ci, pi);
  116. if (err > 0 || (err == 0 && ix > 0)){
  117. err -= dx;
  118. y += iy;
  119. }
  120. err += dy;
  121. });
  122. } else {
  123. var err = dx - (dy/2);
  124. var x = x0;
  125. Utils.range(y0, y1, 1).forEach((y) => {
  126. this.__surface.setColorIndex(x, y, ci, pi);
  127. if (err > 0 || (err == 0 && iy > 0)){
  128. err -= dy;
  129. x += ix;
  130. }
  131. err += dx;
  132. });
  133. }
  134. }).bind(this);
  135. var handle_resize = (function(w,h){
  136. RenderD();
  137. }).bind(this);
  138. GlobalEvents.listen("resize", handle_resize);
  139. var handle_setapppalette = (function(pal){
  140. this.__palette = pal;
  141. if (this.__surface !== null){
  142. this.__surface.palette = pal;
  143. if (this.__onePaletteMode === false)
  144. RenderD();
  145. }
  146. }).bind(this);
  147. GlobalEvents.listen("set_app_palette", handle_setapppalette);
  148. var handle_surface_data_changed = (function(){
  149. RenderD();
  150. }).bind(this);
  151. var handle_change_surface = (function(surf){
  152. if (!(surf instanceof ISurface)){
  153. console.log("WARNING: Attempted to set painter to non-surface instance.");
  154. return;
  155. }
  156. if (surf !== this.__surface){
  157. if (this.__surface !== null){
  158. this.__surface.unlisten("data_changed", handle_surface_data_changed);
  159. }
  160. this.__surface = surf;
  161. this.__surface.listen("data_changed", handle_surface_data_changed);
  162. if (this.__palette === null && this.__surface.palette !== null){
  163. this.__palette = this.__surface.palette;
  164. } else if (this.__palette !== null && this.__surface.palette !== this.__palette){
  165. this.__surface.palette = this.__palette;
  166. }
  167. this.center_surface();
  168. }
  169. RenderD();
  170. }).bind(this);
  171. GlobalEvents.listen("change_surface", handle_change_surface);
  172. var handle_color_change = (function(pi, ci){
  173. this.__brushPalette = pi;
  174. this.__brushColor = ci;
  175. }).bind(this);
  176. GlobalEvents.listen("active_palette_color", handle_color_change);
  177. var handle_mousemove = (function(e){
  178. this.__brushLastPos[0] = this.__brushPos[0];
  179. this.__brushLastPos[1] = this.__brushPos[1];
  180. this.__brushPos[0] = e.x;
  181. this.__brushPos[1] = e.y;
  182. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  183. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  184. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  185. RenderD();
  186. }
  187. }).bind(this);
  188. input.listen("mousemove", handle_mousemove);
  189. input.listen("mouseleft+mousemove", handle_mousemove);
  190. var handle_draw = (function(e){
  191. if (e.isCombo || e.button == 0){
  192. if (this.__surface !== null){
  193. //console.log(this.__brushPos);
  194. //console.log(this.__brushLastPos);
  195. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  196. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  197. var sx = (e.isCombo) ? Math.floor((this.__brushLastPos[0] - this.__offset[0]) * (1.0 / this.__scale)) : x;
  198. var sy = (e.isCombo) ? Math.floor((this.__brushLastPos[1] - this.__offset[1]) * (1.0 / this.__scale)) : y;
  199. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  200. LineToSurface(sx, sy, x, y, this.__brushColor, this.__brushPalette);
  201. //RenderD();
  202. }
  203. }
  204. }
  205. }).bind(this);
  206. input.listen("mouseclick", handle_draw);
  207. input.listen("mouseleft+mousemove", handle_draw);
  208. var handle_offset = (function(e){
  209. this.__offset[0] += e.x - e.lastX;
  210. this.__offset[1] += e.y - e.lastY;
  211. RenderD();
  212. }).bind(this);
  213. input.listen("shift+mouseleft+mousemove", handle_offset);
  214. var handle_scale = (function(e){
  215. if (e.delta < 0){
  216. this.scale_down();
  217. } else if (e.delta > 0){
  218. this.scale_up();
  219. }
  220. if (e.delta !== 0)
  221. RenderD();
  222. }).bind(this);
  223. input.listen("wheel", handle_scale);
  224. var handle_togglegrid = (function(target, args){
  225. if (args.show){
  226. target.classList.add("pure-button-active");
  227. target.setAttribute("emit-args", JSON.stringify({show:false}));
  228. this.__gridEnabled = true;
  229. } else {
  230. target.classList.remove("pure-button-active");
  231. target.setAttribute("emit-args", JSON.stringify({show:true}));
  232. this.__gridEnabled = false;
  233. }
  234. RenderD();
  235. }).bind(this);
  236. GlobalEvents.listen("painter-togglegrid", handle_togglegrid);
  237. var handle_colormode = (function(target, args){
  238. if (args.onePaletteMode){
  239. target.classList.remove("pure-button-active");
  240. target.setAttribute("emit-args", JSON.stringify({onePaletteMode:false}));
  241. this.__onePaletteMode = true;
  242. } else {
  243. target.classList.add("pure-button-active");
  244. target.setAttribute("emit-args", JSON.stringify({onePaletteMode:true}));
  245. this.__onePaletteMode = false;
  246. }
  247. RenderD();
  248. }).bind(this);
  249. GlobalEvents.listen("painter-colormode", handle_colormode);
  250. }
  251. get onePaletteMode(){return this.__onePaletteMode;}
  252. set onePaletteMode(e){
  253. this.__onePaletteMode = (e === true);
  254. this.render();
  255. }
  256. get scale(){
  257. return this.__scale;
  258. }
  259. set scale(s){
  260. if (typeof(s) !== 'number')
  261. throw new TypeError("Expected number value.");
  262. this.__scale = Math.max(0.1, Math.min(100.0, s));
  263. }
  264. get showGrid(){return this.__gridEnabled;}
  265. set showGrid(e){
  266. this.__gridEnabled = (e === true);
  267. }
  268. initialize(){
  269. if (canvas === null){
  270. canvas = document.getElementById(EL_CANVAS_ID);
  271. if (!canvas)
  272. throw new Error("Failed to obtain the canvas element.");
  273. context = canvas.getContext("2d");
  274. if (!context)
  275. throw new Error("Failed to obtain canvas context.");
  276. context.imageSmoothingEnabled = false;
  277. ResizeCanvasImg(canvas.clientWidth, canvas.clientHeight); // A forced "resize".
  278. input.mouseTargetElement = canvas;
  279. this.center_surface();
  280. }
  281. return this;
  282. }
  283. scale_up(amount=1){
  284. this.scale = this.scale + (amount*0.1);
  285. return this;
  286. }
  287. scale_down(amount=1){
  288. this.scale = this.scale - (amount*0.1);
  289. return this;
  290. }
  291. center_surface(){
  292. if (canvas === null || this.__surface === null)
  293. return;
  294. this.__offset[0] = Math.floor((canvas.clientWidth - this.__surface.width) * 0.5);
  295. this.__offset[1] = Math.floor((canvas.clientHeight - this.__surface.height) * 0.5);
  296. return this;
  297. }
  298. render(){
  299. if (context === null || this.__surface === null)
  300. return;
  301. context.save();
  302. // Clearing the context surface...
  303. context.fillStyle = NESPalette.Default[4];
  304. context.fillRect(
  305. 0,0,
  306. Math.floor(canvas.clientWidth),
  307. Math.floor(canvas.clientHeight)
  308. );
  309. OpenCanvasPixels();
  310. for (var j = 0; j < this.__surface.height; j++){
  311. var y = (j*this.__scale) + this.__offset[1];
  312. for (var i = 0; i < this.__surface.width; i++){
  313. var x = (i*this.__scale) + this.__offset[0];
  314. if (x >= 0 && x < canvas.clientWidth && y >= 0 && y < canvas.clientHeight){
  315. var color = NESPalette.Default[4];
  316. if (this.__onePaletteMode){
  317. var pinfo = this.__surface.getColorIndex(i, j);
  318. if (pinfo.ci >= 0)
  319. color = NESPalette.Default[pinfo.ci];
  320. } else {
  321. color = this.__surface.getColor(i, j);
  322. }
  323. PutCanvasPixel(x,y, this.__scale, color);
  324. }
  325. }
  326. }
  327. CloseCanvasPixels();
  328. // Draw the mouse position... if mouse is currently in bounds.
  329. if (input.isMouseInBounds()){
  330. context.fillStyle = "#AA9900";
  331. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0/this.__scale));
  332. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0/this.__scale));
  333. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  334. context.beginPath();
  335. context.rect(
  336. this.__offset[0] + (x*this.__scale),
  337. this.__offset[1] + (y*this.__scale),
  338. Math.ceil(this.__scale),
  339. Math.ceil(this.__scale)
  340. );
  341. context.fill();
  342. context.closePath();
  343. }
  344. }
  345. // Draw grid.
  346. if (this.__gridEnabled && this.__scale > 0.5){
  347. context.strokeStyle = "#00FF00";
  348. var w = this.__surface.width * this.__scale;
  349. var h = this.__surface.height * this.__scale;
  350. var length = Math.max(this.__surface.width, this.__surface.height);
  351. for (var i=0; i < length; i += 8){
  352. var x = (i*this.__scale) + this.__offset[0];
  353. var y = (i*this.__scale) + this.__offset[1];
  354. if (i < this.__surface.width){
  355. context.beginPath();
  356. context.moveTo(x, this.__offset[1]);
  357. context.lineTo(x, this.__offset[1] + h);
  358. context.stroke();
  359. context.closePath();
  360. }
  361. if (i < this.__surface.height){
  362. context.beginPath();
  363. context.moveTo(this.__offset[0], y);
  364. context.lineTo(this.__offset[0] + w, y);
  365. context.stroke();
  366. context.closePath();
  367. }
  368. }
  369. }
  370. context.restore();
  371. return this;
  372. }
  373. }
  374. const instance = new CTRLPainter();
  375. export default instance;