add cancel button to future generations
Signed-off-by: Victor Seiji Hariki <victorseijih@gmail.com>
This commit is contained in:
parent
1d089f2213
commit
f63bbafe0f
2 changed files with 36 additions and 17 deletions
|
@ -1,3 +1,3 @@
|
|||
.dream-interrupt-btn {
|
||||
.dream-stop-btn {
|
||||
width: 100px;
|
||||
}
|
||||
|
|
|
@ -129,10 +129,35 @@ const _generate = async (
|
|||
if (generationAreas.has(areaid)) return;
|
||||
generationAreas.add(areaid);
|
||||
|
||||
// Makes an element in a location
|
||||
const makeElement = (type, x, y) => {
|
||||
const el = document.createElement(type);
|
||||
el.style.position = "absolute";
|
||||
el.style.left = `${x - imageCollection.inputOffset.x}px`;
|
||||
el.style.top = `${y - imageCollection.inputOffset.y}px`;
|
||||
|
||||
// We can use the input element to add interactible html elements in the world
|
||||
imageCollection.inputElement.appendChild(el);
|
||||
|
||||
return el;
|
||||
};
|
||||
|
||||
// Await for queue
|
||||
let cancelled = false;
|
||||
const waitQueue = async () => {
|
||||
const stopQueueMarchingAnts = march(bb, {style: "#AAF"});
|
||||
|
||||
// Add cancel Button
|
||||
const cancelButton = makeElement("button", bb.x + bb.w - 100, bb.y + bb.h);
|
||||
cancelButton.classList.add("dream-stop-btn");
|
||||
cancelButton.textContent = "Cancel";
|
||||
cancelButton.addEventListener("click", () => {
|
||||
cancelled = true;
|
||||
imageCollection.inputElement.removeChild(cancelButton);
|
||||
stopQueueMarchingAnts();
|
||||
});
|
||||
imageCollection.inputElement.appendChild(cancelButton);
|
||||
|
||||
let qPromise = null;
|
||||
let qResolve = null;
|
||||
await new Promise((finish) => {
|
||||
|
@ -151,8 +176,10 @@ const _generate = async (
|
|||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
stopQueueMarchingAnts();
|
||||
if (!cancelled) {
|
||||
imageCollection.inputElement.removeChild(cancelButton);
|
||||
stopQueueMarchingAnts();
|
||||
}
|
||||
|
||||
return {promise: qPromise, resolve: qResolve};
|
||||
};
|
||||
|
@ -167,30 +194,22 @@ const _generate = async (
|
|||
|
||||
const initialQ = await waitQueue();
|
||||
|
||||
if (cancelled) {
|
||||
nextQueue(initialQ);
|
||||
return;
|
||||
}
|
||||
|
||||
// Images to select through
|
||||
let at = 0;
|
||||
/** @type {Array<string|null>} */
|
||||
const images = [null];
|
||||
/** @type {HTMLDivElement} */
|
||||
let imageSelectMenu = null;
|
||||
|
||||
// Layer for the images
|
||||
const layer = imageCollection.registerLayer(null, {
|
||||
after: maskPaintLayer,
|
||||
});
|
||||
|
||||
const makeElement = (type, x, y) => {
|
||||
const el = document.createElement(type);
|
||||
el.style.position = "absolute";
|
||||
el.style.left = `${x - imageCollection.inputOffset.x}px`;
|
||||
el.style.top = `${y - imageCollection.inputOffset.y}px`;
|
||||
|
||||
// We can use the input element to add interactible html elements in the world
|
||||
imageCollection.inputElement.appendChild(el);
|
||||
|
||||
return el;
|
||||
};
|
||||
|
||||
const redraw = (url = images[at]) => {
|
||||
if (url === null)
|
||||
layer.ctx.clearRect(0, 0, layer.canvas.width, layer.canvas.height);
|
||||
|
@ -216,7 +235,7 @@ const _generate = async (
|
|||
|
||||
// Add Interrupt Button
|
||||
const interruptButton = makeElement("button", bb.x + bb.w - 100, bb.y + bb.h);
|
||||
interruptButton.classList.add("dream-interrupt-btn");
|
||||
interruptButton.classList.add("dream-stop-btn");
|
||||
interruptButton.textContent = "Interrupt";
|
||||
interruptButton.addEventListener("click", () => {
|
||||
fetch(`${host}${url}interrupt`, {method: "POST"});
|
||||
|
|
Loading…
Reference in a new issue