2020-04-04 10:41:56 +03:00
|
|
|
var mainMenuItems = document.getElementById('main-menu').children;
|
2019-03-27 02:20:54 +03:00
|
|
|
|
|
|
|
//for each button in main menu (starting at 1 to avoid logo)
|
|
|
|
for (var i = 1; i < mainMenuItems.length; i++) {
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
//get the button that's in the list item
|
|
|
|
var menuItem = mainMenuItems[i];
|
|
|
|
var menuButton = menuItem.children[0];
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//when you click a main menu items button
|
|
|
|
on('click', menuButton, function (e, button) {
|
2020-04-04 10:41:56 +03:00
|
|
|
select(button.parentElement);
|
2019-03-27 02:20:54 +03:00
|
|
|
});
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
var subMenu = menuItem.children[1];
|
|
|
|
var subMenuItems = subMenu.children;
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//when you click an item within a menu button
|
|
|
|
for (var j = 0; j < subMenuItems.length; j++) {
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
var subMenuItem = subMenuItems[j];
|
|
|
|
var subMenuButton = subMenuItem.children[0];
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
subMenuButton.addEventListener('click', function (e) {
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
switch(this.textContent) {
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
//File Menu
|
|
|
|
case 'New':
|
|
|
|
showDialogue('new-pixel');
|
2020-06-27 14:29:28 +03:00
|
|
|
break;
|
|
|
|
case 'Save project':
|
|
|
|
//create name
|
|
|
|
var selectedPalette = getText('palette-button');
|
|
|
|
if (selectedPalette != 'Choose a palette...'){
|
|
|
|
var paletteAbbreviation = palettes[selectedPalette].abbreviation;
|
|
|
|
var fileName = 'pixel-'+paletteAbbreviation+'-'+canvasSize[0]+'x'+canvasSize[1]+'.lpe';
|
|
|
|
} else {
|
|
|
|
var fileName = 'pixel-'+canvasSize[0]+'x'+canvasSize[1]+'.lpe';
|
|
|
|
selectedPalette = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
//set download link
|
|
|
|
var linkHolder = document.getElementById('save-project-link-holder');
|
|
|
|
// create file content
|
|
|
|
var content = getProjectData();
|
|
|
|
|
|
|
|
linkHolder.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(content);
|
|
|
|
linkHolder.download = fileName;
|
|
|
|
linkHolder.click();
|
|
|
|
|
|
|
|
ga('send', 'event', 'Pixel Editor Save', selectedPalette, canvasSize[0]+'/'+canvasSize[1]); /*global ga*/
|
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
break;
|
|
|
|
case 'Open':
|
|
|
|
//if a document exists
|
|
|
|
if (documentCreated) {
|
|
|
|
//check if the user wants to overwrite
|
|
|
|
if (confirm('Opening a pixel will discard your current one. Are you sure you want to do that?'))
|
|
|
|
//open file selection dialog
|
|
|
|
document.getElementById('open-image-browse-holder').click();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
//open file selection dialog
|
|
|
|
document.getElementById('open-image-browse-holder').click();
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
break;
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-06-27 14:29:28 +03:00
|
|
|
case 'Export':
|
2020-04-04 10:41:56 +03:00
|
|
|
if (documentCreated) {
|
|
|
|
//create name
|
|
|
|
var selectedPalette = getText('palette-button');
|
|
|
|
if (selectedPalette != 'Choose a palette...'){
|
|
|
|
var paletteAbbreviation = palettes[selectedPalette].abbreviation;
|
|
|
|
var fileName = 'pixel-'+paletteAbbreviation+'-'+canvasSize[0]+'x'+canvasSize[1]+'.png';
|
|
|
|
} else {
|
|
|
|
var fileName = 'pixel-'+canvasSize[0]+'x'+canvasSize[1]+'.png';
|
|
|
|
selectedPalette = 'none';
|
|
|
|
}
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
//set download link
|
|
|
|
var linkHolder = document.getElementById('save-image-link-holder');
|
2020-06-24 00:10:56 +03:00
|
|
|
// Creating a tmp canvas to flatten everything
|
|
|
|
var exportCanvas = document.createElement("canvas");
|
|
|
|
var emptyCanvas = document.createElement("canvas");
|
2020-06-27 14:29:28 +03:00
|
|
|
var layersCopy = layers.slice();
|
2020-06-24 00:10:56 +03:00
|
|
|
|
|
|
|
exportCanvas.width = canvasSize[0];
|
|
|
|
exportCanvas.height = canvasSize[1];
|
|
|
|
|
|
|
|
emptyCanvas.width = canvasSize[0];
|
|
|
|
emptyCanvas.height = canvasSize[1];
|
|
|
|
|
|
|
|
// Sorting the layers by z index
|
2020-06-27 14:29:28 +03:00
|
|
|
layersCopy.sort((a, b) => (a.canvas.style.zIndex > b.canvas.style.zIndex) ? 1 : -1);
|
2020-06-24 00:10:56 +03:00
|
|
|
|
|
|
|
// Merging every layer on the export canvas
|
|
|
|
for (let i=0; i<layersCopy.length; i++) {
|
|
|
|
if (layersCopy[i].menuEntry != null && layersCopy[i].isVisible) {
|
|
|
|
mergeLayers(exportCanvas.getContext('2d'), layersCopy[i].context);
|
|
|
|
}
|
|
|
|
// I'm not going to find out why the layer ordering screws up if you don't copy
|
|
|
|
// a blank canvas when layers[i] is not set as visible, but if you have time to
|
|
|
|
// spend, feel free to investigate (comment the else, create 3 layers: hide the
|
|
|
|
// middle one and export, the other 2 will be swapped in their order)
|
|
|
|
else {
|
|
|
|
mergeLayers(exportCanvas.getContext('2d'), emptyCanvas.getContext('2d'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
linkHolder.href = exportCanvas.toDataURL();
|
2020-04-04 10:41:56 +03:00
|
|
|
linkHolder.download = fileName;
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
linkHolder.click();
|
2020-03-08 01:46:25 +03:00
|
|
|
|
2020-06-24 00:10:56 +03:00
|
|
|
emptyCanvas.remove();
|
|
|
|
exportCanvas.remove();
|
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
//track google event
|
2020-06-27 14:29:28 +03:00
|
|
|
ga('send', 'event', 'Pixel Editor Export', selectedPalette, canvasSize[0]+'/'+canvasSize[1]); /*global ga*/
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Exit':
|
|
|
|
|
|
|
|
console.log('exit');
|
|
|
|
//if a document exists, make sure they want to delete it
|
|
|
|
if (documentCreated) {
|
|
|
|
|
|
|
|
//ask user if they want to leave
|
|
|
|
if (confirm('Exiting will discard your current pixel. Are you sure you want to do that?'))
|
|
|
|
//skip onbeforeunload prompt
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
else
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
//Edit Menu
|
|
|
|
case 'Undo':
|
|
|
|
undo();
|
|
|
|
break;
|
|
|
|
case 'Redo':
|
|
|
|
redo();
|
|
|
|
break;
|
|
|
|
|
|
|
|
//Palette Menu
|
|
|
|
case 'Add color':
|
|
|
|
addColor('#eeeeee');
|
|
|
|
break;
|
2020-06-06 22:44:52 +03:00
|
|
|
// SELECTION MENU
|
|
|
|
case 'Paste':
|
|
|
|
pasteSelection();
|
|
|
|
break;
|
|
|
|
case 'Copy':
|
|
|
|
copySelection();
|
|
|
|
tool.pencil.switchTo();
|
|
|
|
break;
|
|
|
|
case 'Cut':
|
|
|
|
cutSelectionTool();
|
|
|
|
tool.pencil.switchTo();
|
|
|
|
break;
|
|
|
|
case 'Cancel':
|
|
|
|
tool.pencil.switchTo();
|
|
|
|
break;
|
2020-04-04 10:41:56 +03:00
|
|
|
//Help Menu
|
|
|
|
case 'Settings':
|
|
|
|
//fill form with current settings values
|
|
|
|
setValue('setting-numberOfHistoryStates', settings.numberOfHistoryStates);
|
|
|
|
|
|
|
|
showDialogue('settings');
|
|
|
|
break;
|
|
|
|
//Help Menu
|
|
|
|
case 'Help':
|
|
|
|
showDialogue('help');
|
|
|
|
break;
|
|
|
|
case 'About':
|
|
|
|
showDialogue('about');
|
|
|
|
break;
|
|
|
|
case 'Changelog':
|
|
|
|
showDialogue('changelog');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
closeMenu();
|
|
|
|
});
|
|
|
|
}
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function closeMenu () {
|
2020-04-04 10:41:56 +03:00
|
|
|
//remove .selected class from all menu buttons
|
|
|
|
for (var i = 0; i < mainMenuItems.length; i++) {
|
|
|
|
deselect(mainMenuItems[i]);
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 14:29:28 +03:00
|
|
|
|
|
|
|
function getProjectData() {
|
|
|
|
// use a dictionary
|
2020-06-28 17:57:19 +03:00
|
|
|
let dictionary = {};
|
|
|
|
// sorting layers by increasing z-index
|
|
|
|
let layersCopy = layers.slice();
|
|
|
|
layersCopy.sort((a, b) => (a.canvas.style.zIndex > b.canvas.style.zIndex) ? 1 : -1);
|
2020-07-21 00:33:17 +03:00
|
|
|
// save canvas size
|
2020-06-28 17:57:19 +03:00
|
|
|
dictionary['canvasWidth'] = currentLayer.canvasSize[0];
|
|
|
|
dictionary['canvasHeight'] = currentLayer.canvasSize[1];
|
2020-07-21 00:33:17 +03:00
|
|
|
// save editor mode
|
|
|
|
dictionary['editorMode'] = pixelEditorMode;
|
|
|
|
// save palette
|
2020-06-27 14:29:28 +03:00
|
|
|
for (let i=0; i<currentPalette.length; i++) {
|
2020-06-28 17:57:19 +03:00
|
|
|
dictionary["color" + i] = currentPalette[i];
|
2020-06-27 14:29:28 +03:00
|
|
|
}
|
2020-06-28 17:57:19 +03:00
|
|
|
|
2020-07-21 00:33:17 +03:00
|
|
|
// save number of layers
|
2020-06-28 17:57:19 +03:00
|
|
|
dictionary["nLayers"] = layersCopy.length;
|
|
|
|
|
2020-07-21 00:33:17 +03:00
|
|
|
// save layers
|
2020-06-28 17:57:19 +03:00
|
|
|
for (let i=0; i<layersCopy.length; i++) {
|
2020-06-27 14:29:28 +03:00
|
|
|
// Only saving the layers the user has access to (no vfx, tmp or checkerboard layers)
|
2020-06-28 17:57:19 +03:00
|
|
|
if (layersCopy[i].menuEntry != null) {
|
|
|
|
dictionary["layer" + i] = layersCopy[i];
|
|
|
|
dictionary["layer" + i + "ImageData"] = layersCopy[i].canvas.toDataURL();
|
2020-06-27 14:29:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(dictionary);
|
|
|
|
}
|