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

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