From 5f79f155548adc24a946d2684ef2ae774261b3e5 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Fri, 17 Dec 2021 20:47:12 -0500 Subject: [PATCH] Add onBlockHoverChanged and onBlockHoverCleared --- CHANGELOG.md | 2 +- docsrc/manual/scripting-capabilities.rst | 11 +++++++++++ include/scripting.h | 4 ++++ src/editor.cpp | 4 ++++ src/scripting.cpp | 17 +++++++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54df5f70..fec79e61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ The **"Breaking Changes"** listed below are changes that have been made in the d - Events, current metatile selections, and map images can now be copied and pasted, including between windows. - The grid and map border visibility are now saved as config options. - Add ~60 new API functions, including new features like reading/writing metatile data, layering, moving, and hiding items in the overlay, creating modified images and tile/metatile images, reading tileset sizes, logging warnings and errors, and more. -- Add 5 new scripting callbacks. +- Add 7 new scripting callbacks. ### Changed - New events will be placed in the center of the current view of the map. diff --git a/docsrc/manual/scripting-capabilities.rst b/docsrc/manual/scripting-capabilities.rst index ed49f2e5..f290ab6b 100644 --- a/docsrc/manual/scripting-capabilities.rst +++ b/docsrc/manual/scripting-capabilities.rst @@ -118,6 +118,17 @@ Callbacks :param object prevBlock: the block's state before it was modified. The object's shape is ``{metatileId, collision, elevation, rawValue}`` :param object newBlock: the block's new state after it was modified. The object's shape is ``{metatileId, collision, elevation, rawValue}`` +.. js:function:: onBlockHoverChanged(x, y) + + Called when the mouse enters a new map block. + + :param number x: x coordinate of the block + :param number y: y coordinate of the block + +.. js:function:: onBlockHoverCleared() + + Called when the mouse exits the map. + .. js:function:: onMapResized(oldWidth, oldHeight, newWidth, newHeight) Called when the dimensions of the map are changed. diff --git a/include/scripting.h b/include/scripting.h index 08a5aa5d..176f69e9 100644 --- a/include/scripting.h +++ b/include/scripting.h @@ -12,6 +12,8 @@ enum CallbackType { OnProjectOpened, OnProjectClosed, OnBlockChanged, + OnBlockHoverChanged, + OnBlockHoverCleared, OnMapOpened, OnMapResized, OnMapShifted, @@ -38,6 +40,8 @@ public: static void cb_ProjectOpened(QString projectPath); static void cb_ProjectClosed(QString projectPath); static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock); + static void cb_BlockHoverChanged(int x, int y); + static void cb_BlockHoverCleared(); static void cb_MapOpened(QString mapName); static void cb_MapResized(int oldWidth, int oldHeight, int newWidth, int newHeight); static void cb_MapShifted(int xDelta, int yDelta); diff --git a/src/editor.cpp b/src/editor.cpp index 54bb3c0a..4f96bdf9 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -10,6 +10,7 @@ #include "montabwidget.h" #include "editcommands.h" #include "config.h" +#include "scripting.h" #include #include #include @@ -992,6 +993,7 @@ void Editor::updateCursorRectPos(int x, int y) { this->cursorMapTileRect->updateLocation(x, y); if (ui->graphicsView_Map->scene()) ui->graphicsView_Map->scene()->update(); + Scripting::cb_BlockHoverChanged(x, y); } void Editor::setCursorRectVisible(bool visible) { @@ -1029,6 +1031,7 @@ void Editor::onHoveredMapMetatileCleared() { || map_item->paintingMode == MapPixmapItem::PaintMode::EventObjects) { this->ui->statusBar->clearMessage(); } + Scripting::cb_BlockHoverCleared(); } void Editor::onHoveredMapMovementPermissionChanged(int x, int y) { @@ -1051,6 +1054,7 @@ void Editor::onHoveredMapMovementPermissionCleared() { if (map_item->paintingMode == MapPixmapItem::PaintMode::Metatiles) { this->ui->statusBar->clearMessage(); } + Scripting::cb_BlockHoverCleared(); } QString Editor::getMovementPermissionText(uint16_t collision, uint16_t elevation) { diff --git a/src/scripting.cpp b/src/scripting.cpp index 06f22d8f..a5472846 100644 --- a/src/scripting.cpp +++ b/src/scripting.cpp @@ -5,6 +5,8 @@ QMap callbackFunctions = { {OnProjectOpened, "onProjectOpened"}, {OnProjectClosed, "onProjectClosed"}, {OnBlockChanged, "onBlockChanged"}, + {OnBlockHoverChanged, "onBlockHoverChanged"}, + {OnBlockHoverCleared, "onBlockHoverCleared"}, {OnMapOpened, "onMapOpened"}, {OnMapResized, "onMapResized"}, {OnMapShifted, "onMapShifted"}, @@ -136,6 +138,21 @@ void Scripting::cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock instance->invokeCallback(OnBlockChanged, args); } +void Scripting::cb_BlockHoverChanged(int x, int y) { + if (!instance) return; + + QJSValueList args { + x, + y, + }; + instance->invokeCallback(OnBlockHoverChanged, args); +} + +void Scripting::cb_BlockHoverCleared() { + if (!instance) return; + instance->invokeCallback(OnBlockHoverCleared, QJSValueList()); +} + void Scripting::cb_MapOpened(QString mapName) { if (!instance) return;