Export the tile finder function to lua, and improve mouse event reporting.

develop
Alexander Gavrilov 2012-08-19 20:00:10 +04:00
parent cacb082416
commit 38a07a4ca5
5 changed files with 108 additions and 22 deletions

@ -1219,21 +1219,26 @@ be feasibly used in the core context.
Returns *x,y* of the tile the mouse is over. 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])`` * ``dfhack.screen.paintTile(pen,x,y[,char,tile])``
Paints a tile using given parameters. Pen is a table with following possible fields: Paints a tile using given parameters. Pen is a table with following possible fields:
``ch`` ``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`` ``fg``
Foreground color for the ordinary tile. Defaults to 7. Foreground color for the ordinary tile. Defaults to COLOR_GREY (7).
``bg`` ``bg``
Background color for the ordinary tile. Defaults to 0. Background color for the ordinary tile. Defaults to COLOR_BLACK (0).
``bold`` ``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*. Otherwise should be *true/false*.
``tile`` ``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`` ``tile_color = true``
Specifies that the tile should be shaded with *fg/bg*. Specifies that the tile should be shaded with *fg/bg*.
``tile_fg, tile_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. Fills the rectangle specified by the coordinates with the given pen.
Returns *true* if painting at least one character succeeded. 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()`` * ``dfhack.screen.clear()``
Fills the screen with blank background. 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 Note that this refers to logical keybingings computed from real keys via
options; if multiple interpretations exist, the table will contain multiple keys. 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. If this method is omitted, the screen is dismissed on receival of the ``LEAVESCREEN`` key.

@ -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> <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> <p>Returns <em>x,y</em> of the tile the mouse is over.</p>
</li> </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> <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> <p>Paints a tile using given parameters. Pen is a table with following possible fields:</p>
<dl class="docutils"> <dl class="docutils">
<dt><tt class="docutils literal">ch</tt></dt> <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> </dd>
<dt><tt class="docutils literal">fg</tt></dt> <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> </dd>
<dt><tt class="docutils literal">bg</tt></dt> <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> </dd>
<dt><tt class="docutils literal">bold</tt></dt> <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 &amp; 8); fg is reset to 7 bits. <dd><p class="first last">Bright/bold text flag. If <em>nil</em>, computed based on (fg &amp; 8); fg is masked to 3 bits.
Otherwise should be <em>true/false</em>.</p> Otherwise should be <em>true/false</em>.</p>
</dd> </dd>
<dt><tt class="docutils literal">tile</tt></dt> <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> </dd>
<dt><tt class="docutils literal">tile_color = true</tt></dt> <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> <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. <p>Fills the rectangle specified by the coordinates with the given pen.
Returns <em>true</em> if painting at least one character succeeded.</p> Returns <em>true</em> if painting at least one character succeeded.</p>
</li> </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> <li><p class="first"><tt class="docutils literal">dfhack.screen.clear()</tt></p>
<p>Fills the screen with blank background.</p> <p>Fills the screen with blank background.</p>
</li> </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>. 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 Note that this refers to logical keybingings computed from real keys via
options; if multiple interpretations exist, the table will contain multiple keys.</p> 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 &quot;STRING_A???&quot; 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> <p>If this method is omitted, the screen is dismissed on receival of the <tt class="docutils literal">LEAVESCREEN</tt> key.</p>
</li> </li>
</ul> </ul>

@ -1075,6 +1075,7 @@ static const luaL_Reg dfhack_constructions_funcs[] = {
/***** Screen module *****/ /***** Screen module *****/
static const LuaWrapper::FunctionReg dfhack_screen_module[] = { static const LuaWrapper::FunctionReg dfhack_screen_module[] = {
WRAPM(Screen, inGraphicsMode),
WRAPM(Screen, clear), WRAPM(Screen, clear),
WRAPM(Screen, invalidate), WRAPM(Screen, invalidate),
{ NULL, NULL } { NULL, NULL }
@ -1133,6 +1134,25 @@ static int screen_fillRect(lua_State *L)
return 1; 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 { namespace {
int screen_show(lua_State *L) int screen_show(lua_State *L)
@ -1168,6 +1188,7 @@ static const luaL_Reg dfhack_screen_funcs[] = {
{ "paintTile", screen_paintTile }, { "paintTile", screen_paintTile },
{ "paintString", screen_paintString }, { "paintString", screen_paintString },
{ "fillRect", screen_fillRect }, { "fillRect", screen_fillRect },
{ "findGraphicsTile", screen_findGraphicsTile },
{ "show", &Lua::CallWithCatchWrapper<screen_show> }, { "show", &Lua::CallWithCatchWrapper<screen_show> },
{ "dismiss", screen_dismiss }, { "dismiss", screen_dismiss },
{ "isDismissed", screen_isDismissed }, { "isDismissed", screen_isDismissed },

@ -86,6 +86,9 @@ namespace DFHack
DFHACK_EXPORT df::coord2d getMousePos(); DFHACK_EXPORT df::coord2d getMousePos();
DFHACK_EXPORT df::coord2d getWindowSize(); DFHACK_EXPORT df::coord2d getWindowSize();
/// Returns the state of [GRAPHICS:YES/NO]
DFHACK_EXPORT bool inGraphicsMode();
/// Paint one screen tile with the given pen /// Paint one screen tile with the given pen
DFHACK_EXPORT bool paintTile(const Pen &pen, int x, int y); DFHACK_EXPORT bool paintTile(const Pen &pen, int x, int y);

@ -66,18 +66,24 @@ using Screen::Pen;
df::coord2d Screen::getMousePos() 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); return df::coord2d(gps->mouse_x, gps->mouse_y);
} }
df::coord2d Screen::getWindowSize() df::coord2d Screen::getWindowSize()
{ {
if (!gps) return df::coord2d(); if (!gps) return df::coord2d(80, 25);
return df::coord2d(gps->dimx, gps->dimy); 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) static void doSetTile(const Pen &pen, int index)
{ {
auto screen = gps->screen + index*4; auto screen = gps->screen + index*4;
@ -399,21 +405,37 @@ int dfhack_lua_viewscreen::do_input(lua_State *L)
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
if (keys->empty()) lua_createtable(L, 0, keys->size()+3);
lua_pushnil(L);
else 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_pushinteger(L, key - interface_key::STRING_A000);
lua_pushstring(L, name); lua_setfield(L, -2, "_STRING");
else }
lua_pushinteger(L, *it); }
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_pushboolean(L, true);
lua_rawset(L, -3); lua_setfield(L, -2, "_MOUSE_R");
} }
} }