Constellations/js/Mode/Edit.js

73 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

import GUI;
import Grid;
import Grid.Color;
import Mode;
import Toolbox;
import Share;
var down = false;
2022-08-10 21:12:39 +02:00
Grid.get().root.addEventListener('mouseleave', function() {
down = false;
});
var save = document.getElementById('save');
return {
events: {
onEnter: onEnter,
onLeave: onLeave,
onMousedown: onMousedown,
onMouseup: onMouseup,
onMouseenter: onMouseenter
}
};
function onEnter() {
2022-08-10 21:12:39 +02:00
GUI.activate(true, [Grid.get().root, Toolbox.get(), save]);
if(!Grid.get().missing.isEmpty()) {
Mode.setEnabled(false, ['play', 'solve']);
} else {
Share.link(Grid.get().colors);
}
}
function onLeave() {
2022-08-10 21:12:39 +02:00
GUI.activate(false, [Grid.get().root, Toolbox.get(), save, Share.get()]);
}
2022-08-10 21:12:39 +02:00
function onMousedown(e, cell) {
if(e.button == GUI.mouse.left) {
down = true;
if(Toolbox.tool() == 'draw') {
2022-08-10 21:12:39 +02:00
colorCell(cell);
}
}
}
2022-08-10 21:12:39 +02:00
function onMouseup(e, cell) {
if(e.button == GUI.mouse.left) {
down = false;
if(Toolbox.tool() == 'paint') {
2022-08-10 21:12:39 +02:00
Grid.Color.paint(cell);
checkCompleteness();
}
}
}
2022-08-10 21:12:39 +02:00
function onMouseenter(e, cell) {
if(down && Toolbox.tool() == 'draw') {
2022-08-10 21:12:39 +02:00
colorCell(cell);
}
}
2022-08-10 21:12:39 +02:00
function colorCell(cell) {
Grid.Color.ize(cell);
checkCompleteness();
}
function checkCompleteness() {
if(Grid.get().missing.isEmpty()) {
Mode.setEnabled(true, ['play', 'solve']);
Share.link(Grid.get().colors);
}
}