2023-01-12 20:00:34 -06:00
|
|
|
/**
|
|
|
|
* Generic wheel handler
|
|
|
|
*/
|
|
|
|
let _stamp_wheel_accum = 0;
|
|
|
|
|
|
|
|
const _stamp_onwheel = (evn, state) => {
|
|
|
|
if (evn.mode !== WheelEvent.DOM_DELTA_PIXEL) {
|
|
|
|
// We don't really handle non-pixel scrolling
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let delta = evn.delta;
|
|
|
|
if (evn.evn.shiftKey) delta *= 0.01;
|
|
|
|
|
|
|
|
// A simple but (I hope) effective fix for mouse wheel behavior
|
|
|
|
_stamp_wheel_accum += delta;
|
|
|
|
|
|
|
|
if (
|
|
|
|
!evn.evn.shiftKey &&
|
|
|
|
Math.abs(_stamp_wheel_accum) > config.wheelTickSize
|
|
|
|
) {
|
|
|
|
// Snap to next or previous position
|
|
|
|
const v =
|
|
|
|
state.scale - 0.1 * (_stamp_wheel_accum / Math.abs(_stamp_wheel_accum));
|
|
|
|
|
|
|
|
state.setScale(v + snap(v, 0, 0.1));
|
|
|
|
state.redraw(evn);
|
|
|
|
|
|
|
|
_stamp_wheel_accum = 0; // Zero accumulation
|
|
|
|
} else if (evn.evn.shiftKey && Math.abs(_stamp_wheel_accum) >= 1) {
|
|
|
|
const v = state.scale - _stamp_wheel_accum * 0.01;
|
|
|
|
state.setScale(v);
|
|
|
|
state.redraw(evn);
|
|
|
|
|
|
|
|
_stamp_wheel_accum = 0; // Zero accumulation
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
const stampTool = () =>
|
|
|
|
toolbar.registerTool(
|
2022-12-18 17:50:40 -06:00
|
|
|
"./res/icons/file-up.svg",
|
2022-11-24 21:34:34 -06:00
|
|
|
"Stamp Image",
|
|
|
|
(state, opt) => {
|
2022-11-29 18:16:59 -06:00
|
|
|
state.loaded = true;
|
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
// Draw new cursor immediately
|
2022-12-16 19:57:28 -06:00
|
|
|
ovLayer.clear();
|
2022-11-29 14:55:25 -06:00
|
|
|
state.movecb({...mouse.coords.world.pos});
|
2022-11-24 21:34:34 -06:00
|
|
|
|
|
|
|
// Start Listeners
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.onmousemove.on(state.movecb);
|
|
|
|
mouse.listen.world.btn.left.onclick.on(state.drawcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.on(state.cancelcb);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2023-01-12 20:00:34 -06:00
|
|
|
mouse.listen.world.btn.left.ondragstart.on(state.dragstartcb);
|
|
|
|
mouse.listen.world.btn.left.ondrag.on(state.dragcb);
|
|
|
|
mouse.listen.world.btn.left.ondragend.on(state.dragendcb);
|
|
|
|
|
|
|
|
mouse.listen.world.onwheel.on(state.onwheelcb);
|
2023-02-14 21:37:06 -06:00
|
|
|
keyboard.onShortcut({key: "Equal"}, state.togglemirror);
|
2023-01-12 20:00:34 -06:00
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
// For calls from other tools to paste image
|
|
|
|
if (opt && opt.image) {
|
2022-11-25 12:22:16 -06:00
|
|
|
state.addResource(
|
2022-11-24 21:34:34 -06:00
|
|
|
opt.name || "Clipboard",
|
|
|
|
opt.image,
|
2022-12-02 04:22:47 -06:00
|
|
|
opt.temporary === undefined ? true : opt.temporary,
|
|
|
|
false
|
2022-11-24 21:34:34 -06:00
|
|
|
);
|
|
|
|
state.ctxmenu.uploadButton.disabled = true;
|
|
|
|
state.back = opt.back || null;
|
2022-11-24 21:55:16 -06:00
|
|
|
toolbar.lock();
|
2022-11-24 21:34:34 -06:00
|
|
|
} else if (opt) {
|
|
|
|
throw Error(
|
|
|
|
"Pasting from other tools must be in format {image, name?, temporary?, back?}"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
state.ctxmenu.uploadButton.disabled = "";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(state, opt) => {
|
2022-11-29 18:16:59 -06:00
|
|
|
state.loaded = false;
|
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
// Clear Listeners
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.onmousemove.clear(state.movecb);
|
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.drawcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.clear(state.cancelcb);
|
2022-11-24 22:46:49 -06:00
|
|
|
|
2023-01-12 20:00:34 -06:00
|
|
|
mouse.listen.world.btn.left.ondragstart.clear(state.dragstartcb);
|
|
|
|
mouse.listen.world.btn.left.ondrag.clear(state.dragcb);
|
|
|
|
mouse.listen.world.btn.left.ondragend.clear(state.dragendcb);
|
|
|
|
|
|
|
|
mouse.listen.world.onwheel.clear(state.onwheelcb);
|
2023-02-14 21:37:06 -06:00
|
|
|
keyboard.deleteShortcut(state.togglemirror, "Delete");
|
2023-01-12 20:00:34 -06:00
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
ovLayer.clear();
|
2022-11-24 21:34:34 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
init: (state) => {
|
2022-11-29 18:16:59 -06:00
|
|
|
state.loaded = false;
|
2022-11-24 21:34:34 -06:00
|
|
|
state.snapToGrid = true;
|
|
|
|
state.resources = [];
|
|
|
|
state.selected = null;
|
|
|
|
state.back = null;
|
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
state.lastMouseMove = {x: 0, y: 0};
|
2022-12-18 20:35:48 -06:00
|
|
|
state.block_res_change = true;
|
2022-11-25 12:22:16 -06:00
|
|
|
|
2023-01-12 20:00:34 -06:00
|
|
|
// Current Rotation
|
|
|
|
let rotation = 0;
|
|
|
|
let rotating = null;
|
|
|
|
// Current Scale
|
|
|
|
state.scale = 1;
|
|
|
|
|
2023-02-14 21:37:06 -06:00
|
|
|
state.togglemirror = () => {
|
|
|
|
state.mirrorSetValue(!state.mirrorStamp);
|
|
|
|
state.redraw();
|
|
|
|
};
|
|
|
|
|
2022-12-23 19:18:37 -06:00
|
|
|
state.selectResource = (resource, nolock = true, deselect = true) => {
|
2023-01-12 20:00:34 -06:00
|
|
|
rotation = 0;
|
|
|
|
state.setScale(1);
|
2022-12-02 04:22:47 -06:00
|
|
|
if (nolock && state.ctxmenu.uploadButton.disabled) return;
|
|
|
|
|
2023-02-14 21:37:06 -06:00
|
|
|
state.mirrorSetValue(false);
|
|
|
|
|
2022-12-02 04:22:47 -06:00
|
|
|
console.debug(
|
|
|
|
`[stamp] Selecting Resource '${resource && resource.name}'[${
|
|
|
|
resource && resource.id
|
|
|
|
}]`
|
|
|
|
);
|
2022-11-25 10:16:22 -06:00
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
const resourceWrapper = resource && resource.dom.wrapper;
|
2022-11-25 10:16:22 -06:00
|
|
|
|
2022-11-25 12:22:16 -06:00
|
|
|
const wasSelected =
|
2022-12-11 17:54:38 -06:00
|
|
|
resourceWrapper && resourceWrapper.classList.contains("active");
|
2022-11-25 10:16:22 -06:00
|
|
|
|
|
|
|
Array.from(state.ctxmenu.resourceList.children).forEach((child) => {
|
2022-12-11 17:54:38 -06:00
|
|
|
child.classList.remove("active");
|
2022-11-25 10:16:22 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Select
|
|
|
|
if (!wasSelected) {
|
2022-12-11 17:54:38 -06:00
|
|
|
resourceWrapper && resourceWrapper.classList.add("active");
|
2022-11-25 10:16:22 -06:00
|
|
|
state.selected = resource;
|
|
|
|
}
|
2022-12-23 19:18:37 -06:00
|
|
|
// If already selected, clear selection (if deselection is enabled)
|
2022-12-23 19:19:11 -06:00
|
|
|
else if (deselect) {
|
2022-12-11 17:54:38 -06:00
|
|
|
resourceWrapper.classList.remove("active");
|
2022-11-25 10:16:22 -06:00
|
|
|
state.selected = null;
|
|
|
|
}
|
2022-11-25 12:22:16 -06:00
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
ovLayer.clear();
|
2022-12-23 19:18:37 -06:00
|
|
|
if (state.loaded) state.redraw();
|
2022-11-25 10:16:22 -06:00
|
|
|
};
|
|
|
|
|
2022-12-12 07:03:05 -06:00
|
|
|
// Open IndexedDB connection
|
2022-12-03 07:48:05 -06:00
|
|
|
// Synchronizes resources array with the DOM and Local Storage
|
2022-11-24 21:34:34 -06:00
|
|
|
const syncResources = () => {
|
2022-12-12 07:03:05 -06:00
|
|
|
// Saves to IndexedDB
|
|
|
|
const resources = db
|
|
|
|
.transaction("resources", "readwrite")
|
|
|
|
.objectStore("resources");
|
2022-12-03 07:48:05 -06:00
|
|
|
try {
|
2022-12-12 07:03:05 -06:00
|
|
|
const FetchKeysQuery = resources.getAllKeys();
|
|
|
|
FetchKeysQuery.onsuccess = () => {
|
|
|
|
const keys = FetchKeysQuery.result;
|
|
|
|
keys.forEach((key) => {
|
|
|
|
if (!state.resources.find((resource) => resource.id === key))
|
|
|
|
resources.delete(key);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
state.resources
|
|
|
|
.filter((resource) => !resource.temporary && resource.dirty)
|
|
|
|
.forEach((resource) => {
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = resource.image.width;
|
|
|
|
canvas.height = resource.image.height;
|
|
|
|
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
ctx.drawImage(resource.image, 0, 0);
|
|
|
|
|
|
|
|
resources.put({
|
|
|
|
id: resource.id,
|
|
|
|
name: resource.name,
|
|
|
|
src: canvas.toDataURL(),
|
|
|
|
});
|
|
|
|
|
|
|
|
resource.dirty = false;
|
|
|
|
});
|
2022-12-03 07:48:05 -06:00
|
|
|
} catch (e) {
|
|
|
|
console.warn(
|
2022-12-12 07:03:05 -06:00
|
|
|
"[stamp] Failed to synchronize resources with IndexedDB"
|
2022-12-03 07:48:05 -06:00
|
|
|
);
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
|
2022-11-25 10:16:22 -06:00
|
|
|
// Creates DOM elements when needed
|
2022-11-24 21:34:34 -06:00
|
|
|
state.resources.forEach((resource) => {
|
2022-11-25 10:16:22 -06:00
|
|
|
if (
|
|
|
|
!state.ctxmenu.resourceList.querySelector(
|
|
|
|
`#resource-${resource.id}`
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
console.debug(
|
2022-12-02 04:22:47 -06:00
|
|
|
`[stamp] Creating Resource Element [resource-${resource.id}]`
|
2022-11-25 10:16:22 -06:00
|
|
|
);
|
2022-11-24 21:34:34 -06:00
|
|
|
const resourceWrapper = document.createElement("div");
|
|
|
|
resourceWrapper.id = `resource-${resource.id}`;
|
2022-12-21 15:29:11 -06:00
|
|
|
resourceWrapper.title = resource.name;
|
2022-12-11 17:54:38 -06:00
|
|
|
resourceWrapper.classList.add("resource", "list-item");
|
|
|
|
const resourceTitle = document.createElement("input");
|
|
|
|
resourceTitle.value = resource.name;
|
|
|
|
resourceTitle.style.pointerEvents = "none";
|
|
|
|
resourceTitle.addEventListener("change", () => {
|
|
|
|
resource.name = resourceTitle.value;
|
2022-12-12 07:03:05 -06:00
|
|
|
resource.dirty = true;
|
2022-12-21 15:29:11 -06:00
|
|
|
resourceWrapper.title = resourceTitle.value;
|
2022-12-11 17:54:38 -06:00
|
|
|
|
|
|
|
syncResources();
|
|
|
|
});
|
2022-12-21 15:29:11 -06:00
|
|
|
resourceTitle.addEventListener("keyup", function (event) {
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
resourceTitle.blur();
|
|
|
|
}
|
|
|
|
});
|
2022-12-11 17:54:38 -06:00
|
|
|
|
|
|
|
resourceTitle.addEventListener("blur", () => {
|
|
|
|
resourceTitle.style.pointerEvents = "none";
|
|
|
|
});
|
|
|
|
resourceTitle.classList.add("resource-title", "title");
|
|
|
|
|
2022-12-02 11:31:49 -06:00
|
|
|
resourceWrapper.appendChild(resourceTitle);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-25 10:16:22 -06:00
|
|
|
resourceWrapper.addEventListener("click", () =>
|
2022-11-25 12:22:16 -06:00
|
|
|
state.selectResource(resource)
|
2022-11-25 10:16:22 -06:00
|
|
|
);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-12-11 17:54:38 -06:00
|
|
|
resourceWrapper.addEventListener("dblclick", () => {
|
|
|
|
resourceTitle.style.pointerEvents = "auto";
|
|
|
|
resourceTitle.focus();
|
|
|
|
resourceTitle.select();
|
|
|
|
});
|
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
resourceWrapper.addEventListener("mouseover", () => {
|
|
|
|
state.ctxmenu.previewPane.style.display = "block";
|
|
|
|
state.ctxmenu.previewPane.style.backgroundImage = `url(${resource.image.src})`;
|
|
|
|
});
|
|
|
|
resourceWrapper.addEventListener("mouseleave", () => {
|
|
|
|
state.ctxmenu.previewPane.style.display = "none";
|
|
|
|
});
|
|
|
|
|
2022-12-02 11:31:49 -06:00
|
|
|
// Add action buttons
|
|
|
|
const actionArray = document.createElement("div");
|
|
|
|
actionArray.classList.add("actions");
|
|
|
|
|
2022-12-11 17:54:38 -06:00
|
|
|
const saveButton = document.createElement("button");
|
|
|
|
saveButton.addEventListener(
|
2022-12-04 05:31:43 -06:00
|
|
|
"click",
|
|
|
|
(evn) => {
|
2022-12-11 21:50:25 -06:00
|
|
|
evn.stopPropagation();
|
2022-12-11 17:54:38 -06:00
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = resource.image.width;
|
|
|
|
canvas.height = resource.image.height;
|
|
|
|
canvas.getContext("2d").drawImage(resource.image, 0, 0);
|
|
|
|
|
|
|
|
downloadCanvas({
|
|
|
|
canvas,
|
|
|
|
filename: `openOutpaint - resource '${resource.name}'.png`,
|
|
|
|
});
|
2022-12-04 05:31:43 -06:00
|
|
|
},
|
|
|
|
{passive: false}
|
|
|
|
);
|
2022-12-11 17:54:38 -06:00
|
|
|
saveButton.title = "Download Resource";
|
|
|
|
saveButton.appendChild(document.createElement("div"));
|
|
|
|
saveButton.classList.add("download-btn");
|
2022-12-02 11:31:49 -06:00
|
|
|
|
|
|
|
const trashButton = document.createElement("button");
|
2022-12-03 08:05:43 -06:00
|
|
|
trashButton.addEventListener(
|
|
|
|
"click",
|
|
|
|
(evn) => {
|
|
|
|
evn.stopPropagation();
|
|
|
|
state.ctxmenu.previewPane.style.display = "none";
|
|
|
|
state.deleteResource(resource.id);
|
|
|
|
},
|
|
|
|
{passive: false}
|
|
|
|
);
|
2022-12-02 11:31:49 -06:00
|
|
|
trashButton.title = "Delete Resource";
|
|
|
|
trashButton.appendChild(document.createElement("div"));
|
|
|
|
trashButton.classList.add("delete-btn");
|
|
|
|
|
2022-12-11 17:54:38 -06:00
|
|
|
actionArray.appendChild(saveButton);
|
2022-12-02 11:31:49 -06:00
|
|
|
actionArray.appendChild(trashButton);
|
|
|
|
resourceWrapper.appendChild(actionArray);
|
2022-11-24 21:34:34 -06:00
|
|
|
state.ctxmenu.resourceList.appendChild(resourceWrapper);
|
2022-11-25 10:16:22 -06:00
|
|
|
resource.dom = {wrapper: resourceWrapper};
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-25 10:16:22 -06:00
|
|
|
// Removes DOM elements when needed
|
2022-11-24 21:34:34 -06:00
|
|
|
const elements = Array.from(state.ctxmenu.resourceList.children);
|
|
|
|
|
|
|
|
if (elements.length > state.resources.length)
|
|
|
|
elements.forEach((element) => {
|
2022-11-24 22:46:49 -06:00
|
|
|
let remove = true;
|
|
|
|
state.resources.some((resource) => {
|
2022-12-02 04:22:47 -06:00
|
|
|
if (element.id.endsWith(resource.id)) {
|
|
|
|
remove = false;
|
|
|
|
}
|
2022-11-24 22:46:49 -06:00
|
|
|
});
|
|
|
|
|
2022-12-02 04:22:47 -06:00
|
|
|
if (remove) {
|
|
|
|
console.debug(`[stamp] Sync Removing Element [${element.id}]`);
|
|
|
|
state.ctxmenu.resourceList.removeChild(element);
|
|
|
|
}
|
2022-11-24 21:34:34 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-25 10:16:22 -06:00
|
|
|
// Adds a image resource (temporary allows only one draw, used for pasting)
|
2022-12-02 04:22:47 -06:00
|
|
|
state.addResource = (name, image, temporary = false, nolock = true) => {
|
2022-11-24 21:34:34 -06:00
|
|
|
const id = guid();
|
|
|
|
const resource = {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
image,
|
2022-12-12 07:03:05 -06:00
|
|
|
dirty: true,
|
2022-11-24 21:34:34 -06:00
|
|
|
temporary,
|
|
|
|
};
|
2022-12-02 04:22:47 -06:00
|
|
|
|
|
|
|
console.info(`[stamp] Adding Resource '${name}'[${id}]`);
|
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
state.resources.push(resource);
|
|
|
|
syncResources();
|
2022-11-25 10:16:22 -06:00
|
|
|
|
|
|
|
// Select this resource
|
2022-12-23 19:18:37 -06:00
|
|
|
state.selectResource(resource, nolock, false);
|
2022-11-25 10:16:22 -06:00
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
return resource;
|
|
|
|
};
|
2022-11-25 10:16:22 -06:00
|
|
|
|
|
|
|
// Used for temporary images too
|
2022-11-24 21:34:34 -06:00
|
|
|
state.deleteResource = (id) => {
|
2022-12-02 04:22:47 -06:00
|
|
|
const resourceIndex = state.resources.findIndex((v) => v.id === id);
|
|
|
|
const resource = state.resources[resourceIndex];
|
2022-12-03 08:05:43 -06:00
|
|
|
if (state.selected === resource) state.selected = null;
|
2022-12-02 04:22:47 -06:00
|
|
|
console.info(
|
|
|
|
`[stamp] Deleting Resource '${resource.name}'[${resource.id}]`
|
|
|
|
);
|
|
|
|
|
|
|
|
state.resources.splice(resourceIndex, 1);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
|
|
|
syncResources();
|
|
|
|
};
|
|
|
|
|
2023-01-12 20:00:34 -06:00
|
|
|
state.onwheelcb = (evn) => {
|
|
|
|
_stamp_onwheel(evn, state);
|
|
|
|
};
|
|
|
|
|
|
|
|
state.dragstartcb = (evn) => {
|
|
|
|
const {x, y, sx, sy} = _tool._process_cursor(evn, state.snapToGrid);
|
|
|
|
rotating = {x: sx, y: sy};
|
|
|
|
};
|
|
|
|
|
|
|
|
state.dragcb = (evn) => {
|
|
|
|
if (rotating) {
|
|
|
|
rotation = Math.atan2(rotating.x - evn.x, evn.y - rotating.y);
|
|
|
|
|
|
|
|
if (evn.evn.shiftKey)
|
|
|
|
rotation =
|
|
|
|
config.rotationSnappingAngles.find(
|
|
|
|
(v) =>
|
|
|
|
Math.abs(v - rotation) < config.rotationSnappingDistance
|
|
|
|
) ?? rotation;
|
2022-11-29 14:55:25 -06:00
|
|
|
}
|
2023-01-12 20:00:34 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
state.dragendcb = (evn) => {
|
|
|
|
rotating = null;
|
|
|
|
};
|
2022-11-25 12:22:16 -06:00
|
|
|
|
2023-01-12 20:00:34 -06:00
|
|
|
let erasePrevCursor = () => null;
|
2022-12-20 20:26:52 -06:00
|
|
|
|
2023-01-12 20:00:34 -06:00
|
|
|
state.movecb = (evn) => {
|
|
|
|
const {x, y, sx, sy} = _tool._process_cursor(evn, state.snapToGrid);
|
|
|
|
|
|
|
|
// Erase Previous Cursors
|
|
|
|
erasePrevCursor();
|
2022-12-20 20:26:52 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
state.lastMouseMove = evn;
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
ovLayer.clear();
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2023-01-12 20:00:34 -06:00
|
|
|
let px = sx;
|
|
|
|
let py = sy;
|
|
|
|
|
|
|
|
if (rotating) {
|
|
|
|
px = rotating.x;
|
|
|
|
py = rotating.y;
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Draw selected image
|
|
|
|
if (state.selected) {
|
2023-01-12 20:00:34 -06:00
|
|
|
ovCtx.save();
|
|
|
|
ovCtx.translate(px, py);
|
2023-02-14 21:37:06 -06:00
|
|
|
ovCtx.scale(
|
|
|
|
state.scale * (state.mirrorStamp ? -1 : 1),
|
|
|
|
state.scale
|
|
|
|
);
|
|
|
|
ovCtx.rotate(rotation * (state.mirrorStamp ? -1 : 1));
|
2023-01-12 20:00:34 -06:00
|
|
|
|
|
|
|
ovCtx.drawImage(state.selected.image, 0, 0);
|
|
|
|
ovCtx.restore();
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
2022-11-29 14:55:25 -06:00
|
|
|
|
|
|
|
// Draw current cursor location
|
2023-01-12 20:00:34 -06:00
|
|
|
erasePrevCursor = _tool._cursor_draw(px, py);
|
2022-11-24 21:34:34 -06:00
|
|
|
};
|
|
|
|
|
2022-12-20 20:43:29 -06:00
|
|
|
state.redraw = () => {
|
|
|
|
state.movecb(state.lastMouseMove);
|
|
|
|
};
|
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
state.drawcb = (evn) => {
|
2023-01-12 20:00:34 -06:00
|
|
|
const {x, y, sx, sy} = _tool._process_cursor(evn, state.snapToGrid);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
const resource = state.selected;
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
if (resource) {
|
2023-01-12 20:00:34 -06:00
|
|
|
const {canvas, bb} = cropCanvas(ovCanvas, {border: 10});
|
2023-01-24 20:48:09 -06:00
|
|
|
|
|
|
|
let commandLog = "";
|
|
|
|
|
|
|
|
const addline = (v, newline = true) => {
|
|
|
|
commandLog += v;
|
|
|
|
if (newline) commandLog += "\n";
|
|
|
|
};
|
|
|
|
addline(
|
|
|
|
`Stamped image '${resource.name}' to x: ${bb.x} and y: ${bb.y}`
|
|
|
|
);
|
|
|
|
addline(` - scaling : ${state.scale}`);
|
|
|
|
addline(
|
|
|
|
` - rotation: ${
|
|
|
|
Math.round(1000 * ((180 * rotation) / Math.PI)) / 1000
|
|
|
|
} degrees`,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-01-24 20:37:38 -06:00
|
|
|
commands.runCommand(
|
|
|
|
"drawImage",
|
|
|
|
"Image Stamp",
|
|
|
|
{
|
|
|
|
image: canvas,
|
|
|
|
x: bb.x,
|
|
|
|
y: bb.y,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
extra: {
|
|
|
|
log: commandLog,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2022-12-02 04:22:47 -06:00
|
|
|
if (resource.temporary) {
|
|
|
|
state.deleteResource(resource.id);
|
|
|
|
}
|
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.back) {
|
|
|
|
toolbar.unlock();
|
|
|
|
const backfn = state.back;
|
|
|
|
state.back = null;
|
|
|
|
backfn({message: "Returning from stamp", pasted: true});
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
state.cancelcb = (evn) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
state.selectResource(null);
|
|
|
|
|
|
|
|
if (state.back) {
|
|
|
|
toolbar.unlock();
|
|
|
|
const backfn = state.back;
|
|
|
|
state.back = null;
|
|
|
|
backfn({message: "Returning from stamp", pasted: false});
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
|
|
|
};
|
2022-11-25 10:16:22 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates context menu
|
|
|
|
*/
|
2022-11-24 21:34:34 -06:00
|
|
|
if (!state.ctxmenu) {
|
|
|
|
state.ctxmenu = {};
|
|
|
|
// Snap To Grid Checkbox
|
2023-01-02 18:12:18 -06:00
|
|
|
const array = document.createElement("div");
|
|
|
|
array.classList.add("checkbox-array");
|
|
|
|
array.appendChild(
|
|
|
|
_toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"snapToGrid",
|
|
|
|
"Snap To Grid",
|
|
|
|
"icon-grid"
|
|
|
|
).checkbox
|
|
|
|
);
|
2023-02-14 21:37:06 -06:00
|
|
|
|
|
|
|
// Mirror Stamp Checkbox
|
|
|
|
const {checkbox: mirrorCheckbox, setValue: mirrorSetValue} =
|
|
|
|
_toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"mirrorStamp",
|
|
|
|
"Mirror Stamp",
|
|
|
|
"icon-flip-horizontal"
|
|
|
|
);
|
|
|
|
array.appendChild(mirrorCheckbox);
|
|
|
|
state.mirrorSetValue = mirrorSetValue;
|
|
|
|
|
|
|
|
state.ctxmenu.buttonArray = array;
|
2022-11-24 21:34:34 -06:00
|
|
|
|
2023-01-12 20:00:34 -06:00
|
|
|
// Scale Slider
|
|
|
|
const scaleSlider = _toolbar_input.slider(state, "scale", "Scale", {
|
|
|
|
min: 0.01,
|
|
|
|
max: 10,
|
|
|
|
step: 0.1,
|
2023-01-12 20:41:01 -06:00
|
|
|
textStep: 0.001,
|
2023-01-12 20:00:34 -06:00
|
|
|
});
|
|
|
|
state.ctxmenu.scaleSlider = scaleSlider.slider;
|
|
|
|
state.setScale = scaleSlider.setValue;
|
|
|
|
|
2022-11-24 21:34:34 -06:00
|
|
|
// Create resource list
|
|
|
|
const uploadButtonId = `upload-btn-${guid()}`;
|
|
|
|
|
|
|
|
const resourceManager = document.createElement("div");
|
|
|
|
resourceManager.classList.add("resource-manager");
|
|
|
|
const resourceList = document.createElement("div");
|
2022-12-11 17:54:38 -06:00
|
|
|
resourceList.classList.add("list");
|
2022-11-24 21:34:34 -06:00
|
|
|
|
|
|
|
const previewPane = document.createElement("div");
|
|
|
|
previewPane.classList.add("preview-pane");
|
|
|
|
|
|
|
|
const uploadLabel = document.createElement("label");
|
|
|
|
uploadLabel.classList.add("upload-button");
|
|
|
|
uploadLabel.classList.add("button");
|
|
|
|
uploadLabel.classList.add("tool");
|
|
|
|
uploadLabel.textContent = "Upload Image";
|
|
|
|
uploadLabel.htmlFor = uploadButtonId;
|
|
|
|
const uploadButton = document.createElement("input");
|
|
|
|
uploadButton.id = uploadButtonId;
|
|
|
|
uploadButton.type = "file";
|
|
|
|
uploadButton.accept = "image/*";
|
|
|
|
uploadButton.multiple = true;
|
|
|
|
uploadButton.style.display = "none";
|
|
|
|
|
|
|
|
uploadButton.addEventListener("change", (evn) => {
|
|
|
|
[...uploadButton.files].forEach((file) => {
|
|
|
|
if (file.type.startsWith("image/")) {
|
|
|
|
console.info("Uploading Image " + file.name);
|
|
|
|
const url = window.URL || window.webkitURL;
|
|
|
|
const image = document.createElement("img");
|
|
|
|
image.src = url.createObjectURL(file);
|
|
|
|
|
2022-12-03 10:08:29 -06:00
|
|
|
image.onload = () => state.addResource(file.name, image, false);
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
uploadButton.value = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
uploadLabel.appendChild(uploadButton);
|
|
|
|
resourceManager.appendChild(resourceList);
|
|
|
|
resourceManager.appendChild(uploadLabel);
|
|
|
|
resourceManager.appendChild(previewPane);
|
|
|
|
|
|
|
|
resourceManager.addEventListener(
|
|
|
|
"drop",
|
|
|
|
(evn) => {
|
|
|
|
evn.preventDefault();
|
|
|
|
resourceManager.classList.remove("dragging");
|
|
|
|
|
|
|
|
if (evn.dataTransfer.items) {
|
|
|
|
Array.from(evn.dataTransfer.items).forEach((item) => {
|
|
|
|
if (item.kind === "file" && item.type.startsWith("image/")) {
|
|
|
|
const file = item.getAsFile();
|
|
|
|
const url = window.URL || window.webkitURL;
|
|
|
|
const image = document.createElement("img");
|
|
|
|
image.src = url.createObjectURL(file);
|
|
|
|
|
|
|
|
state.addResource(file.name, image, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{passive: false}
|
|
|
|
);
|
|
|
|
resourceManager.addEventListener(
|
|
|
|
"dragover",
|
|
|
|
(evn) => {
|
|
|
|
evn.preventDefault();
|
|
|
|
},
|
|
|
|
{passive: false}
|
|
|
|
);
|
|
|
|
|
|
|
|
resourceManager.addEventListener("dragover", (evn) => {
|
|
|
|
resourceManager.classList.add("dragging");
|
|
|
|
});
|
|
|
|
|
|
|
|
resourceManager.addEventListener("dragover", (evn) => {
|
|
|
|
resourceManager.classList.remove("dragging");
|
|
|
|
});
|
|
|
|
|
|
|
|
state.ctxmenu.uploadButton = uploadButton;
|
|
|
|
state.ctxmenu.previewPane = previewPane;
|
|
|
|
state.ctxmenu.resourceManager = resourceManager;
|
|
|
|
state.ctxmenu.resourceList = resourceList;
|
2022-12-03 07:48:05 -06:00
|
|
|
|
2022-12-12 07:03:05 -06:00
|
|
|
// Performs resource fetch from IndexedDB
|
2023-01-26 22:39:08 -06:00
|
|
|
const loadResources = async () => {
|
2022-12-12 07:03:05 -06:00
|
|
|
console.debug("[stamp] Connected to IndexedDB");
|
|
|
|
|
|
|
|
/** @type {IDBRequest<{id: string, name: string, src: string}[]>} */
|
|
|
|
const FetchAllTransaction = db
|
|
|
|
.transaction("resources")
|
|
|
|
.objectStore("resources")
|
|
|
|
.getAll();
|
|
|
|
|
|
|
|
FetchAllTransaction.onsuccess = async () => {
|
|
|
|
const data = FetchAllTransaction.result;
|
|
|
|
|
2022-12-03 07:48:05 -06:00
|
|
|
state.resources.push(
|
2022-12-03 19:51:31 -06:00
|
|
|
...(await Promise.all(
|
2022-12-12 07:03:05 -06:00
|
|
|
data.map((resource) => {
|
2022-12-03 19:51:31 -06:00
|
|
|
const image = document.createElement("img");
|
|
|
|
image.src = resource.src;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
image.onload = () =>
|
2022-12-12 07:03:05 -06:00
|
|
|
resolve({
|
|
|
|
id: resource.id,
|
|
|
|
name: resource.name,
|
|
|
|
image,
|
|
|
|
});
|
2022-12-03 19:51:31 -06:00
|
|
|
});
|
|
|
|
})
|
|
|
|
))
|
2022-12-03 07:48:05 -06:00
|
|
|
);
|
|
|
|
syncResources();
|
2022-12-12 07:03:05 -06:00
|
|
|
};
|
|
|
|
};
|
2023-01-26 22:39:08 -06:00
|
|
|
|
|
|
|
if (db) loadResources();
|
|
|
|
else ondatabaseload.on(loadResources);
|
2022-11-24 21:34:34 -06:00
|
|
|
}
|
2022-11-25 10:16:22 -06:00
|
|
|
},
|
|
|
|
populateContextMenu: (menu, state) => {
|
2023-02-14 21:37:06 -06:00
|
|
|
menu.appendChild(state.ctxmenu.buttonArray);
|
2023-01-12 20:00:34 -06:00
|
|
|
menu.appendChild(state.ctxmenu.scaleSlider);
|
2022-11-24 21:34:34 -06:00
|
|
|
menu.appendChild(state.ctxmenu.resourceManager);
|
|
|
|
},
|
|
|
|
shortcut: "U",
|
|
|
|
}
|
|
|
|
);
|