Constellations/js/Mode/Edit.js

69 lines
1.3 KiB
JavaScript
Raw Normal View History

import GUI;
import Grid;
import Grid.Color;
import Mode;
import Toolbox;
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']);
}
}
function onLeave() {
GUI.activate(false, [Grid.get().element, Toolbox.get(), save]);
}
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']);
}
}