From 38a07a4ca584e2cccc2c2814c35266ba33d692c8 Mon Sep 17 00:00:00 2001
From: Alexander Gavrilov
Date: Sun, 19 Aug 2012 20:00:10 +0400
Subject: [PATCH] Export the tile finder function to lua, and improve mouse
event reporting.
---
LUA_API.rst | 31 +++++++++++++++++----
Lua API.html | 29 ++++++++++++++++----
library/LuaApi.cpp | 21 +++++++++++++++
library/include/modules/Screen.h | 3 +++
library/modules/Screen.cpp | 46 +++++++++++++++++++++++---------
5 files changed, 108 insertions(+), 22 deletions(-)
diff --git a/LUA_API.rst b/LUA_API.rst
index a59be7430..d6044c235 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 ebb8a14db..610202ce3 100644
--- a/Lua API.html
+++ b/Lua API.html
@@ -1398,24 +1398,28 @@ be feasibly used in the core context.
dfhack.screen.getMousePos()
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.
@@ -1435,6 +1439,12 @@ in sequence to override the ch field of pen.
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.
@@ -1488,6 +1498,15 @@ In order to make a see-through dialog, call self._n
If any keys are pressed, the keys argument is a table mapping them to true.
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/library/LuaApi.cpp b/library/LuaApi.cpp
index 0f3ed9357..112cc4fdc 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 },
{ "dismiss", screen_dismiss },
{ "isDismissed", screen_isDismissed },
diff --git a/library/include/modules/Screen.h b/library/include/modules/Screen.h
index 0e4963fb6..334b466ff 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 ba0313e2d..57b014aca 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");
}
}