From bb9b300d6f9e18413d86742405a771bfc30c33d6 Mon Sep 17 00:00:00 2001 From: Asaki Yuki Date: Mon, 5 Jan 2026 10:58:28 +0700 Subject: [PATCH] idk --- src/compilers/FormatProperties.ts | 21 ++++++++++++++-- src/compilers/Memory.ts | 19 ++++++++++++--- src/compilers/PreCompile.ts | 11 +++++++++ src/compilers/RunEnd.ts | 7 ++++++ src/components/UI.ts | 16 +++++++++---- src/index.ts | 3 +++ src/types/properties/element/Control.ts | 3 ++- test/app.ts | 32 ++++++++++--------------- tsconfig.json | 2 +- 9 files changed, 83 insertions(+), 31 deletions(-) create mode 100644 src/compilers/PreCompile.ts create mode 100644 src/compilers/RunEnd.ts diff --git a/src/compilers/FormatProperties.ts b/src/compilers/FormatProperties.ts index e47d716..3e8b547 100644 --- a/src/compilers/FormatProperties.ts +++ b/src/compilers/FormatProperties.ts @@ -1,11 +1,28 @@ -import { Type } from "../index.js" -import { Properties } from "../types/properties/components.js" +import { Binding } from "../types/properties/value.js" export function FormatProperties(properties: any) { + const property_bags: Record = {} + + for (const key in properties) { + const value = properties[key] + + if (key.startsWith("#")) { + property_bags[key] = value + delete properties[key] + } + } + if (properties.anchor) { properties.anchor_from = properties.anchor_to = properties.anchor delete properties.anchor } + if (Object.keys(property_bags)) + if (properties.property_bags) { + properties.property_bags = { ...property_bags, ...properties.property_bags } + } else { + properties.property_bags = property_bags + } + return properties } diff --git a/src/compilers/Memory.ts b/src/compilers/Memory.ts index 091e29b..1fff7f7 100644 --- a/src/compilers/Memory.ts +++ b/src/compilers/Memory.ts @@ -1,9 +1,11 @@ import { UI } from "../components/UI.js" +import { Renderer } from "../types/enums/Renderer.js" +import { Type } from "../types/enums/Type.js" export const Memory = { - cache: new Map> }>(), + cache: new Map> }>(), - register_ui(path: string, element: UI) { + register_ui(path: string, element: UI) { const { elements: saver, namespace } = this.get_file(path, element.namespace!) if (saver.get(element.name!)) { @@ -15,11 +17,22 @@ export const Memory = { return namespace }, + gen_ui_file_content(namespace: string, elements: Map>) { + return JSON.stringify( + { + namespace, + ...elements.toJSON(), + }, + null, + 4 + ) + }, + get_file(path: string, namespace: string) { let cached = this.cache.get(path) if (!cached) { - cached = { namespace, elements: new Map>() } + cached = { namespace, elements: new Map>() } this.cache.set(path, cached) } diff --git a/src/compilers/PreCompile.ts b/src/compilers/PreCompile.ts new file mode 100644 index 0000000..bd03047 --- /dev/null +++ b/src/compilers/PreCompile.ts @@ -0,0 +1,11 @@ +declare global { + interface Map { + toJSON(): Record + } +} + +Map.prototype.toJSON = function () { + const obj: any = {} + this.forEach((value, key) => (obj[key] = value)) + return obj +} diff --git a/src/compilers/RunEnd.ts b/src/compilers/RunEnd.ts new file mode 100644 index 0000000..53f16fa --- /dev/null +++ b/src/compilers/RunEnd.ts @@ -0,0 +1,7 @@ +import { Memory } from "./Memory.js" + +process.on("beforeExit", () => { + Memory.cache.forEach(({ elements, namespace }) => { + console.log(Memory.gen_ui_file_content(namespace, elements)) + }) +}) diff --git a/src/components/UI.ts b/src/components/UI.ts index b073649..098eacc 100644 --- a/src/components/UI.ts +++ b/src/components/UI.ts @@ -21,6 +21,11 @@ export class UI extends Class constructor(public type?: T, name?: string, namespace?: string, path?: string) { super() + if (name === "namespace") { + console.error("The 'namespace' cannot be used as a name") + process.exit(1) + } + this.name = name?.match(/^\w+/)?.[0] || RandomString(16) this.namespace = namespace || RandomString(16) @@ -31,7 +36,7 @@ export class UI extends Class } setProperties(properties: Properties) { - this.properties = properties + this.properties = { ...this.properties, ...properties } return this } @@ -40,7 +45,7 @@ export class UI extends Class throw new Error("Cannot add a child to itself") } - this.controls.set(name || child.name, [child, properties || {}]) + this.controls.set(name || RandomString(16), [child, properties || {}]) return this } @@ -50,10 +55,13 @@ export class UI extends Class toJSON() { const obj: any = { - type: this.type, ...FormatProperties(this.properties), } + if (this.type) { + obj.type = this.type + } + if (this.controls.size) { obj.controls = [] this.controls.forEach((e, key) => obj.controls.push({ [key + e[0]]: e[1] })) @@ -63,7 +71,7 @@ export class UI extends Class } [util.inspect.custom]($: any, opts: any) { - const obj: any = this.properties + const obj: any = FormatProperties(this.properties) if (this.controls.size) { obj.controls = [] diff --git a/src/index.ts b/src/index.ts index b187ce5..ff6add8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,6 @@ +import "./compilers/PreCompile.js" +import "./compilers/RunEnd.js" + export * from "./components/UI.js" export { Animation } from "./components/Animation.js" export * from "./components/Utils.js" diff --git a/src/types/properties/element/Control.ts b/src/types/properties/element/Control.ts index c28b53e..4fa97dd 100644 --- a/src/types/properties/element/Control.ts +++ b/src/types/properties/element/Control.ts @@ -1,4 +1,4 @@ -import { AnimValue, Array2, Binding, PropertyBags, Value } from "../value.js" +import { AnimValue, Array2, Binding, PropertyBags, Value, Variable } from "../value.js" export interface Control { visible?: Value @@ -18,4 +18,5 @@ export interface Control { follows_cursor?: Value property_bags?: Value [key: Binding]: Value + [key: Variable]: Value } diff --git a/test/app.ts b/test/app.ts index 382760e..2799a38 100644 --- a/test/app.ts +++ b/test/app.ts @@ -1,21 +1,13 @@ -import { Anchor, Button, Custom, Extends, GlobalVariables, Panel, Renderer, Type, UI } from ".." +import { Binding, Color, Label, Panel } from ".." -const paperDoll = Custom(Renderer.ANIMATED_GIF_RENDERER, { - camera_tilt_degrees: 360, - starting_rotation: 0, -}) - -const panel = Panel({ - anchor: Anchor.BOTTOM_LEFT, - offset: [50, 50], -}).setProperties({ - camera_tilt_degrees: 360, -}) - -paperDoll.setProperties({ - camera_tilt_degrees: 360, -}) - -panel.setProperties({ - hover_control: "cac", -}) +Panel({ + "#test": 123, + [Binding.IS_CREATIVE_LAYOUT]: true, + [Binding.INVENTORY_SELECTED_ITEM_COLOR]: Color(0x00ff00), +}).addChild( + Label({ + text: "Hello, World!", + shadow: true, + color: Color("#3b0202"), + }) +) diff --git a/tsconfig.json b/tsconfig.json index e4cc890..a459abe 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,7 @@ "outDir": "dist", "declaration": true, - "sourceMap": true, + "sourceMap": false, "strict": true, "esModuleInterop": true, "incremental": true,