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

283 行
7.8KB

  1. import GlobalEvents from "/app/js/common/EventCaller.js";
  2. import Utils from "/app/js/common/Utils.js";
  3. import JSONSchema from "/app/js/common/JSONSchema.js";
  4. import NESBank from "/app/js/models/NESBank.js";
  5. import NESPalette from "/app/js/models/NESPalette.js";
  6. import NESNameTable from "/app/js/models/NESNameTable.js";
  7. import CTRLPalettesStore from "/app/js/ctrls/CTRLPalettesStore.js";
  8. import CTRLBanksStore from "/app/js/ctrls/CTRLBanksStore.js";
  9. import CTRLNameTablesStore from "/app/js/ctrls/CTRLNameTablesStore.js";
  10. const SUPPORTED_PROJECT_VERSIONS=[
  11. "0.1"
  12. ];
  13. const PROJECT_ID="NESPProj"
  14. const SCHEMA_ID = "http://nespaint/Project.json";
  15. JSONSchema.add({
  16. "$schema": "http://json-schema.org/draft-07/schema#",
  17. "$id": SCHEMA_ID,
  18. "type":"object",
  19. "properties":{
  20. "id":{
  21. "type":"string",
  22. "enum":["NESPProj"]
  23. },
  24. "version":{
  25. "type":"string",
  26. "pattern":"^[0-9]{1,}\.[0-9]{1,}$"
  27. },
  28. "paletteStore":{"$ref":"http://nespaint/PalettesStoreSchema.json"},
  29. "bankStore":{"$ref":"http://nespaint/BanksStoreSchema.json"},
  30. "nametableStore":{"$ref":"http://nespaint/NametableStoreSchema.json"}
  31. },
  32. "required":["id","version","paletteStore","bankStore"]
  33. });
  34. var SURF = null;
  35. function JSONFromProject(){
  36. var proj = {
  37. id:PROJECT_ID,
  38. version:SUPPORTED_PROJECT_VERSIONS[SUPPORTED_PROJECT_VERSIONS.length - 1],
  39. paletteStore:CTRLPalettesStore.obj,
  40. bankStore:CTRLBanksStore.obj
  41. };
  42. if (CTRLNameTablesStore.keys.length > 0)
  43. proj.nametableStore = CTRLNameTablesStore.obj;
  44. return JSON.stringify(proj);
  45. }
  46. function RequestDownload(filename, datblob){
  47. var a = document.createElement("a");
  48. a.href = window.URL.createObjectURL(datblob);
  49. a.download = filename;
  50. var body = document.querySelector("body");
  51. body.appendChild(a);
  52. a.click();
  53. setTimeout(function(){ // fixes firefox html removal bug
  54. window.URL.revokeObjectURL(a.href);
  55. a.remove();
  56. }, 500);
  57. }
  58. function LoadFile(file){
  59. if (SURF !== null){
  60. var reader = new FileReader();
  61. if (SURF instanceof NESBank){
  62. reader.onload = function(e){
  63. try {
  64. SURF.chr = new Uint8Array(e.target.result);
  65. } catch (e) {
  66. console.log(e.toString());
  67. }
  68. }
  69. reader.readAsArrayBuffer(file);
  70. }
  71. }
  72. }
  73. function HANDLE_DragOver(e){
  74. e.stopPropagation();
  75. e.preventDefault();
  76. e.dataTransfer.dropEffect = 'copy';
  77. };
  78. function HANDLE_FileDrop(e){
  79. e.stopPropagation();
  80. e.preventDefault();
  81. var files = e.dataTransfer.files;
  82. for (let i=0; i < files.length; i++){
  83. LoadFile(files[i]);
  84. }
  85. }
  86. function HANDLE_SaveProject(e){
  87. //var a = document.createElement("a");
  88. var file = new Blob([JSONFromProject()], {type: "text/plain"});
  89. RequestDownload("nesproject.json", file);
  90. }
  91. function HANDLE_LoadProjectRequest(){
  92. var input = document.querySelectorAll("input.project-loader");
  93. if (input.length > 0){
  94. input[0].click();
  95. }
  96. }
  97. function HANDLE_LoadProject(e){
  98. if (this.files && this.files.length > 0){
  99. var reader = new FileReader();
  100. reader.onload = (function(e) {
  101. var o = null;
  102. var validator = JSONSchema.getValidator(SCHEMA_ID);
  103. try {
  104. o = JSON.parse(e.target.result);
  105. } catch (e) {
  106. console.log("Failed to parse JSON string. " + e.toString());
  107. }
  108. if (validator !== null && validator(o)){
  109. if (o.id !== PROJECT_ID){
  110. console.log("ERROR: Project ID is invalid. Canceling project load.");
  111. return;
  112. }
  113. if (SUPPORTED_PROJECT_VERSIONS.indexOf(o.version) < 0){
  114. console.log("ERROR: Project file version does not match any supported version numbers. Canceling project load.");
  115. return;
  116. }
  117. // NOTE: For future... branch out to specific version loaders as needed.
  118. // Since only one version is currently supported, however, just continue on, my friends!
  119. CTRLPalettesStore.obj = o.paletteStore;
  120. CTRLBanksStore.obj = o.bankStore;
  121. if ("nametableStore" in o)
  122. CTRLNameTablesStore.obj = o.nametableStore;
  123. }
  124. if (this.parentNode.nodeName.toLowerCase() === "form"){
  125. this.parentNode.reset();
  126. } else {
  127. console.log("WARNING: Parent node is NOT a <form> element.");
  128. }
  129. }).bind(this);
  130. reader.readAsText(this.files[0]);
  131. } else {
  132. console.log("Project file not found or no file selected.");
  133. }
  134. }
  135. function HANDLE_ExportCHR(e){
  136. e.preventDefault();
  137. var bank = CTRLBanksStore.currentBank;
  138. if (bank !== null){
  139. var dat = null;
  140. var size = document.querySelector('input[name="exportchr-size"]:checked').value;
  141. switch (size){
  142. case "full":
  143. dat = bank.getCHR(0,0);
  144. break;
  145. case "current":
  146. dat = bank.chr;
  147. }
  148. if (dat !== null){
  149. var file = new Blob([dat], {type:"application/octet-stream"});
  150. var filename = CTRLBanksStore.currentBankName.replace(/[^a-z0-9\-_.]/gi, '_').toLowerCase() + ".chr";
  151. RequestDownload(filename, file);
  152. }
  153. }
  154. GlobalEvents.emit("modal-close");
  155. }
  156. function HANDLE_ExportPalASM(e){
  157. var pal = CTRLPalettesStore.currentPalette;
  158. var palname = CTRLPalettesStore.currentPaletteName.replace(/[^a-z0-9\-_.]/gi, '_');
  159. if (pal !== null && palname !== ""){
  160. var asmtxt = pal.to_asm(palname);
  161. var file = new Blob([asmtxt], {type: "text/plain"});
  162. RequestDownload(palname + ".asm", file);
  163. }
  164. }
  165. function HANDLE_ExportNameTableASM(e){
  166. var nt = CTRLNameTablesStore.currentNametable;
  167. if (nt !== null){
  168. var basename = CTRLNameTablesStore.currentNTName.replace(/[^a-z0-9\-_.]/gi, '_');
  169. var mode = document.querySelector('input[name="exportnt-op"]:checked').value;
  170. var asm = "";
  171. switch (mode){
  172. case "both":
  173. asm = nt.to_asm(basename + "_NT", basename + "_AT");
  174. break;
  175. case "nametable":
  176. asm = nt.nametable_asm(basename + "_NT");
  177. break;
  178. case "attribtable":
  179. asm = nt.attribtable_asm(basename + "_AT");
  180. break;
  181. }
  182. if (asm !== ""){
  183. var file = new Blob([asm], {type: "text/plain"});
  184. RequestDownload(basename + ".asm", file);
  185. }
  186. }
  187. }
  188. function HANDLE_SurfChange(surf){
  189. var enableclass = "";
  190. if (surf instanceof NESBank){
  191. SURF = surf;
  192. enableclass = "surf-bank";
  193. } else {
  194. SURF = null;
  195. if (surf instanceof NESNameTable)
  196. enableclass="surf-nametable";
  197. }
  198. var e = document.querySelectorAll(".surf-export");
  199. for (let i=0; i < e.length; i++){
  200. var ea = e[i].querySelector("a");
  201. if (ea){
  202. if (e[i].classList.contains(enableclass)){
  203. e[i].classList.remove("disable-links");
  204. ea.classList.remove("pure-menu-disabled");
  205. } else {
  206. e[i].classList.add("disable-links");
  207. ea.classList.add("pure-menu-disabled");
  208. }
  209. }
  210. }
  211. }
  212. class CTRLIO{
  213. constructor(){
  214. GlobalEvents.listen("change_surface", HANDLE_SurfChange);
  215. GlobalEvents.listen("save-project", HANDLE_SaveProject);
  216. GlobalEvents.listen("load-project", HANDLE_LoadProjectRequest);
  217. GlobalEvents.listen("export-pal-asm", HANDLE_ExportPalASM);
  218. GlobalEvents.listen("export-nametable", HANDLE_ExportNameTableASM);
  219. var input = document.querySelectorAll("input.project-loader");
  220. if (input.length > 0){
  221. input[0].addEventListener("change", HANDLE_LoadProject);
  222. }
  223. }
  224. initialize(){
  225. // Connecting drag/drop ability.
  226. var e = document.querySelectorAll(".drop-zone");
  227. for (let i=0; i < e.length; e++){
  228. e[i].addEventListener("dragover", HANDLE_DragOver);
  229. e[i].addEventListener("drop", HANDLE_FileDrop);
  230. }
  231. // Connecting to button for export request.
  232. var e = document.querySelectorAll(".export-chr-btn");
  233. for (let i=0; i < e.length; i++){
  234. e[i].addEventListener("click", HANDLE_ExportCHR);
  235. }
  236. CTRLPalettesStore.initialize();
  237. CTRLBanksStore.initialize();
  238. CTRLNameTablesStore.initialize();
  239. }
  240. }
  241. const instance = new CTRLIO();
  242. export default instance;