import Template from "./Template"; import { base64ToUint8, colorpaletteForBlueMarble, numberToEncoded } from "./utils"; /** Manages the template system. * This class handles all external requests for template modification, creation, and analysis. * It serves as the central coordinator between template instances and the user interface. * @class TemplateManager * @since 0.55.8 * @example * // JSON structure for a template. * // Note: The pixel "colors" Object contains more than 2 keys. * { * "whoami": "BlueMarble", * "scriptVersion": "1.13.0", * "schemaVersion": "2.1.0", * "templates": { * "0 $Z": { * "name": "My Template", * "enabled": true, * "pixels": { * "total": 40399, * "colors": { * "-2": 40000, * "0": 399 * } * } * "tiles": { * "1231,0047,183,593": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA", * "1231,0048,183,000": "data:image/png;AAAFCAYAAACNbyblAAAAHElEQVQI12P4" * } * }, * "1 $Z": { * "name": "My Template", * "URL": "https://github.com/SwingTheVine/Wplace-BlueMarble/blob/main/dist/assets/Favicon.png", * "URLType": "template", * "enabled": false, * "pixels": { * "total": 40399, * "colors": { * "-2": 40000, * "0": 399 * } * } * "tiles": { * "375,1846,276,188": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA", * "376,1846,000,188": "data:image/png;AAAFCAYAAACNbyblAAAAHElEQVQI12P4" * } * } * } * } */ export default class TemplateManager { /** The constructor for the {@link TemplateManager} class. * @since 0.55.8 */ constructor(name, version, overlay) { // Meta this.name = name; // Name of userscript this.version = version; // Version of userscript this.overlay = overlay; // The main instance of the Overlay class this.templatesVersion = '1.0.0'; // Version of JSON schema this.userID = null; // The ID of the current user this.encodingBase = '!#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'; // Characters to use for encoding/decoding this.tileSize = 1000; // The number of pixels in a tile. Assumes the tile is square this.drawMult = 3; // The enlarged size for each pixel. E.g. when "3", a 1x1 pixel becomes a 1x1 pixel inside a 3x3 area. MUST BE ODD this.paletteTolerance = 3; // Tolerance for how close an RGB value has to be in order to be considered a color. A tolerance of "3" means the sum of the RGB can be up to 3 away from the actual value. this.paletteBM = colorpaletteForBlueMarble(this.paletteTolerance); // Retrieves the color palette BM will use as an Object containing multiple Uint32Arrays // Template this.template = null; // The template image. this.templateState = ''; // The state of the template ('blob', 'proccessing', 'template', etc.) /** @type {Array