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

287 行
6.7KB

  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 EditableText from "/app/js/ui/EditableText.js";
  5. import Renderer from "/app/js/ui/Renderer.js";
  6. import NESBank from "/app/js/models/NESBank.js";
  7. import NESPalette from "/app/js/models/NESPalette.js";
  8. const BLI_TEMPLATE = "bank-list-item-template";
  9. const BLI_CANVAS = "bank-img";
  10. const BLI_TITLE = "title";
  11. const BLI_SELECTED = "list-item-selected";
  12. var Banks = {};
  13. var CurrentBank = "";
  14. const SCHEMA_ID="http://nespaint/BanksStoreSchema.json";
  15. JSONSchema.add({
  16. "$schema": "http://json-schema.org/draft-07/schema#",
  17. "$id": SCHEMA_ID,
  18. "type": "array",
  19. "items":{
  20. "type": "object",
  21. "properties":{
  22. "name":{
  23. "type": "string",
  24. "minLength": 1
  25. },
  26. "data":{
  27. "type": "string",
  28. "media": {
  29. "binaryEncoding": "base64"
  30. }
  31. }
  32. },
  33. "required":["name", "data"]
  34. }
  35. });
  36. function HANDLE_BankClick(e){
  37. var name = this.getAttribute("bankname");
  38. if (name !== CurrentBank){
  39. if (CurrentBank !== "")
  40. Banks[CurrentBank].el.classList.remove(BLI_SELECTED);
  41. CurrentBank = name;
  42. Banks[CurrentBank].el.classList.add(BLI_SELECTED);
  43. GlobalEvents.emit("change_surface", Banks[CurrentBank].bank);
  44. }
  45. }
  46. function SetElBankName(el, name){
  47. var et = new EditableText(el, "title");
  48. et.listen("value_change", (v) => {el.setAttribute("bankname", v);});
  49. et.value = name;
  50. return et;
  51. //var sel = el.querySelector("." + BLI_TITLE);
  52. //if (sel){
  53. // sel.innerHTML = name;
  54. //}
  55. }
  56. var RenderBankToEl = Utils.throttle(function(el, bank){
  57. var cnv = el.querySelector("." + BLI_CANVAS);
  58. var ctx = cnv.getContext("2d");
  59. Renderer.renderToFit(bank, ctx);
  60. }, 500); // Only update twice a second.
  61. function HANDLE_BankDataChange(bank, e){
  62. RenderBankToEl(this, bank);
  63. }
  64. function ConnectElementToBank(el, bank){
  65. bank.listen("data_changed", HANDLE_BankDataChange.bind(el, bank));
  66. }
  67. function CreateBankDOMEntry(name, bank){
  68. var baseel = document.querySelector("." + BLI_TEMPLATE);
  69. if (!baseel){
  70. console.log("WARNING: Failed to find bank list item template.");
  71. return null;
  72. }
  73. var el = baseel.cloneNode(true);
  74. el.classList.remove(BLI_TEMPLATE);
  75. el.classList.remove("hidden");
  76. el.setAttribute("bankname", name);
  77. ConnectElementToBank(el, bank);
  78. el.addEventListener("click", HANDLE_BankClick);
  79. baseel.parentNode.appendChild(el);
  80. setTimeout(()=>{
  81. RenderBankToEl(el, bank);
  82. }, 500); // Make the render call in about a half second. Allow DOM time to catch up?
  83. return el;
  84. }
  85. class CTRLBanksStore{
  86. constructor(){
  87. var HANDLE_ChangeSurface = function(surf){
  88. if (!(surf instanceof NESBank)){
  89. if (CurrentBank !== ""){
  90. Banks[CurrentBank].el.classList.remove(BLI_SELECTED);
  91. CurrentBank = "";
  92. }
  93. } else {
  94. if (Banks.length <= 0 || (CurrentBank !== "" && Banks[CurrentBank].bank !== surf)){
  95. console.log("WARNING: Bank object being set outside of Bank Store.");
  96. }
  97. }
  98. }
  99. GlobalEvents.listen("change_surface", HANDLE_ChangeSurface);
  100. GlobalEvents.listen("bankstore-add", (function(ev){
  101. /*if (e.hasOwnProperty("bankname")){
  102. this.createBank(e.bankname);
  103. this.activateBank(e.bankname);
  104. }*/
  105. GlobalEvents.emit("modal-close");
  106. var e = document.querySelector(".banks-store-add");
  107. if (e){
  108. var eform = e.querySelector("form");
  109. var einput = e.querySelector('input[name="storeitemname"]');
  110. if (eform && einput){
  111. var name = einput.value;
  112. eform.reset();
  113. this.createBank(name);
  114. this.activateBank(name);
  115. }
  116. }
  117. }).bind(this));
  118. GlobalEvents.listen("bankstore-remove", (function(e){
  119. if (CurrentBank !== "")
  120. this.removeBank(CurrentBank);
  121. }).bind(this));
  122. }
  123. get length(){
  124. return Object.keys(Banks).length;
  125. }
  126. get obj(){
  127. var data = [];
  128. Object.keys(Banks).forEach((key) => {
  129. if (Banks.hasOwnProperty(key)){
  130. data.push({name:key, data:Banks[key].bank.base64});
  131. }
  132. });
  133. return data;
  134. }
  135. set obj(d){
  136. var validator = JSONSchema.getValidator(SCHEMA_ID);
  137. if (validator !== null && validator(d)){
  138. this.clear();
  139. d.forEach((item) => {
  140. this.createBank(item.name, item.data);
  141. });
  142. } else {
  143. var errs = JSONSchema.getLastErrors();
  144. if (errs !== null){
  145. console.log(errs);
  146. }
  147. throw new Error("Object failed to validate against BanksStoreSchema.");
  148. }
  149. }
  150. get json(){
  151. return JSON.stringify(this.obj);
  152. }
  153. set json(j){
  154. try {
  155. this.obj = JSON.parse(j);
  156. } catch (e) {
  157. throw e;
  158. }
  159. }
  160. get currentBank(){
  161. return (CurrentBank === "") ? null : Banks[CurrentBank].bank;
  162. }
  163. get currentBankName(){
  164. return CurrentBank;
  165. }
  166. initialize(){
  167. if (this.length <= 0){
  168. this.createBank("Bank");
  169. }
  170. return this;
  171. }
  172. createBank(name, bbase64){
  173. if (!(name in Banks)){
  174. var bank = new NESBank();
  175. if (typeof(bbase64) === "string"){
  176. try {
  177. bank.base64 = bbase64;
  178. } catch (e) {
  179. console.log("Failed to create Bank. " + e.toString());
  180. bank = null;
  181. }
  182. }
  183. if (bank !== null){
  184. var el = CreateBankDOMEntry(name, bank);
  185. if (el){
  186. var elname = SetElBankName(el, name);
  187. Banks[name] = {bank:bank, el:el, elname:elname};
  188. if (this.length <= 1){
  189. Banks[name].el.click();
  190. }
  191. }
  192. }
  193. }
  194. return this;
  195. }
  196. removeBank(name){
  197. if (name in Banks){
  198. if (name === CurrentBank){
  199. var keys = Object.keys(Banks);
  200. if (keys.length > 1){
  201. CurrentBank = (keys[0] !== name) ? keys[0] : keys[1];
  202. } else {
  203. CurrentBank = "";
  204. }
  205. }
  206. Banks[name].el.parentNode.removeChild(Banks[name].el);
  207. delete Banks[name];
  208. if (CurrentBank !== ""){
  209. Banks[CurrentBank].el.click();
  210. } else {
  211. GlobalEvents.emit("change_surface", null);
  212. }
  213. }
  214. return this;
  215. }
  216. renameBank(name, newname){
  217. if ((name in Banks) && !(newname in Banks)){
  218. Banks[newname] = Banks[name];
  219. Banks[newname].elname.value = newname;
  220. delete Banks[name];
  221. }
  222. return this;
  223. }
  224. activateBank(name){
  225. if (CurrentBank !== name && (name in Banks)){
  226. Banks[name].el.click();
  227. }
  228. return this;
  229. }
  230. clear(){
  231. Object.keys(Banks).forEach((item) => {
  232. Banks[item].el.parentNode.removeChild(Banks[item].el);
  233. });
  234. Banks = {};
  235. if (CurrentBank !== ""){
  236. CurrentBank = "";
  237. GlobalEvents.emit("change_surface", null);
  238. }
  239. }
  240. }
  241. const instance = new CTRLBanksStore();
  242. export default instance;