| var NESPaletteSchema = JSON.stringify({ | |||||
| "$schema": "http://json-schema.org/draft-07/schema#", | |||||
| "$id": "NESPaletteSchema.json", | |||||
| "type":"array", | |||||
| "minItems":25, | |||||
| "maxItems":25, | |||||
| "items":{ | |||||
| "type":"number", | |||||
| "minimum": 0, | |||||
| "exclusiveMaximum": 64 | |||||
| var SCHEMA_LIST = []; | |||||
| var DIRTY = false; | |||||
| var CUR_AJV = null; | |||||
| export default Object.freeze({ | |||||
| add:function(s){ | |||||
| if (!("$id" in s)) | |||||
| throw new Error("Missing '$id' property in schema."); | |||||
| for (let i=0; i < SCHEMA_LIST.length; i++){ | |||||
| if (SCHEMA_LIST[i]["$id"] === s["$id"]) | |||||
| throw new Error("Schema already exists with $id '" + s["$id"] + "'."); | |||||
| } | |||||
| SCHEMA_LIST.push(s); | |||||
| DIRTY = true; | |||||
| }, | |||||
| remove:function(id){ | |||||
| var idx = SCHEMA_LIST.findIndex((item) => { | |||||
| return item["$id"] === id; | |||||
| }); | |||||
| if (idx >= 0){ | |||||
| SCHEMA_LIST.splice(idx, 1); | |||||
| DIRTY = true; | |||||
| } | |||||
| }, | |||||
| has:function(id){ | |||||
| return SCHEMA_LIST.findIndex((item) => { | |||||
| return item["$id"] === id; | |||||
| }) >= 0; | |||||
| }, | |||||
| getValidator:function(id){ | |||||
| if (DIRTY){ | |||||
| DIRTY = false; | |||||
| if (SCHEMA_LIST.length <= 0){ | |||||
| CUR_AJV = null; | |||||
| } else { | |||||
| CUR_AJV = new Ajv({schema:SCHEMA_LIST}); | |||||
| } | |||||
| } | |||||
| return (CUR_AJV !== null) ? CUR_AJV.getSchema(id) : null; | |||||
| } | } | ||||
| }); | }); | ||||
| var PalettesStoreSchema = JSON.stringify({ | |||||
| "$schema": "http://json-schema.org/draft-07/schema#", | |||||
| "$id": "PalettesStoreSchema.json", | |||||
| }); | |||||
| import GlobalEvents from "/app/js/common/EventCaller.js"; | import GlobalEvents from "/app/js/common/EventCaller.js"; | ||||
| import Utils from "/app/js/common/Utils.js"; | import Utils from "/app/js/common/Utils.js"; | ||||
| import JSONSchema from "/app/js/common/JSONSchema.js"; | |||||
| import EditableText from "/app/js/ui/EditableText.js"; | import EditableText from "/app/js/ui/EditableText.js"; | ||||
| import Renderer from "/app/js/ui/Renderer.js"; | import Renderer from "/app/js/ui/Renderer.js"; | ||||
| import NESBank from "/app/js/models/NESBank.js"; | import NESBank from "/app/js/models/NESBank.js"; | ||||
| var CurrentBank = ""; | var CurrentBank = ""; | ||||
| JSONSchema.add({ | |||||
| "$schema": "http://json-schema.org/draft-07/schema#", | |||||
| "$id": "BanksStoreSchema.json", | |||||
| "type": "array", | |||||
| "items":{ | |||||
| "type": "object", | |||||
| "properties":{ | |||||
| "name":{ | |||||
| "type": "string", | |||||
| "minLength": 1 | |||||
| }, | |||||
| "data":{ | |||||
| "type": "string", | |||||
| "media": { | |||||
| "binaryEncoding": "base64" | |||||
| } | |||||
| } | |||||
| }, | |||||
| "required":["name", "data"] | |||||
| } | |||||
| }); | |||||
| function HANDLE_BankClick(e){ | function HANDLE_BankClick(e){ | ||||
| var name = this.getAttribute("bankname"); | var name = this.getAttribute("bankname"); | ||||
| if (name !== CurrentBank){ | if (name !== CurrentBank){ | ||||
| } | } | ||||
| set obj(d){ | set obj(d){ | ||||
| if (!(d instanceof Array)) | |||||
| try { | |||||
| this.json = JSON.stringify(d); | |||||
| } catch (e) { | |||||
| throw e; | |||||
| } | |||||
| /*if (!(d instanceof Array)) | |||||
| throw new TypeError("Expected Array object."); | throw new TypeError("Expected Array object."); | ||||
| this.clear(); | this.clear(); | ||||
| d.forEach((item) => { | d.forEach((item) => { | ||||
| console.log("WARNING: Bank object missing required properties. Skipped."); | console.log("WARNING: Bank object missing required properties. Skipped."); | ||||
| } | } | ||||
| } | } | ||||
| }); | |||||
| });*/ | |||||
| } | } | ||||
| get json(){ | get json(){ | ||||
| return JSON.stringify(this.obj); | return JSON.stringify(this.obj); | ||||
| } | } | ||||
| set json(j){ | |||||
| var validator = null; | |||||
| try { | |||||
| validator = JSONSchema.getValidator("BanksStoreSchema.json"); | |||||
| } catch (e) { | |||||
| throw e; | |||||
| } | |||||
| if (validator(j)){ | |||||
| this.clear(); | |||||
| var o = JSON.parse(j); | |||||
| o.forEach((item) => { | |||||
| this.createBank(item.name, item.data); | |||||
| }); | |||||
| } else { | |||||
| throw new Error("JSON Object validation failed."); | |||||
| } | |||||
| } | |||||
| initialize(){ | initialize(){ | ||||
| if (this.length <= 0){ | if (this.length <= 0){ | ||||
| this.createBank("Bank"); | this.createBank("Bank"); |
| import GlobalEvents from "/app/js/common/EventCaller.js"; | import GlobalEvents from "/app/js/common/EventCaller.js"; | ||||
| import Utils from "/app/js/common/Utils.js"; | import Utils from "/app/js/common/Utils.js"; | ||||
| import JSONSchema from "/app/js/common/JSONSchema.js"; | |||||
| import NESBank from "/app/js/models/NESBank.js"; | import NESBank from "/app/js/models/NESBank.js"; | ||||
| import NESPalette from "/app/js/models/NESPalette.js"; | import NESPalette from "/app/js/models/NESPalette.js"; | ||||
| import CTRLPalettesStore from "/app/js/ctrls/CTRLPalettesStore.js"; | import CTRLPalettesStore from "/app/js/ctrls/CTRLPalettesStore.js"; | ||||
| import CTRLBanksStore from "/app/js/ctrls/CTRLBanksStore.js"; | import CTRLBanksStore from "/app/js/ctrls/CTRLBanksStore.js"; | ||||
| const SUPPORTED_PROJECT_VERSIONS=[ | |||||
| "0.1" | |||||
| ]; | |||||
| JSONSchema.add({ | |||||
| "$schema": "http://json-schema.org/draft-07/schema#", | |||||
| "$id": "NESPainterProject.json", | |||||
| "type":"object", | |||||
| "properties":{ | |||||
| "id":{ | |||||
| "type":"string", | |||||
| "enum":["NESPProj"] | |||||
| }, | |||||
| "version":{ | |||||
| "type":"string", | |||||
| "pattern":"^[0-9]{1,}\.[0-9]{1,}$" | |||||
| }, | |||||
| "paletteStore":{"$ref":"PalettesStoreSchema.json"}, | |||||
| "bankStore":{"$ref":"BanksStoreSchema.json"} | |||||
| }, | |||||
| "required":["id","version","paletteStore","bankStore"] | |||||
| }); | |||||
| var SURF = null; | var SURF = null; | ||||
| function JSONFromProject(){ | function JSONFromProject(){ | ||||
| var proj = { | var proj = { | ||||
| palettesStore:CTRLPalettesStore.obj, | |||||
| banksStore:CTRLBanksStore.obj | |||||
| id:"NESPProj", | |||||
| version:SUPPORTED_PROJECT_VERSIONS[SUPPORTED_PROJECT_VERSIONS.length - 1], | |||||
| paletteStore:CTRLPalettesStore.obj, | |||||
| bankStore:CTRLBanksStore.obj | |||||
| }; | }; | ||||
| return JSON.stringify(proj); | return JSON.stringify(proj); | ||||
| } | } | ||||
| if (this.files && this.files.length > 0){ | if (this.files && this.files.length > 0){ | ||||
| var reader = new FileReader(); | var reader = new FileReader(); | ||||
| reader.onload = (function(e) { | reader.onload = (function(e) { | ||||
| var content = e.target.result; | |||||
| console.log(content); | |||||
| var validator = null; | |||||
| try{ | |||||
| validator = JSONSchema.getValidator("NESPainterProject.json"); | |||||
| } catch (e) { | |||||
| console.log("Failed to validate project file. " + e.toString()); | |||||
| return; | |||||
| } | |||||
| if (validator(e.target.result)){ | |||||
| var o = JSON.parse(e.target.result); | |||||
| // TODO: Validate 'id' and 'version' properties. | |||||
| CTRLPalettesStore.obj = o.paletteStore; | |||||
| CTRLBanksStore.obj = o.banksStore; | |||||
| } | |||||
| if (this.parentNode.nodeName.toLowerCase() === "form"){ | if (this.parentNode.nodeName.toLowerCase() === "form"){ | ||||
| this.parentNode.reset(); | this.parentNode.reset(); | ||||
| } else { | } else { |
| import GlobalEvents from "/app/js/common/EventCaller.js"; | import GlobalEvents from "/app/js/common/EventCaller.js"; | ||||
| import Utils from "/app/js/common/Utils.js"; | import Utils from "/app/js/common/Utils.js"; | ||||
| import JSONSchema from "/app/js/common/JSONSchema.js"; | |||||
| import EditableText from "/app/js/ui/EditableText.js"; | import EditableText from "/app/js/ui/EditableText.js"; | ||||
| import NESPalette from "/app/js/models/NESPalette.js"; | import NESPalette from "/app/js/models/NESPalette.js"; | ||||
| var BlockEmits = false; | var BlockEmits = false; | ||||
| JSONSchema.add({ | |||||
| "$schema": "http://json-schema.org/draft-07/schema#", | |||||
| "$id": "PalettesStoreSchema.json", | |||||
| "type": "array", | |||||
| "items": { | |||||
| "type": "object", | |||||
| "properties": { | |||||
| "name":{ | |||||
| "type":"string", | |||||
| "minLength":1 | |||||
| }, | |||||
| "palette":{ | |||||
| "$ref":"NESPaletteSchema.json" | |||||
| } | |||||
| }, | |||||
| "required":["name","palette"] | |||||
| } | |||||
| }); | |||||
| function HANDLE_PaletteClick(e){ | function HANDLE_PaletteClick(e){ | ||||
| if (!this.hasAttribute("palname")){return;} | if (!this.hasAttribute("palname")){return;} | ||||
| var pname = this.getAttribute("palname"); | var pname = this.getAttribute("palname"); | ||||
| } | } | ||||
| get obj(){ | get obj(){ | ||||
| var d = { | |||||
| cpi: CurrentPaletteIndex, | |||||
| pals: [] | |||||
| }; | |||||
| var d = []; | |||||
| for (let i=0; i < Palettes.length; i++){ | for (let i=0; i < Palettes.length; i++){ | ||||
| d.pals.push([Palettes[i][0].value, Palettes[i][1].json]); | |||||
| d.push({name:Palettes[i][0].value, palette:Palettes[i][1].obj}); | |||||
| } | } | ||||
| return d; | return d; | ||||
| } | } | ||||
| set obj(d){ | set obj(d){ | ||||
| if (d.hasOwnProperty("cpi") && d.hasOwnProperty("pals")){ | |||||
| try { | |||||
| this.json = JSON.stringify(d); | |||||
| } catch (e) { | |||||
| throw e; | |||||
| } | |||||
| /*if (d.hasOwnProperty("cpi") && d.hasOwnProperty("pals")){ | |||||
| if (Utils.isInt(d.cpi) && d.pals instanceof Array){ | if (Utils.isInt(d.cpi) && d.pals instanceof Array){ | ||||
| var newPalettes = [] | var newPalettes = [] | ||||
| for (let i=0; i < d.pals.length; i++){ | for (let i=0; i < d.pals.length; i++){ | ||||
| } | } | ||||
| } else { | } else { | ||||
| throw new TypeError("Object missing expected properties."); | throw new TypeError("Object missing expected properties."); | ||||
| } | |||||
| }*/ | |||||
| } | } | ||||
| get json(){ | get json(){ | ||||
| } | } | ||||
| set json(j){ | set json(j){ | ||||
| var validator = null; | |||||
| try { | try { | ||||
| this.obj = JSON.parse(j); | |||||
| validator = JSONSchema.getValidator("PalettesStoreSchema.json"); | |||||
| } catch (e) { | } catch (e) { | ||||
| throw e; | throw e; | ||||
| } | } | ||||
| if (validator(j)){ | |||||
| this.clear(); | |||||
| var o = JSON.parse(j); | |||||
| for (let i=0; i < o.length; i++){ | |||||
| this.createPalette(o[i].name, JSON.stringify(o[i].palette)); | |||||
| } | |||||
| } else { | |||||
| throw new Error("JSON Object failed verification."); | |||||
| } | |||||
| } | } | ||||
| initialize(){ | initialize(){ |
| import {EventCaller} from "/app/js/common/EventCaller.js" | |||||
| import {EventCaller} from "/app/js/common/EventCaller.js"; | |||||
| import JSONSchema from "/app/js/common/JSONSchema.js"; | |||||
| JSONSchema.add({ | |||||
| "$schema": "http://json-schema.org/draft-07/schema#", | |||||
| "$id": "NESPaletteSchema.json", | |||||
| "type":"array", | |||||
| "minItems":25, | |||||
| "maxItems":25, | |||||
| "items":{ | |||||
| "type":"number", | |||||
| "minimum": 0, | |||||
| "exclusiveMaximum": 64 | |||||
| } | |||||
| }); | |||||
| /** | /** | ||||
| * Object for manipulating the eight NES palettes. | * Object for manipulating the eight NES palettes. | ||||
| } | } | ||||
| set obj(d){ | set obj(d){ | ||||
| if (!(d instanceof Array) || d.length !== 25) | |||||
| throw new TypeError("Invalid Object or value range."); | |||||
| try { | try { | ||||
| this.set_palette(d); | |||||
| this.json = JSON.stringify(d); | |||||
| } catch (e) { | } catch (e) { | ||||
| throw e; | throw e; | ||||
| } | } | ||||
| set json(j){ | set json(j){ | ||||
| try{ | try{ | ||||
| this.obj = JSON.parse(j); | |||||
| var validator = JSONSchema.getValidator("NESPaletteSchema.json"); | |||||
| } catch (e) { | } catch (e) { | ||||
| throw e; | throw e; | ||||
| } | } | ||||
| if (validator(j)){ | |||||
| this.set_palette(JSON.parse(j)); | |||||
| } else { | |||||
| throw new Error("JSON Object failed to pass validation."); | |||||
| } | |||||
| } | } | ||||
| /** | /** |