File size: 4,284 Bytes
4450790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { app } from "../../../scripts/app.js";

// Adds context menu entries, code partly from pyssssscustom-scripts

function addMenuHandler(nodeType, cb) {
	const getOpts = nodeType.prototype.getExtraMenuOptions;
	nodeType.prototype.getExtraMenuOptions = function () {
		const r = getOpts.apply(this, arguments);
		cb.apply(this, arguments);
		return r;
	};
}

function addNode(name, nextTo, options) {
	console.log("name:", name);
	console.log("nextTo:", nextTo);
	options = { side: "left", select: true, shiftY: 0, shiftX: 0, ...(options || {}) };
	const node = LiteGraph.createNode(name);
	app.graph.add(node);
	
	node.pos = [
		options.side === "left" ? nextTo.pos[0] - (node.size[0] + options.offset): nextTo.pos[0] + nextTo.size[0] + options.offset,
		
		nextTo.pos[1] + options.shiftY,
	];
	if (options.select) {
		app.canvas.selectNode(node, false);
	}
	return node;
}

app.registerExtension({
	name: "KJNodesContextmenu",
	async beforeRegisterNodeDef(nodeType, nodeData, app) {
		if (nodeData.input && nodeData.input.required) {
			addMenuHandler(nodeType, function (_, options) {
				options.unshift(
					{
					content: "Add GetNode",
					callback: () => {addNode("GetNode", this, { side:"left", offset: 30});}
					},
					{
					content: "Add SetNode",
					callback: () => {addNode("SetNode", this, { side:"right", offset: 30 });
					},
				});
			});
		}
	},
		async setup(app) {
			const onChange = (value) => {
				if (value) {
					const valuesToAddToIn = ["GetNode"];
					const valuesToAddToOut = ["SetNode"];
			
					for (const arr of Object.values(LiteGraph.slot_types_default_in)) {
						for (const valueToAdd of valuesToAddToIn) {
							const idx = arr.indexOf(valueToAdd);
							if (idx !== -1) {
								arr.splice(idx, 1);
							}
							arr.unshift(valueToAdd);
						}
					}
			
					for (const arr of Object.values(LiteGraph.slot_types_default_out)) {
						for (const valueToAdd of valuesToAddToOut) {
							const idx = arr.indexOf(valueToAdd);
							if (idx !== -1) {
								arr.splice(idx, 1);
							}
							arr.unshift(valueToAdd);
						}
					}
				}
			};
			
			app.ui.settings.addSetting({
				id: "KJNodes.SetGetMenu",
				name: "KJNodes: Make Set/Get -nodes defaults (turn off and reload to disable)",
				defaultValue: false,
				type: "boolean",
				options: (value) => [
					{
						value: true,
						text: "On",
						selected: value === true,
					},
					{
						value: false,
						text: "Off",
						selected: value === false,
					},
				],
				onChange: onChange,
				
			});
			app.ui.settings.addSetting({
				id: "KJNodes.DisableMiddleClickDefault",
				name: "KJNodes: Middle click default node adding",
				defaultValue: false,
				type: "boolean",
				options: (value) => [
					{ value: true, text: "On", selected: value === true },
					{ value: false, text: "Off", selected: value === false },
				],
				onChange: (value) => {
					LiteGraph.middle_click_slot_add_default_node = value;
				},
			});
			app.ui.settings.addSetting({
				id: "KJNodes.nodeAutoColor",
				name: "KJNodes: Automatically set node colors",
				defaultValue: true,
				type: "boolean",
				options: (value) => [
					{ value: true, text: "On", selected: value === true },
					{ value: false, text: "Off", selected: value === false },
				],
			});
			app.ui.settings.addSetting({
				id: "KJNodes.helpPopup",
				name: "KJNodes: Help popups",
				defaultValue: true,
				type: "boolean",
				options: (value) => [
					{ value: true, text: "On", selected: value === true },
					{ value: false, text: "Off", selected: value === false },
				],
			});
			app.ui.settings.addSetting({
				id: "KJNodes.disablePrefix",
				name: "KJNodes: Disable automatic Set_ and Get_ prefix",
				defaultValue: false,
				type: "boolean",
				options: (value) => [
					{ value: true, text: "On", selected: value === true },
					{ value: false, text: "Off", selected: value === false },
				],
			});
			app.ui.settings.addSetting({
				id: "KJNodes.browserStatus",
				name: "KJNodes: 🟢 Stoplight browser status icon 🔴",
				defaultValue: false,
				type: "boolean",
				options: (value) => [
					{ value: true, text: "On", selected: value === true },
					{ value: false, text: "Off", selected: value === false },
				],
			});
}
});