MaterialXLab API  0.0.1
APIs For MaterialXLab Libraries
Loading...
Searching...
No Matches
JsPublishInputs.js
Go to the documentation of this file.
7 this.sortable = null;
8 }
9
13 hexToRgb(hex) {
14 let r = 0, g = 0, b = 0;
15 // 3 digits
16 if (hex.length === 4) {
17 r = parseInt(hex[1] + hex[1], 16);
18 g = parseInt(hex[2] + hex[2], 16);
19 b = parseInt(hex[3] + hex[3], 16);
20 }
21 // 6 digits
22 else if (hex.length === 7) {
23 r = parseInt(hex[1] + hex[2], 16);
24 g = parseInt(hex[3] + hex[4], 16);
25 b = parseInt(hex[5] + hex[6], 16);
26 }
27 return [r, g, b];
28 }
29
33 rgbToHex(r, g, b) {
34 return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
35 }
36
42 createRow(data, index, auto_assign_default = true) {
43
44 //console.log('------- Creating row for data:', data);
45
46 let boxStyle = "width: 100%; font-size: 12px; border:solid thin lightskyblue; background-color: #000000"
47
48 // Create a new div with class "row"
49 let row = document.createElement("div");
50 row.classList.add("row", "row_hover", "pb-2", "pt-2", 'g-1');
51
52 // String Name Column (col)
53 let nameCol = document.createElement("div");
54 nameCol.classList.add("col-sm-2", "text-start");
55 let nameLabel = document.createElement("label");
56 nameLabel.textContent = data.name;
57 nameLabel.classList.add("form-label");
58 nameCol.appendChild(nameLabel);
59 row.appendChild(nameCol);
60
61 // Type Column (Read-only) (col)
62 let typeCol = document.createElement("div");
63 typeCol.classList.add("col-sm-2");
64 let typeLabel = document.createElement("label");
65 typeLabel.textContent = data.type;
66 typeLabel.classList.add("form-label");
67 typeCol.setAttribute("data-type", data.type); // Add type to the element's data attributes
68 typeCol.appendChild(typeLabel);
69 row.appendChild(typeCol);
70
71 // UI Name Column (Editable) (col)
72 let uinameCol = document.createElement("div");
73 uinameCol.classList.add("col-sm");
74 let uinameInput = document.createElement("input");
75 uinameInput.type = "text";
76 uinameInput.style = boxStyle;
77 uinameInput.classList.add("form-control", "form-control-sm");
78 uinameInput.value = data.uiname || "";
79 uinameInput.setAttribute("data-ui-name", "true"); // Set data attribute for identification
80 uinameCol.appendChild(uinameInput);
81 row.appendChild(uinameCol);
82
83 // UI group Column (Editable) (col)
84 let uifolderCol = document.createElement("div");
85 uifolderCol.classList.add("col-sm");
86 let uifolderInput = document.createElement("input");
87 uifolderInput.type = "text";
88 uifolderInput.style = boxStyle;
89 uifolderInput.classList.add("form-control", "form-control-sm");
90 uifolderInput.value = data.uifolder || ""
91 uifolderInput.setAttribute("data-ui-group", "true"); // Set data attribute for identification
92 uifolderCol.appendChild(uifolderInput);
93 row.appendChild(uifolderCol);
94
95 // Add uimin, uimax, uistep for vectors, integers and floats
96 // Make list for data.uimin, data.uimax, data.uistep
97 let ui_items = [data.uimin, data.uimax, data.uistep, data.uisoftmin, data.uisoftmax];
98 let ui_item_ids = ['data-ui-min', 'data-ui-max', 'data-ui-step', 'data-ui-softmin', 'data-ui-softmax'];
99
100 for (let u = 0; u < 5; u++) {
101
102 // Input Column (dynamically filled based on type) (col)
103 let inputCol = document.createElement("div");
104 inputCol.classList.add("col-sm");
105
106 let ui_item = ui_items[u];
107 let ui_item_id = ui_item_ids[u];
108 let ui_default = 0;
109 if (u == 1 || u == 4) {
110 if (data.type === 'integer')
111 ui_default = 10
112 else
113 ui_default = 1;
114 }
115 else if (u == 2) {
116 if (data.type === 'integer')
117 ui_default = 1
118 else
119 ui_default = 0.01;
120 }
121
122 if (data.type === "float" || data.type == "integer") {
123 // Single input for int and float type
124 let input = document.createElement("input");
125 input.setAttribute(ui_item_id, "true"); // Set data attribute for identification
126 input.type = "number";
127 input.style = boxStyle;
128 input.classList.add("form-control", "form-control-sm");
129 if (ui_item != undefined)
130 input.value = ui_item
131 else
132 input.value = (auto_assign_default ? ui_default : null);
133 inputCol.appendChild(input);
134 }
135 else if (data.type === "vector2" || data.type == "vector3" || data.type == "vector4") {
136 let count = 0;
137 if (data.type === "vector2") {
138 count = 2;
139 }
140 else if (data.type === "vector3") {
141 count = 3;
142 }
143 else if (data.type === "vector4") {
144 count = 4;
145 }
146
147 // Add a div container for all the vector inputs
148 let container = document.createElement("div");
149 container.classList.add("container", "p-2", "rounded-2");
150 // Add a border
151 container.style.border = "1px solid #f0f0f0";
152
153 for (let i = 0; i < count; i++) {
154
155 let input = document.createElement("input");
156 input.type = "number";
157 input.style = boxStyle;
158 input.classList.add("form-control", "form-control-sm", "me-2", "mb-2");
159 if (ui_item && ui_item[i] != undefined)
160 {
161 //console.log('ui_item[i]', ui_item[i], i);
162 input.value = ui_item[i]
163 }
164 else
165 {
166 //console.log('ui_default', ui_default);
167 input.value = (auto_assign_default ? ui_default : null);
168 }
169 input.setAttribute(ui_item_id, "true"); // Set data attribute for identification
170
171 container.appendChild(input);
172 }
173 inputCol.appendChild(container);
174 }
175 row.appendChild(inputCol);
176 }
177 return row;
178 }
179
184 populateContainer(sampleData, auto_assign_default = true) {
185 const inputContainer = document.getElementById("input-container");
186
187 if (this.sortable)
188 {
189 this.sortable.destroy();
190 this.sortable = null;
191 }
192
193 // Remove all children from the container
194 inputContainer.innerHTML = "";
195
196 sampleData.forEach((data, index) => {
197 let row = this.createRow(data, index, auto_assign_default);
198 inputContainer.appendChild(row);
199 });
200
201 // Initialize SortableJS for reordering rows
202 this.sortable = new Sortable(inputContainer, {
203 animation: 150,
204 ghostClass: "sortable-ghost", // Class name for the drop placeholder
205 chosenClass: "sortable-ghost" // Class name for the dragged element
206 });
207 }
208
215 const rows = document.querySelectorAll("#input-container .row");
216 const extractedData = [];
217
218 rows.forEach(row => {
219 const name = row.querySelector("label").textContent;
220 const uiname = row.querySelector("input[data-ui-name]").value;
221 const uifolder = row.querySelector("input[data-ui-group]").value;
222 const type = row.querySelector("[data-type]").textContent;
223
224 let uimin = null;
225 let uimax = null;
226 let uistep = null;
227 let uisoftmin = null;
228 let uisoftmax = null;
229 if (type === "integer" || type === "float" || type === "vector2" || type === "vector3" || type == "vector4") {
230 let inputs = row.querySelectorAll("input[data-ui-min]");
231 uimin = Array.from(inputs).map(input => input.value && input.value !== '' ? parseFloat(input.value) : null);
232 if (uimin.some(value => value === null)) {
233 uimin = null;
234 }
235 //Array.from(inputs).map(input => console.log('ui min value:', uimin));
236
237 inputs = row.querySelectorAll("input[data-ui-max]");
238 uimax = Array.from(inputs).map(input => input.value && input.value !== '' ? parseFloat(input.value) : null);
239 if (uimax.some(value => value === null)) {
240 uimax = null;
241 }
242
243 inputs = row.querySelectorAll("input[data-ui-step]");
244 uistep = Array.from(inputs).map(input => input.value && input.value !== '' ? parseFloat(input.value) : null);
245 if (uistep.some(value => value === null)) {
246 uistep = null;
247 }
248
249 inputs = row.querySelectorAll("input[data-ui-softmin]");
250 uisoftmin = Array.from(inputs).map(input => input.value && input.value !== '' ? parseFloat(input.value) : null);
251 if (uisoftmin.some(value => value === null)) {
252 uisoftmin = null;
253 }
254
255 inputs = row.querySelectorAll("input[data-ui-softmax]");
256 uisoftmax = Array.from(inputs).map(input => input.value && input.value !== '' ? parseFloat(input.value) : null);
257 if (uisoftmax.some(value => value === null)) {
258 uisoftmax = null;
259 }
260 }
261 extractedData.push({ name, type, uiname, uifolder, uimin, uimax, uistep, uisoftmin, uisoftmax });
262 });
263
264 return extractedData;
265 }
266
267}
268
createRow(data, index, auto_assign_default=true)
Function to create a row per input with columns for each metadata attribute as applicable.
populateContainer(sampleData, auto_assign_default=true)
Function to populate the input container with sample data We make it so that the rows can be reordere...
readData()
Function to read the data from the input fields and return an array of objects containing the metadat...
hexToRgb(hex)
Helper function to convert hex to RGB.
constructor()
Class to create an an editor to add / modify metadata attributes for inputs.
rgbToHex(r, g, b)
Helper function to convert RGB to hex.