diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 0fa95dd03..7c6e72f19 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1121,7 +1121,8 @@ Other * ``dfhack.gui.getDepthAt(x, y)`` Returns 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 overridden by plugins. + the closest rendered ground z-level below. Defaults to 0, unless overridden by + plugins. Job module ---------- @@ -2161,7 +2162,14 @@ Functions: * ``dfhack.screen.getMousePos()`` - Returns *x,y* of the tile the mouse is over. + Returns *x,y* of the UI interface tile the mouse is over, with the upper left + corner being ``0,0``. To get the map tile coordinate that the mouse is over, + see ``dfhack.gui.getMousePos()``. + +* ``dfhack.screen.getMousePixels()`` + + Returns *x,y* of the screen coordinates the mouse is over in pixels, with the + upper left corner being ``0,0``. * ``dfhack.screen.inGraphicsMode()`` @@ -3632,11 +3640,6 @@ considered stable. Misc ---- -* ``USE_GRAPHICS`` - - Contains the value of ``dfhack.screen.inGraphicsMode()``, which cannot be - changed without restarting the game and thus is constant during the session. - * ``CLEAR_PEN`` The black pen used to clear the screen. diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index ef0b5c3dd..31a6ee753 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2373,6 +2373,11 @@ static int screen_getMousePos(lua_State *L) return Lua::PushPosXY(L, Screen::getMousePos()); } +static int screen_getMousePixels(lua_State *L) +{ + return Lua::PushPosXY(L, Screen::getMousePixels()); +} + static int screen_getWindowSize(lua_State *L) { return Lua::PushPosXY(L, Screen::getWindowSize()); @@ -2563,6 +2568,7 @@ static int screen_zoom(lua_State *L) static const luaL_Reg dfhack_screen_funcs[] = { { "getMousePos", screen_getMousePos }, + { "getMousePixels", screen_getMousePixels }, { "getWindowSize", screen_getWindowSize }, { "paintTile", screen_paintTile }, { "readTile", screen_readTile }, diff --git a/library/lua/gui.lua b/library/lua/gui.lua index 709ff65f9..1eaa9861a 100644 --- a/library/lua/gui.lua +++ b/library/lua/gui.lua @@ -7,8 +7,6 @@ local utils = require('utils') local dscreen = dfhack.screen local getval = utils.getval -USE_GRAPHICS = dscreen.inGraphicsMode() - local to_pen = dfhack.pen.parse CLEAR_PEN = to_pen{tile=909, ch=32, fg=0, bg=0} diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 9ce42f2b2..55d8facf9 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -988,7 +988,7 @@ function render_text(obj,dc,x0,y0,pen,dpen,disabled) if token.tile then x = x + 1 if dc then - dc:char(nil, token.tile) + dc:tile(nil, token.tile) end end diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 4b600a661..c436a0ca5 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -61,6 +61,7 @@ using namespace DFHack; #include "df/general_ref.h" #include "df/global_objects.h" #include "df/graphic.h" +#include "df/graphic_viewportst.h" #include "df/historical_figure.h" #include "df/interfacest.h" #include "df/item_corpsepiecest.h" @@ -2151,16 +2152,31 @@ df::coord Gui::getMousePos() df::coord pos; if (gps && gps->precise_mouse_x > -1) { pos = getViewportPos(); -/* TODO: understand how this changes for v50 - pos.x += gps->mouse_x_pixel / tile_width; - pos.y += gps->mouse_y_pixel / tile_height; -*/ + if (Screen::inGraphicsMode()) { + int32_t map_tile_pixels = gps->viewport_zoom_factor / 4; + pos.x += gps->precise_mouse_x / map_tile_pixels; + pos.y += gps->precise_mouse_y / map_tile_pixels; + } else { + pos.x += gps->mouse_x; + pos.y += gps->mouse_y; + } } + if (!Maps::isValidTilePos(pos.x, pos.y, pos.z)) + return df::coord(); return pos; } int getDepthAt_default (int32_t x, int32_t y) { + auto &main_vp = gps->main_viewport; + if (x < 0 || x >= main_vp->dim_x || y < 0 || y >= main_vp->dim_y) + return 0; + const size_t num_viewports = gps->viewport.size(); + const size_t index = (x * main_vp->dim_y) + y; + for (size_t depth = 0; depth < num_viewports; ++depth) { + if (gps->viewport[depth]->screentexpos_background[index]) + return depth; + } return 0; }