2022-11-24 09:30:13 -06:00
|
|
|
const selectTransformTool = () =>
|
|
|
|
toolbar.registerTool(
|
2022-12-18 17:50:40 -06:00
|
|
|
"./res/icons/box-select.svg",
|
2022-11-24 09:30:13 -06:00
|
|
|
"Select Image",
|
|
|
|
(state, opt) => {
|
|
|
|
// Draw new cursor immediately
|
2022-12-16 21:55:53 -06:00
|
|
|
ovLayer.clear();
|
2022-11-29 14:55:25 -06:00
|
|
|
state.movecb(mouse.coords.world.pos);
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Canvas left mouse handlers
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.onmousemove.on(state.movecb);
|
|
|
|
mouse.listen.world.btn.left.onclick.on(state.clickcb);
|
|
|
|
mouse.listen.world.btn.left.ondragstart.on(state.dragstartcb);
|
|
|
|
mouse.listen.world.btn.left.ondragend.on(state.dragendcb);
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Canvas right mouse handler
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.btn.right.onclick.on(state.cancelcb);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Keyboard click handlers
|
2022-11-24 21:34:34 -06:00
|
|
|
keyboard.listen.onkeyclick.on(state.keyclickcb);
|
|
|
|
keyboard.listen.onkeydown.on(state.keydowncb);
|
2022-11-25 12:22:16 -06:00
|
|
|
|
2022-12-21 09:07:29 -06:00
|
|
|
// Layer system handlers
|
|
|
|
uil.onactive.on(state.uilayeractivecb);
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Registers keyboard shortcuts
|
2022-11-24 21:34:34 -06:00
|
|
|
keyboard.onShortcut({ctrl: true, key: "KeyC"}, state.ctrlccb);
|
|
|
|
keyboard.onShortcut({ctrl: true, key: "KeyV"}, state.ctrlvcb);
|
|
|
|
keyboard.onShortcut({ctrl: true, key: "KeyX"}, state.ctrlxcb);
|
2022-11-25 10:16:22 -06:00
|
|
|
|
|
|
|
state.selected = null;
|
2022-11-24 09:30:13 -06:00
|
|
|
},
|
|
|
|
(state, opt) => {
|
2022-11-25 12:22:16 -06:00
|
|
|
// Clear all those listeners and shortcuts we set up
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.onmousemove.clear(state.movecb);
|
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.clickcb);
|
|
|
|
mouse.listen.world.btn.left.ondragstart.clear(state.dragstartcb);
|
|
|
|
mouse.listen.world.btn.left.ondragend.clear(state.dragendcb);
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.btn.right.onclick.clear(state.cancelcb);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
|
|
|
keyboard.listen.onkeyclick.clear(state.keyclickcb);
|
|
|
|
keyboard.listen.onkeydown.clear(state.keydowncb);
|
|
|
|
keyboard.deleteShortcut(state.ctrlccb, "KeyC");
|
|
|
|
keyboard.deleteShortcut(state.ctrlvcb, "KeyV");
|
|
|
|
keyboard.deleteShortcut(state.ctrlxcb, "KeyX");
|
|
|
|
|
2022-12-21 09:07:29 -06:00
|
|
|
uil.onactive.clear(state.uilayeractivecb);
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Clear any selections
|
2022-11-24 21:34:34 -06:00
|
|
|
state.reset();
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Resets cursor
|
2022-12-16 21:55:53 -06:00
|
|
|
ovLayer.clear();
|
2022-12-06 09:25:06 -06:00
|
|
|
|
|
|
|
// Clears overlay
|
2022-11-29 14:55:25 -06:00
|
|
|
imageCollection.inputElement.style.cursor = "auto";
|
2022-11-24 09:30:13 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
init: (state) => {
|
2022-11-24 21:34:34 -06:00
|
|
|
state.clipboard = {};
|
|
|
|
|
2022-11-24 09:30:13 -06:00
|
|
|
state.snapToGrid = true;
|
2022-11-24 21:34:34 -06:00
|
|
|
state.keepAspectRatio = true;
|
2022-12-18 20:35:48 -06:00
|
|
|
state.block_res_change = true;
|
2022-12-03 08:40:40 -06:00
|
|
|
state.useClipboard = !!(
|
|
|
|
navigator.clipboard && navigator.clipboard.write
|
|
|
|
); // Use it by default if supported
|
2022-12-21 09:07:29 -06:00
|
|
|
state.selectionPeekOpacity = 40;
|
2022-11-24 21:34:34 -06:00
|
|
|
|
|
|
|
state.original = null;
|
2022-11-24 09:30:13 -06:00
|
|
|
state.dragging = null;
|
2022-11-25 10:16:22 -06:00
|
|
|
state._selected = null;
|
|
|
|
Object.defineProperty(state, "selected", {
|
|
|
|
get: () => state._selected,
|
|
|
|
set: (v) => {
|
|
|
|
if (v) state.ctxmenu.enableButtons();
|
|
|
|
else state.ctxmenu.disableButtons();
|
|
|
|
|
|
|
|
return (state._selected = v);
|
|
|
|
},
|
|
|
|
});
|
2022-11-24 21:34:34 -06:00
|
|
|
state.moving = null;
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Some things to easy request for a redraw
|
2022-11-24 21:34:34 -06:00
|
|
|
state.lastMouseTarget = null;
|
2022-12-21 09:07:29 -06:00
|
|
|
state.lastMouseMove = {x: 0, y: 0};
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-12-06 09:25:06 -06:00
|
|
|
state.redraw = () => {
|
2022-12-16 21:55:53 -06:00
|
|
|
ovLayer.clear();
|
2022-11-24 21:34:34 -06:00
|
|
|
state.movecb(state.lastMouseMove);
|
|
|
|
};
|
|
|
|
|
2022-12-21 09:07:29 -06:00
|
|
|
state.uilayeractivecb = ({uilayer}) => {
|
|
|
|
if (state.originalDisplayLayer) {
|
|
|
|
state.originalDisplayLayer.moveAfter(uilayer.layer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Clears selection and make things right
|
2022-12-21 09:07:29 -06:00
|
|
|
state.reset = (erase = false) => {
|
|
|
|
if (state.selected && !erase)
|
|
|
|
state.originalLayer.ctx.drawImage(
|
2022-11-25 12:22:16 -06:00
|
|
|
state.original.image,
|
|
|
|
state.original.x,
|
|
|
|
state.original.y
|
2022-11-24 21:59:10 -06:00
|
|
|
);
|
|
|
|
|
2022-12-21 09:07:29 -06:00
|
|
|
if (state.originalDisplayLayer) {
|
|
|
|
imageCollection.deleteLayer(state.originalDisplayLayer);
|
|
|
|
state.originalDisplayLayer = null;
|
|
|
|
}
|
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
if (state.dragging) state.dragging = null;
|
|
|
|
else state.selected = null;
|
|
|
|
|
2022-12-06 09:25:06 -06:00
|
|
|
state.redraw();
|
2022-11-24 21:34:34 -06:00
|
|
|
};
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Selection bounding box object. Has some witchery to deal with handles.
|
2022-11-24 09:30:13 -06:00
|
|
|
const selectionBB = (x1, y1, x2, y2) => {
|
2022-12-07 15:25:59 -06:00
|
|
|
x1 = Math.round(x1);
|
|
|
|
y1 = Math.round(y1);
|
|
|
|
x2 = Math.round(x2);
|
|
|
|
y2 = Math.round(y2);
|
2022-11-24 09:30:13 -06:00
|
|
|
return {
|
2022-11-24 21:34:34 -06:00
|
|
|
original: {
|
|
|
|
x: Math.min(x1, x2),
|
|
|
|
y: Math.min(y1, y2),
|
|
|
|
w: Math.abs(x1 - x2),
|
|
|
|
h: Math.abs(y1 - y2),
|
|
|
|
},
|
2022-11-24 09:30:13 -06:00
|
|
|
x: Math.min(x1, x2),
|
|
|
|
y: Math.min(y1, y2),
|
|
|
|
w: Math.abs(x1 - x2),
|
|
|
|
h: Math.abs(y1 - y2),
|
2022-11-24 22:46:49 -06:00
|
|
|
updateOriginal() {
|
|
|
|
this.original.x = this.x;
|
|
|
|
this.original.y = this.y;
|
|
|
|
this.original.w = this.w;
|
|
|
|
this.original.h = this.h;
|
|
|
|
},
|
2022-11-24 21:34:34 -06:00
|
|
|
contains(x, y) {
|
|
|
|
return (
|
|
|
|
this.x <= x &&
|
|
|
|
x <= this.x + this.w &&
|
|
|
|
this.y <= y &&
|
|
|
|
y <= this.y + this.h
|
|
|
|
);
|
|
|
|
},
|
|
|
|
handles() {
|
2022-12-14 11:02:36 -06:00
|
|
|
const _createHandle = (x, y, originOffset = null) => {
|
2022-11-24 21:34:34 -06:00
|
|
|
return {
|
2022-12-14 11:02:36 -06:00
|
|
|
x,
|
|
|
|
y,
|
2022-11-24 21:34:34 -06:00
|
|
|
scaleTo: (tx, ty, keepAspectRatio = true) => {
|
|
|
|
const origin = {
|
|
|
|
x: this.original.x + this.original.w / 2,
|
|
|
|
y: this.original.y + this.original.h / 2,
|
|
|
|
};
|
|
|
|
let nx = tx;
|
|
|
|
let ny = ty;
|
|
|
|
|
|
|
|
let xRatio = (nx - origin.x) / (x - origin.x);
|
|
|
|
let yRatio = (ny - origin.y) / (y - origin.y);
|
|
|
|
if (keepAspectRatio)
|
|
|
|
xRatio = yRatio = Math.min(xRatio, yRatio);
|
|
|
|
|
|
|
|
if (Number.isFinite(xRatio)) {
|
|
|
|
let left = this.original.x;
|
|
|
|
let right = this.original.x + this.original.w;
|
|
|
|
|
|
|
|
left = (left - origin.x) * xRatio + origin.x;
|
|
|
|
right = (right - origin.x) * xRatio + origin.x;
|
|
|
|
|
|
|
|
this.x = left;
|
|
|
|
this.w = right - left;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Number.isFinite(yRatio)) {
|
|
|
|
let top = this.original.y;
|
|
|
|
let bottom = this.original.y + this.original.h;
|
|
|
|
|
|
|
|
top = (top - origin.y) * yRatio + origin.y;
|
|
|
|
bottom = (bottom - origin.y) * yRatio + origin.y;
|
|
|
|
|
|
|
|
this.y = top;
|
|
|
|
this.h = bottom - top;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
2022-12-14 11:02:36 -06:00
|
|
|
|
|
|
|
const size = viewport.zoom * 10;
|
2022-11-24 21:34:34 -06:00
|
|
|
return [
|
2022-12-14 11:02:36 -06:00
|
|
|
_createHandle(this.x, this.y, size),
|
|
|
|
_createHandle(this.x + this.w, this.y, size),
|
|
|
|
_createHandle(this.x, this.y + this.h, size),
|
|
|
|
_createHandle(this.x + this.w, this.y + this.h, size),
|
2022-11-24 21:34:34 -06:00
|
|
|
];
|
|
|
|
},
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-11-30 16:28:05 -06:00
|
|
|
// Mouse move handler. As always, also renders cursor
|
2022-11-24 09:30:13 -06:00
|
|
|
state.movecb = (evn) => {
|
2022-12-16 21:55:53 -06:00
|
|
|
ovLayer.clear();
|
2022-12-06 09:25:06 -06:00
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
2022-12-21 15:29:11 -06:00
|
|
|
state.erasePrevCursor && state.erasePrevCursor();
|
2022-11-29 14:55:25 -06:00
|
|
|
imageCollection.inputElement.style.cursor = "auto";
|
2022-11-24 21:34:34 -06:00
|
|
|
state.lastMouseTarget = evn.target;
|
|
|
|
state.lastMouseMove = evn;
|
2022-11-29 14:55:25 -06:00
|
|
|
let x = evn.x;
|
|
|
|
let y = evn.y;
|
|
|
|
if (state.snapToGrid) {
|
2022-12-03 06:53:12 -06:00
|
|
|
x += snap(evn.x, 0, 64);
|
|
|
|
y += snap(evn.y, 0, 64);
|
2022-11-29 14:55:25 -06:00
|
|
|
}
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-12-06 09:25:06 -06:00
|
|
|
const vpc = viewport.canvasToView(x, y);
|
|
|
|
|
|
|
|
uiCtx.save();
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Update scale
|
|
|
|
if (state.scaling) {
|
|
|
|
state.scaling.scaleTo(x, y, state.keepAspectRatio);
|
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Update position
|
|
|
|
if (state.moving) {
|
2022-12-07 20:09:41 -06:00
|
|
|
state.selected.x = Math.round(x - state.moving.offset.x);
|
|
|
|
state.selected.y = Math.round(y - state.moving.offset.y);
|
2022-11-29 14:55:25 -06:00
|
|
|
state.selected.updateOriginal();
|
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Draw dragging box
|
|
|
|
if (state.dragging) {
|
2022-12-06 09:25:06 -06:00
|
|
|
uiCtx.setLineDash([2, 2]);
|
|
|
|
uiCtx.lineWidth = 1;
|
|
|
|
uiCtx.strokeStyle = "#FFF";
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
const ix = state.dragging.ix;
|
|
|
|
const iy = state.dragging.iy;
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
const bb = selectionBB(ix, iy, x, y);
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-12-06 09:25:06 -06:00
|
|
|
const bbvp = {
|
|
|
|
...viewport.canvasToView(bb.x, bb.y),
|
|
|
|
w: viewport.zoom * bb.w,
|
|
|
|
h: viewport.zoom * bb.h,
|
|
|
|
};
|
|
|
|
|
|
|
|
uiCtx.strokeRect(bbvp.x, bbvp.y, bbvp.w, bbvp.h);
|
|
|
|
uiCtx.setLineDash([]);
|
2022-11-29 14:55:25 -06:00
|
|
|
}
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
if (state.selected) {
|
|
|
|
ovCtx.lineWidth = 1;
|
|
|
|
ovCtx.strokeStyle = "#FFF";
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
const bb = {
|
|
|
|
x: state.selected.x,
|
|
|
|
y: state.selected.y,
|
|
|
|
w: state.selected.w,
|
|
|
|
h: state.selected.h,
|
|
|
|
};
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-12-06 09:25:06 -06:00
|
|
|
const bbvp = {
|
|
|
|
...viewport.canvasToView(bb.x, bb.y),
|
|
|
|
w: viewport.zoom * bb.w,
|
|
|
|
h: viewport.zoom * bb.h,
|
|
|
|
};
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Draw Image
|
2022-12-21 09:07:29 -06:00
|
|
|
ovCtx.save();
|
|
|
|
ovCtx.filter = `opacity(${state.selectionPeekOpacity}%)`;
|
2022-11-29 14:55:25 -06:00
|
|
|
ovCtx.drawImage(
|
|
|
|
state.selected.image,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.selected.image.width,
|
|
|
|
state.selected.image.height,
|
|
|
|
state.selected.x,
|
|
|
|
state.selected.y,
|
|
|
|
state.selected.w,
|
|
|
|
state.selected.h
|
|
|
|
);
|
2022-12-21 09:07:29 -06:00
|
|
|
ovCtx.restore();
|
|
|
|
|
|
|
|
state.originalDisplayLayer.clear();
|
|
|
|
state.originalDisplayLayer.ctx.save();
|
|
|
|
state.originalDisplayLayer.ctx.drawImage(
|
|
|
|
state.selected.image,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.selected.image.width,
|
|
|
|
state.selected.image.height,
|
|
|
|
state.selected.x,
|
|
|
|
state.selected.y,
|
|
|
|
state.selected.w,
|
|
|
|
state.selected.h
|
|
|
|
);
|
|
|
|
state.originalDisplayLayer.ctx.restore();
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Draw selection box
|
2022-12-06 09:30:55 -06:00
|
|
|
uiCtx.strokeStyle = "#FFF";
|
2022-12-06 09:25:06 -06:00
|
|
|
uiCtx.setLineDash([4, 2]);
|
|
|
|
uiCtx.strokeRect(bbvp.x, bbvp.y, bbvp.w, bbvp.h);
|
|
|
|
uiCtx.setLineDash([]);
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Draw Scaling/Rotation Origin
|
2022-12-06 09:25:06 -06:00
|
|
|
uiCtx.beginPath();
|
|
|
|
uiCtx.arc(
|
|
|
|
bbvp.x + bbvp.w / 2,
|
|
|
|
bbvp.y + bbvp.h / 2,
|
2022-11-29 14:55:25 -06:00
|
|
|
5,
|
|
|
|
0,
|
|
|
|
2 * Math.PI
|
|
|
|
);
|
2022-12-06 09:25:06 -06:00
|
|
|
uiCtx.stroke();
|
2022-11-29 14:55:25 -06:00
|
|
|
|
|
|
|
// Draw Scaling Handles
|
|
|
|
let cursorInHandle = false;
|
|
|
|
state.selected.handles().forEach((handle) => {
|
2022-12-06 09:25:06 -06:00
|
|
|
const bbvph = {
|
|
|
|
...viewport.canvasToView(handle.x, handle.y),
|
2022-12-14 11:02:36 -06:00
|
|
|
w: 10,
|
|
|
|
h: 10,
|
2022-12-06 09:25:06 -06:00
|
|
|
};
|
2022-12-14 11:02:36 -06:00
|
|
|
|
|
|
|
bbvph.x -= 5;
|
|
|
|
bbvph.y -= 5;
|
|
|
|
|
|
|
|
const inhandle =
|
|
|
|
evn.evn.clientX > bbvph.x &&
|
|
|
|
evn.evn.clientX < bbvph.x + bbvph.w &&
|
|
|
|
evn.evn.clientY > bbvph.y &&
|
|
|
|
evn.evn.clientY < bbvph.y + bbvph.h;
|
|
|
|
|
|
|
|
if (inhandle) {
|
2022-11-29 14:55:25 -06:00
|
|
|
cursorInHandle = true;
|
2022-12-06 09:25:06 -06:00
|
|
|
uiCtx.strokeRect(
|
|
|
|
bbvph.x - 1,
|
|
|
|
bbvph.y - 1,
|
|
|
|
bbvph.w + 2,
|
|
|
|
bbvph.h + 2
|
2022-11-29 14:55:25 -06:00
|
|
|
);
|
|
|
|
} else {
|
2022-12-06 09:25:06 -06:00
|
|
|
uiCtx.strokeRect(bbvph.x, bbvph.y, bbvph.w, bbvph.h);
|
2022-11-29 14:55:25 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Change cursor
|
|
|
|
if (cursorInHandle || state.selected.contains(evn.x, evn.y))
|
|
|
|
imageCollection.inputElement.style.cursor = "pointer";
|
2022-11-24 09:30:13 -06:00
|
|
|
}
|
2022-11-29 14:55:25 -06:00
|
|
|
|
|
|
|
// Draw current cursor location
|
2022-12-21 15:29:11 -06:00
|
|
|
state.erasePrevCursor = _tool._cursor_draw(x, y);
|
2022-12-06 09:25:06 -06:00
|
|
|
|
|
|
|
uiCtx.restore();
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Handles left mouse clicks
|
2022-11-24 21:34:34 -06:00
|
|
|
state.clickcb = (evn) => {
|
2022-11-30 16:28:05 -06:00
|
|
|
if (
|
2022-12-14 11:02:36 -06:00
|
|
|
!state.original ||
|
2022-12-21 09:07:29 -06:00
|
|
|
(state.originalLayer === uil.layer &&
|
|
|
|
state.original.x === state.selected.x &&
|
2022-12-14 11:02:36 -06:00
|
|
|
state.original.y === state.selected.y &&
|
|
|
|
state.original.w === state.selected.w &&
|
|
|
|
state.original.h === state.selected.h)
|
2022-11-30 16:28:05 -06:00
|
|
|
) {
|
|
|
|
state.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// If something is selected, commit changes to the canvas
|
|
|
|
if (state.selected) {
|
2022-12-21 09:07:29 -06:00
|
|
|
state.originalLayer.ctx.drawImage(
|
2022-11-29 14:55:25 -06:00
|
|
|
state.selected.image,
|
|
|
|
state.original.x,
|
|
|
|
state.original.y
|
|
|
|
);
|
2022-12-21 09:07:29 -06:00
|
|
|
commands.runCommand("eraseImage", "Image Transform Erase", {
|
|
|
|
...state.original,
|
|
|
|
ctx: state.originalLayer.ctx,
|
|
|
|
});
|
2022-12-07 20:17:46 -06:00
|
|
|
commands.runCommand("drawImage", "Image Transform Draw", {
|
|
|
|
image: state.selected.image,
|
|
|
|
x: Math.round(state.selected.x),
|
|
|
|
y: Math.round(state.selected.y),
|
|
|
|
w: Math.round(state.selected.w),
|
|
|
|
h: Math.round(state.selected.h),
|
|
|
|
});
|
2022-12-21 09:07:29 -06:00
|
|
|
state.reset(true);
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
|
|
|
};
|
2022-11-25 12:22:16 -06:00
|
|
|
|
|
|
|
// Handles left mouse drag events
|
2022-11-24 09:30:13 -06:00
|
|
|
state.dragstartcb = (evn) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
let ix = evn.ix;
|
|
|
|
let iy = evn.iy;
|
|
|
|
if (state.snapToGrid) {
|
2022-12-03 06:53:12 -06:00
|
|
|
ix += snap(evn.ix, 0, 64);
|
|
|
|
iy += snap(evn.iy, 0, 64);
|
2022-11-29 14:55:25 -06:00
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// If is selected, check if drag is in handles/body and act accordingly
|
|
|
|
if (state.selected) {
|
|
|
|
const handles = state.selected.handles();
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-12-14 11:02:36 -06:00
|
|
|
const activeHandle = handles.find((v) => {
|
|
|
|
const vpc = viewport.canvasToView(v.x, v.y);
|
|
|
|
const tlc = viewport.viewToCanvas(vpc.x - 5, vpc.y - 5);
|
|
|
|
const brc = viewport.viewToCanvas(vpc.x + 5, vpc.y + 5);
|
|
|
|
const bb = {
|
|
|
|
x: tlc.x,
|
|
|
|
y: tlc.y,
|
|
|
|
w: brc.x - tlc.x,
|
|
|
|
h: brc.y - tlc.y,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
evn.ix > bb.x &&
|
|
|
|
evn.ix < bb.x + bb.w &&
|
|
|
|
evn.iy > bb.y &&
|
|
|
|
evn.iy < bb.y + bb.h
|
|
|
|
);
|
|
|
|
});
|
2022-11-29 14:55:25 -06:00
|
|
|
if (activeHandle) {
|
|
|
|
state.scaling = activeHandle;
|
|
|
|
return;
|
|
|
|
} else if (state.selected.contains(ix, iy)) {
|
|
|
|
state.moving = {
|
|
|
|
offset: {x: ix - state.selected.x, y: iy - state.selected.y},
|
|
|
|
};
|
|
|
|
return;
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
}
|
2022-11-29 14:55:25 -06:00
|
|
|
// If it is not, just create new selection
|
|
|
|
state.reset();
|
|
|
|
state.dragging = {ix, iy};
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Handles left mouse drag end events
|
2022-11-24 09:30:13 -06:00
|
|
|
state.dragendcb = (evn) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
let x = evn.x;
|
|
|
|
let y = evn.y;
|
|
|
|
if (state.snapToGrid) {
|
2022-12-03 06:53:12 -06:00
|
|
|
x += snap(evn.x, 0, 64);
|
|
|
|
y += snap(evn.y, 0, 64);
|
2022-11-29 14:55:25 -06:00
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// If we are scaling, stop scaling and do some handler magic
|
|
|
|
if (state.scaling) {
|
|
|
|
state.selected.updateOriginal();
|
|
|
|
state.scaling = null;
|
|
|
|
// If we are moving the selection, just... stop
|
|
|
|
} else if (state.moving) {
|
|
|
|
state.moving = null;
|
|
|
|
/**
|
|
|
|
* If we are dragging, create a cutout selection area and save to an auxiliar image
|
|
|
|
* We will be rendering the image to the overlay, so it will not be noticeable
|
|
|
|
*/
|
|
|
|
} else if (state.dragging) {
|
|
|
|
state.original = selectionBB(
|
|
|
|
state.dragging.ix,
|
|
|
|
state.dragging.iy,
|
|
|
|
x,
|
|
|
|
y
|
|
|
|
);
|
|
|
|
state.selected = selectionBB(
|
|
|
|
state.dragging.ix,
|
|
|
|
state.dragging.iy,
|
|
|
|
x,
|
|
|
|
y
|
|
|
|
);
|
2022-12-21 09:07:29 -06:00
|
|
|
state.originalLayer = uil.layer;
|
|
|
|
state.originalDisplayLayer = imageCollection.registerLayer(null, {
|
|
|
|
after: uil.layer,
|
|
|
|
category: "select-display",
|
|
|
|
});
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Cut out selected portion of the image for manipulation
|
|
|
|
const cvs = document.createElement("canvas");
|
|
|
|
cvs.width = state.selected.w;
|
|
|
|
cvs.height = state.selected.h;
|
|
|
|
const ctx = cvs.getContext("2d");
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
ctx.drawImage(
|
2022-12-04 13:22:35 -06:00
|
|
|
uil.canvas,
|
2022-11-29 14:55:25 -06:00
|
|
|
state.selected.x,
|
|
|
|
state.selected.y,
|
|
|
|
state.selected.w,
|
|
|
|
state.selected.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.selected.w,
|
|
|
|
state.selected.h
|
|
|
|
);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-12-04 13:22:35 -06:00
|
|
|
uil.ctx.clearRect(
|
2022-11-29 14:55:25 -06:00
|
|
|
state.selected.x,
|
|
|
|
state.selected.y,
|
|
|
|
state.selected.w,
|
|
|
|
state.selected.h
|
|
|
|
);
|
|
|
|
state.selected.image = cvs;
|
|
|
|
state.original.image = cvs;
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
if (state.selected.w === 0 || state.selected.h === 0)
|
|
|
|
state.selected = null;
|
|
|
|
|
|
|
|
state.dragging = null;
|
2022-11-24 09:30:13 -06:00
|
|
|
}
|
2022-12-06 09:25:06 -06:00
|
|
|
state.redraw();
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Handler for right clicks. Basically resets everything
|
2022-11-24 09:30:13 -06:00
|
|
|
state.cancelcb = (evn) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
state.reset();
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Keyboard callbacks (For now, they just handle the "delete" key)
|
2022-11-24 21:34:34 -06:00
|
|
|
state.keydowncb = (evn) => {};
|
2022-11-24 09:30:13 -06:00
|
|
|
|
|
|
|
state.keyclickcb = (evn) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
switch (evn.code) {
|
|
|
|
case "Delete":
|
|
|
|
// Deletes selected area
|
|
|
|
state.selected &&
|
|
|
|
commands.runCommand("eraseImage", "Erase Area", state.selected);
|
|
|
|
state.selected = null;
|
2022-12-06 09:25:06 -06:00
|
|
|
state.redraw();
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register Ctrl-C/V Shortcut
|
2022-11-25 12:22:16 -06:00
|
|
|
|
|
|
|
// Handles copying
|
|
|
|
state.ctrlccb = (evn, cut = false) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
// We create a new canvas to store the data
|
|
|
|
state.clipboard.copy = document.createElement("canvas");
|
|
|
|
|
|
|
|
state.clipboard.copy.width = state.selected.w;
|
|
|
|
state.clipboard.copy.height = state.selected.h;
|
|
|
|
|
|
|
|
const ctx = state.clipboard.copy.getContext("2d");
|
|
|
|
|
|
|
|
ctx.clearRect(0, 0, state.selected.w, state.selected.h);
|
|
|
|
ctx.drawImage(
|
|
|
|
state.selected.image,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.selected.image.width,
|
|
|
|
state.selected.image.height,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.selected.w,
|
|
|
|
state.selected.h
|
|
|
|
);
|
|
|
|
|
|
|
|
// If cutting, we reverse the selection and erase the selection area
|
|
|
|
if (cut) {
|
|
|
|
const aux = state.original;
|
|
|
|
state.reset();
|
2022-11-25 12:22:16 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
commands.runCommand("eraseImage", "Cut Image", aux);
|
|
|
|
}
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Because firefox needs manual activation of the feature
|
|
|
|
if (state.useClipboard) {
|
|
|
|
// Send to clipboard
|
|
|
|
state.clipboard.copy.toBlob((blob) => {
|
|
|
|
const item = new ClipboardItem({"image/png": blob});
|
2022-12-03 08:40:40 -06:00
|
|
|
navigator.clipboard &&
|
|
|
|
navigator.clipboard.write([item]).catch((e) => {
|
|
|
|
console.warn("Error sending to clipboard");
|
|
|
|
console.warn(e);
|
|
|
|
});
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
|
|
|
};
|
2022-11-25 12:22:16 -06:00
|
|
|
|
|
|
|
// Handles pasting
|
2022-11-24 21:34:34 -06:00
|
|
|
state.ctrlvcb = (evn) => {
|
|
|
|
if (state.useClipboard) {
|
2022-11-25 12:22:16 -06:00
|
|
|
// If we use the clipboard, do some proccessing of clipboard data (ugly but kind of minimum required)
|
2022-12-03 08:40:40 -06:00
|
|
|
navigator.clipboard &&
|
|
|
|
navigator.clipboard.read().then((items) => {
|
|
|
|
for (const item of items) {
|
|
|
|
for (const type of item.types) {
|
|
|
|
if (type.startsWith("image/")) {
|
2022-12-26 07:35:33 -06:00
|
|
|
item.getType(type).then(async (blob) => {
|
2022-12-03 08:40:40 -06:00
|
|
|
// Converts blob to image
|
|
|
|
const url = window.URL || window.webkitURL;
|
|
|
|
const image = document.createElement("img");
|
2022-12-26 07:35:33 -06:00
|
|
|
image.src = url.createObjectURL(blob);
|
|
|
|
await image.decode();
|
2022-12-03 08:40:40 -06:00
|
|
|
tools.stamp.enable({
|
|
|
|
image,
|
|
|
|
back: tools.selecttransform.enable,
|
|
|
|
});
|
2022-11-24 21:34:34 -06:00
|
|
|
});
|
2022-12-03 08:40:40 -06:00
|
|
|
}
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 08:40:40 -06:00
|
|
|
});
|
2022-11-24 21:34:34 -06:00
|
|
|
} else if (state.clipboard.copy) {
|
2022-11-25 12:22:16 -06:00
|
|
|
// Use internal clipboard
|
2022-11-24 21:34:34 -06:00
|
|
|
const image = document.createElement("img");
|
|
|
|
image.src = state.clipboard.copy.toDataURL();
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Send to stamp, as clipboard temporary data
|
2022-11-24 21:34:34 -06:00
|
|
|
tools.stamp.enable({
|
|
|
|
image,
|
|
|
|
back: tools.selecttransform.enable,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2022-11-25 12:22:16 -06:00
|
|
|
|
|
|
|
// Cut shortcut. Basically, send to copy handler
|
|
|
|
state.ctrlxcb = (evn) => {
|
|
|
|
state.ctrlccb(evn, true);
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
|
|
|
},
|
|
|
|
populateContextMenu: (menu, state) => {
|
|
|
|
if (!state.ctxmenu) {
|
|
|
|
state.ctxmenu = {};
|
2022-11-25 12:22:16 -06:00
|
|
|
|
2022-11-24 09:30:13 -06:00
|
|
|
// Snap To Grid Checkbox
|
|
|
|
state.ctxmenu.snapToGridLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"snapToGrid",
|
2023-01-02 18:12:18 -06:00
|
|
|
"Snap To Grid",
|
|
|
|
"icon-grid"
|
|
|
|
).checkbox;
|
2022-11-25 12:22:16 -06:00
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
// Keep Aspect Ratio
|
|
|
|
state.ctxmenu.keepAspectRatioLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"keepAspectRatio",
|
2023-01-02 18:12:18 -06:00
|
|
|
"Keep Aspect Ratio",
|
|
|
|
"icon-maximize"
|
|
|
|
).checkbox;
|
2022-11-25 12:22:16 -06:00
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
// Use Clipboard
|
|
|
|
const clipboardCheckbox = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"useClipboard",
|
2023-01-02 18:12:18 -06:00
|
|
|
"Use clipboard",
|
|
|
|
"icon-clipboard-list"
|
2022-11-24 21:34:34 -06:00
|
|
|
);
|
2023-01-02 18:12:18 -06:00
|
|
|
state.ctxmenu.useClipboardLabel = clipboardCheckbox.checkbox;
|
2022-12-03 08:40:40 -06:00
|
|
|
if (!(navigator.clipboard && navigator.clipboard.write))
|
2022-11-24 21:34:34 -06:00
|
|
|
clipboardCheckbox.checkbox.disabled = true; // Disable if not available
|
2022-11-25 10:16:22 -06:00
|
|
|
|
2022-12-21 09:07:29 -06:00
|
|
|
// Selection Peek Opacity
|
|
|
|
state.ctxmenu.selectionPeekOpacitySlider = _toolbar_input.slider(
|
|
|
|
state,
|
|
|
|
"selectionPeekOpacity",
|
|
|
|
"Peek Opacity",
|
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
max: 100,
|
|
|
|
step: 10,
|
|
|
|
textStep: 1,
|
|
|
|
cb: () => {
|
|
|
|
state.redraw();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
).slider;
|
|
|
|
|
2022-11-25 10:16:22 -06:00
|
|
|
// Some useful actions to do with selection
|
|
|
|
const actionArray = document.createElement("div");
|
|
|
|
actionArray.classList.add("button-array");
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Save button
|
2022-11-25 10:16:22 -06:00
|
|
|
const saveSelectionButton = document.createElement("button");
|
|
|
|
saveSelectionButton.classList.add("button", "tool");
|
2022-12-21 09:07:29 -06:00
|
|
|
saveSelectionButton.textContent = "Save Sel.";
|
2022-11-25 10:16:22 -06:00
|
|
|
saveSelectionButton.title = "Saves Selection";
|
|
|
|
saveSelectionButton.onclick = () => {
|
|
|
|
downloadCanvas({
|
|
|
|
cropToContent: false,
|
|
|
|
canvas: state.selected.image,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Save as Resource Button
|
2022-11-25 10:16:22 -06:00
|
|
|
const createResourceButton = document.createElement("button");
|
|
|
|
createResourceButton.classList.add("button", "tool");
|
|
|
|
createResourceButton.textContent = "Resource";
|
|
|
|
createResourceButton.title = "Saves Selection as a Resource";
|
|
|
|
createResourceButton.onclick = () => {
|
|
|
|
const image = document.createElement("img");
|
|
|
|
image.src = state.selected.image.toDataURL();
|
2022-12-03 20:07:53 -06:00
|
|
|
image.onload = () => {
|
|
|
|
tools.stamp.state.addResource("Selection Resource", image);
|
|
|
|
tools.stamp.enable();
|
|
|
|
};
|
2022-11-25 10:16:22 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
actionArray.appendChild(saveSelectionButton);
|
|
|
|
actionArray.appendChild(createResourceButton);
|
|
|
|
|
2022-12-21 09:07:29 -06:00
|
|
|
// Some useful actions to do with selection
|
|
|
|
const visibleActionArray = document.createElement("div");
|
|
|
|
visibleActionArray.classList.add("button-array");
|
|
|
|
|
|
|
|
// Save Visible button
|
|
|
|
const saveVisibleSelectionButton = document.createElement("button");
|
|
|
|
saveVisibleSelectionButton.classList.add("button", "tool");
|
|
|
|
saveVisibleSelectionButton.textContent = "Save Vis.";
|
|
|
|
saveVisibleSelectionButton.title = "Saves Visible Selection";
|
|
|
|
saveVisibleSelectionButton.onclick = () => {
|
|
|
|
const canvas = uil.getVisible(state.selected, {
|
|
|
|
categories: ["image", "user", "select-display"],
|
|
|
|
});
|
|
|
|
downloadCanvas({
|
|
|
|
cropToContent: false,
|
|
|
|
canvas,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Save Visible as Resource Button
|
|
|
|
const createVisibleResourceButton = document.createElement("button");
|
|
|
|
createVisibleResourceButton.classList.add("button", "tool");
|
|
|
|
createVisibleResourceButton.textContent = "Vis. to Res.";
|
|
|
|
createVisibleResourceButton.title =
|
|
|
|
"Saves Visible Selection as a Resource";
|
|
|
|
createVisibleResourceButton.onclick = () => {
|
|
|
|
const canvas = uil.getVisible(state.selected, {
|
|
|
|
categories: ["image", "user", "select-display"],
|
|
|
|
});
|
|
|
|
const image = document.createElement("img");
|
|
|
|
image.src = canvas.toDataURL();
|
|
|
|
image.onload = () => {
|
|
|
|
tools.stamp.state.addResource("Selection Resource", image);
|
|
|
|
tools.stamp.enable();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
visibleActionArray.appendChild(saveVisibleSelectionButton);
|
|
|
|
visibleActionArray.appendChild(createVisibleResourceButton);
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
// Disable buttons (if nothing is selected)
|
2022-11-25 10:16:22 -06:00
|
|
|
state.ctxmenu.disableButtons = () => {
|
|
|
|
saveSelectionButton.disabled = true;
|
|
|
|
createResourceButton.disabled = true;
|
2022-12-21 09:07:29 -06:00
|
|
|
saveVisibleSelectionButton.disabled = true;
|
|
|
|
createVisibleResourceButton.disabled = true;
|
2022-11-25 10:16:22 -06:00
|
|
|
};
|
2022-11-25 12:22:16 -06:00
|
|
|
|
|
|
|
// Disable buttons (if something is selected)
|
2022-11-25 10:16:22 -06:00
|
|
|
state.ctxmenu.enableButtons = () => {
|
|
|
|
saveSelectionButton.disabled = "";
|
|
|
|
createResourceButton.disabled = "";
|
2022-12-21 09:07:29 -06:00
|
|
|
saveVisibleSelectionButton.disabled = "";
|
|
|
|
createVisibleResourceButton.disabled = "";
|
2022-11-25 10:16:22 -06:00
|
|
|
};
|
|
|
|
state.ctxmenu.actionArray = actionArray;
|
2022-12-21 09:07:29 -06:00
|
|
|
state.ctxmenu.visibleActionArray = visibleActionArray;
|
2022-11-24 09:30:13 -06:00
|
|
|
}
|
2023-01-02 18:12:18 -06:00
|
|
|
const array = document.createElement("div");
|
|
|
|
array.classList.add("checkbox-array");
|
|
|
|
array.appendChild(state.ctxmenu.snapToGridLabel);
|
|
|
|
array.appendChild(state.ctxmenu.keepAspectRatioLabel);
|
|
|
|
array.appendChild(state.ctxmenu.useClipboardLabel);
|
|
|
|
menu.appendChild(array);
|
2022-12-21 09:07:29 -06:00
|
|
|
menu.appendChild(state.ctxmenu.selectionPeekOpacitySlider);
|
2022-11-25 10:16:22 -06:00
|
|
|
menu.appendChild(state.ctxmenu.actionArray);
|
2022-12-21 09:07:29 -06:00
|
|
|
menu.appendChild(state.ctxmenu.visibleActionArray);
|
2022-11-24 09:30:13 -06:00
|
|
|
},
|
2022-11-24 21:34:34 -06:00
|
|
|
shortcut: "S",
|
2022-11-24 09:30:13 -06:00
|
|
|
}
|
|
|
|
);
|