Update worldEdit.js

This commit is contained in:
redbirdly 2025-08-11 14:28:26 +08:00 committed by GitHub
parent 2afcb6d97c
commit 5c38ac7c38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 360 additions and 340 deletions

View File

@ -29,6 +29,7 @@ dependOn("betterSettings.js", () => {
w_settingsTab.registerSettings("Selection", w_deselectOnResetSetting); w_settingsTab.registerSettings("Selection", w_deselectOnResetSetting);
settingsManager.registerTab(w_settingsTab); settingsManager.registerTab(w_settingsTab);
}, true); }, true);
// Classes // Classes
class Rect { class Rect {
constructor(x, y, w, h) { constructor(x, y, w, h) {
@ -37,47 +38,60 @@ class Rect {
this.w = w; this.w = w;
this.h = h; this.h = h;
} }
static fromCorners(start, end) { static fromCorners(start, end) {
return new Rect(start.x, start.y, end.x - start.x, end.y - start.y); return new Rect(start.x, start.y, end.x - start.x, end.y - start.y);
} }
static fromCornersXYXY(x, y, x2, y2) { static fromCornersXYXY(x, y, x2, y2) {
return new Rect(x, y, x2 - x, y2 - y); 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); return new Rect(origin.x, origin.y, grid[0].length, grid.length);
} }
get area() { get area() {
return this.w * this.h; return this.w * this.h;
} }
get x2() { get x2() {
return this.x + this.w; return this.x + this.w;
} }
get y2() { get y2() {
return this.y + this.h; return this.y + this.h;
} }
set x2(val) { set x2(val) {
this.w = val - this.x; this.w = val - this.x;
} }
set y2(val) { set y2(val) {
this.h = val - this.y; this.h = val - this.y;
} }
copy() { copy() {
return new Rect(this.x, this.y, this.w, this.h); return new Rect(this.x, this.y, this.w, this.h);
} }
normalized() { 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)); 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 // Functions
function isPointInWorld(point) { function isPointInWorld(point) {
return point.x >= 0 && point.x <= width && point.y >= 0 && point.y <= height; return point.x >= 0 && point.x <= width && point.y >= 0 && point.y <= height;
} }
function limitPointToWorld(point) { function limitPointToWorld(point) {
return { return {
x: Math.max(0, Math.min(point.x, width)), x: Math.max(0, Math.min(point.x, width)),
y: Math.max(0, Math.min(point.y, height)) y: Math.max(0, Math.min(point.y, height))
}; };
} }
function mousePosToWorldPos(pos) { function mousePosToWorldPos(pos) {
const rect = canvas.getBoundingClientRect(); const rect = canvas.getBoundingClientRect();
let x = pos.x - rect.left; let x = pos.x - rect.left;
@ -86,6 +100,7 @@ function mousePosToWorldPos(pos) {
y = Math.floor((y / canvas.clientHeight) * (height + 1)); y = Math.floor((y / canvas.clientHeight) * (height + 1));
return {x: x, y: y}; return {x: x, y: y};
} }
function updatePastePreviewCanvas() { function updatePastePreviewCanvas() {
const clipboard = w_state.clipboard; const clipboard = w_state.clipboard;
if (!clipboard) if (!clipboard)
@ -105,6 +120,7 @@ function updatePastePreviewCanvas() {
} }
pastePreviewCtx.putImageData(imageData, 0, 0); pastePreviewCtx.putImageData(imageData, 0, 0);
} }
function renderSelection(ctx) { function renderSelection(ctx) {
const selection = w_state.selection; const selection = w_state.selection;
if (!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.strokeRect(selection.x * pixelSize, selection.y * pixelSize, selection.w * pixelSize, selection.h * pixelSize);
ctx.setLineDash([]); ctx.setLineDash([]);
} }
function renderPastePreview(ctx) { function renderPastePreview(ctx) {
if (currentElement !== 'w_paste') if (currentElement !== 'w_paste')
return; return;
@ -145,6 +162,7 @@ function renderPastePreview(ctx) {
if (pastePreviewCanvas) if (pastePreviewCanvas)
ctx.drawImage(pastePreviewCanvas, mousePos.x * pixelSize, mousePos.y * pixelSize, clipboardRect.w * pixelSize, clipboardRect.h * pixelSize); ctx.drawImage(pastePreviewCanvas, mousePos.x * pixelSize, mousePos.y * pixelSize, clipboardRect.w * pixelSize, clipboardRect.h * pixelSize);
} }
function addWorldEditKeybinds() { function addWorldEditKeybinds() {
keybinds.w = () => { keybinds.w = () => {
selectCategory("worldEdit"); selectCategory("worldEdit");
@ -176,6 +194,7 @@ function addWorldEditKeybinds() {
elements.w_fill.rawOnSelect(); elements.w_fill.rawOnSelect();
}; };
} }
function modifySelectElement() { function modifySelectElement() {
const originalSelectElement = selectElement; const originalSelectElement = selectElement;
// @ts-ignore // @ts-ignore
@ -186,6 +205,7 @@ function modifySelectElement() {
originalSelectElement(element); originalSelectElement(element);
}; };
} }
function addWorldEditElements(elementsToAdd) { function addWorldEditElements(elementsToAdd) {
for (const elementName in elementsToAdd) { for (const elementName in elementsToAdd) {
const element = elementsToAdd[elementName]; const element = elementsToAdd[elementName];
@ -206,6 +226,7 @@ function addWorldEditElements(elementsToAdd) {
} }
} }
} }
// Elements // Elements
worldEditElements.w_deselect = { worldEditElements.w_deselect = {
onSelect: function () { onSelect: function () {
@ -372,8 +393,7 @@ worldEditElements.w_fill = {
return; return;
if (currentPixels.length > maxPixelCount || !fillElement) { if (currentPixels.length > maxPixelCount || !fillElement) {
currentPixels[currentPixels.length - 1].del = true; 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]); elements[fillElement].onPlace(currentPixels[currentPixels.length - 1]);
} }
} }