Merge pull request #2290 from myk002/myk_mouse_pos

Make screen elements clickable when using TWBT
develop
Myk 2022-09-19 11:24:38 -07:00 committed by GitHub
commit e2a8e0bffb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 65 additions and 59 deletions

@ -1088,6 +1088,11 @@ Announcements
Uses the type to look up options from announcements.txt, and calls the above Uses the type to look up options from announcements.txt, and calls the above
operations accordingly. The units are used to call ``addCombatReportAuto``. operations accordingly. The units are used to call ``addCombatReportAuto``.
* ``dfhack.gui.getMousePos()``
Returns the map coordinates of the map tile the mouse is over as a table of
``{x, y, z}``. If the cursor is not over the map, returns ``nil``.
Other Other
~~~~~ ~~~~~

@ -1523,6 +1523,16 @@ static int gui_getDwarfmodeViewDims(lua_State *state)
return 1; return 1;
} }
static int gui_getMousePos(lua_State *L)
{
auto pos = Gui::getMousePos();
if (pos.isValid())
Lua::Push(L, pos);
else
lua_pushnil(L);
return 1;
}
static const LuaWrapper::FunctionReg dfhack_gui_module[] = { static const LuaWrapper::FunctionReg dfhack_gui_module[] = {
WRAPM(Gui, getCurViewscreen), WRAPM(Gui, getCurViewscreen),
WRAPM(Gui, getFocusString), WRAPM(Gui, getFocusString),
@ -1555,6 +1565,7 @@ static const LuaWrapper::FunctionReg dfhack_gui_module[] = {
static const luaL_Reg dfhack_gui_funcs[] = { static const luaL_Reg dfhack_gui_funcs[] = {
{ "getDwarfmodeViewDims", gui_getDwarfmodeViewDims }, { "getDwarfmodeViewDims", gui_getDwarfmodeViewDims },
{ "getMousePos", gui_getMousePos },
{ NULL, NULL } { NULL, NULL }
}; };
@ -2282,18 +2293,12 @@ static const LuaWrapper::FunctionReg dfhack_screen_module[] = {
static int screen_getMousePos(lua_State *L) static int screen_getMousePos(lua_State *L)
{ {
auto pos = Screen::getMousePos(); return Lua::PushPosXY(L, Screen::getMousePos());
lua_pushinteger(L, pos.x);
lua_pushinteger(L, pos.y);
return 2;
} }
static int screen_getWindowSize(lua_State *L) static int screen_getWindowSize(lua_State *L)
{ {
auto pos = Screen::getWindowSize(); return Lua::PushPosXY(L, Screen::getWindowSize());
lua_pushinteger(L, pos.x);
lua_pushinteger(L, pos.y);
return 2;
} }
static int screen_paintTile(lua_State *L) static int screen_paintTile(lua_State *L)

@ -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); 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 getViewportPos();
DFHACK_EXPORT df::coord getCursorPos(); DFHACK_EXPORT df::coord getCursorPos();
DFHACK_EXPORT df::coord getMousePos();
static const int AREA_MAP_WIDTH = 23; static const int AREA_MAP_WIDTH = 23;
static const int MENU_WIDTH = 30; 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 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 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 // 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 // Defaults to 0, unless overriden by plugins
DFHACK_EXPORT int getDepthAt (int32_t x, int32_t y); DFHACK_EXPORT int getDepthAt (int32_t x, int32_t y);

@ -104,7 +104,7 @@ function getCursorPos()
end end
function setCursorPos(cursor) function setCursorPos(cursor)
df.global.cursor = cursor df.global.cursor = copyall(cursor)
end end
function clearCursorPos() function clearCursorPos()

@ -1831,17 +1831,22 @@ bool Gui::setDesignationCoords (const int32_t x, const int32_t y, const int32_t
return true; 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; df::coord pos;
y = gps->mouse_y; if (gps && gps->mouse_x > -1) {
} // return invalid coords if the cursor is not over the map
else { DwarfmodeDims dims = getDwarfmodeViewDims();
x = -1; if (gps->mouse_x < dims.map_x1 || gps->mouse_x > dims.map_x2 ||
y = -1; gps->mouse_y < dims.map_y1 || gps->mouse_y > dims.map_y2) {
return pos;
}
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) int getDepthAt_default (int32_t x, int32_t y)

@ -31,6 +31,7 @@ distribution.
#include <set> #include <set>
using namespace std; using namespace std;
#include "modules/Renderer.h"
#include "modules/Screen.h" #include "modules/Screen.h"
#include "modules/GuiHooks.h" #include "modules/GuiHooks.h"
#include "MemAccess.h" #include "MemAccess.h"
@ -75,12 +76,14 @@ using std::string;
* Screen painting API. * Screen painting API.
*/ */
// returns text grid coordinates, even if the game map is scaled differently
df::coord2d Screen::getMousePos() df::coord2d Screen::getMousePos()
{ {
if (!gps || (enabler && !enabler->tracking_on)) int32_t x = Renderer::GET_MOUSE_COORDS_SENTINEL, y = (int32_t)true;
if (!enabler || !enabler->renderer->get_mouse_coords(&x, &y)) {
return df::coord2d(-1, -1); return df::coord2d(-1, -1);
}
return df::coord2d(gps->mouse_x, gps->mouse_y); return df::coord2d(x, y);
} }
df::coord2d Screen::getWindowSize() df::coord2d Screen::getWindowSize()
@ -769,7 +772,7 @@ int dfhack_lua_viewscreen::do_input(lua_State *L)
} }
} }
if (enabler && enabler->tracking_on) if (enabler)
{ {
if (enabler->mouse_lbut_down) { if (enabler->mouse_lbut_down) {
lua_pushboolean(L, true); lua_pushboolean(L, true);

@ -135,13 +135,12 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
last_designation[2] = desig_z; last_designation[2] = desig_z;
out.print("Designation: %d %d %d\n",desig_x, desig_y, desig_z); out.print("Designation: %d %d %d\n",desig_x, desig_y, desig_z);
} }
int mouse_x, mouse_y; df::coord mousePos = Gui::getMousePos();
Gui::getMousePos(mouse_x,mouse_y); if(mousePos.x != last_mouse[0] || mousePos.y != last_mouse[1])
if(mouse_x != last_mouse[0] || mouse_y != last_mouse[1])
{ {
last_mouse[0] = mouse_x; last_mouse[0] = mousePos.x;
last_mouse[1] = mouse_y; last_mouse[1] = mousePos.y;
out.print("Mouse: %d %d\n",mouse_x, mouse_y); out.print("Mouse: %d %d\n",mousePos.x, mousePos.y);
} }
} }
return CR_OK; return CR_OK;

@ -20,6 +20,7 @@
#include "uicommon.h" #include "uicommon.h"
#include "TileTypes.h" #include "TileTypes.h"
#include "DataFuncs.h" #include "DataFuncs.h"
#include "Debug.h"
DFHACK_PLUGIN("mousequery"); DFHACK_PLUGIN("mousequery");
REQUIRE_GLOBAL(enabler); REQUIRE_GLOBAL(enabler);
@ -32,6 +33,10 @@ using namespace df::enums::ui_sidebar_mode;
#define PLUGIN_VERSION 0.18 #define PLUGIN_VERSION 0.18
namespace DFHack {
DBG_DECLARE(mousequery,log,DebugCategory::LINFO);
}
static int32_t last_clicked_x, last_clicked_y, last_clicked_z; static int32_t last_clicked_x, last_clicked_y, last_clicked_z;
static int32_t last_pos_x, last_pos_y, last_pos_z; static int32_t last_pos_x, last_pos_y, last_pos_z;
static df::coord last_move_pos; static df::coord last_move_pos;
@ -53,22 +58,12 @@ static enum { None, Left, Right } drag_mode;
static df::coord get_mouse_pos(int32_t &mx, int32_t &my) static df::coord get_mouse_pos(int32_t &mx, int32_t &my)
{ {
df::coord pos; df::coord pos = Gui::getMousePos();
pos.x = -30000; pos.z -= Gui::getDepthAt(pos.x, pos.y);
if (!enabler->tracking_on)
return pos;
if (!Gui::getMousePos(mx, my)) df::coord vpos = Gui::getViewportPos();
return pos; mx = pos.x - vpos.x + 1;
my = pos.y - vpos.y + 1;
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);
return pos; return pos;
} }
@ -536,6 +531,7 @@ struct mousequery_hook : public df::viewscreen_dwarfmodest
if (mpos.x == x && mpos.y == y && mpos.z == z) if (mpos.x == x && mpos.y == y && mpos.z == z)
return; return;
DEBUG(log).print("moving cursor to %d, %d, %d\n", x, y, z);
Gui::setCursorCoords(mpos.x, mpos.y, mpos.z); Gui::setCursorCoords(mpos.x, mpos.y, mpos.z);
Gui::refreshSidebar(); Gui::refreshSidebar();
} }
@ -575,8 +571,6 @@ struct mousequery_hook : public df::viewscreen_dwarfmodest
int32_t mx, my; int32_t mx, my;
auto mpos = get_mouse_pos(mx, my); auto mpos = get_mouse_pos(mx, my);
bool mpos_valid = mpos.x != -30000 && mpos.y != -30000 && mpos.z != -30000; bool mpos_valid = mpos.x != -30000 && mpos.y != -30000 && mpos.z != -30000;
if (mx < 1 || mx > dims.map_x2 || my < 1 || my > dims.map_y2)
mpos_valid = false;
// Check if in lever binding mode // Check if in lever binding mode
if (Gui::getFocusString(Core::getTopViewscreen()) == if (Gui::getFocusString(Core::getTopViewscreen()) ==
@ -723,7 +717,7 @@ struct mousequery_hook : public df::viewscreen_dwarfmodest
} }
} }
OutputString(color, mx, my, "X", false, 0, 0, true); Screen::paintTile(Screen::Pen('X', color), mx, my, true);
return; return;
} }

@ -87,7 +87,7 @@
#include "VTableInterpose.h" #include "VTableInterpose.h"
#include "uicommon.h" #include "uicommon.h"
#include "modules/Renderer.h" #include "modules/Screen.h"
using namespace DFHack; using namespace DFHack;
@ -104,16 +104,12 @@ static const std::string button_text = "[ DFHack Launcher ]";
static bool clicked = false; static bool clicked = false;
static bool handle_click() { static bool handle_click() {
int32_t x = Renderer::GET_MOUSE_COORDS_SENTINEL, y = (int32_t)true; if (!enabler->mouse_lbut_down || clicked) {
if (!enabler->mouse_lbut_down || clicked ||
!enabler->renderer->get_mouse_coords(&x, &y)) {
DEBUG(log).print(
"lbut_down=%s; clicked=%s; mouse_x=%d; mouse_y=%d\n",
enabler->mouse_lbut_down ? "true" : "false",
clicked ? "true" : "false", x, y);
return false; return false;
} }
if (y == gps->dimy - 1 && x >= 1 && (size_t)x <= button_text.size()) { df::coord2d pos = Screen::getMousePos();
DEBUG(log).print("clicked at screen coordinates (%d, %d)\n", pos.x, pos.y);
if (pos.y == gps->dimy - 1 && pos.x >= 1 && (size_t)pos.x <= button_text.size()) {
clicked = true; clicked = true;
Core::getInstance().setHotkeyCmd("gui/launcher"); Core::getInstance().setHotkeyCmd("gui/launcher");
return true; return true;