File size: 5,853 Bytes
1e3b872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { app } from "../../scripts/app.js";
import { ComfyWidgets } from "../../scripts/widgets.js";

class SeedControl {
    constructor(node) {
        this.node = node;

        for (const [i, w] of this.node.widgets.entries()) {
            if (w.name === "seed" || w.name === "noise_seed") {
                this.seedWidget = w;
            }
            else if (w.name === "control_after_generate" || w.name === "control_before_generate") {
                this.controlWidget = w;
            }
        }
        if (!this.seedWidget) {
            throw new Error("Something's wrong; expected seed widget");
        }
        const randMax = Math.min(1125899906842624, this.seedWidget.options.max);
        const randMin = Math.max(0, this.seedWidget.options.min);
        const randomRange = (randMax - Math.max(0, randMin)) / (this.seedWidget.options.step / 10);
        this.randomSeedButton = this.node.addWidget("button", "🎲 New Fixed Random", null, () => {
            this.seedWidget.value =
                Math.floor(Math.random() * randomRange) * (this.seedWidget.options.step / 10) + randMin;
            this.controlWidget.value = "fixed";
        }, { serialize: false });

        this.seedWidget.linkedWidgets = [this.randomSeedButton, this.controlWidget];
    }
}

function addTextDisplay(nodeType) {
    const onNodeCreated = nodeType.prototype.onNodeCreated;
    nodeType.prototype.onNodeCreated = function () {
        const r = onNodeCreated?.apply(this, arguments);
        const w = ComfyWidgets["STRING"](this, "display", ["STRING", { multiline: true, placeholder: " " }], app).widget;
        w.inputEl.readOnly = true;
        w.inputEl.style.opacity = 0.7;
        w.inputEl.style.cursor = "auto";
        return r;
    };

    const onExecuted = nodeType.prototype.onExecuted;
    nodeType.prototype.onExecuted = function (message) {
        onExecuted?.apply(this, arguments);

        for (const widget of this.widgets) {
            if (widget.type === "customtext" && widget.name === "display" && widget.inputEl.readOnly === true) {
                widget.value = message.text.join('');
            }
        }
        
        this.onResize?.(this.size);
    };
}

function overwriteSeedControl(nodeType) {
    const onNodeCreated = nodeType.prototype.onNodeCreated;
    nodeType.prototype.onNodeCreated = function () {
        onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined;
        this.seedControl = new SeedControl(this);
    }
}

const HAS_EXECUTED = Symbol();
class IndexControl {
    constructor(node) {
        this.node = node;
        this.node.properties = this.node.properties || {};
        for (const [i, w] of this.node.widgets.entries()) {
            if (w.name === "index") {
                this.indexWidget = w;
            }
            else if (w.name === "index_control") {
                this.controlWidget = w;
            } else if (w.name === "text") {
                this.textWidget = w;
            }
        }

        if (!this.indexWidget) {
            throw new Error("Something's wrong; expected index widget");
        }

        const applyWidgetControl = () => {
            var v = this.controlWidget.value;
    
            //number
            let min = this.indexWidget.options.min;
            let max = this.textWidget.value.split("\n").length - 1;
            // limit to something that javascript can handle
            max = Math.min(1125899906842624, max);
            min = Math.max(-1125899906842624, min);
    
            //adjust values based on valueControl Behaviour
            switch (v) {
                case "fixed":
                    break;
                case "increment":
                    this.indexWidget.value += 1;
                    break;
                case "decrement":
                    this.indexWidget.value -= 1;
                    break;
                case "randomize":
                    this.indexWidget.value = Math.floor(Math.random() * (max - min + 1)) + min;
                default:
                    break;
            }
            /*check if values are over or under their respective
                * ranges and set them to min or max.*/
            if (this.indexWidget.value < min) this.indexWidget.value = max;
    
            if (this.indexWidget.value > max)
                this.indexWidget.value = min;
            this.indexWidget.callback(this.indexWidget.value);
        };
    
        this.controlWidget.beforeQueued = () => {
            // Don't run on first execution
            if (this.controlWidget[HAS_EXECUTED]) {
                applyWidgetControl();
            }
            this.controlWidget[HAS_EXECUTED] = true;
        };

        this.indexWidget.linkedWidgets = [this.controlWidget];
    }
}

function overwriteIndexControl(nodeType) {
    const onNodeCreated = nodeType.prototype.onNodeCreated;
    nodeType.prototype.onNodeCreated = function () {
        onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined;
        this.indexControl = new IndexControl(this);
    }
}

app.registerExtension({
    name: "comfy.ttN.widgets",
    async beforeRegisterNodeDef(nodeType, nodeData, app) {
        if (nodeData.name.startsWith("ttN ") && ["ttN pipeLoader_v2", "ttN pipeKSampler_v2", "ttN pipeKSamplerAdvanced_v2", "ttN pipeLoaderSDXL_v2", "ttN pipeKSamplerSDXL_v2", "ttN KSampler_v2"].includes(nodeData.name)) {
            if (nodeData.output_name.includes('seed')) {
                overwriteSeedControl(nodeType)
            }
        }
        if (["ttN textDebug", "ttN advPlot range", "ttN advPlot string", "ttN advPlot combo", "ttN debugInput"].includes(nodeData.name)) {
            addTextDisplay(nodeType)
        }
        if (nodeData.name.startsWith("ttN textCycle")) {
            overwriteIndexControl(nodeType)
        }
    },
});