From 07f8fad7876047aeec46b1d6ba61565a236772fa Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 28 Dec 2022 16:00:10 -0800 Subject: [PATCH 1/7] implement Gui::getMousePos and Gui::getDepthAt --- library/modules/Gui.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 4b600a661..71a166b89 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,24 @@ 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; -*/ + int32_t map_tile_pixels = gps->viewport_zoom_factor >> 2; + pos.x += gps->precise_mouse_x / map_tile_pixels; + pos.y += gps->precise_mouse_y / map_tile_pixels; } 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 (int depth = 0; depth < num_viewports; ++depth) { + if (gps->viewport[depth]->screentexpos_background[index]) + return depth; + } return 0; } From fc523f12fecf2e1cdb42f7f4328d61799f295952 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 28 Dec 2022 16:05:27 -0800 Subject: [PATCH 2/7] expose and document dfhack.screen.getMousePixels() --- docs/dev/Lua API.rst | 12 ++++++++++-- library/LuaApi.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 0fa95dd03..4b9e2e972 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()`` 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 }, From d7b67e9528b1e5296ee959eb564304e6ab631af8 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 28 Dec 2022 16:27:44 -0800 Subject: [PATCH 3/7] remove cached USE_GRAPHICS from gui.lua it's no longer constant --- docs/dev/Lua API.rst | 5 ----- library/lua/gui.lua | 2 -- 2 files changed, 7 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 4b9e2e972..7c6e72f19 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -3640,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/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} From cff603e2433fea4b2a698bb0e8b900135cbbfd68 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 28 Dec 2022 23:16:13 -0800 Subject: [PATCH 4/7] allow Labels to write tile textures --- library/lua/gui/widgets.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 371f5be050bd38be40592b73abe95a23f4c9385e Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 28 Dec 2022 23:31:06 -0800 Subject: [PATCH 5/7] fix signedness of comparison var --- library/modules/Gui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 71a166b89..7283b6774 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -2166,7 +2166,7 @@ int getDepthAt_default (int32_t x, int32_t y) return 0; const size_t num_viewports = gps->viewport.size(); const size_t index = (x * main_vp->dim_y) + y; - for (int depth = 0; depth < num_viewports; ++depth) { + for (size_t depth = 0; depth < num_viewports; ++depth) { if (gps->viewport[depth]->screentexpos_background[index]) return depth; } From a4c11de4fbb2c43cb441545664cfa1705d218074 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Thu, 29 Dec 2022 00:01:40 -0800 Subject: [PATCH 6/7] returns correct mouse pos when in ascii --- library/modules/Gui.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 7283b6774..83ca8c2e9 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -2152,10 +2152,17 @@ df::coord Gui::getMousePos() df::coord pos; if (gps && gps->precise_mouse_x > -1) { pos = getViewportPos(); - int32_t map_tile_pixels = gps->viewport_zoom_factor >> 2; - pos.x += gps->precise_mouse_x / map_tile_pixels; - pos.y += gps->precise_mouse_y / map_tile_pixels; + if (Screen::inGraphicsMode()) { + int32_t map_tile_pixels = gps->viewport_zoom_factor >> 2; + 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; } From f45eac993a2031f998f93a3db4b4b769cb7230eb Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Thu, 29 Dec 2022 22:18:35 -0800 Subject: [PATCH 7/7] use semantically clearer div rather than bit shift --- library/modules/Gui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 83ca8c2e9..c436a0ca5 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -2153,7 +2153,7 @@ df::coord Gui::getMousePos() if (gps && gps->precise_mouse_x > -1) { pos = getViewportPos(); if (Screen::inGraphicsMode()) { - int32_t map_tile_pixels = gps->viewport_zoom_factor >> 2; + 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 {