-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorsetPanel.js
484 lines (423 loc) · 15.6 KB
/
ColorsetPanel.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import Panel from './Panel.js';
import Modal from './Modal.js';
import Notification from './Notification.js';
import ColorPickerPanel from './ColorPickerPanel.js';
export default class ColorsetPanel extends Panel {
constructor(editor) {
//<div id="colorset-selected-leds" class="selected-leds-bar"></div>
const content = `
<div id="colorset-status">
<button id="colorset-preset-one" class="preset-button"></button>
<button id="colorset-preset-two" class="preset-button"></button>
<button id="colorset-preset-three" class="preset-button"></button>
<button id="colorset-preset-four" class="preset-button"></button>
<button id="colorset-preset-rainbow" class="preset-button"></button>
<button id="colorset-preset-grayscale" class="preset-button"></button>
<button id="colorset-preset-pastel" class="preset-button"></button>
<button id="colorset-preset-dark" class="preset-button"></button>
</div>
<hr id="patternDivider">
<div id="colorset" class="color-row"></div>
`;
super('colorsetPanel', content, 'Colorset');
this.editor = editor
this.lightshow = editor.lightshow;
this.vortexPort = editor.vortexPort;
this.targetLed = 0;
this.targetLeds = [ this.targetLed ];
this.isMulti = false;
}
initialize() {
this.refresh();
// Add event listeners for preset buttons
document.getElementById('colorset-preset-rainbow').addEventListener('click', () => this.applyPreset('rainbow'));
document.getElementById('colorset-preset-grayscale').addEventListener('click', () => this.applyPreset('grayscale'));
document.getElementById('colorset-preset-pastel').addEventListener('click', () => this.applyPreset('pastel'));
document.getElementById('colorset-preset-dark').addEventListener('click', () => this.applyPreset('dark'));
document.getElementById('colorset-preset-one').addEventListener('click', () => this.applyPreset('one'));
document.getElementById('colorset-preset-two').addEventListener('click', () => this.applyPreset('two'));
document.getElementById('colorset-preset-three').addEventListener('click', () => this.applyPreset('three'));
document.getElementById('colorset-preset-four').addEventListener('click', () => this.applyPreset('four'));
// Listen for the modeChange event
document.addEventListener('modeChange', (event) => {
//console.log("Mode change detected by control panel, refreshing");
const selectedLeds = event.detail;
// if array is just multi do this:
if (selectedLeds.includes('multi')) {
this.setTargetMulti();
} else {
this.setTargetSingles(selectedLeds);
}
//console.log('mode changed:', selectedLeds);
this.refresh(true);
this.editor.demoModeOnDevice();
});
document.addEventListener('ledsChange', (event) => {
const selectedLeds = event.detail;
// if array is just multi do this:
if (selectedLeds.includes('multi')) {
this.setTargetMulti();
} else {
this.setTargetSingles(selectedLeds);
}
//console.log('LEDs changed:', this.targetLeds);
this.refresh(true);
});
document.addEventListener('deviceChange', this.handleDeviceEvent.bind(this));
}
applyPreset(preset) {
const cur = this.lightshow.vortex.engine().modes().curMode();
if (!cur) return;
const set = cur.getColorset(this.targetLed);
set.clear();
let numCols = ((Math.random() * 100) % 8);
switch (preset) {
case 'rainbow':
if (numCols < 5) {
numCols = 4 + (numCols % 4);
}
for (let i = 0; i < numCols; i++) {
const hue = (i / numCols) * 360;
const rgb = this.hslToRgb(hue, 1, 0.5); // Full saturation, medium lightness
set.addColor(new this.lightshow.vortexLib.RGBColor(rgb.r, rgb.g, rgb.b));
}
break;
case 'grayscale':
for (let i = 0; i < numCols; i++) {
const gray = Math.floor(Math.random() * 256);
set.addColor(new this.lightshow.vortexLib.RGBColor(gray, gray, gray));
}
break;
case 'pastel':
for (let i = 0; i < numCols; i++) {
const pastel = {
r: Math.floor(Math.random() * 128 + 127),
g: Math.floor(Math.random() * 128 + 127),
b: Math.floor(Math.random() * 128 + 127),
};
set.addColor(new this.lightshow.vortexLib.RGBColor(pastel.r, pastel.g, pastel.b));
}
break;
case 'dark':
for (let i = 0; i < numCols; i++) {
const dark = {
r: Math.floor(Math.random() * 128),
g: Math.floor(Math.random() * 128),
b: Math.floor(Math.random() * 128),
};
set.addColor(new this.lightshow.vortexLib.RGBColor(dark.r, dark.g, dark.b));
}
break;
case 'one':
const color = this.getRandomColor();
set.addColor(new this.lightshow.vortexLib.RGBColor(color.r, color.g, color.b));
break;
case 'two':
for (let i = 0; i < 2; i++) {
const color = this.getRandomColor();
set.addColor(new this.lightshow.vortexLib.RGBColor(color.r, color.g, color.b));
}
break;
case 'three':
for (let i = 0; i < 3; i++) {
const color = this.getRandomColor();
set.addColor(new this.lightshow.vortexLib.RGBColor(color.r, color.g, color.b));
}
break;
case 'four':
for (let i = 0; i < 4; i++) {
const color = this.getRandomColor();
set.addColor(new this.lightshow.vortexLib.RGBColor(color.r, color.g, color.b));
}
break;
default:
break;
}
for (let i = 0; i < this.targetLeds.length; ++i) {
cur.setColorset(set, this.targetLeds[i]);
}
cur.init();
this.lightshow.vortex.engine().modes().saveCurMode();
this.refreshColorset();
this.editor.demoModeOnDevice();
}
hslToRgb(h, s, l) {
const c = (1 - Math.abs(2 * l - 1)) * s;
const x = c * (1 - Math.abs((h / 60) % 2 - 1));
const m = l - c / 2;
let r = 0,
g = 0,
b = 0;
if (h >= 0 && h < 60) {
r = c;
g = x;
b = 0;
} else if (h >= 60 && h < 120) {
r = x;
g = c;
b = 0;
} else if (h >= 120 && h < 180) {
r = 0;
g = c;
b = x;
} else if (h >= 180 && h < 240) {
r = 0;
g = x;
b = c;
} else if (h >= 240 && h < 300) {
r = x;
g = 0;
b = c;
} else if (h >= 300 && h < 360) {
r = c;
g = 0;
b = x;
}
return {
r: Math.round((r + m) * 255),
g: Math.round((g + m) * 255),
b: Math.round((b + m) * 255),
};
}
getRandomColor() {
return {
r: Math.floor(Math.random() * 256),
g: Math.floor(Math.random() * 256),
b: Math.floor(Math.random() * 256),
};
}
async onDeviceSelected(deviceName) {
// nothing yet
}
async onDeviceConnect(deviceName) {
// nothing yet
}
async onDeviceDisconnect(deviceName) {
// nothing yet
}
setTargetSingles(selectedLeds = null) {
if (!selectedLeds) {
const ledCount = this.lightshow.vortex.engine().leds().ledCount();
selectedLeds = []
for (let i = 0; i < ledCount; i++) {
selectedLeds.push(i.toString());
}
}
this.targetLeds = selectedLeds.map(led => parseInt(led, 10));;
this.targetLed = this.targetLeds[0];
this.isMulti = false;
}
setTargetMulti() {
this.targetLed = this.lightshow.vortex.engine().leds().ledMulti();
this.targetLeds = [ this.targetLed ];
this.isMulti = true;
}
refresh(fromEvent = false) {
//this.refreshSelectedLedsBar();
this.refreshColorset(fromEvent);
}
async refreshColorset(fromEvent = false) {
const colorsetElement = document.getElementById('colorset');
const cur = this.lightshow.vortex.engine().modes().curMode();
colorsetElement.innerHTML = ''; // Clear colorset
if (!cur) return;
const set = cur.getColorset(this.targetLed);
const numColors = set ? set.numColors() : 0;
let draggingElement = null;
let dragStartIndex = null;
let placeholder = null;
const createPlaceholder = () => {
placeholder = document.createElement('div');
placeholder.className = 'color-cube placeholder';
placeholder.style.width = `${draggingElement.offsetWidth}px`;
placeholder.style.height = `${draggingElement.offsetHeight}px`;
};
const updatePlaceholder = (target) => {
if (!placeholder || !target || placeholder === target) return;
if (target.classList.contains('empty') || target.classList.contains('add-color')) return; // Skip the + icon
const targetRect = target.getBoundingClientRect();
const placeholderRect = placeholder.getBoundingClientRect();
// Check if dragging to the right or left
const isDraggingRight = draggingElement.getBoundingClientRect().left > targetRect.left;
if (isDraggingRight) {
// Insert the placeholder after the target
colorsetElement.insertBefore(placeholder, target.nextSibling);
} else {
// Insert the placeholder before the target
colorsetElement.insertBefore(placeholder, target);
}
};
const DRAG_THRESHOLD = 5; // Adjust for better responsiveness
let isDragging = false;
let startX = 0;
let startY = 0;
const handlePointerDown = (e) => {
if (e.button !== 0) {
e.preventDefault(); // Prevent default right-click behavior if necessary
return; // Abort drag handling for non-left-clicks
}
const target = e.target.closest('.color-cube');
if (!target || target.classList.contains('empty')) return;
draggingElement = target;
dragStartIndex = parseInt(target.dataset.index, 10);
startX = e.clientX;
startY = e.clientY;
document.addEventListener('pointermove', checkForDragStart);
document.addEventListener('pointerup', cancelDragStart);
};
const checkForDragStart = (e) => {
const movedX = Math.abs(e.clientX - startX);
const movedY = Math.abs(e.clientY - startY);
if (movedX > DRAG_THRESHOLD || movedY > DRAG_THRESHOLD) {
isDragging = true;
draggingElement.classList.add('dragging');
draggingElement.style.position = 'absolute';
draggingElement.style.zIndex = 1000;
draggingElement.style.pointerEvents = 'none';
const rect = colorsetElement.getBoundingClientRect();
draggingElement.style.left = `${e.clientX - rect.left}px`;
draggingElement.style.top = `${e.clientY - rect.top + 70}px`;
createPlaceholder();
colorsetElement.insertBefore(placeholder, draggingElement.nextSibling);
document.addEventListener('pointermove', handlePointerMove);
document.addEventListener('pointerup', handlePointerUp);
document.removeEventListener('pointermove', checkForDragStart);
document.removeEventListener('pointerup', cancelDragStart);
}
};
const cancelDragStart = () => {
document.removeEventListener('pointermove', checkForDragStart);
document.removeEventListener('pointerup', cancelDragStart);
draggingElement = null;
isDragging = false;
};
const handlePointerMove = (e) => {
if (!draggingElement || !isDragging) return;
const rect = colorsetElement.getBoundingClientRect();
draggingElement.style.left = `${e.clientX - rect.left}px`;
draggingElement.style.top = `${e.clientY - rect.top + 70}px`;
const target = document.elementFromPoint(e.clientX, e.clientY)?.closest('.color-cube:not(.dragging):not(.empty)');
updatePlaceholder(target);
};
const handlePointerUp = () => {
if (!draggingElement || !isDragging) return;
const children = Array.from(colorsetElement.children);
let dropIndex = children.indexOf(placeholder);
if (dropIndex > dragStartIndex) dropIndex -= 1; // Adjust for placeholder
if (dragStartIndex !== dropIndex && dropIndex >= 0) {
const set = cur.getColorset(this.targetLed);
set.shift(dragStartIndex, dropIndex);
cur.setColorset(set, this.targetLed);
cur.init();
this.lightshow.vortex.engine().modes().saveCurMode();
}
placeholder?.remove();
placeholder = null;
draggingElement.classList.remove('dragging');
draggingElement.style.position = '';
draggingElement.style.zIndex = '';
draggingElement.style.pointerEvents = '';
draggingElement.style.left = '';
draggingElement.style.top = '';
draggingElement = null;
isDragging = false;
document.removeEventListener('pointermove', handlePointerMove);
document.removeEventListener('pointerup', handlePointerUp);
this.refreshColorset();
};
for (let i = 0; i < 8; i++) {
const container = document.createElement('div');
container.className = 'color-cube';
container.dataset.index = i;
if (i < numColors) {
const col = set.get(i);
const hexColor = `#${((1 << 24) + (col.red << 16) + (col.green << 8) + col.blue).toString(16).slice(1).toUpperCase()}`;
container.style.backgroundColor = hexColor;
container.style.boxShadow = `0 0 10px ${hexColor}`;
// Left-click to open color picker
container.addEventListener('click', () => {
if (!isDragging) {
this.editor.colorPickerPanel.open(i, set, this.updateColor.bind(this));
}
});
// Right-click to delete
container.addEventListener('contextmenu', (e) => {
e.preventDefault();
this.delColor(i);
});
container.addEventListener('pointerdown', handlePointerDown);
} else if (i === numColors) {
container.classList.add('add-color');
container.textContent = '+';
container.addEventListener('click', () => {
this.addColor();
});
} else {
container.classList.add('empty');
}
colorsetElement.appendChild(container);
}
}
// Helper: Convert Hex to RGB
hexToRGB(hex) {
let bigint = parseInt(hex.replace(/^#/, ''), 16);
return { r: (bigint >> 16) & 255, g: (bigint >> 8) & 255, b: bigint & 255 };
}
updateColor(index, hexValue, isDragging) {
let hex = hexValue ? hexValue.replace(/^#/, '') : 0;
let bigint = parseInt(hex, 16);
let r = (bigint >> 16) & 255;
let g = (bigint >> 8) & 255;
let b = bigint & 255;
const cur = this.lightshow.vortex.engine().modes().curMode();
if (!cur) {
return;
}
let set = cur.getColorset(this.targetLed);
let col = new this.lightshow.vortexLib.RGBColor(r, g, b);
set.set(index, col);
this.targetLeds.forEach(led => {
cur.setColorset(set, led);
});
// re-initialize the demo mode because num colors may have changed
cur.init();
// save
this.lightshow.vortex.engine().modes().saveCurMode();
// refresh
this.refreshColorset();
if (isDragging) {
this.editor.demoColorOnDevice(col);
} else {
// demo on device
this.editor.demoModeOnDevice();
}
}
addColor() {
this.lightshow.addColor(255, 255, 255, this.targetLeds);
this.refreshColorset();
// demo on device
this.editor.demoModeOnDevice();
}
delColor(index) {
const cur = this.lightshow.vortex.engine().modes().curMode();
if (!cur) {
return;
}
let set = cur.getColorset(this.targetLed);
if (set.numColors() <= 0) {
return;
}
set.removeColor(index);
this.targetLeds.forEach(led => {
cur.setColorset(set, led);
});
// re-initialize the demo mode because num colors may have changed
cur.init();
// save
this.lightshow.vortex.engine().modes().saveCurMode();
// refresh
this.refreshColorset();
// demo on device
this.editor.demoModeOnDevice();
}
}