A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.2KB

  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 NESTile from "/app/js/models/NESTile.js";
  5. import NESBank from "/app/js/models/NESBank.js";
  6. const EL_CANVAS_ID = "painter";
  7. /* --------------------------------------------------------------------
  8. * Univeral data and functions.
  9. ------------------------------------------------------------------- */
  10. var canvas = null;
  11. var context = null;
  12. // Handling window resize events...
  13. var HANDLE_Resize = Utils.debounce(function(e){
  14. if (canvas !== null){
  15. var w = canvas.clientWidth;
  16. var h = canvas.clientHeight;
  17. canvas.width = w;
  18. canvas.height = h;
  19. }
  20. }, 250);
  21. window.addEventListener("resize", HANDLE_Resize);
  22. // Setting-up Input controls.
  23. var input = new Input();
  24. input.preventDefaults = true;
  25. // Mouse handling...
  26. /*input.listen("mousemove", handle_mouseevent);
  27. input.listen("mousedown", handle_mouseevent);
  28. input.listen("mouseup", handle_mouseevent);
  29. input.listen("mouseclick", handle_mouseclickevent);
  30. */
  31. /* --------------------------------------------------------------------
  32. * CTRLPainter
  33. * Actual controlling class.
  34. ------------------------------------------------------------------- */
  35. // For reference...
  36. // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke
  37. class CTRLPainter {
  38. constructor(){
  39. this.__scale = 1.0; // This is the scale the painter will display source information.
  40. this.__offset = [0.0, 0.0]; // This is the X,Y offset from origin to display source information.
  41. this.__onePaletteMode = true; // If true, ALL tiles will be drawing using the same palette.
  42. this.__brushSize = 1;
  43. this.__brushColor = 0;
  44. this.__brushPalette = 0;
  45. this.__surface = null;
  46. var self = this;
  47. var handle_change_surface = (function(surf){
  48. if (!(surf instanceof ISurface)){
  49. console.log("WARNING: Attempted to set painter to non-surface instance.");
  50. return;
  51. }
  52. this.__surface = surf;
  53. // TODO: Call a rerender
  54. }).bind(this);
  55. GlobalEvents.listen("change_surface", handle_change_surface);
  56. var handle_color_change = (function(pi, ci){
  57. this.__brushPalette = pi;
  58. this.__brushColor = ci;
  59. }).bind(this);
  60. GlobalEvents.listen("active_palette_color", handle_color_change);
  61. }
  62. get onePaletteMode(){return this.__onePaletteMode;}
  63. set onePaletteMode(e){
  64. this.__onePaletteMode = (e === true);
  65. // TODO: Call a rerender.
  66. }
  67. get scale(){
  68. return this.__scale;
  69. }
  70. set scale(s){
  71. if (typeof(s) !== 'number')
  72. throw new TypeError("Expected number value.");
  73. this.__scale = Math.max(0.1, Math.min(100.0, s));
  74. }
  75. initialize(){
  76. if (canvas === null){
  77. canvas = document.getElementById(EL_CANVAS_ID);
  78. if (!canvas)
  79. throw new Error("Failed to obtain the canvas element.");
  80. context = canvas.getContext("2d");
  81. if (!context)
  82. throw new Error("Failed to obtain canvas context.");
  83. input.mouseTargetElement = canvas;
  84. }
  85. }
  86. scale_up(amount=1){
  87. this.scale = this.scale + (amount*0.1);
  88. }
  89. scale_down(amount=1){
  90. this.scale = this.scale - (amount*0.1);
  91. }
  92. }
  93. const instance = new CTRLPainter();
  94. export default instance;