From 215a5b0a24be00d91dcf9376680e6311cf3df574 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 18 Sep 2022 08:19:02 -0700 Subject: [PATCH] add Gui::getMousePos() that always gets map coords --- library/include/modules/Gui.h | 5 ++--- library/modules/Gui.cpp | 17 ++++++++--------- plugins/devel/kittens.cpp | 11 +++++------ plugins/mousequery.cpp | 20 ++++---------------- 4 files changed, 19 insertions(+), 34 deletions(-) diff --git a/library/include/modules/Gui.h b/library/include/modules/Gui.h index 455032fea..a0ae27889 100644 --- a/library/include/modules/Gui.h +++ b/library/include/modules/Gui.h @@ -126,10 +126,11 @@ namespace DFHack DFHACK_EXPORT void showAutoAnnouncement(df::announcement_type type, df::coord pos, std::string message, int color = 7, bool bright = true, df::unit *unit1 = NULL, df::unit *unit2 = NULL); /* - * Cursor and window coords + * Cursor and window map coords */ DFHACK_EXPORT df::coord getViewportPos(); DFHACK_EXPORT df::coord getCursorPos(); + DFHACK_EXPORT df::coord getMousePos(); static const int AREA_MAP_WIDTH = 23; static const int MENU_WIDTH = 30; @@ -162,8 +163,6 @@ namespace DFHack DFHACK_EXPORT bool getDesignationCoords (int32_t &x, int32_t &y, int32_t &z); DFHACK_EXPORT bool setDesignationCoords (const int32_t x, const int32_t y, const int32_t z); - DFHACK_EXPORT bool getMousePos (int32_t & x, int32_t & y); - // The distance from the z-level of the tile at map coordinates (x, y) to the closest ground z-level below // Defaults to 0, unless overriden by plugins DFHACK_EXPORT int getDepthAt (int32_t x, int32_t y); diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index fc78bb57d..f9e15aac4 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -1831,17 +1831,16 @@ bool Gui::setDesignationCoords (const int32_t x, const int32_t y, const int32_t return true; } -bool Gui::getMousePos (int32_t & x, int32_t & y) +// returns the map coordinates that the mouse cursor is over +df::coord Gui::getMousePos() { - if (gps) { - x = gps->mouse_x; - y = gps->mouse_y; - } - else { - x = -1; - y = -1; + df::coord pos; + if (gps && gps->mouse_x > -1) { + pos = getViewportPos(); + pos.x += gps->mouse_x - 1; + pos.y += gps->mouse_y - 1; } - return (x == -1) ? false : true; + return pos; } int getDepthAt_default (int32_t x, int32_t y) diff --git a/plugins/devel/kittens.cpp b/plugins/devel/kittens.cpp index fce924aff..56ca813ae 100644 --- a/plugins/devel/kittens.cpp +++ b/plugins/devel/kittens.cpp @@ -135,13 +135,12 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) last_designation[2] = desig_z; out.print("Designation: %d %d %d\n",desig_x, desig_y, desig_z); } - int mouse_x, mouse_y; - Gui::getMousePos(mouse_x,mouse_y); - if(mouse_x != last_mouse[0] || mouse_y != last_mouse[1]) + df:coord mousePos = Gui::getMousePos(); + if(mousePos.x != last_mouse[0] || mousePos.y != last_mouse[1]) { - last_mouse[0] = mouse_x; - last_mouse[1] = mouse_y; - out.print("Mouse: %d %d\n",mouse_x, mouse_y); + last_mouse[0] = mousePos.x; + last_mouse[1] = mousePos.y; + out.print("Mouse: %d %d\n",mousePos.x, mousePos.y); } } return CR_OK; diff --git a/plugins/mousequery.cpp b/plugins/mousequery.cpp index cd85de2bc..2bfa08470 100644 --- a/plugins/mousequery.cpp +++ b/plugins/mousequery.cpp @@ -53,23 +53,11 @@ static enum { None, Left, Right } drag_mode; static df::coord get_mouse_pos(int32_t &mx, int32_t &my) { - df::coord pos; - pos.x = -30000; - - if (!enabler->tracking_on) - return pos; - - if (!Gui::getMousePos(mx, my)) - return pos; - - int32_t vx, vy, vz; - if (!Gui::getViewCoords(vx, vy, vz)) - return pos; - - pos.x = vx + mx - 1; - pos.y = vy + my - 1; - pos.z = vz - Gui::getDepthAt(mx, my); + df::coord pos = Gui::getMousePos(); + pos.z -= Gui::getDepthAt(pos.x, pos.y); + mx = pos.x; + my = pos.y; return pos; }