2022-12-04 13:42:24 -06:00
|
|
|
const interrogateTool = () =>
|
|
|
|
toolbar.registerTool(
|
2022-12-18 17:50:40 -06:00
|
|
|
"./res/icons/microscope.svg",
|
2022-12-04 13:42:24 -06:00
|
|
|
"Interrogate",
|
|
|
|
(state, opt) => {
|
|
|
|
// Draw new cursor immediately
|
2022-12-16 21:55:53 -06:00
|
|
|
ovLayer.clear();
|
|
|
|
state.redraw();
|
2022-12-04 13:42:24 -06:00
|
|
|
|
|
|
|
// Start Listeners
|
|
|
|
mouse.listen.world.onmousemove.on(state.mousemovecb);
|
|
|
|
mouse.listen.world.onwheel.on(state.wheelcb);
|
|
|
|
mouse.listen.world.btn.left.onclick.on(state.interrogatecb);
|
|
|
|
},
|
|
|
|
(state, opt) => {
|
|
|
|
// Clear Listeners
|
|
|
|
mouse.listen.world.onmousemove.clear(state.mousemovecb);
|
|
|
|
mouse.listen.world.onwheel.clear(state.wheelcb);
|
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.interrogatecb);
|
|
|
|
|
|
|
|
// Hide Mask
|
|
|
|
setMask("none");
|
2022-12-05 09:26:52 -06:00
|
|
|
|
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
2022-12-04 13:42:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
init: (state) => {
|
|
|
|
state.config = {
|
|
|
|
cursorSizeScrollSpeed: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
state.cursorSize = 512;
|
|
|
|
|
|
|
|
state.snapToGrid = true;
|
|
|
|
state.invertMask = false;
|
2022-12-18 20:35:48 -06:00
|
|
|
state.block_res_change = true;
|
2022-12-04 13:42:24 -06:00
|
|
|
|
2022-12-16 21:55:53 -06:00
|
|
|
state.erasePrevReticle = () => ovLayer.clear();
|
2022-12-04 13:42:24 -06:00
|
|
|
|
|
|
|
state.mousemovecb = (evn) => {
|
|
|
|
state.erasePrevReticle();
|
2022-12-15 21:15:50 -06:00
|
|
|
state.erasePrevReticle = _tool._reticle_draw(
|
|
|
|
getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
),
|
|
|
|
"Interrogate",
|
|
|
|
{
|
|
|
|
w: stableDiffusionData.width,
|
|
|
|
h: stableDiffusionData.height,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sizeTextStyle: "#AFA5",
|
|
|
|
}
|
|
|
|
);
|
2022-12-04 13:42:24 -06:00
|
|
|
};
|
2022-12-07 15:51:33 -06:00
|
|
|
|
|
|
|
state.redraw = () => {
|
|
|
|
state.mousemovecb({
|
|
|
|
x: mouse.coords.world.pos.x,
|
|
|
|
y: mouse.coords.world.pos.y,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-04 13:42:24 -06:00
|
|
|
state.wheelcb = (evn) => {
|
2022-12-07 15:51:33 -06:00
|
|
|
_dream_onwheel(evn, state);
|
2022-12-04 13:42:24 -06:00
|
|
|
};
|
2022-12-06 17:00:50 -06:00
|
|
|
|
2022-12-04 13:42:24 -06:00
|
|
|
state.interrogatecb = (evn) => {
|
|
|
|
interrogate_callback(evn, state);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
populateContextMenu: (menu, state) => {
|
|
|
|
if (!state.ctxmenu) {
|
|
|
|
state.ctxmenu = {};
|
|
|
|
|
|
|
|
// Cursor Size Slider
|
|
|
|
const cursorSizeSlider = _toolbar_input.slider(
|
|
|
|
state,
|
|
|
|
"cursorSize",
|
|
|
|
"Cursor Size",
|
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
max: 2048,
|
|
|
|
step: 128,
|
|
|
|
textStep: 2,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
state.setCursorSize = cursorSizeSlider.setValue;
|
|
|
|
state.ctxmenu.cursorSizeSlider = cursorSizeSlider.slider;
|
|
|
|
|
|
|
|
// Snap to Grid Checkbox
|
|
|
|
state.ctxmenu.snapToGridLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"snapToGrid",
|
2023-01-02 18:59:15 -06:00
|
|
|
"Snap To Grid",
|
|
|
|
"icon-grid"
|
|
|
|
).checkbox;
|
2022-12-04 13:42:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
menu.appendChild(state.ctxmenu.cursorSizeSlider);
|
|
|
|
menu.appendChild(state.ctxmenu.snapToGridLabel);
|
|
|
|
},
|
|
|
|
shortcut: "N",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-12-05 09:26:52 -06:00
|
|
|
const interrogate_callback = async (evn, state) => {
|
2022-12-04 13:42:24 -06:00
|
|
|
const bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
|
|
|
// Do nothing if no image exists
|
2022-12-05 09:26:52 -06:00
|
|
|
const sectionCanvas = uil.getVisible({x: bb.x, y: bb.y, w: bb.w, h: bb.h});
|
|
|
|
|
|
|
|
if (isCanvasBlank(0, 0, bb.w, bb.h, sectionCanvas)) return;
|
2022-12-04 13:42:24 -06:00
|
|
|
|
|
|
|
// Build request to the API
|
|
|
|
const request = {};
|
2022-12-04 13:53:16 -06:00
|
|
|
|
|
|
|
// Temporary canvas for interrogated image
|
|
|
|
const auxCanvas = document.createElement("canvas");
|
|
|
|
auxCanvas.width = bb.w;
|
|
|
|
auxCanvas.height = bb.h;
|
|
|
|
const auxCtx = auxCanvas.getContext("2d");
|
|
|
|
|
|
|
|
auxCtx.fillStyle = "#000F";
|
|
|
|
|
|
|
|
// Get init image
|
|
|
|
auxCtx.fillRect(0, 0, bb.w, bb.h);
|
2022-12-05 09:26:52 -06:00
|
|
|
auxCtx.drawImage(sectionCanvas, 0, 0);
|
2022-12-04 13:53:16 -06:00
|
|
|
request.image = auxCanvas.toDataURL();
|
|
|
|
|
2022-12-04 13:42:24 -06:00
|
|
|
request.model = "clip"; //TODO maybe make a selectable option once A1111 supports the new openclip thingy?
|
2022-12-05 09:26:52 -06:00
|
|
|
const stopMarching = march(bb, {style: "#AFAF"});
|
|
|
|
try {
|
|
|
|
const result = await _interrogate(request);
|
|
|
|
const text = prompt(
|
|
|
|
result +
|
|
|
|
"\n\nDo you want to replace your prompt with this? You can change it down below:",
|
|
|
|
result
|
|
|
|
);
|
|
|
|
if (text) {
|
|
|
|
document.getElementById("prompt").value = text;
|
2022-12-04 13:42:24 -06:00
|
|
|
tools.dream.enable();
|
|
|
|
}
|
2022-12-05 09:26:52 -06:00
|
|
|
} finally {
|
|
|
|
stopMarching();
|
|
|
|
}
|
2022-12-04 13:42:24 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* our private eye
|
|
|
|
*
|
|
|
|
* @param {StableDiffusionRequest} request Stable diffusion request
|
2022-12-06 18:05:43 -06:00
|
|
|
* @returns {Promise<string>}
|
2022-12-04 13:42:24 -06:00
|
|
|
*/
|
|
|
|
const _interrogate = async (request) => {
|
2022-12-26 19:24:26 -06:00
|
|
|
const apiURL = `${host}${config.api.path}interrogate`;
|
2022-12-04 13:42:24 -06:00
|
|
|
|
|
|
|
/** @type {StableDiffusionResponse} */
|
|
|
|
let data = null;
|
|
|
|
try {
|
|
|
|
const response = await fetch(apiURL, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Accept: "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(request),
|
|
|
|
});
|
|
|
|
|
|
|
|
data = await response.json();
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.caption;
|
|
|
|
};
|