From d97fd5b2a6a96bf581668cf201736562ec067657 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 23 Apr 2025 21:54:42 -0400 Subject: [PATCH] Update onMapResized --- docsrc/manual/scripting-capabilities.rst | 8 +++--- include/core/maplayout.h | 4 +-- include/scripting.h | 5 +++- resources/text/script_template.txt | 2 +- src/core/maplayout.cpp | 34 +++++++++--------------- src/scriptapi/scripting.cpp | 14 +++++++--- 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/docsrc/manual/scripting-capabilities.rst b/docsrc/manual/scripting-capabilities.rst index ac1cf521..0651f7d9 100644 --- a/docsrc/manual/scripting-capabilities.rst +++ b/docsrc/manual/scripting-capabilities.rst @@ -204,7 +204,7 @@ Callbacks Called when the mouse exits the map. -.. js:function:: onMapResized(oldWidth, oldHeight, newWidth, newHeight) +.. js:function:: onMapResized(oldWidth, oldHeight, delta) Called when the dimensions of the map are changed. @@ -212,10 +212,8 @@ Callbacks :type oldWidth: number :param oldHeight: the height of the map before the change :type oldHeight: number - :param newWidth: the width of the map after the change - :type newWidth: number - :param newHeight: the height of the map after the change - :type newHeight: number + :param delta: the amount the map size changed in each direction. The object's shape is ``{left, right, top, bottom}`` + :type prevBlock: delta .. js:function:: onBorderResized(oldWidth, oldHeight, newWidth, newHeight) diff --git a/include/core/maplayout.h b/include/core/maplayout.h index 6e82604a..3e67af18 100644 --- a/include/core/maplayout.h +++ b/include/core/maplayout.h @@ -107,8 +107,8 @@ public: void setBlock(int x, int y, Block block, bool enableScriptCallback = false); void setBlockdata(Blockdata blockdata, bool enableScriptCallback = false); - void adjustDimensions(QMargins margins, bool setNewBlockdata = true); - void setDimensions(int newWidth, int newHeight, bool setNewBlockdata = true, bool enableScriptCallback = false); + void adjustDimensions(const QMargins &margins, bool setNewBlockdata = true); + void setDimensions(int newWidth, int newHeight, bool setNewBlockdata = true); void setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata = true, bool enableScriptCallback = false); void cacheBlockdata(); diff --git a/include/scripting.h b/include/scripting.h index 6870f159..b85e9e26 100644 --- a/include/scripting.h +++ b/include/scripting.h @@ -39,6 +39,7 @@ public: static void populateGlobalObject(MainWindow *mainWindow); static QJSEngine *getEngine(); static void invokeAction(int actionIndex); + static void cb_ProjectOpened(QString projectPath); static void cb_ProjectClosed(QString projectPath); static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock); @@ -47,18 +48,20 @@ public: static void cb_BlockHoverCleared(); static void cb_MapOpened(QString mapName); static void cb_LayoutOpened(QString layoutName); - static void cb_MapResized(int oldWidth, int oldHeight, int newWidth, int newHeight); + static void cb_MapResized(int oldWidth, int oldHeight, const QMargins &delta); static void cb_BorderResized(int oldWidth, int oldHeight, int newWidth, int newHeight); static void cb_MapShifted(int xDelta, int yDelta); static void cb_TilesetUpdated(QString tilesetName); static void cb_MainTabChanged(int oldTab, int newTab); static void cb_MapViewTabChanged(int oldTab, int newTab); static void cb_BorderVisibilityToggled(bool visible); + static bool tryErrorJS(QJSValue js); static QJSValue fromBlock(Block block); static QJSValue fromTile(Tile tile); static Tile toTile(QJSValue obj); static QJSValue dimensions(int width, int height); + static QJSValue margins(const QMargins &margins); static QJSValue position(int x, int y); static const QImage * getImage(const QString &filepath, bool useCache); static QJSValue dialogInput(QJSValue input, bool selectedOk); diff --git a/resources/text/script_template.txt b/resources/text/script_template.txt index 4b5134d1..bee6e56e 100644 --- a/resources/text/script_template.txt +++ b/resources/text/script_template.txt @@ -39,7 +39,7 @@ export function onBlockHoverCleared() { } // Called when the dimensions of the map are changed. -export function onMapResized(oldWidth, oldHeight, newWidth, newHeight) { +export function onMapResized(oldWidth, oldHeight, delta) { } diff --git a/src/core/maplayout.cpp b/src/core/maplayout.cpp index fb0f714c..c3e91ba3 100644 --- a/src/core/maplayout.cpp +++ b/src/core/maplayout.cpp @@ -189,46 +189,38 @@ void Layout::setBorderBlockData(Blockdata newBlockdata, bool enableScriptCallbac } } -void Layout::setDimensions(int newWidth, int newHeight, bool setNewBlockdata, bool enableScriptCallback) { +void Layout::setDimensions(int newWidth, int newHeight, bool setNewBlockdata) { if (setNewBlockdata) { setNewDimensionsBlockdata(newWidth, newHeight); } - - int oldWidth = this->width; - int oldHeight = this->height; this->width = newWidth; this->height = newHeight; - - if (enableScriptCallback && (oldWidth != newWidth || oldHeight != newHeight)) { - Scripting::cb_MapResized(oldWidth, oldHeight, newWidth, newHeight); - } - - emit dimensionsChanged(QSize(getWidth(), getHeight())); + emit dimensionsChanged(QSize(this->width, this->height)); } -void Layout::adjustDimensions(QMargins margins, bool setNewBlockdata) { - int newWidth = this->width + margins.left() + margins.right(); - int newHeight = this->height + margins.top() + margins.bottom(); +void Layout::adjustDimensions(const QMargins &margins, bool setNewBlockdata) { + int oldWidth = this->width; + int oldHeight = this->height; + this->width = oldWidth + margins.left() + margins.right(); + this->height = oldHeight + margins.top() + margins.bottom(); if (setNewBlockdata) { // Fill new blockdata Blockdata newBlockdata; - for (int y = 0; y < newHeight; y++) - for (int x = 0; x < newWidth; x++) { - if ((x < margins.left()) || (x >= newWidth - margins.right()) || (y < margins.top()) || (y >= newHeight - margins.bottom())) { + for (int y = 0; y < this->height; y++) + for (int x = 0; x < this->width; x++) { + if ((x < margins.left()) || (x >= this->width - margins.right()) || (y < margins.top()) || (y >= this->height - margins.bottom())) { newBlockdata.append(0); } else { - int index = (y - margins.top()) * this->width + (x - margins.left()); + int index = (y - margins.top()) * oldWidth + (x - margins.left()); newBlockdata.append(this->blockdata.value(index)); } } this->blockdata = newBlockdata; } - this->width = newWidth; - this->height = newHeight; - - emit dimensionsChanged(QSize(getWidth(), getHeight())); + Scripting::cb_MapResized(oldWidth, oldHeight, margins); + emit dimensionsChanged(QSize(this->width, this->height)); } void Layout::setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata, bool enableScriptCallback) { diff --git a/src/scriptapi/scripting.cpp b/src/scriptapi/scripting.cpp index 05fd31d9..93823de2 100644 --- a/src/scriptapi/scripting.cpp +++ b/src/scriptapi/scripting.cpp @@ -268,14 +268,13 @@ void Scripting::cb_LayoutOpened(QString layoutName) { instance->invokeCallback(OnLayoutOpened, args); } -void Scripting::cb_MapResized(int oldWidth, int oldHeight, int newWidth, int newHeight) { +void Scripting::cb_MapResized(int oldWidth, int oldHeight, const QMargins &delta) { if (!instance) return; QJSValueList args { oldWidth, oldHeight, - newWidth, - newHeight, + Scripting::margins(delta), }; instance->invokeCallback(OnMapResized, args); } @@ -356,6 +355,15 @@ QJSValue Scripting::dimensions(int width, int height) { return obj; } +QJSValue Scripting::margins(const QMargins &margins) { + QJSValue obj = instance->engine->newObject(); + obj.setProperty("left", margins.left()); + obj.setProperty("right", margins.right()); + obj.setProperty("top", margins.top()); + obj.setProperty("bottom", margins.bottom()); + return obj; +} + QJSValue Scripting::position(int x, int y) { QJSValue obj = instance->engine->newObject(); obj.setProperty("x", x);