Skip to content

Commit

Permalink
attempt change
Browse files Browse the repository at this point in the history
  • Loading branch information
Unreal-Dan committed Dec 22, 2024
1 parent dec2827 commit f3ebee9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 125 deletions.
172 changes: 48 additions & 124 deletions js/ModesPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,8 @@ export default class ModesPanel extends Panel {
});
}

addMode() {
let modeCount = this.lightshow.vortex.numModes();
getMaxModes(device) {
let maxModes = 16;
const device = this.editor.devicePanel.selectedDevice;
switch (device) {
case 'Orbit':
case 'Handle':
Expand All @@ -347,7 +345,14 @@ export default class ModesPanel extends Panel {
default:
break;
}
return maxModes;
}

addMode() {
let modeCount = this.lightshow.vortex.numModes();
// check the mode count against max
const device = this.editor.devicePanel.selectedDevice;
const maxModes = this.getMaxModes(device)
if (modeCount >= maxModes) {
Notification.failure(`The ${device} can only hold ${maxModes} modes`);
return;
Expand Down Expand Up @@ -417,144 +422,55 @@ export default class ModesPanel extends Panel {
placeholder: 'Paste a JSON mode',
title: 'Import/Paste a Mode',
onInput: (event) => {
this.importModeFromData(event.target.value);
this.importModeFromData(JSON.parse(event.target.value));
}
});
this.importModal.selectText();
}

importPatternFromData(patternJson) {
if (!patternJson) {
Notification.failure("No pattern data");
return;
}
let patternData;
try {
patternData = JSON.parse(patternJson);
} catch (error) {
Notification.failure("Invalid JSON pattern");
return;
}
importPatternFromData(patternData, addNew = false) {
if (!patternData) {
Notification.failure("Invalid pattern data");
return;
}
if ('num_leds' in patternData) {
let initialDevice = null;
switch (patternData.num_leds) {
case 28:
this.lightshow.setLedCount(28);
initialDevice = 'Orbit';
break;
case 3:
this.lightshow.setLedCount(3);
initialDevice = 'Handle';
break;
case 10:
this.lightshow.setLedCount(10);
initialDevice = 'Gloves';
break;
case 20:
this.lightshow.setLedCount(20);
initialDevice = 'Chromadeck';
break;
case 6:
this.lightshow.setLedCount(6);
initialDevice = 'Spark';
break;
case 2:
this.lightshow.setLedCount(2);
initialDevice = 'Duo';
break;
default:
// technically this doesn't really need to be done, the engine starts at 1
this.lightshow.setLedCount(1);
break;
}
this.refreshModeList();
this.renderLedIndicators(initialDevice);
this.handleLedSelectionChange();
// Change the height of the #modesListScrollContainer when the device connects
const modesListScrollContainer = document.getElementById('modesListScrollContainer');
if (modesListScrollContainer) {
modesListScrollContainer.style.height = '200px';
}
return this.importModeFromData(patternJson, false);
}

let curSel;
const cur = this.lightshow.vortex.engine().modes().curMode();

if (!patternData.colorset) {
Notification.failure("Invalid pattern data");
Notification.failure("No pattern data");
return;
}

const set = new this.lightshow.vortexLib.Colorset();
patternData.colorset.forEach(hexCode => {
const normalizedHex = hexCode.replace('0x', '#');
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(normalizedHex);
if (result) {
set.addColor(new this.lightshow.vortexLib.RGBColor(
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16)
));
}
});

cur.setColorset(set, 0); // Assuming the pattern data is for the first pattern slot
const patID = this.lightshow.vortexLib.intToPatternID(patternData.pattern_id);
cur.setPattern(patID, 0, null, null);
const args = new this.lightshow.vortexLib.PatternArgs();
patternData.args.forEach(arg => args.addArgs(arg));
this.lightshow.vortex.setPatternArgs(this.lightshow.vortex.engine().leds().ledCount(), args, 0);

cur.init();
this.lightshow.vortex.engine().modes().saveCurMode();

this.refreshPatternControlPanel();
this.refresh();
Notification.success("Successfully imported pattern");
const modeData = {
flags: 6,
num_leds: 1,
single_pats: [
patternData
]
};
return this.importModeFromData(modeData, addNew);
}

importModeFromData(modeJson, addNew = true) {
if (!modeJson) {
Notification.failure("No mode data");
return;
}
let modeData;
try {
modeData = JSON.parse(modeJson);
} catch (error) {
Notification.failure("Invalid JSON mode");
return;
}
importModeFromData(modeData, addNew = true) {
if (!modeData) {
Notification.failure("Invalid mode data");
return;
}

const patterns = modeData.single_pats ? modeData.single_pats : [modeData.multi_pat];
if (!patterns) {
console.log("Patterns empty!");
Notification.failure("No mode data");
return;
}
let initialDevice = null;
const deviceMap = {
28: 'Orbit',
3: 'Handle',
10: 'Gloves',
20: 'Chromadeck',
6: 'Spark',
2: 'Duo'
};
initialDevice = deviceMap[modeData.num_leds] || (() => {
modeData.num_leds = 1; // Default case
})();
this.lightshow.setLedCount(modeData.num_leds);
let curSel;
if (addNew) {
curSel = this.lightshow.vortex.engine().modes().curModeIndex();
let modeCount = this.lightshow.vortex.numModes();
switch (this.editor.devicePanel.selectedDevice) {
case 'Orbit':
case 'Handle':
case 'Gloves':
if (modeCount >= 14) {
Notification.failure("This device can only hold 14 modes");
return;
}
break;
default:
break;
// check the mode count against max
const device = this.editor.devicePanel.selectedDevice;
const maxModes = this.getMaxModes(device)
if (modeCount >= maxModes) {
Notification.failure(`The ${device} can only hold ${maxModes} modes`);
return;
}
if (!this.lightshow.vortex.addNewMode(false)) {
Notification.failure("Failed to add another mode");
Expand All @@ -573,6 +489,11 @@ export default class ModesPanel extends Panel {
// actually might have been the fetching of cur once for all operations
cur.init();

const patterns = modeData.single_pats ? modeData.single_pats : [modeData.multi_pat];
if (!patterns) {
console.log("Patterns empty!");
return;
}
patterns.forEach((pat, index) => {
if (!pat.colorset) {
Notification.failure("Invalid pattern data");
Expand Down Expand Up @@ -610,6 +531,9 @@ export default class ModesPanel extends Panel {
this.lightshow.vortex.setCurMode(curSel, false);
}

this.refreshModeList();
this.editor.ledSelectPanel.renderLedIndicators(initialDevice);
this.editor.ledSelectPanel.handleLedSelectionChange();
this.refreshPatternControlPanel();
this.refresh();
Notification.success("Successfully imported mode");
Expand Down
29 changes: 28 additions & 1 deletion js/VortexEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,33 @@ export default class VortexEditor {
this.updatePanel.show();
}
});

// detect the postmessage from vortex community to send over a mode
window.addEventListener('message', (event) => {
console.log('Received message:', event);
if (event.origin !== 'https://vortex.community') {
console.warn('Rejected message from unauthorized origin:', event.origin);
return;
}
const { type, data } = event.data;
const parsedData = JSON.parse(atob(data));
if (type === 'mode') {
try {
this.modesPanel.importModeFromData(parsedData, true);
console.log('Mode loaded successfully via postMessage');
} catch (error) {
console.error('Error loading mode via postMessage:', error);
}
}
if (type === 'pattern') {
try {
this.modesPanel.importPatternFromData(parsedData, true);
console.log('Mode loaded successfully via postMessage');
} catch (error) {
console.error('Error loading pattern via postMessage:', error);
}
}
});
}

importModeDataFromUrl() {
Expand All @@ -141,7 +168,7 @@ export default class VortexEditor {

if (encodedData) {
try {
this.modesPanel.importPatternFromData(atob(encodedData), false);
this.modesPanel.importPatternFromData(JSON.parse(atob(encodedData)), false);
} catch (error) {
console.error('Error parsing mode data:', error);
}
Expand Down

0 comments on commit f3ebee9

Please sign in to comment.