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 символів.

243 lines
6.5KB

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