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

465 行
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_palinfochanged = (function(pdat){
  140. RenderD();
  141. }).bind(this);
  142. var handle_setapppalette = (function(pal){
  143. if (this.__palette !== null)
  144. this.__palette.unlisten("palettes_changed", handle_palinfochanged);
  145. this.__palette = pal;
  146. this.__palette.listen("palettes_changed", handle_palinfochanged);
  147. if (this.__surface !== null){
  148. this.__surface.palette = pal;
  149. if (this.__onePaletteMode === false)
  150. RenderD();
  151. }
  152. }).bind(this);
  153. GlobalEvents.listen("set_app_palette", handle_setapppalette);
  154. var handle_surface_data_changed = (function(){
  155. RenderD();
  156. }).bind(this);
  157. var handle_change_surface = (function(surf){
  158. if (!(surf instanceof ISurface)){
  159. console.log("WARNING: Attempted to set painter to non-surface instance.");
  160. return;
  161. }
  162. if (surf !== this.__surface){
  163. if (this.__surface !== null){
  164. this.__surface.unlisten("data_changed", handle_surface_data_changed);
  165. }
  166. this.__surface = surf;
  167. this.__surface.listen("data_changed", handle_surface_data_changed);
  168. if (this.__palette === null && this.__surface.palette !== null){
  169. this.__palette = this.__surface.palette;
  170. } else if (this.__palette !== null && this.__surface.palette !== this.__palette){
  171. this.__surface.palette = this.__palette;
  172. }
  173. this.center_surface();
  174. }
  175. RenderD();
  176. }).bind(this);
  177. GlobalEvents.listen("change_surface", handle_change_surface);
  178. var handle_color_change = (function(pi, ci){
  179. this.__brushPalette = pi;
  180. this.__brushColor = ci;
  181. }).bind(this);
  182. GlobalEvents.listen("active_palette_color", handle_color_change);
  183. var handle_mousemove = (function(e){
  184. this.__brushLastPos[0] = this.__brushPos[0];
  185. this.__brushLastPos[1] = this.__brushPos[1];
  186. this.__brushPos[0] = e.x;
  187. this.__brushPos[1] = e.y;
  188. if (this.__surface !== null){
  189. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  190. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  191. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  192. RenderD();
  193. }
  194. }
  195. }).bind(this);
  196. input.listen("mousemove", handle_mousemove);
  197. input.listen("mouseleft+mousemove", handle_mousemove);
  198. var handle_draw = (function(e){
  199. if (e.isCombo || e.button == 0){
  200. if (this.__surface !== null){
  201. //console.log(this.__brushPos);
  202. //console.log(this.__brushLastPos);
  203. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  204. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  205. var sx = (e.isCombo) ? Math.floor((this.__brushLastPos[0] - this.__offset[0]) * (1.0 / this.__scale)) : x;
  206. var sy = (e.isCombo) ? Math.floor((this.__brushLastPos[1] - this.__offset[1]) * (1.0 / this.__scale)) : y;
  207. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  208. LineToSurface(sx, sy, x, y, this.__brushColor, this.__brushPalette);
  209. //RenderD();
  210. }
  211. }
  212. }
  213. }).bind(this);
  214. input.listen("mouseclick", handle_draw);
  215. input.listen("mouseleft+mousemove", handle_draw);
  216. var handle_offset = (function(e){
  217. this.__offset[0] += e.x - e.lastX;
  218. this.__offset[1] += e.y - e.lastY;
  219. RenderD();
  220. }).bind(this);
  221. input.listen("shift+mouseleft+mousemove", handle_offset);
  222. var handle_scale = (function(e){
  223. if (e.delta < 0){
  224. this.scale_down();
  225. } else if (e.delta > 0){
  226. this.scale_up();
  227. }
  228. if (e.delta !== 0)
  229. RenderD();
  230. }).bind(this);
  231. input.listen("wheel", handle_scale);
  232. var handle_togglegrid = (function(target, args){
  233. if (args.show){
  234. target.classList.add("pure-button-active");
  235. target.setAttribute("emit-args", JSON.stringify({show:false}));
  236. this.__gridEnabled = true;
  237. } else {
  238. target.classList.remove("pure-button-active");
  239. target.setAttribute("emit-args", JSON.stringify({show:true}));
  240. this.__gridEnabled = false;
  241. }
  242. RenderD();
  243. }).bind(this);
  244. GlobalEvents.listen("painter-togglegrid", handle_togglegrid);
  245. var handle_colormode = (function(target, args){
  246. if (args.onePaletteMode){
  247. target.classList.remove("pure-button-active");
  248. target.setAttribute("emit-args", JSON.stringify({onePaletteMode:false}));
  249. this.__onePaletteMode = true;
  250. } else {
  251. target.classList.add("pure-button-active");
  252. target.setAttribute("emit-args", JSON.stringify({onePaletteMode:true}));
  253. this.__onePaletteMode = false;
  254. }
  255. RenderD();
  256. }).bind(this);
  257. GlobalEvents.listen("painter-colormode", handle_colormode);
  258. }
  259. get onePaletteMode(){return this.__onePaletteMode;}
  260. set onePaletteMode(e){
  261. this.__onePaletteMode = (e === true);
  262. this.render();
  263. }
  264. get scale(){
  265. return this.__scale;
  266. }
  267. set scale(s){
  268. if (typeof(s) !== 'number')
  269. throw new TypeError("Expected number value.");
  270. this.__scale = Math.max(0.1, Math.min(100.0, s));
  271. }
  272. get showGrid(){return this.__gridEnabled;}
  273. set showGrid(e){
  274. this.__gridEnabled = (e === true);
  275. }
  276. initialize(){
  277. if (canvas === null){
  278. canvas = document.getElementById(EL_CANVAS_ID);
  279. if (!canvas)
  280. throw new Error("Failed to obtain the canvas element.");
  281. context = canvas.getContext("2d");
  282. if (!context)
  283. throw new Error("Failed to obtain canvas context.");
  284. context.imageSmoothingEnabled = false;
  285. ResizeCanvasImg(canvas.clientWidth, canvas.clientHeight); // A forced "resize".
  286. input.mouseTargetElement = canvas;
  287. this.center_surface();
  288. }
  289. return this;
  290. }
  291. scale_up(amount=1){
  292. this.scale = this.scale + (amount*0.1);
  293. return this;
  294. }
  295. scale_down(amount=1){
  296. this.scale = this.scale - (amount*0.1);
  297. return this;
  298. }
  299. center_surface(){
  300. if (canvas === null || this.__surface === null)
  301. return;
  302. this.__offset[0] = Math.floor((canvas.clientWidth - this.__surface.width) * 0.5);
  303. this.__offset[1] = Math.floor((canvas.clientHeight - this.__surface.height) * 0.5);
  304. return this;
  305. }
  306. render(){
  307. if (context === null || this.__surface === null)
  308. return;
  309. context.save();
  310. // Clearing the context surface...
  311. context.fillStyle = NESPalette.Default[4];
  312. context.fillRect(
  313. 0,0,
  314. Math.floor(canvas.clientWidth),
  315. Math.floor(canvas.clientHeight)
  316. );
  317. OpenCanvasPixels();
  318. for (var j = 0; j < this.__surface.height; j++){
  319. var y = (j*this.__scale) + this.__offset[1];
  320. for (var i = 0; i < this.__surface.width; i++){
  321. var x = (i*this.__scale) + this.__offset[0];
  322. if (x >= 0 && x < canvas.clientWidth && y >= 0 && y < canvas.clientHeight){
  323. var color = NESPalette.Default[4];
  324. if (this.__onePaletteMode){
  325. var pinfo = this.__surface.getColorIndex(i, j);
  326. if (pinfo.ci >= 0)
  327. color = NESPalette.Default[pinfo.ci];
  328. } else {
  329. color = this.__surface.getColor(i, j);
  330. }
  331. PutCanvasPixel(x,y, this.__scale, color);
  332. }
  333. }
  334. }
  335. CloseCanvasPixels();
  336. // Draw the mouse position... if mouse is currently in bounds.
  337. if (input.isMouseInBounds()){
  338. context.fillStyle = "#AA9900";
  339. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0/this.__scale));
  340. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0/this.__scale));
  341. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  342. context.beginPath();
  343. context.rect(
  344. this.__offset[0] + (x*this.__scale),
  345. this.__offset[1] + (y*this.__scale),
  346. Math.ceil(this.__scale),
  347. Math.ceil(this.__scale)
  348. );
  349. context.fill();
  350. context.closePath();
  351. }
  352. }
  353. // Draw grid.
  354. if (this.__gridEnabled && this.__scale > 0.5){
  355. context.strokeStyle = "#00FF00";
  356. var w = this.__surface.width * this.__scale;
  357. var h = this.__surface.height * this.__scale;
  358. var length = Math.max(this.__surface.width, this.__surface.height);
  359. for (var i=0; i < length; i += 8){
  360. var x = (i*this.__scale) + this.__offset[0];
  361. var y = (i*this.__scale) + this.__offset[1];
  362. if (i < this.__surface.width){
  363. context.beginPath();
  364. context.moveTo(x, this.__offset[1]);
  365. context.lineTo(x, this.__offset[1] + h);
  366. context.stroke();
  367. context.closePath();
  368. }
  369. if (i < this.__surface.height){
  370. context.beginPath();
  371. context.moveTo(this.__offset[0], y);
  372. context.lineTo(this.__offset[0] + w, y);
  373. context.stroke();
  374. context.closePath();
  375. }
  376. }
  377. }
  378. context.restore();
  379. return this;
  380. }
  381. }
  382. const instance = new CTRLPainter();
  383. export default instance;