asajs
Version:
Make your Minecraft JsonUI with ScriptingAPI
396 lines (395 loc) • 13.1 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var UI_exports = {};
__export(UI_exports, {
UI: () => UI
});
module.exports = __toCommonJS(UI_exports);
var import_Config = require("../compilers/Config");
var import_JsonBuilder = require("../compilers/generator/JsonBuilder");
var import_Log = require("../compilers/generator/Log");
var import_CurrentLine = require("../compilers/reader/CurrentLine");
var import_Object = require("../compilers/reader/Object");
var import_ReadBinding = require("../compilers/reader/ReadBinding");
var import_ReadProperties = require("../compilers/reader/ReadProperties");
var import_MappingTypes = require("../types/enums/MappingTypes");
var import_Types = require("../types/enums/Types");
var import_Modify = require("./Modify");
var import_Random = require("./Random");
const typeExtend = {};
const _UI = class _UI {
constructor(identifier) {
__publicField(this, "name");
__publicField(this, "namespace");
__publicField(this, "extends");
__publicField(this, "sourceBindings", {});
__publicField(this, "type");
__publicField(this, "controls");
__publicField(this, "bindings");
__publicField(this, "button_mappings");
__publicField(this, "variables");
__publicField(this, "anims");
__publicField(this, "properties");
const config = import_Config.Configs.getConfig();
if (identifier instanceof _UI || identifier instanceof import_Modify.Modify) {
this.name = import_Random.Random.getName();
this.namespace = import_Random.Random.getNamespace();
this.extends = identifier.getPath();
} else {
this.name = !config.compiler.UI.obfuscateName && (identifier == null ? void 0 : identifier.name) || import_Random.Random.getName();
this.namespace = !config.compiler.UI.obfuscateName && (identifier == null ? void 0 : identifier.namespace) || import_Random.Random.getNamespace();
if (identifier == null ? void 0 : identifier.extends) {
if (identifier.type) {
this.type = identifier.type;
}
if (identifier.extends instanceof _UI)
this.extends = "".concat(identifier.extends.getPath());
else if (identifier.extends instanceof import_Modify.Modify) this.extends = identifier.extends.getPath();
else if (typeof identifier.extends === "string") this.extends = identifier.extends;
else this.extends = "".concat(identifier.extends.namespace, ".").concat(identifier.extends.name);
} else {
if (config.compiler.UI.obfuscateType && identifier.namespace !== "_type_c") {
const type = (identifier == null ? void 0 : identifier.type) || import_Types.Types.Panel;
this.extends = typeExtend[type] || (typeExtend[type] = new _UI({
name: type,
namespace: "_type_c",
type
}).getPath());
} else this.type = (identifier == null ? void 0 : identifier.type) || import_Types.Types.Panel;
}
if (identifier == null ? void 0 : identifier.properties) this.setProperties(identifier.properties);
}
import_JsonBuilder.JsonBuilder.registerElement(this.namespace, this);
}
static panel(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Panel,
properties
});
}
static stackPanel(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.StackPanel,
properties
});
}
static collectionPanel(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.CollectionPanel,
properties
});
}
static inputPanel(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.InputPanel,
properties
});
}
static grid(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Grid,
properties
});
}
static button(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Button,
properties
});
}
static toggle(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Toggle,
properties
});
}
static label(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Label,
properties
});
}
static image(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Image,
properties
});
}
static dropdown(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Dropdown,
properties
});
}
static slider(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Slider,
properties
});
}
static sliderBox(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.SliderBox,
properties
});
}
static editBox(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.EditBox,
properties
});
}
static scrollView(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.ScrollView,
properties
});
}
static scrollbarTrack(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.ScrollbarTrack,
properties
});
}
static scrollbarBox(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.ScrollbarBox,
properties
});
}
static screen(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Screen,
properties
});
}
static custom(renderer, properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.Custom,
properties: {
...properties,
renderer
}
});
}
static tooltipTrigger(properties, identifier) {
return new _UI({
...identifier,
type: import_Types.Types.TooltipTrigger,
properties
});
}
static extend(extendElement, properties, identifier) {
if (identifier)
return new _UI({
extends: extendElement,
...identifier
});
else
return new _UI({
extends: extendElement,
properties
});
}
searchBinding(bindingName, controlName, targetBindingName) {
var _a, _b;
for (let index = 0; index < (((_a = this.bindings) == null ? void 0 : _a.length) || 0); index++) {
const binding = (_b = this.bindings) == null ? void 0 : _b[index];
if (controlName) {
if ((binding == null ? void 0 : binding.source_control_name) === controlName && binding.source_property_name === bindingName) {
if (targetBindingName) {
if (binding.target_property_name === targetBindingName) {
return targetBindingName;
} else return void 0;
} else return binding.target_property_name;
}
} else {
if ((binding == null ? void 0 : binding.source_property_name) === bindingName) {
if (targetBindingName) {
if (binding.target_property_name === targetBindingName) {
return targetBindingName;
} else return void 0;
} else return binding.target_property_name;
}
}
}
return void 0;
}
setProperties(properties) {
var _a;
if (properties.property_bag) {
properties.property_bag = {
...(_a = this.properties) == null ? void 0 : _a.property_bag,
...properties.property_bag
};
}
this.properties = {
...this.properties || {},
...properties
};
return this;
}
isDuplicate(name) {
for (const childElement of this.controls || []) {
const childKey = Object.keys(childElement)[0];
const childName = childKey.split("@")[0];
if (childName === name) return true;
}
return false;
}
isRecusive(name) {
return name === this.name;
}
addChild(element, properties, name, callback) {
this.controls || (this.controls = []);
if (!element) return this;
name || (name = import_Random.Random.getName());
if (this.isDuplicate(name)) {
import_Log.Log.warning("".concat((0, import_CurrentLine.CurrentLine)(), " child element should have a unique name!"));
}
if (typeof element === "string") {
this.controls.push({
["".concat(name, "@").concat(element)]: properties ? (0, import_ReadProperties.ReadProperties)(properties) : {}
});
} else if (element instanceof _UI || element instanceof import_Modify.Modify) {
{
if ((element == null ? void 0 : element.getPath()) === this.getPath()) {
import_Log.Log.warning("".concat((0, import_CurrentLine.CurrentLine)(), " child element should have a unique name!"));
}
this.controls.push({
["".concat(name, "@").concat(element == null ? void 0 : element.getPath())]: properties ? (0, import_ReadProperties.ReadProperties)(properties) : {}
});
}
}
callback == null ? void 0 : callback(this, name);
return this;
}
addBindings(bindings) {
this.bindings || (this.bindings = []);
if (!bindings) return this;
if (Array.isArray(bindings)) for (const binding of bindings) this.addBindings(binding);
else this.bindings.push((0, import_ReadBinding.ReadBinding)(bindings, this));
return this;
}
addMapping(mapping) {
this.button_mappings || (this.button_mappings = []);
if (!mapping) return this;
if (Array.isArray(mapping)) mapping.forEach((v) => this.addMapping(v));
else {
mapping.mapping_type || (mapping.mapping_type = import_MappingTypes.MappingType.Global);
this.button_mappings.push(mapping);
}
return this;
}
addVariables(variables) {
this.variables || (this.variables = {});
if (variables)
import_Object.Obj.forEach(variables, (key, value) => {
this.variables[key] = {
...import_Object.Obj.map(value, (k, v) => {
return { key: k, value: (0, import_ReadProperties.ReadValue)(v) };
})
};
});
return this;
}
addAnimation(animation, startIndex) {
this.anims || (this.anims = []);
if (animation) this.anims.push(animation.getKeyIndex(startIndex || 0));
return this;
}
getUI() {
var _a;
const code = (0, import_ReadProperties.ReadProperties)((_a = this.properties) != null ? _a : {});
for (const key of ["type", "controls", "bindings", "button_mappings", "anims"])
if (this[key]) code[key] = this[key];
for (const bindingKey in this.sourceBindings) {
const targetBinding = this.sourceBindings[bindingKey];
const [sourceBinding, sourceControl] = bindingKey.split(":");
code.bindings.push({
binding_type: "view",
source_control_name: sourceControl,
source_property_name: sourceBinding,
target_property_name: targetBinding
});
}
if (this.variables) code.variables || (code.variables = []);
if (this.variables && Object(this.variables).length !== 0)
import_Object.Obj.forEach(this.variables, (k, v) => {
code.variables.push({
requires: k,
...v
});
});
return code;
}
getPath() {
return "".concat(this.namespace, ".").concat(this.name);
}
getElement() {
return "@".concat(this.getPath());
}
getFullPath() {
return "".concat(this.name).concat(this.extends ? "@".concat(this.extends) : "");
}
extend(identifier, properties) {
return new _UI({
...identifier,
extends: this,
properties
});
}
static apply() {
}
static bind() {
}
static call() {
}
static toString() {
}
};
__publicField(_UI, "arguments", "");
__publicField(_UI, "caller", "");
__publicField(_UI, "length", "");
__publicField(_UI, "name", "");
let UI = _UI;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
UI
});