diff options
| -rw-r--r-- | LUA_API.rst | 31 | ||||
| -rw-r--r-- | Lua API.html | 29 | ||||
| -rw-r--r-- | library/LuaApi.cpp | 21 | ||||
| -rw-r--r-- | library/include/modules/Screen.h | 3 | ||||
| -rw-r--r-- | library/modules/Screen.cpp | 46 |
5 files changed, 108 insertions, 22 deletions
diff --git a/LUA_API.rst b/LUA_API.rst index a59be743..d6044c23 100644 --- a/LUA_API.rst +++ b/LUA_API.rst @@ -1219,21 +1219,26 @@ be feasibly used in the core context. Returns *x,y* of the tile the mouse is over. +* ``dfhack.screen.inGraphicsMode()`` + + Checks if [GRAPHICS:YES] was specified in init. + * ``dfhack.screen.paintTile(pen,x,y[,char,tile])`` Paints a tile using given parameters. Pen is a table with following possible fields: ``ch`` - Provides the ordinary tile character. Can be overridden with the ``char`` function parameter. + Provides the ordinary tile character, as either a 1-character string or a number. + Can be overridden with the ``char`` function parameter. ``fg`` - Foreground color for the ordinary tile. Defaults to 7. + Foreground color for the ordinary tile. Defaults to COLOR_GREY (7). ``bg`` - Background color for the ordinary tile. Defaults to 0. + Background color for the ordinary tile. Defaults to COLOR_BLACK (0). ``bold`` - Bright/bold text flag. If *nil*, computed based on (fg & 8); fg is reset to 7 bits. + Bright/bold text flag. If *nil*, computed based on (fg & 8); fg is masked to 3 bits. Otherwise should be *true/false*. ``tile`` - Graphical tile id. Ignored unless [GRAPHICS:YES] in init.txt. + Graphical tile id. Ignored unless [GRAPHICS:YES] was in init.txt. ``tile_color = true`` Specifies that the tile should be shaded with *fg/bg*. ``tile_fg, tile_bg`` @@ -1253,6 +1258,14 @@ be feasibly used in the core context. Fills the rectangle specified by the coordinates with the given pen. Returns *true* if painting at least one character succeeded. +* ``dfhack.screen.findGraphicsTile(pagename,x,y)`` + + Finds a tile from a graphics set (i.e. the raws used for creatures), + if in graphics mode and loaded. + + Returns: *tile, tile_grayscale*, or *nil* if not found. + The values can then be used for the *tile* field of *pen* structures. + * ``dfhack.screen.clear()`` Fills the screen with blank background. @@ -1319,6 +1332,14 @@ Supported callbacks and fields are: Note that this refers to logical keybingings computed from real keys via options; if multiple interpretations exist, the table will contain multiple keys. + The table also may contain special keys: + + ``_STRING`` + Maps to an integer in range 0-255. Duplicates a separate "STRING_A???" code for convenience. + + ``_MOUSE_L, _MOUSE_R`` + If the left or right mouse button is pressed. + If this method is omitted, the screen is dismissed on receival of the ``LEAVESCREEN`` key. diff --git a/Lua API.html b/Lua API.html index ebb8a14d..610202ce 100644 --- a/Lua API.html +++ b/Lua API.html @@ -1398,24 +1398,28 @@ be feasibly used in the core context.</p> <li><p class="first"><tt class="docutils literal">dfhack.screen.getMousePos()</tt></p> <p>Returns <em>x,y</em> of the tile the mouse is over.</p> </li> +<li><p class="first"><tt class="docutils literal">dfhack.screen.inGraphicsMode()</tt></p> +<p>Checks if [GRAPHICS:YES] was specified in init.</p> +</li> <li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.screen.paintTile(pen,x,y[,char,tile])</span></tt></p> <p>Paints a tile using given parameters. Pen is a table with following possible fields:</p> <dl class="docutils"> <dt><tt class="docutils literal">ch</tt></dt> -<dd><p class="first last">Provides the ordinary tile character. Can be overridden with the <tt class="docutils literal">char</tt> function parameter.</p> +<dd><p class="first last">Provides the ordinary tile character, as either a 1-character string or a number. +Can be overridden with the <tt class="docutils literal">char</tt> function parameter.</p> </dd> <dt><tt class="docutils literal">fg</tt></dt> -<dd><p class="first last">Foreground color for the ordinary tile. Defaults to 7.</p> +<dd><p class="first last">Foreground color for the ordinary tile. Defaults to COLOR_GREY (7).</p> </dd> <dt><tt class="docutils literal">bg</tt></dt> -<dd><p class="first last">Background color for the ordinary tile. Defaults to 0.</p> +<dd><p class="first last">Background color for the ordinary tile. Defaults to COLOR_BLACK (0).</p> </dd> <dt><tt class="docutils literal">bold</tt></dt> -<dd><p class="first last">Bright/bold text flag. If <em>nil</em>, computed based on (fg & 8); fg is reset to 7 bits. +<dd><p class="first last">Bright/bold text flag. If <em>nil</em>, computed based on (fg & 8); fg is masked to 3 bits. Otherwise should be <em>true/false</em>.</p> </dd> <dt><tt class="docutils literal">tile</tt></dt> -<dd><p class="first last">Graphical tile id. Ignored unless [GRAPHICS:YES] in init.txt.</p> +<dd><p class="first last">Graphical tile id. Ignored unless [GRAPHICS:YES] was in init.txt.</p> </dd> <dt><tt class="docutils literal">tile_color = true</tt></dt> <dd><p class="first last">Specifies that the tile should be shaded with <em>fg/bg</em>.</p> @@ -1435,6 +1439,12 @@ in sequence to override the <tt class="docutils literal">ch</tt> field of pen.</ <p>Fills the rectangle specified by the coordinates with the given pen. Returns <em>true</em> if painting at least one character succeeded.</p> </li> +<li><p class="first"><tt class="docutils literal">dfhack.screen.findGraphicsTile(pagename,x,y)</tt></p> +<p>Finds a tile from a graphics set (i.e. the raws used for creatures), +if in graphics mode and loaded.</p> +<p>Returns: <em>tile, tile_grayscale</em>, or <em>nil</em> if not found. +The values can then be used for the <em>tile</em> field of <em>pen</em> structures.</p> +</li> <li><p class="first"><tt class="docutils literal">dfhack.screen.clear()</tt></p> <p>Fills the screen with blank background.</p> </li> @@ -1488,6 +1498,15 @@ In order to make a see-through dialog, call <tt class="docutils literal">self._n If any keys are pressed, the keys argument is a table mapping them to <em>true</em>. Note that this refers to logical keybingings computed from real keys via options; if multiple interpretations exist, the table will contain multiple keys.</p> +<p>The table also may contain special keys:</p> +<dl class="docutils"> +<dt><tt class="docutils literal">_STRING</tt></dt> +<dd><p class="first last">Maps to an integer in range 0-255. Duplicates a separate "STRING_A???" code for convenience.</p> +</dd> +<dt><tt class="docutils literal">_MOUSE_L, _MOUSE_R</tt></dt> +<dd><p class="first last">If the left or right mouse button is pressed.</p> +</dd> +</dl> <p>If this method is omitted, the screen is dismissed on receival of the <tt class="docutils literal">LEAVESCREEN</tt> key.</p> </li> </ul> diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 0f3ed935..112cc4fd 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1075,6 +1075,7 @@ static const luaL_Reg dfhack_constructions_funcs[] = { /***** Screen module *****/ static const LuaWrapper::FunctionReg dfhack_screen_module[] = { + WRAPM(Screen, inGraphicsMode), WRAPM(Screen, clear), WRAPM(Screen, invalidate), { NULL, NULL } @@ -1133,6 +1134,25 @@ static int screen_fillRect(lua_State *L) return 1; } +static int screen_findGraphicsTile(lua_State *L) +{ + auto str = luaL_checkstring(L, 1); + int x = luaL_checkint(L, 2); + int y = luaL_checkint(L, 3); + int tile, tile_gs; + if (Screen::findGraphicsTile(str, x, y, &tile, &tile_gs)) + { + lua_pushinteger(L, tile); + lua_pushinteger(L, tile_gs); + return 2; + } + else + { + lua_pushnil(L); + return 1; + } +} + namespace { int screen_show(lua_State *L) @@ -1168,6 +1188,7 @@ static const luaL_Reg dfhack_screen_funcs[] = { { "paintTile", screen_paintTile }, { "paintString", screen_paintString }, { "fillRect", screen_fillRect }, + { "findGraphicsTile", screen_findGraphicsTile }, { "show", &Lua::CallWithCatchWrapper<screen_show> }, { "dismiss", screen_dismiss }, { "isDismissed", screen_isDismissed }, diff --git a/library/include/modules/Screen.h b/library/include/modules/Screen.h index 0e4963fb..334b466f 100644 --- a/library/include/modules/Screen.h +++ b/library/include/modules/Screen.h @@ -86,6 +86,9 @@ namespace DFHack DFHACK_EXPORT df::coord2d getMousePos(); DFHACK_EXPORT df::coord2d getWindowSize(); + /// Returns the state of [GRAPHICS:YES/NO] + DFHACK_EXPORT bool inGraphicsMode(); + /// Paint one screen tile with the given pen DFHACK_EXPORT bool paintTile(const Pen &pen, int x, int y); diff --git a/library/modules/Screen.cpp b/library/modules/Screen.cpp index ba0313e2..57b014ac 100644 --- a/library/modules/Screen.cpp +++ b/library/modules/Screen.cpp @@ -66,18 +66,24 @@ using Screen::Pen; df::coord2d Screen::getMousePos() { - if (!gps) return df::coord2d(); + if (!gps || (enabler && !enabler->tracking_on)) + return df::coord2d(-1, -1); return df::coord2d(gps->mouse_x, gps->mouse_y); } df::coord2d Screen::getWindowSize() { - if (!gps) return df::coord2d(); + if (!gps) return df::coord2d(80, 25); return df::coord2d(gps->dimx, gps->dimy); } +bool Screen::inGraphicsMode() +{ + return init && init->display.flag.is_set(init_display_flags::USE_GRAPHICS); +} + static void doSetTile(const Pen &pen, int index) { auto screen = gps->screen + index*4; @@ -399,21 +405,37 @@ int dfhack_lua_viewscreen::do_input(lua_State *L) lua_pushvalue(L, -2); - if (keys->empty()) - lua_pushnil(L); - else + lua_createtable(L, 0, keys->size()+3); + + for (auto it = keys->begin(); it != keys->end(); ++it) { - lua_createtable(L, 0, keys->size()); + auto key = *it; + + if (auto name = enum_item_raw_key(key)) + lua_pushstring(L, name); + else + lua_pushinteger(L, key); - for (auto it = keys->begin(); it != keys->end(); ++it) + lua_pushboolean(L, true); + lua_rawset(L, -3); + + if (key >= interface_key::STRING_A000 && + key <= interface_key::STRING_A255) { - if (auto name = enum_item_raw_key(*it)) - lua_pushstring(L, name); - else - lua_pushinteger(L, *it); + lua_pushinteger(L, key - interface_key::STRING_A000); + lua_setfield(L, -2, "_STRING"); + } + } + if (enabler && enabler->tracking_on) + { + if (enabler->mouse_lbut) { + lua_pushboolean(L, true); + lua_setfield(L, -2, "_MOUSE_L"); + } + if (enabler->mouse_rbut) { lua_pushboolean(L, true); - lua_rawset(L, -3); + lua_setfield(L, -2, "_MOUSE_R"); } } |
