Better mode switching action

This commit is contained in:
space-nuko
2023-05-09 11:36:02 -05:00
parent 2df43c6b04
commit 30198c3808
6 changed files with 1070 additions and 848 deletions

View File

@@ -1,8 +1,8 @@
import type { SerializedPrompt } from "$lib/components/ComfyApp";
import notify from "$lib/notify";
import layoutState from "$lib/stores/layoutState";
import layoutState, { type DragItemID } from "$lib/stores/layoutState";
import queueState from "$lib/stores/queueState";
import { BuiltInSlotType, LiteGraph, NodeMode, type ITextWidget, type IToggleWidget, type SerializedLGraphNode, type SlotLayout } from "@litegraph-ts/core";
import { BuiltInSlotType, LiteGraph, NodeMode, type ITextWidget, type IToggleWidget, type SerializedLGraphNode, type SlotLayout, type PropertyLayout } from "@litegraph-ts/core";
import { get } from "svelte/store";
import ComfyGraphNode, { type ComfyGraphNodeProperties } from "./ComfyGraphNode";
import type { ComfyWidgetNode, GalleryOutput } from "./ComfyWidgetNodes";
@@ -279,16 +279,21 @@ export class ComfySetNodeModeAction extends ComfyGraphNode {
constructor(title?: string) {
super(title)
this.displayWidget = this.addWidget("text", "Tags", this.properties.targetTags, "targetTags")
this.enableWidget = this.addWidget("toggle", "Enable", this.properties.enable, "enable");
}
override onPropertyChanged(property: any, value: any) {
if (property === "enabled") {
if (property === "enable") {
this.enableWidget.value = value
}
}
override onAction(action: any, param: any) {
let enabled = this.getInputData(0)
let input = this.getInputData(0)
if (input == null)
input = this.properties.enable
let enabled = Boolean(input)
if (typeof param === "object" && "enabled" in param)
enabled = param["enabled"]
@@ -332,3 +337,145 @@ LiteGraph.registerNodeType({
desc: "Sets a group of nodes/UI containers as enabled/disabled based on their tags (comma-separated)",
type: "actions/set_node_mode"
})
export type TagAction = {
tag: string,
enable: boolean
}
export interface ComfySetNodeModeAdvancedActionProperties extends ComfyGraphNodeProperties {
targetTags: TagAction[],
enable: boolean,
}
export class ComfySetNodeModeAdvancedAction extends ComfyGraphNode {
override properties: ComfySetNodeModeAdvancedActionProperties = {
targetTags: [{ tag: "myTag", enable: true }, { tag: "anotherTag", enable: false }],
enable: true,
tags: []
}
static slotLayout: SlotLayout = {
inputs: [
{ name: "enabled", type: "boolean" },
{ name: "set", type: BuiltInSlotType.ACTION },
],
}
static propertyLayout: PropertyLayout = [
{ name: "enable", defaultValue: true, type: "boolean", },
{ name: "targetTags", defaultValue: [{ tag: "myTag", enable: true }, { tag: "anotherTag", enable: false }], type: "array", options: { multiline: true, inputStyle: { fontFamily: "monospace" } } }
]
displayWidget: ITextWidget;
enableWidget: IToggleWidget;
constructor(title?: string) {
super(title)
this.displayWidget = this.addWidget("text", "Tags", this.formatTags(), null);
this.displayWidget.disabled = true;
this.enableWidget = this.addWidget("toggle", "Enable", this.properties.enable, "enable");
}
override onPropertyChanged(property: any, value: any) {
if (property === "enable") {
this.enableWidget.value = value
}
else if (property === "targetTags") {
this.displayWidget.value = this.formatTags()
}
}
private formatTags(): string {
if (!Array.isArray(this.properties.targetTags) || this.properties.targetTags.length === 0)
return "(No tags)";
return this.properties.targetTags.map(t => {
let s = t.tag
if (t.enable)
s = "+" + s
else
s = "!" + s
return s
}).join(", ")
}
private getModeChanges(action: TagAction, enable: boolean, nodeChanges: Record<string, NodeMode>, widgetChanges: Record<DragItemID, boolean>) {
for (const node of this.graph._nodes) {
if ("tags" in node.properties) {
const comfyNode = node as ComfyGraphNode;
const hasTag = comfyNode.properties.tags.indexOf(action.tag) != -1;
if (hasTag) {
let newMode: NodeMode;
if (enable && action.enable) {
newMode = NodeMode.ALWAYS;
} else {
newMode = NodeMode.NEVER;
}
nodeChanges[node.id] = newMode
node.changeMode(newMode);
if ("notifyPropsChanged" in node)
(node as ComfyWidgetNode).notifyPropsChanged();
}
}
}
for (const entry of Object.values(get(layoutState).allItems)) {
if (entry.dragItem.type === "container") {
const container = entry.dragItem;
const hasTag = container.attrs.tags.indexOf(action.tag) != -1;
if (hasTag) {
const hidden = !(enable && action.enable)
widgetChanges[container.id] = hidden
}
}
}
}
override onExecute() {
this.boxcolor = LiteGraph.NODE_DEFAULT_BOXCOLOR;
for (const action of this.properties.targetTags) {
if (typeof action !== "object" || !("tag" in action) || !("enable" in action)) {
this.boxcolor = "red";
break;
}
}
}
override onAction(action: any, param: any) {
let input = this.getInputData(0)
if (input == null)
input = this.properties.enable
let enabled = Boolean(input)
if (typeof param === "object" && "enabled" in param)
enabled = param["enabled"]
const nodeChanges: Record<string, NodeMode> = {} // nodeID => newState
const widgetChanges: Record<string, boolean> = {} // dragItemID => isHidden
for (const action of this.properties.targetTags) {
this.getModeChanges(action, enabled, nodeChanges, widgetChanges)
}
for (const [nodeId, newMode] of Object.entries(nodeChanges)) {
this.graph.getNodeById(parseInt(nodeId)).changeMode(newMode);
}
const layout = get(layoutState);
for (const [dragItemID, isHidden] of Object.entries(widgetChanges)) {
const container = layout.allItems[dragItemID].dragItem
container.attrs.hidden = isHidden;
container.attrsChanged.set(get(container.attrsChanged) + 1)
}
}
}
LiteGraph.registerNodeType({
class: ComfySetNodeModeAdvancedAction,
title: "Comfy.SetNodeModeAdvancedAction",
desc: "Turns multiple groups of nodes on/off at once based on an array of rules [{ tag: string, enable: boolean }, ...]",
type: "actions/set_node_mode_advanced"
})

View File

@@ -0,0 +1,56 @@
import { BuiltInSlotType, LGraphNode, LiteGraph, type ITextWidget, type SlotLayout, type PropertyLayout } from "@litegraph-ts/core"
import type { ComfyGraphNodeProperties } from "./ComfyGraphNode";
import { Watch } from "@litegraph-ts/nodes-basic";
export interface ComfyTriggerNewEventNodeProperties extends ComfyGraphNodeProperties {
param: any,
}
export default class ComfyTriggerNewEventNode extends LGraphNode {
override properties: ComfyTriggerNewEventNodeProperties = {
param: true,
tags: []
}
static slotLayout: SlotLayout = {
inputs: [
{ name: "in", type: BuiltInSlotType.ACTION },
{ name: "param", type: "*" },
],
outputs: [
{ name: "out", type: BuiltInSlotType.EVENT },
],
}
paramWidget: ITextWidget;
constructor(title?: string) {
super(title)
this.paramWidget = this.addWidget("text", "Param", Watch.toString(this.properties.param), "param")
}
override onPropertyChanged(property: any, value: any) {
if (property === "param")
this.paramWidget.value = Watch.toString(value)
}
override onExecute() {
const newParam = this.getInputData(1);
if (newParam != null)
this.setProperty("param", newParam)
}
override onAction(action: any, param: any, options: { action_call?: string }) {
let newParam = this.getInputData(1);
if (newParam == null)
newParam = this.properties.param
this.triggerSlot(0, newParam, null, options);
}
}
LiteGraph.registerNodeType({
class: ComfyTriggerNewEventNode,
title: "Comfy.TriggerNewEvent",
desc: "Triggers a new event with the specified parameter when an event is received",
type: "events/trigger_new_event"
})

File diff suppressed because it is too large Load Diff

View File

@@ -5,3 +5,4 @@ export { default as ComfyPickFirstNode } from "./ComfyPickFirstNode"
export { default as ComfyValueControl } from "./ComfyValueControl"
export { default as ComfySelector } from "./ComfySelector"
export { default as ComfyImageCacheNode } from "./ComfyImageCacheNode"
export { default as ComfyTriggerNewEventNode } from "./ComfyTriggerNewEventNode"

View File

@@ -603,7 +603,7 @@ export interface WidgetLayout extends IDragItem {
node: ComfyWidgetNode
}
type DragItemID = string;
export type DragItemID = string;
type LayoutStateOps = {
addContainer: (parent: ContainerLayout | null, attrs: Partial<Attributes>, index?: number) => ContainerLayout,