Constellations/js/Mode/Edit.js

72 lines
1.3 KiB
JavaScript

import GUI;
import Grid;
import Grid.Color;
import Mode;
import Toolbox;
import Share;
var down = false;
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() {
GUI.activate(true, [Grid.get().root, Toolbox.get(), save]);
if(Grid.get().missing.size() > 0) {
Mode.setEnabled(false, ['play', 'solve']);
} else {
Share.link(Grid.get().colors);
}
}
function onLeave() {
GUI.activate(false, [Grid.get().root, Toolbox.get(), save, Share.get()]);
}
function onMousedown(e, cell) {
if(e.button == GUI.mouse.left) {
down = true;
if(Toolbox.tool() == 'draw') {
colorCell(cell);
}
}
}
function onMouseup(e, cell) {
if(e.button == GUI.mouse.left) {
down = false;
if(Toolbox.tool() == 'paint') {
Grid.Color.paint(cell);
checkCompleteness();
}
}
}
function onMouseenter(e, cell) {
if(down && Toolbox.tool() == 'draw') {
colorCell(cell);
}
}
function colorCell(cell) {
Grid.Color.ize(cell);
checkCompleteness();
}
function checkCompleteness() {
if(Grid.get().missing.size() < 1) {
Mode.setEnabled(true, ['play', 'solve']);
Share.link(Grid.get().colors);
}
}