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个字符

463 行
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 Renderer from "/app/js/ui/Renderer.js";
  5. import NESPalette from "/app/js/models/NESPalette.js";
  6. import ISurface from "/app/js/ifaces/ISurface.js";
  7. const EL_CANVAS_ID = "painter";
  8. /* --------------------------------------------------------------------
  9. * Univeral data and functions.
  10. ------------------------------------------------------------------- */
  11. var canvas = null;
  12. var context = null;
  13. var ctximg = 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.enableKeyboardInput(true);
  34. input.preventDefaults = true;
  35. /* --------------------------------------------------------------------
  36. * CTRLPainter
  37. * Actual controlling class.
  38. ------------------------------------------------------------------- */
  39. // For reference...
  40. // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke
  41. class CTRLPainter {
  42. constructor(){
  43. this.__scale = 1.0; // This is the scale the painter will display source information.
  44. this.__offset = [0.0, 0.0]; // This is the X,Y offset from origin to display source information.
  45. this.__onePaletteMode = true; // If true, ALL tiles will be drawing using the same palette.
  46. this.__brushSize = 1;
  47. this.__brushLastPos = [0.0, 0.0];
  48. this.__brushPos = [0.0, 0.0];
  49. this.__brushColor = 0;
  50. this.__brushPalette = 0;
  51. this.__gridEnabled = false;
  52. this.__gridSize = 1;
  53. this.__surface = null;
  54. this.__palette = null;
  55. this.__snapshotTriggered = false;
  56. // var self = this;
  57. var RenderD = Utils.throttle((function(){
  58. this.render();
  59. }).bind(this), 20);
  60. var LineToSurface = (function(x0, y0, x1, y1, ci, pi){
  61. var dx = x1 - x0;
  62. var ix = Math.sign(dx);
  63. dx = 2 * Math.abs(dx);
  64. var dy = y1 - y0;
  65. var iy = Math.sign(dy);
  66. dy = 2 * Math.abs(dy);
  67. if (dx > dy){
  68. var err = dy - (dx/2);
  69. var y = y0;
  70. Utils.range(x0, x1, 1).forEach((x) => {
  71. this.__surface.setColorIndex(x, y, ci, pi);
  72. if (err > 0 || (err == 0 && ix > 0)){
  73. err -= dx;
  74. y += iy;
  75. }
  76. err += dy;
  77. });
  78. } else {
  79. var err = dx - (dy/2);
  80. var x = x0;
  81. Utils.range(y0, y1, 1).forEach((y) => {
  82. this.__surface.setColorIndex(x, y, ci, pi);
  83. if (err > 0 || (err == 0 && iy > 0)){
  84. err -= dy;
  85. x += ix;
  86. }
  87. err += dx;
  88. });
  89. }
  90. }).bind(this);
  91. var handle_resize = (function(w,h){
  92. RenderD();
  93. }).bind(this);
  94. GlobalEvents.listen("resize", handle_resize);
  95. var handle_palinfochanged = (function(pdat){
  96. RenderD();
  97. }).bind(this);
  98. var handle_setapppalette = (function(pal){
  99. if (this.__palette !== null)
  100. this.__palette.unlisten("palettes_changed", handle_palinfochanged);
  101. this.__palette = pal;
  102. this.__palette.listen("palettes_changed", handle_palinfochanged);
  103. if (this.__surface !== null){
  104. this.__surface.palette = pal;
  105. if (this.__onePaletteMode === false)
  106. RenderD();
  107. }
  108. }).bind(this);
  109. GlobalEvents.listen("set_app_palette", handle_setapppalette);
  110. var handle_surface_data_changed = (function(){
  111. RenderD();
  112. }).bind(this);
  113. var handle_change_surface = (function(surf){
  114. if (surf !== null && !(surf instanceof ISurface)){
  115. console.log("WARNING: Attempted to set painter to non-surface instance.");
  116. return;
  117. }
  118. if (surf !== this.__surface){
  119. if (this.__surface !== null){
  120. this.__surface.unlisten("data_changed", handle_surface_data_changed);
  121. }
  122. this.__surface = surf;
  123. if (this.__surface !== null){
  124. this.__surface.listen("data_changed", handle_surface_data_changed);
  125. if (this.__palette === null && this.__surface.palette !== null){
  126. this.__palette = this.__surface.palette;
  127. } else if (this.__palette !== null && this.__surface.palette !== this.__palette){
  128. this.__surface.palette = this.__palette;
  129. }
  130. this.scale_to_fit();
  131. this.center_surface();
  132. }
  133. }
  134. RenderD();
  135. }).bind(this);
  136. GlobalEvents.listen("change_surface", handle_change_surface);
  137. var handle_color_change = (function(pi, ci){
  138. this.__brushPalette = pi;
  139. this.__brushColor = ci;
  140. }).bind(this);
  141. GlobalEvents.listen("active_palette_color", handle_color_change);
  142. var handle_mousemove = (function(e){
  143. this.__brushLastPos[0] = this.__brushPos[0];
  144. this.__brushLastPos[1] = this.__brushPos[1];
  145. this.__brushPos[0] = e.x;
  146. this.__brushPos[1] = e.y;
  147. if (this.__surface !== null){
  148. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  149. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  150. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  151. RenderD();
  152. }
  153. }
  154. }).bind(this);
  155. input.listen("mousemove", handle_mousemove);
  156. input.listen("mouseleft+mousemove", handle_mousemove);
  157. var handle_draw = (function(e){
  158. if (e.isCombo || e.button == 0){
  159. if (this.__surface !== null){
  160. //console.log(this.__brushPos);
  161. //console.log(this.__brushLastPos);
  162. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  163. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  164. var sx = (e.isCombo) ? Math.floor((this.__brushLastPos[0] - this.__offset[0]) * (1.0 / this.__scale)) : x;
  165. var sy = (e.isCombo) ? Math.floor((this.__brushLastPos[1] - this.__offset[1]) * (1.0 / this.__scale)) : y;
  166. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  167. if (!this.__snapshotTriggered){
  168. this.__snapshotTriggered = true;
  169. this.__surface.snapshot();
  170. }
  171. LineToSurface(sx, sy, x, y, this.__brushColor, this.__brushPalette);
  172. }
  173. }
  174. }
  175. }).bind(this);
  176. input.listen("mouseclick", handle_draw);
  177. input.listen("mouseleft+mousemove", handle_draw);
  178. var handle_mouseup = (function(e){
  179. this.__snapshotTriggered = false;
  180. }).bind(this);
  181. input.listen("mouseup", handle_mouseup);
  182. var handle_undo = (function(e){
  183. if (this.__surface !== null){
  184. this.__surface.undo();
  185. }
  186. }).bind(this);
  187. input.listen("ctrl+z", handle_undo);
  188. var handle_redo = (function(e){
  189. if (this.__surface !== null){
  190. this.__surface.redo();
  191. }
  192. }).bind(this);
  193. input.listen("ctrl+y", handle_redo);
  194. var handle_offset = (function(e){
  195. this.__offset[0] += e.x - e.lastX;
  196. this.__offset[1] += e.y - e.lastY;
  197. RenderD();
  198. }).bind(this);
  199. input.listen("shift+mouseleft+mousemove", handle_offset);
  200. var handle_scale = (function(e){
  201. if (e.delta < 0){
  202. this.scale_down();
  203. } else if (e.delta > 0){
  204. this.scale_up();
  205. }
  206. if (e.delta !== 0)
  207. RenderD();
  208. }).bind(this);
  209. input.listen("wheel", handle_scale);
  210. var elscale = document.querySelector("#painter_scale");
  211. if (elscale){
  212. var self = this;
  213. elscale.addEventListener("change", function(e){
  214. var val = Number(this.value);
  215. self.scale = val;
  216. RenderD();
  217. });
  218. elscale.value = this.__scale;
  219. }
  220. var handle_fittocanvas = (function(){
  221. this.scale_to_fit();
  222. this.center_surface();
  223. RenderD();
  224. }).bind(this);
  225. GlobalEvents.listen("painter-fit-to-canvas", handle_fittocanvas);
  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. var elscale = document.querySelector("#painter_scale");
  266. if (elscale){
  267. elscale.value = this.__scale;
  268. }
  269. }
  270. get showGrid(){return this.__gridEnabled;}
  271. set showGrid(e){
  272. this.__gridEnabled = (e === true);
  273. }
  274. initialize(){
  275. if (canvas === null){
  276. canvas = document.getElementById(EL_CANVAS_ID);
  277. if (!canvas)
  278. throw new Error("Failed to obtain the canvas element.");
  279. context = canvas.getContext("2d");
  280. if (!context)
  281. throw new Error("Failed to obtain canvas context.");
  282. context.imageSmoothingEnabled = false;
  283. ResizeCanvasImg(canvas.clientWidth, canvas.clientHeight); // A forced "resize".
  284. input.enableMouseInput(true, canvas);
  285. input.enableKeyboardInput(true);
  286. //input.mouseTargetElement = canvas;
  287. this.scale_to_fit();
  288. this.center_surface();
  289. }
  290. return this;
  291. }
  292. scale_up(amount=1){
  293. this.scale = this.scale + (amount*0.1);
  294. return this;
  295. }
  296. scale_down(amount=1){
  297. this.scale = this.scale - (amount*0.1);
  298. return this;
  299. }
  300. center_surface(){
  301. if (canvas === null || this.__surface === null)
  302. return;
  303. this.__offset[0] = Math.floor((canvas.clientWidth - (this.__surface.width * this.__scale)) * 0.5);
  304. this.__offset[1] = Math.floor((canvas.clientHeight - (this.__surface.height * this.__scale)) * 0.5);
  305. return this;
  306. }
  307. scale_to_fit(){
  308. if (canvas === null || this.__surface === null)
  309. return;
  310. var sw = canvas.clientWidth / this.__surface.width;
  311. var sh = canvas.clientHeight / this.__surface.height;
  312. var s = Math.min(sw, sh).toString();
  313. var di = s.indexOf(".");
  314. if (di >= 0)
  315. s = s.slice(0, di+2);
  316. this.scale = Number(s);
  317. }
  318. render(){
  319. if (context === null){return this;}
  320. if (this.__surface === null){
  321. Renderer.clear(context, NESPalette.Default[4]);
  322. return this;
  323. }
  324. // Drawing the surface to the canvas.
  325. Renderer.render(
  326. this.__surface,
  327. 0, 0,
  328. this.__surface.width, this.__surface.height,
  329. this.__scale,
  330. context,
  331. this.__offset[0], this.__offset[1],
  332. !this.__onePaletteMode
  333. );
  334. context.save();
  335. // Draw the mouse position... if mouse is currently in bounds.
  336. if (input.isMouseInBounds()){
  337. context.fillStyle = "#AA9900";
  338. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0/this.__scale));
  339. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0/this.__scale));
  340. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  341. context.beginPath();
  342. context.rect(
  343. this.__offset[0] + (x*this.__scale),
  344. this.__offset[1] + (y*this.__scale),
  345. Math.ceil(this.__scale),
  346. Math.ceil(this.__scale)
  347. );
  348. context.fill();
  349. context.closePath();
  350. }
  351. }
  352. // Draw grid.
  353. if (this.__gridEnabled && this.__scale > 0.5){
  354. context.strokeStyle = "#00FF00";
  355. var w = this.__surface.width * this.__scale;
  356. var h = this.__surface.height * this.__scale;
  357. var length = Math.max(this.__surface.width, this.__surface.height);
  358. for (var i=0; i < length; i += 8){
  359. var x = (i*this.__scale) + this.__offset[0];
  360. var y = (i*this.__scale) + this.__offset[1];
  361. if (i < this.__surface.width){
  362. context.beginPath();
  363. context.moveTo(x, this.__offset[1]);
  364. context.lineTo(x, this.__offset[1] + h);
  365. context.stroke();
  366. context.closePath();
  367. }
  368. if (i < this.__surface.height){
  369. context.beginPath();
  370. context.moveTo(this.__offset[0], y);
  371. context.lineTo(this.__offset[0] + w, y);
  372. context.stroke();
  373. context.closePath();
  374. }
  375. }
  376. }
  377. context.restore();
  378. return this;
  379. }
  380. }
  381. const instance = new CTRLPainter();
  382. export default instance;