Update worldEdit.js
This commit is contained in:
parent
2afcb6d97c
commit
5c38ac7c38
|
|
@ -15,7 +15,7 @@ const w_style = {
|
|||
let worldEditElements = {};
|
||||
let pastePreviewCanvas;
|
||||
let w_state = {
|
||||
firstSelectionPos: { x: 0, y: 0 },
|
||||
firstSelectionPos: {x: 0, y: 0},
|
||||
selection: null,
|
||||
clipboard: null,
|
||||
prevNonWorldEditElement: "unknown"
|
||||
|
|
@ -29,6 +29,7 @@ dependOn("betterSettings.js", () => {
|
|||
w_settingsTab.registerSettings("Selection", w_deselectOnResetSetting);
|
||||
settingsManager.registerTab(w_settingsTab);
|
||||
}, true);
|
||||
|
||||
// Classes
|
||||
class Rect {
|
||||
constructor(x, y, w, h) {
|
||||
|
|
@ -37,55 +38,69 @@ class Rect {
|
|||
this.w = w;
|
||||
this.h = h;
|
||||
}
|
||||
|
||||
static fromCorners(start, end) {
|
||||
return new Rect(start.x, start.y, end.x - start.x, end.y - start.y);
|
||||
}
|
||||
|
||||
static fromCornersXYXY(x, y, x2, y2) {
|
||||
return new Rect(x, y, x2 - x, y2 - y);
|
||||
}
|
||||
static fromGrid(grid, origin = { x: 0, y: 0 }) {
|
||||
|
||||
static fromGrid(grid, origin = {x: 0, y: 0}) {
|
||||
return new Rect(origin.x, origin.y, grid[0].length, grid.length);
|
||||
}
|
||||
|
||||
get area() {
|
||||
return this.w * this.h;
|
||||
}
|
||||
|
||||
get x2() {
|
||||
return this.x + this.w;
|
||||
}
|
||||
|
||||
get y2() {
|
||||
return this.y + this.h;
|
||||
}
|
||||
|
||||
set x2(val) {
|
||||
this.w = val - this.x;
|
||||
}
|
||||
|
||||
set y2(val) {
|
||||
this.h = val - this.y;
|
||||
}
|
||||
|
||||
copy() {
|
||||
return new Rect(this.x, this.y, this.w, this.h);
|
||||
}
|
||||
|
||||
normalized() {
|
||||
return Rect.fromCornersXYXY(Math.min(this.x, this.x2), Math.min(this.y, this.y2), Math.max(this.x, this.x2), Math.max(this.y, this.y2));
|
||||
}
|
||||
}
|
||||
|
||||
// Functions
|
||||
function isPointInWorld(point) {
|
||||
return point.x >= 0 && point.x <= width && point.y >= 0 && point.y <= height;
|
||||
}
|
||||
|
||||
function limitPointToWorld(point) {
|
||||
return {
|
||||
x: Math.max(0, Math.min(point.x, width)),
|
||||
y: Math.max(0, Math.min(point.y, height))
|
||||
};
|
||||
}
|
||||
|
||||
function mousePosToWorldPos(pos) {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
let x = pos.x - rect.left;
|
||||
let y = pos.y - rect.top;
|
||||
x = Math.floor((x / canvas.clientWidth) * (width + 1));
|
||||
y = Math.floor((y / canvas.clientHeight) * (height + 1));
|
||||
return { x: x, y: y };
|
||||
return {x: x, y: y};
|
||||
}
|
||||
|
||||
function updatePastePreviewCanvas() {
|
||||
const clipboard = w_state.clipboard;
|
||||
if (!clipboard)
|
||||
|
|
@ -105,6 +120,7 @@ function updatePastePreviewCanvas() {
|
|||
}
|
||||
pastePreviewCtx.putImageData(imageData, 0, 0);
|
||||
}
|
||||
|
||||
function renderSelection(ctx) {
|
||||
const selection = w_state.selection;
|
||||
if (!selection)
|
||||
|
|
@ -127,6 +143,7 @@ function renderSelection(ctx) {
|
|||
ctx.strokeRect(selection.x * pixelSize, selection.y * pixelSize, selection.w * pixelSize, selection.h * pixelSize);
|
||||
ctx.setLineDash([]);
|
||||
}
|
||||
|
||||
function renderPastePreview(ctx) {
|
||||
if (currentElement !== 'w_paste')
|
||||
return;
|
||||
|
|
@ -145,6 +162,7 @@ function renderPastePreview(ctx) {
|
|||
if (pastePreviewCanvas)
|
||||
ctx.drawImage(pastePreviewCanvas, mousePos.x * pixelSize, mousePos.y * pixelSize, clipboardRect.w * pixelSize, clipboardRect.h * pixelSize);
|
||||
}
|
||||
|
||||
function addWorldEditKeybinds() {
|
||||
keybinds.w = () => {
|
||||
selectCategory("worldEdit");
|
||||
|
|
@ -176,6 +194,7 @@ function addWorldEditKeybinds() {
|
|||
elements.w_fill.rawOnSelect();
|
||||
};
|
||||
}
|
||||
|
||||
function modifySelectElement() {
|
||||
const originalSelectElement = selectElement;
|
||||
// @ts-ignore
|
||||
|
|
@ -186,6 +205,7 @@ function modifySelectElement() {
|
|||
originalSelectElement(element);
|
||||
};
|
||||
}
|
||||
|
||||
function addWorldEditElements(elementsToAdd) {
|
||||
for (const elementName in elementsToAdd) {
|
||||
const element = elementsToAdd[elementName];
|
||||
|
|
@ -206,6 +226,7 @@ function addWorldEditElements(elementsToAdd) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Elements
|
||||
worldEditElements.w_deselect = {
|
||||
onSelect: function () {
|
||||
|
|
@ -222,7 +243,7 @@ worldEditElements.w_select_all = {
|
|||
};
|
||||
worldEditElements.w_select = {
|
||||
onPointerDown: function (e) {
|
||||
const pos = mousePosToWorldPos({ x: e.clientX, y: e.clientY });
|
||||
const pos = mousePosToWorldPos({x: e.clientX, y: e.clientY});
|
||||
if (showingMenu)
|
||||
return;
|
||||
if (!isPointInWorld(pos))
|
||||
|
|
@ -232,7 +253,7 @@ worldEditElements.w_select = {
|
|||
w_state.firstSelectionPos = pos;
|
||||
},
|
||||
onPointerMove: function (e) {
|
||||
const pos = mousePosToWorldPos({ x: e.clientX, y: e.clientY });
|
||||
const pos = mousePosToWorldPos({x: e.clientX, y: e.clientY});
|
||||
if (!mouseIsDown)
|
||||
return;
|
||||
if (showingMenu)
|
||||
|
|
@ -287,7 +308,7 @@ worldEditElements.w_paste = {
|
|||
for (let y = 0; y < clipboard.length; y++) {
|
||||
for (let x = 0; x < clipboard[0].length; x++) {
|
||||
const clipboardPixel = clipboard[y][x];
|
||||
const dest = { x: pasteOrigin.x + x, y: pasteOrigin.y + y };
|
||||
const dest = {x: pasteOrigin.x + x, y: pasteOrigin.y + y};
|
||||
if (!isPointInWorld(dest))
|
||||
continue; // Skip if out of bounds
|
||||
if (pixelMap[dest.x][dest.y])
|
||||
|
|
@ -372,8 +393,7 @@ worldEditElements.w_fill = {
|
|||
return;
|
||||
if (currentPixels.length > maxPixelCount || !fillElement) {
|
||||
currentPixels[currentPixels.length - 1].del = true;
|
||||
}
|
||||
else if (elements[fillElement] && elements[fillElement].onPlace !== undefined) {
|
||||
} else if (elements[fillElement] && elements[fillElement].onPlace !== undefined) {
|
||||
elements[fillElement].onPlace(currentPixels[currentPixels.length - 1]);
|
||||
}
|
||||
}
|
||||
|
|
@ -400,10 +420,10 @@ runAfterReset(() => {
|
|||
gameCanvas.addEventListener("pointerdown", (e) => {
|
||||
if (elements[currentElement] && elements[currentElement].onPointerDown)
|
||||
elements[currentElement].onPointerDown(e);
|
||||
}, { passive: false });
|
||||
}, {passive: false});
|
||||
gameCanvas.addEventListener("pointermove", (e) => {
|
||||
if (elements[currentElement] && elements[currentElement].onPointerMove)
|
||||
elements[currentElement].onPointerMove(e);
|
||||
}, { passive: false });
|
||||
}, {passive: false});
|
||||
addedCustomEventListeners = true;
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue