A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

267 rindas
6.2KB

  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. initialize(){
  149. if (this.length <= 0){
  150. this.createBank("Bank");
  151. }
  152. return this;
  153. }
  154. createBank(name, bbase64){
  155. if (!(name in Banks)){
  156. var bank = new NESBank();
  157. if (typeof(bbase64) === "string"){
  158. try {
  159. bank.base64 = bbase64;
  160. } catch (e) {
  161. console.log("Failed to create Bank. " + e.toString());
  162. bank = null;
  163. }
  164. }
  165. if (bank !== null){
  166. var el = CreateBankDOMEntry(name, bank);
  167. if (el){
  168. var elname = SetElBankName(el, name);
  169. Banks[name] = {bank:bank, el:el, elname:elname};
  170. if (this.length <= 1){
  171. Banks[name].el.click();
  172. }
  173. }
  174. }
  175. }
  176. return this;
  177. }
  178. removeBank(name){
  179. if (name in Banks){
  180. if (name === CurrentBank){
  181. var keys = Object.keys(Banks);
  182. if (keys.length > 1){
  183. CurrentBank = (keys[0] !== name) ? keys[0] : keys[1];
  184. } else {
  185. CurrentBank = "";
  186. }
  187. }
  188. Banks[name].el.parentNode.removeChild(Banks[name].el);
  189. delete Banks[name];
  190. if (CurrentBank !== ""){
  191. Banks[CurrentBank].el.click();
  192. } else {
  193. GlobalEvents.emit("change_surface", null);
  194. }
  195. }
  196. return this;
  197. }
  198. renameBank(name, newname){
  199. if ((name in Banks) && !(newname in Banks)){
  200. Banks[newname] = Banks[name];
  201. Banks[newname].elname.value = newname;
  202. delete Banks[name];
  203. }
  204. return this;
  205. }
  206. activateBank(name){
  207. if (CurrentBank !== name && (name in Banks)){
  208. Banks[name].el.click();
  209. }
  210. return this;
  211. }
  212. clear(){
  213. Object.keys(Banks).forEach((item) => {
  214. Banks[item].el.parentNode.removeChild(Banks[item].el);
  215. });
  216. Banks = {};
  217. if (CurrentBank !== ""){
  218. CurrentBank = "";
  219. GlobalEvents.emit("change_surface", null);
  220. }
  221. }
  222. }
  223. const instance = new CTRLBanksStore();
  224. export default instance;