From 246e56187c40ca6b24c4fbbb94c7e814272c2299 Mon Sep 17 00:00:00 2001 From: "Eevee (Evelyn Woods)" Date: Mon, 6 May 2024 23:12:35 -0600 Subject: [PATCH] Fix the editor to also crop the selection --- js/editor/dialogs.js | 2 +- js/editor/main.js | 32 +++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/js/editor/dialogs.js b/js/editor/dialogs.js index 336eaae..5a24c1d 100644 --- a/js/editor/dialogs.js +++ b/js/editor/dialogs.js @@ -197,7 +197,7 @@ export class EditorLevelMetaOverlay extends DialogOverlay { let size_x = Math.max(1, Math.min(100, parseInt(els.size_x.value, 10))); let size_y = Math.max(1, Math.min(100, parseInt(els.size_y.value, 10))); if (size_x !== stored_level.size_x || size_y !== stored_level.size_y) { - this.conductor.editor.resize_level(size_x, size_y); + this.conductor.editor.crop_level(0, 0, size_x, size_y); } stored_level.blob_behavior = parseInt(els.blob_behavior.value, 10); diff --git a/js/editor/main.js b/js/editor/main.js index 0337bd0..2e1d321 100644 --- a/js/editor/main.js +++ b/js/editor/main.js @@ -1632,7 +1632,26 @@ export class Editor extends PrimaryView { ); } - resize_level(size_x, size_y, x0 = 0, y0 = 0) { + crop_level(x0, y0, size_x, size_y) { + let original_cells = this.stored_level.linear_cells; + let original_size_x = this.stored_level.size_x; + let original_size_y = this.stored_level.size_y; + + let old_selection_cells, new_selection_cells; + if (this.selection) { + this.selection.commit_floating(); + + old_selection_cells = this.selection.cells; + new_selection_cells = new Set; + for (let n of old_selection_cells) { + let x = n % original_size_x - x0; + let y = Math.floor(n / original_size_x) - y0; + if (0 <= x && x < size_x && 0 <= y && y < size_y) { + new_selection_cells.add(x + y * size_x); + } + } + } + let new_cells = []; for (let y = y0; y < y0 + size_y; y++) { for (let x = x0; x < x0 + size_x; x++) { @@ -1640,21 +1659,24 @@ export class Editor extends PrimaryView { } } - let original_cells = this.stored_level.linear_cells; - let original_size_x = this.stored_level.size_x; - let original_size_y = this.stored_level.size_y; - this._do(() => { this.stored_level.linear_cells = new_cells; this.stored_level.size_x = size_x; this.stored_level.size_y = size_y; this.update_after_size_change(); + if (this.selection) { + this.selection._set_from_set(new_selection_cells); + } }, () => { this.stored_level.linear_cells = original_cells; this.stored_level.size_x = original_size_x; this.stored_level.size_y = original_size_y; this.update_after_size_change(); + if (this.selection) { + this.selection._set_from_set(old_selection_cells); + } }); + this.commit_undo(); }