Constellations/js/Mode/Edit.js

73 lines
1.4 KiB
JavaScript
Raw Normal View History

import GUI;
import Grid;
import Grid.Color;
import Mode;
import Toolbox;
import Share;
var down = false;
Grid.get().element.addEventListener('mouseleave', function() {
down = false;
});
var save = document.getElementById('save');
return {
events: {
onEnter: onEnter,
onLeave: onLeave,
onMousedown: onMousedown,
onMouseup: onMouseup,
onMouseenter: onMouseenter
}
};
function onEnter() {
GUI.activate(true, [Grid.get().element, Toolbox.get(), save]);
if(!Grid.get().missing.isEmpty()) {
Mode.setEnabled(false, ['play', 'solve']);
} else {
Share.link(Grid.get().colors);
}
}
function onLeave() {
GUI.activate(false, [Grid.get().element, Toolbox.get(), save, Share.get()]);
}
function onMousedown(e, row, column) {
if(e.button == GUI.mouse.left) {
down = true;
if(Toolbox.tool() == 'draw') {
colorCell(row, column);
}
}
}
function onMouseup(e, row, column) {
if(e.button == GUI.mouse.left) {
down = false;
if(Toolbox.tool() == 'paint') {
Grid.Color.paint(row, column);
checkCompleteness();
}
}
}
function onMouseenter(e, row, column) {
if(down && Toolbox.tool() == 'draw') {
colorCell(row, column);
}
}
function colorCell(row, column) {
Grid.Color.ize(row, column);
checkCompleteness();
}
function checkCompleteness() {
if(Grid.get().missing.isEmpty()) {
Mode.setEnabled(true, ['play', 'solve']);
Share.link(Grid.get().colors);
}
}