From 4419a9e8ed32c635ae1b0bf11ae6ee90d5223d05 Mon Sep 17 00:00:00 2001 From: York Xiang Date: Tue, 24 Dec 2024 18:15:44 +0800 Subject: [PATCH] Initial commit --- .gitignore | 1 + __init__.py | 22 ++++++++++++ remember_last_seed.py | 23 ++++++++++++ show_last_seed.py | 24 +++++++++++++ show_last_text.py | 26 ++++++++++++++ web/main.js | 83 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 __init__.py create mode 100644 remember_last_seed.py create mode 100644 show_last_seed.py create mode 100644 show_last_text.py create mode 100644 web/main.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2af571a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..7b77fc2 --- /dev/null +++ b/__init__.py @@ -0,0 +1,22 @@ +from .remember_last_seed import RememberLastSeed +from .show_last_seed import ShowLastSeed +from .show_last_text import ShowLastText + + +NODE_CLASS_MAPPINGS = { + "RememberLastSeed": RememberLastSeed, + "ShowLastSeed": ShowLastSeed, + "ShowLastText": ShowLastText, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "ShowLastSeed": "Show Last Seed", + "RememberLastSeed": "Remember Last Seed", + "ShowLastText": "Show Last Text", +} + +WEB_DIRECTORY = "./web" + +__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"] + + diff --git a/remember_last_seed.py b/remember_last_seed.py new file mode 100644 index 0000000..2967e14 --- /dev/null +++ b/remember_last_seed.py @@ -0,0 +1,23 @@ +class RememberLastSeed: + last_seed = None + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "seed": ("INT", {}), + } + } + + OUTPUT_NODE = True + RETURN_TYPES = ("INT",) + FUNCTION = "read_seed" + CATEGORY = "utils" + + def read_seed(self, seed): + ret = self.last_seed + self.last_seed = seed + return (ret,) + diff --git a/show_last_seed.py b/show_last_seed.py new file mode 100644 index 0000000..b521572 --- /dev/null +++ b/show_last_seed.py @@ -0,0 +1,24 @@ +from server import PromptServer + +class ShowLastSeed: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "hidden": {"node_id": "UNIQUE_ID"}, + "required": { + "seed": ("INT", {}), + } + } + + OUTPUT_NODE = True + RETURN_TYPES = ("STRING",) + FUNCTION = "read_seed" + CATEGORY = "utils" + + def read_seed(self, seed, node_id): + PromptServer.instance.send_sync("RememberingUtils:change", {"node": node_id, "next": seed}) + return (str(seed),) + diff --git a/show_last_text.py b/show_last_text.py new file mode 100644 index 0000000..019d78f --- /dev/null +++ b/show_last_text.py @@ -0,0 +1,26 @@ +from server import PromptServer + +class ShowLastText: + last_text = None + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "hidden": {"node_id": "UNIQUE_ID"}, + "required": { + "text": ("STRING", {}), + } + } + + OUTPUT_NODE = True + RETURN_TYPES = ("STRING",) + FUNCTION = "read_text" + CATEGORY = "utils" + + def read_text(self, text, node_id): + PromptServer.instance.send_sync("RememberingUtils:change", {"node": node_id, "next": text}) + + return (text,) + diff --git a/web/main.js b/web/main.js new file mode 100644 index 0000000..7429f97 --- /dev/null +++ b/web/main.js @@ -0,0 +1,83 @@ +import { app } from "../../scripts/app.js"; +import { api } from "../../scripts/api.js"; + +const propertiesMapping = {}; +const nodes = []; + +function drawTextWithLineBreaks(ctx, text, x, y, maxWidth, lineHeight) { + const words = text.split(' '); // 将文本按空格分割为单词 + let line = ''; // 当前行的文本 + let currentY = y; // 当前行的Y坐标 + + words.forEach(word => { + const testLine = line + (line ? ' ' : '') + word; // 尝试添加单词到当前行 + const metrics = ctx.measureText(testLine); // 测量当前行宽度 + //console.log('metrics.width', metrics.width, 'maxWidth', maxWidth); + if (metrics.width > maxWidth && line !== '') { + // 如果当前行宽度超过了最大宽度且行不为空,则绘制当前行 + ctx.fillText(line, x, currentY); + line = word; // 将当前单词移到下一行 + currentY += lineHeight; // 更新Y坐标 + } else { + // 否则,将单词添加到当前行 + line = testLine; + } + }); + + // 绘制最后一行 + if (line) { + ctx.fillText(line, x, currentY); + } +} + +app.registerExtension({ + name: "RememberingUtils", + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeData.python_module !== "custom_nodes.RememberingUtils") return; + nodeType.prototype.onDrawForeground = function(ctx, graphcanvas) { + //console.log(arguments) + if (this.flags.collapsed) return; + if (!this.properties.curr) return; + ctx.save(); + ctx.font = "16px serif"; + if (typeof this.properties.curr === 'string') { + drawTextWithLineBreaks(ctx, this.properties.curr, 10, 35, this.size[0] - 20, 16); + } else { + ctx.fillText(this.properties.curr, 10, 50); + } + ctx.restore(); + } + nodeType.prototype.onGetInputs = function() { + // handle the situation when node is freshly added + const {id, properties} = this; + propertiesMapping[id] = properties; + node_callback(this); + }; + api.addEventListener('RememberingUtils:change', function(data) { + //console.log('RememberingUtils:change', data.detail) + const id = data.detail.node; + if (!(id in propertiesMapping)) { + return; + } + const properties = propertiesMapping[id]; + properties.next = data.detail.next; + // console.log('next', properties.next); + }); + api.addEventListener('progress', function({detail}) { + if (detail.value < detail.max) return; + Object.values(propertiesMapping).forEach(v => { + v.curr = v.next; + }) + // console.log('propertiesMapping', propertiesMapping) + }) + }, + async nodeCreated(node) { + nodes.push(node); + }, + async setup() { + nodes.forEach(node => { + const {id, properties} = node; + propertiesMapping[id] = properties; + }); + }, +});