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

196 行
4.7KB

  1. import GlobalEvents from "/app/js/common/EventCaller.js";
  2. import Utils from "/app/js/common/Utils.js";
  3. import Renderer from "/app/js/ui/Renderer.js";
  4. import NESBank from "/app/js/models/NESBank.js";
  5. import NESPalette from "/app/js/models/NESPalette.js";
  6. const BLI_TEMPLATE = "bank-list-item-template";
  7. const BLI_CANVAS = "bank-img";
  8. const BLI_TITLE = "title";
  9. const BLI_SELECTED = "list-item-selected";
  10. var Banks = {};
  11. var CurrentBank = "";
  12. function HANDLE_BankClick(e){
  13. var name = this.getAttribute("bankname");
  14. if (name !== CurrentBank){
  15. if (CurrentBank !== "")
  16. Banks[CurrentBank].el.classList.remove(BLI_SELECTED);
  17. CurrentBank = name;
  18. Banks[CurrentBank].el.classList.add(BLI_SELECTED);
  19. GlobalEvents.emit("change_surface", Banks[CurrentBank].bank);
  20. }
  21. }
  22. function SetElBankName(el, name){
  23. el.setAttribute("bankname", name);
  24. var sel = el.querySelector("." + BLI_TITLE);
  25. if (sel){
  26. sel.innerHTML = name;
  27. }
  28. }
  29. var RenderBankToEl = Utils.throttle(function(el, bank){
  30. var cnv = el.querySelector("." + BLI_CANVAS);
  31. var ctx = cnv.getContext("2d");
  32. Renderer.renderToFit(bank, ctx);
  33. }, 500); // Only update twice a second.
  34. function HANDLE_BankDataChange(bank, e){
  35. RenderBankToEl(this, bank);
  36. }
  37. function ConnectElementToBank(el, bank){
  38. bank.listen("data_changed", HANDLE_BankDataChange.bind(el, bank));
  39. }
  40. function CreateBankDOMEntry(name, bank){
  41. var baseel = document.querySelector("." + BLI_TEMPLATE);
  42. if (!baseel){
  43. console.log("WARNING: Failed to find bank list item template.");
  44. return null;
  45. }
  46. var el = baseel.cloneNode(true);
  47. el.classList.remove(BLI_TEMPLATE);
  48. el.classList.remove("hidden");
  49. el.setAttribute("bankname", name);
  50. ConnectElementToBank(el, bank);
  51. SetElBankName(el, name);
  52. el.addEventListener("click", HANDLE_BankClick);
  53. baseel.parentNode.appendChild(el);
  54. setTimeout(()=>{
  55. RenderBankToEl(el, bank);
  56. }, 500); // Make the render call in about a half second. Allow DOM time to catch up?
  57. return el;
  58. }
  59. class CTRLBanksStore{
  60. constructor(){
  61. var HANDLE_ChangeSurface = function(surf){
  62. if (!(surf instanceof NESBank)){
  63. // TODO: Unselect any current bank element.
  64. CurrentBankIndex = "";
  65. } else {
  66. if (Banks.length <= 0 || (CurrentBank !== "" && Banks[CurrentBank].bank !== surf)){
  67. console.log("WARNING: Bank object being set outside of Bank Store.");
  68. }
  69. }
  70. }
  71. GlobalEvents.listen("change_surface", HANDLE_ChangeSurface);
  72. }
  73. get length(){
  74. return Object.keys(Banks).length;
  75. }
  76. get json(){
  77. var data = [];
  78. Object.keys(Banks).forEach((key) => {
  79. if (Banks.hasOwnProperty(key)){
  80. data.push({name:key, data:Banks[key].bank.base64});
  81. }
  82. });
  83. return JSON.stringify(data);
  84. }
  85. initialize(){
  86. if (this.length <= 0){
  87. this.createBank("Bank");
  88. }
  89. return this;
  90. }
  91. createBank(name, bbase64){
  92. if (!(name in Banks)){
  93. var bank = new NESBank();
  94. if (typeof(bbase64) === "string"){
  95. try {
  96. bank.base64 = bbase64;
  97. } catch (e) {
  98. console.log("Failed to create Bank. " + e.toString());
  99. bank = null;
  100. }
  101. }
  102. if (bank !== null){
  103. var el = CreateBankDOMEntry(name, bank);
  104. if (el){
  105. Banks[name] = {bank:bank, el:el};
  106. //Banks.push([name, bank, el]);
  107. if (this.length <= 1){
  108. Banks[name].el.click();
  109. //GlobalEvents.emit("change_surface", bank);
  110. }
  111. }
  112. }
  113. }
  114. return this;
  115. }
  116. removeBank(name){
  117. if (name in Banks){
  118. if (name === CurrentBank){
  119. var keys = Object.keys(Banks);
  120. if (keys.length > 1){
  121. CurrentBank = (keys[0] !== name) ? keys[0] : keys[1];
  122. } else {
  123. CurrentBank = "";
  124. }
  125. }
  126. Banks[name].el.parentNode.removeChild(Banks[name].el);
  127. delete Banks[name];
  128. if (CurrentBank !== ""){
  129. // TODO: Activate new Bank.
  130. }
  131. }
  132. return this;
  133. }
  134. renameBank(name, newname){
  135. if ((name in Banks) && !(newname in Banks)){
  136. Banks[newname] = Banks[name];
  137. delete Banks[name];
  138. SetElBankName(Banks[newname].el, newname);
  139. }
  140. return this;
  141. }
  142. activateBank(name){
  143. if (CurrentBank !== name && (name in Banks)){
  144. Banks[name].el.click();
  145. //CurrentBank = name;
  146. //GlobalEvents.emit("change_surface", Banks[CurrentBank].bank);
  147. }
  148. return this;
  149. }
  150. clear(){
  151. Object.keys(Banks).forEach((item) => {
  152. item.el.parentNode.removeChild(item.el);
  153. });
  154. Banks = {};
  155. if (CurrentBank !== "")
  156. GlobalEvents.emit("change_surface", null);
  157. CurrentBank = "";
  158. }
  159. }
  160. const instance = new CTRLBanksStore();
  161. export default instance;