introduce tags, structs, and reticles

this was meant to be split into smaller changes, but I realised I edited my existing revision too late.
This commit is contained in:
りき萌 2024-09-08 13:53:29 +02:00
parent 8356b6c750
commit 5b7d9586ea
26 changed files with 1113 additions and 351 deletions

View file

@ -1,43 +1,69 @@
export class Painter {
constructor(paintArea) {
this.paintArea = paintArea;
}
import { listen } from "rkgk/framework.js";
renderBrushToWall(haku, centerX, centerY, wall) {
haku.resetVm();
let evalResult = haku.evalBrush();
if (evalResult.status != "ok")
return { status: "error", phase: "eval", result: evalResult };
let left = centerX - this.paintArea / 2;
let top = centerY - this.paintArea / 2;
let leftChunk = Math.floor(left / wall.chunkSize);
let topChunk = Math.floor(top / wall.chunkSize);
let rightChunk = Math.ceil((left + this.paintArea) / wall.chunkSize);
let bottomChunk = Math.ceil((top + this.paintArea) / wall.chunkSize);
for (let chunkY = topChunk; chunkY < bottomChunk; ++chunkY) {
for (let chunkX = leftChunk; chunkX < rightChunk; ++chunkX) {
let x = Math.floor(-chunkX * wall.chunkSize + centerX);
let y = Math.floor(-chunkY * wall.chunkSize + centerY);
let chunk = wall.getOrCreateChunk(chunkX, chunkY);
chunk.markModified();
let renderResult = haku.renderValue(chunk.pixmap, x, y);
if (renderResult.status != "ok") {
return { status: "error", phase: "render", result: renderResult };
}
}
function* chunksInRectangle(left, top, right, bottom, chunkSize) {
let leftChunk = Math.floor(left / chunkSize);
let topChunk = Math.floor(top / chunkSize);
let rightChunk = Math.ceil(right / chunkSize);
let bottomChunk = Math.ceil(bottom / chunkSize);
for (let chunkY = topChunk; chunkY < bottomChunk; ++chunkY) {
for (let chunkX = leftChunk; chunkX < rightChunk; ++chunkX) {
yield [chunkX, chunkY];
}
for (let y = topChunk; y < bottomChunk; ++y) {
for (let x = leftChunk; x < rightChunk; ++x) {
let chunk = wall.getChunk(x, y);
chunk.syncFromPixmap();
}
}
return { status: "ok" };
}
}
export function renderToChunksInArea(wall, renderArea, renderToPixmap) {
for (let [chunkX, chunkY] of chunksInRectangle(
renderArea.left,
renderArea.top,
renderArea.right,
renderArea.bottom,
wall.chunkSize,
)) {
let chunk = wall.getOrCreateChunk(chunkX, chunkY);
let translationX = -chunkX * wall.chunkSize;
let translationY = -chunkY * wall.chunkSize;
let result = renderToPixmap(chunk.pixmap, translationX, translationY);
chunk.markModified();
if (result.status != "ok") return result;
}
return { status: "ok" };
}
export function dotterRenderArea(wall, dotter) {
let halfPaintArea = wall.paintArea / 2;
return {
left: dotter.toX - halfPaintArea,
top: dotter.toY - halfPaintArea,
right: dotter.toX + halfPaintArea,
bottom: dotter.toY + halfPaintArea,
};
}
export function selfController(interactionQueue, wall, event) {
let renderArea = null;
return {
async runScribble(renderToPixmap) {
interactionQueue.push({ kind: "scribble" });
if (renderArea != null) {
return renderToChunksInArea(wall, renderArea, renderToPixmap);
} else {
console.debug("render area is empty, nothing will be rendered");
}
return { status: "ok" };
},
async runDotter() {
let dotter = await event.continueAsDotter();
interactionQueue.push({
kind: "dotter",
from: { x: dotter.fromX, y: dotter.fromY },
to: { x: dotter.toX, y: dotter.toY },
num: dotter.num,
});
renderArea = dotterRenderArea(wall, dotter);
return dotter;
},
};
}