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文字以内のものにしてください。

275 行
6.3KB

  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(e){
  101. if (e.hasOwnProperty("bankname")){
  102. this.createBank(e.bankname);
  103. this.activateBank(e.bankname);
  104. }
  105. }).bind(this));
  106. GlobalEvents.listen("bankstore-remove", (function(e){
  107. if (CurrentBank !== "")
  108. this.removeBank(CurrentBank);
  109. }).bind(this));
  110. }
  111. get length(){
  112. return Object.keys(Banks).length;
  113. }
  114. get obj(){
  115. var data = [];
  116. Object.keys(Banks).forEach((key) => {
  117. if (Banks.hasOwnProperty(key)){
  118. data.push({name:key, data:Banks[key].bank.base64});
  119. }
  120. });
  121. return data;
  122. }
  123. set obj(d){
  124. var validator = JSONSchema.getValidator(SCHEMA_ID);
  125. if (validator !== null && validator(d)){
  126. this.clear();
  127. d.forEach((item) => {
  128. this.createBank(item.name, item.data);
  129. });
  130. } else {
  131. var errs = JSONSchema.getLastErrors();
  132. if (errs !== null){
  133. console.log(errs);
  134. }
  135. throw new Error("Object failed to validate against BanksStoreSchema.");
  136. }
  137. }
  138. get json(){
  139. return JSON.stringify(this.obj);
  140. }
  141. set json(j){
  142. try {
  143. this.obj = JSON.parse(j);
  144. } catch (e) {
  145. throw e;
  146. }
  147. }
  148. get currentBank(){
  149. return (CurrentBank === "") ? null : Banks[CurrentBank].bank;
  150. }
  151. get currentBankName(){
  152. return CurrentBank;
  153. }
  154. initialize(){
  155. if (this.length <= 0){
  156. this.createBank("Bank");
  157. }
  158. return this;
  159. }
  160. createBank(name, bbase64){
  161. if (!(name in Banks)){
  162. var bank = new NESBank();
  163. if (typeof(bbase64) === "string"){
  164. try {
  165. bank.base64 = bbase64;
  166. } catch (e) {
  167. console.log("Failed to create Bank. " + e.toString());
  168. bank = null;
  169. }
  170. }
  171. if (bank !== null){
  172. var el = CreateBankDOMEntry(name, bank);
  173. if (el){
  174. var elname = SetElBankName(el, name);
  175. Banks[name] = {bank:bank, el:el, elname:elname};
  176. if (this.length <= 1){
  177. Banks[name].el.click();
  178. }
  179. }
  180. }
  181. }
  182. return this;
  183. }
  184. removeBank(name){
  185. if (name in Banks){
  186. if (name === CurrentBank){
  187. var keys = Object.keys(Banks);
  188. if (keys.length > 1){
  189. CurrentBank = (keys[0] !== name) ? keys[0] : keys[1];
  190. } else {
  191. CurrentBank = "";
  192. }
  193. }
  194. Banks[name].el.parentNode.removeChild(Banks[name].el);
  195. delete Banks[name];
  196. if (CurrentBank !== ""){
  197. Banks[CurrentBank].el.click();
  198. } else {
  199. GlobalEvents.emit("change_surface", null);
  200. }
  201. }
  202. return this;
  203. }
  204. renameBank(name, newname){
  205. if ((name in Banks) && !(newname in Banks)){
  206. Banks[newname] = Banks[name];
  207. Banks[newname].elname.value = newname;
  208. delete Banks[name];
  209. }
  210. return this;
  211. }
  212. activateBank(name){
  213. if (CurrentBank !== name && (name in Banks)){
  214. Banks[name].el.click();
  215. }
  216. return this;
  217. }
  218. clear(){
  219. Object.keys(Banks).forEach((item) => {
  220. Banks[item].el.parentNode.removeChild(Banks[item].el);
  221. });
  222. Banks = {};
  223. if (CurrentBank !== ""){
  224. CurrentBank = "";
  225. GlobalEvents.emit("change_surface", null);
  226. }
  227. }
  228. }
  229. const instance = new CTRLBanksStore();
  230. export default instance;