Update onMapResized

This commit is contained in:
GriffinR 2025-04-23 21:54:42 -04:00
parent fc0b1b1b58
commit d97fd5b2a6
6 changed files with 34 additions and 33 deletions

View File

@ -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)

View File

@ -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();

View File

@ -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);

View File

@ -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) {
}

View File

@ -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) {

View File

@ -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);