Update worldEdit.js
This commit is contained in:
parent
2afcb6d97c
commit
5c38ac7c38
|
|
@ -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,47 +38,60 @@ 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}) {
|
||||
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;
|
||||
|
|
@ -86,6 +100,7 @@ function mousePosToWorldPos(pos) {
|
|||
y = Math.floor((y / canvas.clientHeight) * (height + 1));
|
||||
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 () {
|
||||
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue