Fix tools not working with Mirror Painting option

Fix #1267 Gradient tool
Fix #805 Shape tool
This commit is contained in:
JannisX11 2022-02-01 16:48:18 +01:00
parent d5ec1571af
commit 7ec192bcbe

View File

@ -287,7 +287,13 @@ const Painter = {
let uvFactorY = texture.display_height / Project.texture_height;
if (Painter.mirror_painting && !is_opposite) {
Painter.runMirrorBrush(texture, x, y, event, uvTag);
let target = Painter.getMirrorPaintTarget(texture, x, y, uvTag)
if (target) {
let old_element = Painter.current.element;
Painter.current.element = target.element;
Painter.useBrushlike(texture, target.x, target.y, event, target.uv_tag, true, true);
Painter.current.element = old_element;
}
}
let ctx = Painter.current.ctx;
@ -494,9 +500,9 @@ const Painter = {
ctx.globalAlpha = 1.0;
ctx.globalCompositeOperation = 'source-over'
},
runMirrorBrush(texture, x, y, event, uvTag) {
if (uvTag && Painter.current.element) {
let mirror_element = Painter.getMirrorCube(Painter.current.element);
getMirrorPaintTarget(texture, x, y, uvTag) {
if (!uvTag || !Painter.current.element) return;
let mirror_element = Painter.getMirrorElement(Painter.current.element);
let even_brush_size = BarItems.slider_brush_size.get()%2 == 0 && Toolbox.selected.brushTool;
if (mirror_element instanceof Cube) {
@ -532,10 +538,12 @@ const Painter = {
point_on_uv[1] = Math.max(face.uv[1], face.uv[1+2]) * uvFactorY - point_on_uv[1] - 1;
}
let cube = Painter.current.element;
Painter.current.element = mirror_element;
Painter.useBrushlike(texture, ...point_on_uv, event, face.uv, true, true);
Painter.current.element = cube;
return {
element: mirror_element,
x: point_on_uv[0],
y: point_on_uv[1],
uv_tag: face.uv
}
} else if (mirror_element instanceof Mesh) {
@ -572,10 +580,11 @@ const Painter = {
point_on_uv = point_on_uv.map(v => Math.floor(v))
}
let old_mesh = Painter.current.element;
Painter.current.element = mesh;
Painter.useBrushlike(texture, ...point_on_uv, event, face.uv, true, true);
Painter.current.element = old_mesh;
return {
element: mesh,
x: point_on_uv[0],
y: point_on_uv[1],
uv_tag: face.uv
}
}
},
@ -614,11 +623,13 @@ const Painter = {
let hollow = shape.substr(-1) == 'h';
shape = shape.replace(/_h$/, '');
function drawShape(start_x, start_y, x, y, uvTag) {
var rect = Painter.setupRectFromFace(uvTag, texture);
var [w, h] = [rect[2] - rect[0], rect[3] - rect[1]]
let diff_x = x - Painter.startPixel[0];
let diff_y = y - Painter.startPixel[1];
let diff_x = x - start_x;
let diff_y = y - start_y;
if (event.shiftKey || Pressing.overrides.shift) {
let clamp = Math.floor((Math.abs(diff_x) + Math.abs(diff_y))/2);
@ -635,7 +646,7 @@ const Painter = {
ctx.strokeStyle = ctx.fillStyle = tinycolor(ColorPanel.get()).setAlpha(b_opacity).toRgbString();
ctx.lineWidth = width;
ctx.beginPath();
var rect = getRectangle(Painter.startPixel[0], Painter.startPixel[1], Painter.startPixel[0]+diff_x, Painter.startPixel[1]+diff_y);
var rect = getRectangle(start_x, start_y, start_x+diff_x, start_y+diff_y);
if (hollow) {
ctx.rect(rect.ax+(width%2 ? 0.5 : 1), rect.ay+(width%2 ? 0.5 : 1), rect.x, rect.y);
@ -667,8 +678,8 @@ const Painter = {
for (var i = 0; i < Math.abs(diff_x_m); i++) {
for (var j = 0; j < 4; j++) {
changePixel(
Painter.startPixel[0] + (j<2?1:-1) * i,
Painter.startPixel[1] + (j%2?1:-1) * Math.round(Math.cos(Math.asin(i / Math.abs(diff_x_m))) * diff_y_m),
start_x + (j<2?1:-1) * i,
start_y + (j%2?1:-1) * Math.round(Math.cos(Math.asin(i / Math.abs(diff_x_m))) * diff_y_m),
editPx
)
}
@ -676,8 +687,8 @@ const Painter = {
for (var i = 0; i < Math.abs(diff_y_m); i++) {
for (var j = 0; j < 4; j++) {
changePixel(
Painter.startPixel[0] + (j<2?1:-1) * Math.round(Math.sin(Math.acos(i / Math.abs(diff_y_m))) * diff_x_m),
Painter.startPixel[1] + (j%2?1:-1) * i,
start_x + (j<2?1:-1) * Math.round(Math.sin(Math.acos(i / Math.abs(diff_y_m))) * diff_x_m),
start_y + (j%2?1:-1) * i,
editPx
)
}
@ -692,8 +703,8 @@ const Painter = {
for (var k = 0; k <= radius; k++) {
for (var j = 0; j < 4; j++) {
changePixel(
Painter.startPixel[0] + (j<2?1:-1) * i,
Painter.startPixel[1] + (j%2?1:-1) * k,
start_x + (j<2?1:-1) * i,
start_y + (j%2?1:-1) * k,
editPx
)
}
@ -704,8 +715,8 @@ const Painter = {
for (var k = 0; k <= radius; k++) {
for (var j = 0; j < 4; j++) {
changePixel(
Painter.startPixel[0] + (j<2?1:-1) * k,
Painter.startPixel[1] + (j%2?1:-1) * i,
start_x + (j<2?1:-1) * k,
start_y + (j%2?1:-1) * i,
editPx
)
}
@ -714,6 +725,21 @@ const Painter = {
}
})
}
}
drawShape(Painter.startPixel[0], Painter.startPixel[1], x, y, uvTag);
if (Painter.mirror_painting) {
let target = Painter.getMirrorPaintTarget(texture, x, y, uvTag);
if (target) {
let start_target = Painter.getMirrorPaintTarget(texture, Painter.startPixel[0], Painter.startPixel[1], uvTag);
let old_element = Painter.current.element;
Painter.current.element = target.element;
drawShape(start_target.x, start_target.y, target.x, target.y, target.uv_tag)
Painter.current.element = old_element;
}
}
//Painter.editing_area = undefined;
ctx.globalAlpha = 1.0;
ctx.globalCompositeOperation = 'source-over';
@ -724,16 +750,16 @@ const Painter = {
Painter.brushChanges = true;
texture.edit(function(canvas) {
let b_opacity = BarItems.slider_brush_opacity.get()/255;
var ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(Painter.current.clear, 0, 0)
let b_opacity = BarItems.slider_brush_opacity.get()/255;
function drawGradient(start_x, start_y, x, y, uvTag) {
let rect = Painter.setupRectFromFace(uvTag, texture);
var [w, h] = [rect[2] - rect[0], rect[3] - rect[1]];
let diff_x = x - Painter.startPixel[0];
let diff_y = y - Painter.startPixel[1];
let diff_x = x - start_x;
let diff_y = y - start_y;
if (event.shiftKey || Pressing.overrides.shift) {
let length = Math.sqrt(Math.pow(diff_x, 2) + Math.pow(diff_y, 2));
@ -760,19 +786,33 @@ const Painter = {
diff_x = length;
diff_y = 0;
}
x = Painter.startPixel[0] + diff_x * Math.sign(x - Painter.startPixel[0]);
y = Painter.startPixel[1] + diff_y * Math.sign(y - Painter.startPixel[1]);
x = start_x + diff_x * Math.sign(x - start_x);
y = start_y + diff_y * Math.sign(y - start_y);
}
let gradient = ctx.createLinearGradient(Painter.startPixel[0], Painter.startPixel[1], x, y);
let gradient = ctx.createLinearGradient(start_x, start_y, x, y);
gradient.addColorStop(0, tinycolor(ColorPanel.get()).setAlpha(b_opacity).toRgbString());
gradient.addColorStop(1, tinycolor(ColorPanel.get()).setAlpha(0).toRgbString());
ctx.beginPath();
ctx.fillStyle = gradient;
ctx.rect(rect[0], rect[1], w, h);
ctx.fill();
return [diff_x, diff_y];
}
let [diff_x, diff_y] = drawGradient(Painter.startPixel[0], Painter.startPixel[1], x, y, uvTag);
if (Painter.mirror_painting) {
let target = Painter.getMirrorPaintTarget(texture, x, y, uvTag);
if (target) {
let start_target = Painter.getMirrorPaintTarget(texture, Painter.startPixel[0], Painter.startPixel[1], uvTag);
let old_element = Painter.current.element;
Painter.current.element = target.element;
drawGradient(start_target.x, start_target.y, target.x, target.y, target.uv_tag)
Painter.current.element = old_element;
}
}
let degrees = Math.round(Math.radToDeg(Math.atan2(diff_x, diff_y)) * 4) / 4;
Blockbench.setStatusBarText(`${Math.round(diff_x)} x ${Math.round(diff_y)}, ${degrees}°`);
@ -802,7 +842,7 @@ const Painter = {
added.a = original_a
return mix;
},
getMirrorCube(element) {
getMirrorElement(element) {
let center = Format.centered_grid ? 0 : 8;
let e = 0.01
if (element instanceof Cube) {