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

437 行
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. // var self = this;
  56. var RenderD = Utils.throttle((function(){
  57. this.render();
  58. }).bind(this), 20);
  59. var LineToSurface = (function(x0, y0, x1, y1, ci, pi){
  60. var dx = x1 - x0;
  61. var ix = Math.sign(dx);
  62. dx = 2 * Math.abs(dx);
  63. var dy = y1 - y0;
  64. var iy = Math.sign(dy);
  65. dy = 2 * Math.abs(dy);
  66. if (dx > dy){
  67. var err = dy - (dx/2);
  68. var y = y0;
  69. Utils.range(x0, x1, 1).forEach((x) => {
  70. this.__surface.setColorIndex(x, y, ci, pi);
  71. if (err > 0 || (err == 0 && ix > 0)){
  72. err -= dx;
  73. y += iy;
  74. }
  75. err += dy;
  76. });
  77. } else {
  78. var err = dx - (dy/2);
  79. var x = x0;
  80. Utils.range(y0, y1, 1).forEach((y) => {
  81. this.__surface.setColorIndex(x, y, ci, pi);
  82. if (err > 0 || (err == 0 && iy > 0)){
  83. err -= dy;
  84. x += ix;
  85. }
  86. err += dx;
  87. });
  88. }
  89. }).bind(this);
  90. var handle_resize = (function(w,h){
  91. RenderD();
  92. }).bind(this);
  93. GlobalEvents.listen("resize", handle_resize);
  94. var handle_palinfochanged = (function(pdat){
  95. RenderD();
  96. }).bind(this);
  97. var handle_setapppalette = (function(pal){
  98. if (this.__palette !== null)
  99. this.__palette.unlisten("palettes_changed", handle_palinfochanged);
  100. this.__palette = pal;
  101. this.__palette.listen("palettes_changed", handle_palinfochanged);
  102. if (this.__surface !== null){
  103. this.__surface.palette = pal;
  104. if (this.__onePaletteMode === false)
  105. RenderD();
  106. }
  107. }).bind(this);
  108. GlobalEvents.listen("set_app_palette", handle_setapppalette);
  109. var handle_surface_data_changed = (function(){
  110. RenderD();
  111. }).bind(this);
  112. var handle_change_surface = (function(surf){
  113. if (surf !== null && !(surf instanceof ISurface)){
  114. console.log("WARNING: Attempted to set painter to non-surface instance.");
  115. return;
  116. }
  117. if (surf !== this.__surface){
  118. if (this.__surface !== null){
  119. this.__surface.unlisten("data_changed", handle_surface_data_changed);
  120. }
  121. this.__surface = surf;
  122. if (this.__surface !== null){
  123. this.__surface.listen("data_changed", handle_surface_data_changed);
  124. if (this.__palette === null && this.__surface.palette !== null){
  125. this.__palette = this.__surface.palette;
  126. } else if (this.__palette !== null && this.__surface.palette !== this.__palette){
  127. this.__surface.palette = this.__palette;
  128. }
  129. this.scale_to_fit();
  130. this.center_surface();
  131. }
  132. }
  133. RenderD();
  134. }).bind(this);
  135. GlobalEvents.listen("change_surface", handle_change_surface);
  136. var handle_color_change = (function(pi, ci){
  137. this.__brushPalette = pi;
  138. this.__brushColor = ci;
  139. }).bind(this);
  140. GlobalEvents.listen("active_palette_color", handle_color_change);
  141. var handle_mousemove = (function(e){
  142. this.__brushLastPos[0] = this.__brushPos[0];
  143. this.__brushLastPos[1] = this.__brushPos[1];
  144. this.__brushPos[0] = e.x;
  145. this.__brushPos[1] = e.y;
  146. if (this.__surface !== null){
  147. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  148. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  149. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  150. RenderD();
  151. }
  152. }
  153. }).bind(this);
  154. input.listen("mousemove", handle_mousemove);
  155. input.listen("mouseleft+mousemove", handle_mousemove);
  156. var handle_draw = (function(e){
  157. if (e.isCombo || e.button == 0){
  158. if (this.__surface !== null){
  159. //console.log(this.__brushPos);
  160. //console.log(this.__brushLastPos);
  161. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0 / this.__scale));
  162. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0 / this.__scale));
  163. var sx = (e.isCombo) ? Math.floor((this.__brushLastPos[0] - this.__offset[0]) * (1.0 / this.__scale)) : x;
  164. var sy = (e.isCombo) ? Math.floor((this.__brushLastPos[1] - this.__offset[1]) * (1.0 / this.__scale)) : y;
  165. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  166. LineToSurface(sx, sy, x, y, this.__brushColor, this.__brushPalette);
  167. //RenderD();
  168. }
  169. }
  170. }
  171. }).bind(this);
  172. input.listen("mouseclick", handle_draw);
  173. input.listen("mouseleft+mousemove", handle_draw);
  174. var handle_offset = (function(e){
  175. this.__offset[0] += e.x - e.lastX;
  176. this.__offset[1] += e.y - e.lastY;
  177. RenderD();
  178. }).bind(this);
  179. input.listen("shift+mouseleft+mousemove", handle_offset);
  180. var handle_scale = (function(e){
  181. if (e.delta < 0){
  182. this.scale_down();
  183. } else if (e.delta > 0){
  184. this.scale_up();
  185. }
  186. if (e.delta !== 0)
  187. RenderD();
  188. }).bind(this);
  189. input.listen("wheel", handle_scale);
  190. var elscale = document.querySelector("#painter_scale");
  191. if (elscale){
  192. var self = this;
  193. elscale.addEventListener("change", function(e){
  194. var val = Number(this.value);
  195. self.scale = val;
  196. RenderD();
  197. });
  198. elscale.value = this.__scale;
  199. }
  200. var handle_fittocanvas = (function(){
  201. this.scale_to_fit();
  202. this.center_surface();
  203. RenderD();
  204. }).bind(this);
  205. GlobalEvents.listen("painter-fit-to-canvas", handle_fittocanvas);
  206. var handle_togglegrid = (function(target, args){
  207. if (args.show){
  208. target.classList.add("pure-button-active");
  209. target.setAttribute("emit-args", JSON.stringify({show:false}));
  210. this.__gridEnabled = true;
  211. } else {
  212. target.classList.remove("pure-button-active");
  213. target.setAttribute("emit-args", JSON.stringify({show:true}));
  214. this.__gridEnabled = false;
  215. }
  216. RenderD();
  217. }).bind(this);
  218. GlobalEvents.listen("painter-togglegrid", handle_togglegrid);
  219. var handle_colormode = (function(target, args){
  220. if (args.onePaletteMode){
  221. target.classList.remove("pure-button-active");
  222. target.setAttribute("emit-args", JSON.stringify({onePaletteMode:false}));
  223. this.__onePaletteMode = true;
  224. } else {
  225. target.classList.add("pure-button-active");
  226. target.setAttribute("emit-args", JSON.stringify({onePaletteMode:true}));
  227. this.__onePaletteMode = false;
  228. }
  229. RenderD();
  230. }).bind(this);
  231. GlobalEvents.listen("painter-colormode", handle_colormode);
  232. }
  233. get onePaletteMode(){return this.__onePaletteMode;}
  234. set onePaletteMode(e){
  235. this.__onePaletteMode = (e === true);
  236. this.render();
  237. }
  238. get scale(){
  239. return this.__scale;
  240. }
  241. set scale(s){
  242. if (typeof(s) !== 'number')
  243. throw new TypeError("Expected number value.");
  244. this.__scale = Math.max(0.1, Math.min(100.0, s));
  245. var elscale = document.querySelector("#painter_scale");
  246. if (elscale){
  247. elscale.value = this.__scale;
  248. }
  249. }
  250. get showGrid(){return this.__gridEnabled;}
  251. set showGrid(e){
  252. this.__gridEnabled = (e === true);
  253. }
  254. initialize(){
  255. if (canvas === null){
  256. canvas = document.getElementById(EL_CANVAS_ID);
  257. if (!canvas)
  258. throw new Error("Failed to obtain the canvas element.");
  259. context = canvas.getContext("2d");
  260. if (!context)
  261. throw new Error("Failed to obtain canvas context.");
  262. context.imageSmoothingEnabled = false;
  263. ResizeCanvasImg(canvas.clientWidth, canvas.clientHeight); // A forced "resize".
  264. input.enableMouseInput(true, canvas);
  265. //input.mouseTargetElement = canvas;
  266. this.scale_to_fit();
  267. this.center_surface();
  268. }
  269. return this;
  270. }
  271. scale_up(amount=1){
  272. this.scale = this.scale + (amount*0.1);
  273. return this;
  274. }
  275. scale_down(amount=1){
  276. this.scale = this.scale - (amount*0.1);
  277. return this;
  278. }
  279. center_surface(){
  280. if (canvas === null || this.__surface === null)
  281. return;
  282. this.__offset[0] = Math.floor((canvas.clientWidth - (this.__surface.width * this.__scale)) * 0.5);
  283. this.__offset[1] = Math.floor((canvas.clientHeight - (this.__surface.height * this.__scale)) * 0.5);
  284. return this;
  285. }
  286. scale_to_fit(){
  287. if (canvas === null || this.__surface === null)
  288. return;
  289. var sw = canvas.clientWidth / this.__surface.width;
  290. var sh = canvas.clientHeight / this.__surface.height;
  291. var s = Math.min(sw, sh).toString();
  292. var di = s.indexOf(".");
  293. if (di >= 0)
  294. s = s.slice(0, di+2);
  295. this.scale = Number(s);
  296. }
  297. render(){
  298. if (context === null){return this;}
  299. if (this.__surface === null){
  300. Renderer.clear(context, NESPalette.Default[4]);
  301. return this;
  302. }
  303. // Drawing the surface to the canvas.
  304. Renderer.render(
  305. this.__surface,
  306. 0, 0,
  307. this.__surface.width, this.__surface.height,
  308. this.__scale,
  309. context,
  310. this.__offset[0], this.__offset[1],
  311. !this.__onePaletteMode
  312. );
  313. context.save();
  314. // Draw the mouse position... if mouse is currently in bounds.
  315. if (input.isMouseInBounds()){
  316. context.fillStyle = "#AA9900";
  317. var x = Math.floor((this.__brushPos[0] - this.__offset[0]) * (1.0/this.__scale));
  318. var y = Math.floor((this.__brushPos[1] - this.__offset[1]) * (1.0/this.__scale));
  319. if (x >= 0 && x < this.__surface.width && y >= 0 && y < this.__surface.height){
  320. context.beginPath();
  321. context.rect(
  322. this.__offset[0] + (x*this.__scale),
  323. this.__offset[1] + (y*this.__scale),
  324. Math.ceil(this.__scale),
  325. Math.ceil(this.__scale)
  326. );
  327. context.fill();
  328. context.closePath();
  329. }
  330. }
  331. // Draw grid.
  332. if (this.__gridEnabled && this.__scale > 0.5){
  333. context.strokeStyle = "#00FF00";
  334. var w = this.__surface.width * this.__scale;
  335. var h = this.__surface.height * this.__scale;
  336. var length = Math.max(this.__surface.width, this.__surface.height);
  337. for (var i=0; i < length; i += 8){
  338. var x = (i*this.__scale) + this.__offset[0];
  339. var y = (i*this.__scale) + this.__offset[1];
  340. if (i < this.__surface.width){
  341. context.beginPath();
  342. context.moveTo(x, this.__offset[1]);
  343. context.lineTo(x, this.__offset[1] + h);
  344. context.stroke();
  345. context.closePath();
  346. }
  347. if (i < this.__surface.height){
  348. context.beginPath();
  349. context.moveTo(this.__offset[0], y);
  350. context.lineTo(this.__offset[0] + w, y);
  351. context.stroke();
  352. context.closePath();
  353. }
  354. }
  355. }
  356. context.restore();
  357. return this;
  358. }
  359. }
  360. const instance = new CTRLPainter();
  361. export default instance;