Simplify colorize function to increase reuse

This commit is contained in:
Tissevert 2022-07-26 20:01:09 +02:00
parent e86312b162
commit ec5279127f

View file

@ -2,7 +2,7 @@ import * as File from WTK.File;
import * as Async from UnitJS.Async;
import compose from UnitJS.Fun;
import * as Dom from UnitJS.Dom;
import {color, tool} from Toolbox;
import Toolbox;
var grid = {
@ -32,8 +32,8 @@ function init(size, elementId) {
function emptyGrid(size) {
var result = {};
for(var i = 0; i < size; i++) {
result[i] = {};
for(var row = 0; row < size; row++) {
result[row] = {};
}
return result;
}
@ -46,31 +46,31 @@ function makeRow(config) {
return Dom.make('tr', {}, cells);
}
function bodyAttributes(i, j) {
function bodyAttributes(row, column) {
return {
onMousedown: function(e) {
onMousedown: function() {
down = true;
if(tool() == 'draw') {
colorize(i, j, e.target);
if(Toolbox.tool() == 'draw') {
colorize(row, column);
}
},
onMouseup: function(e) {
onMouseup: function() {
down = false;
if(tool() == 'paint') {
paint(i, j);
if(Toolbox.tool() == 'paint') {
paint(row, column);
}
},
onMouseenter: function(e) {
if(down && tool() == 'draw') {
colorize(i, j, e.target);
onMouseenter: function() {
if(down && Toolbox.tool() == 'draw') {
colorize(row, column);
}
}
};
}
function colorize(i, j, element) {
grid.data[i][j] = color();
element.className = grid.data[i][j];
function colorize(row, column, color) {
grid.data[row][column] = color || Toolbox.color();
grid.element.children[row].children[column].className = grid.data[row][column];
}
function paint(i0, j0) {
@ -80,7 +80,7 @@ function paint(i0, j0) {
var queue = [{i: i0, j: j0}];
while(queue.length > 0) {
var p0 = queue[0];
colorize(p0.i, p0.j, grid.element.children[p0.i].children[p0.j]);
colorize(p0.i, p0.j);
done[p0.i][p0.j] = true;
for(var d = -1; d < 2; d += 2) {
[{i: p0.i + d, j: p0.j}, {i: p0.i, j: p0.j + d}].forEach(function(p1) {
@ -111,8 +111,7 @@ function setGridData(data) {
if(data != undefined) {
for(var row = 0; row < grid.element.children.length; row++) {
for(var column = 0; column < grid.element.children[row].children.length; column++) {
grid.data[row][column] = data[row][column];
grid.element.children[row].children[column].className = data[row][column];
colorize(row, column, data[row][column]);
}
}
}