Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Template for new versions:
## Fixes
- a safety check was added to ``Screen::doSetTile_char`` fur out of bound pen color values
- ``TextArea`` widget corrected to use ``COLOR_BLACK`` instead of ``COLOR_RESET`` as default background color
- ``Screen::Pen``: COLOR_RESET from Lua is changed to grey (fg) or black (bg); color values are masked to 4 bits to keep them in the expected range

## Misc Improvements

Expand Down
43 changes: 30 additions & 13 deletions library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ must not be misrepresented as being the original software.
distribution.
*/

#include "ColorText.h"
#include "Core.h"
#include "Error.h"
#include "Internal.h"
Expand Down Expand Up @@ -232,14 +233,30 @@ static bool get_bool_field(lua_State *L, bool *pf, int idx, const char *name, bo
return !nil;
}

static int8_t mask_pen_color(int8_t color, int8_t default_color) {
if (color == COLOR_RESET)
color = default_color;
return color & 15;
}

static bool get_color_field(lua_State *L, int8_t *pf, int idx, const char *name, int defval)
{
bool nonnil = get_int_field(L, pf, idx, name, defval); // defval for nil
*pf = mask_pen_color(*pf, defval); // defval for COLOR_RESET
return nonnil;
}

static constexpr int8_t DEFAULT_FG = COLOR_GREY;
static constexpr int8_t DEFAULT_BG = COLOR_BLACK;

static void decode_pen(lua_State *L, Pen &pen, int idx)
{
idx = lua_absindex(L, idx);

get_char_field(L, &pen.ch, idx, "ch", 0);

get_int_field(L, &pen.fg, idx, "fg", 7);
get_int_field(L, &pen.bg, idx, "bg", 0);
get_color_field(L, &pen.fg, idx, "fg", DEFAULT_FG);
get_color_field(L, &pen.bg, idx, "bg", DEFAULT_BG);

lua_getfield(L, idx, "bold");
if (lua_isnil(L, -1))
Expand All @@ -252,8 +269,8 @@ static void decode_pen(lua_State *L, Pen &pen, int idx)

get_int_or_closure_field(L, &pen.tile, idx, "tile", 0);

bool tcolor = get_int_field(L, &pen.tile_fg, idx, "tile_fg", 7);
tcolor = get_int_field(L, &pen.tile_bg, idx, "tile_bg", 0) || tcolor;
bool tcolor = get_color_field(L, &pen.tile_fg, idx, "tile_fg", DEFAULT_FG);
tcolor = get_color_field(L, &pen.tile_bg, idx, "tile_bg", DEFAULT_BG) || tcolor;

if (tcolor)
pen.tile_mode = Pen::TileColor;
Expand Down Expand Up @@ -672,7 +689,7 @@ void Lua::CheckPen(lua_State *L, Screen::Pen *pen, int index, bool allow_nil, bo
}
else if (allow_color && lua_isnumber(L, index))
{
*pen = Pen(0, lua_tointeger(L, index)&15, 0);
*pen = Pen(0, mask_pen_color(lua_tointeger(L, index), DEFAULT_FG), 0);
}
else
{
Expand All @@ -698,8 +715,8 @@ static int adjust_pen(lua_State *L, bool no_copy)

iidx = -1;

pen.fg = luaL_optint(L, 2, pen.fg) & 15;
pen.bg = luaL_optint(L, 3, pen.bg);
pen.fg = mask_pen_color(luaL_optint(L, 2, pen.fg), DEFAULT_FG);
pen.bg = mask_pen_color(luaL_optint(L, 3, pen.bg), DEFAULT_BG);

if (!lua_isnil(L, 4))
pen.bold = lua_toboolean(L, 4);
Expand Down Expand Up @@ -864,15 +881,15 @@ static int dfhack_pen_newindex(lua_State *L)
lua_pushinteger(L, (unsigned char)pen.ch);
break;
case 1:
pen.fg = luaL_checkint(L, 3) & 15;
pen.fg = mask_pen_color(luaL_checkint(L, 3), DEFAULT_FG);
lua_pushinteger(L, pen.fg);
break;
case 2:
pen.bold = lua_toboolean(L, 3);
lua_pushboolean(L, pen.bold);
break;
case 3:
pen.bg = luaL_checkint(L, 3) & 15;
pen.bg = mask_pen_color(luaL_checkint(L, 3), DEFAULT_BG);
lua_pushinteger(L, pen.bg);
break;
case 4:
Expand All @@ -891,14 +908,14 @@ static int dfhack_pen_newindex(lua_State *L)
lua_pushboolean(L, pen.tile_mode == Pen::CharColor);
break;
case 6:
if (pen.tile_mode != Pen::TileColor) { wipe_tc = true; pen.tile_bg = 0; }
pen.tile_fg = luaL_checkint(L, 3) & 15;
if (pen.tile_mode != Pen::TileColor) { wipe_tc = true; pen.tile_bg = DEFAULT_BG; }
pen.tile_fg = mask_pen_color(luaL_checkint(L, 3), DEFAULT_FG);
pen.tile_mode = Pen::TileColor;
lua_pushinteger(L, pen.tile_fg);
break;
case 7:
if (pen.tile_mode != Pen::TileColor) { wipe_tc = true; pen.tile_fg = 7; }
pen.tile_bg = luaL_checkint(L, 3) & 15;
if (pen.tile_mode != Pen::TileColor) { wipe_tc = true; pen.tile_fg = DEFAULT_FG; }
pen.tile_bg = mask_pen_color(luaL_checkint(L, 3), DEFAULT_BG);
pen.tile_mode = Pen::TileColor;
lua_pushinteger(L, pen.tile_bg);
break;
Expand Down
18 changes: 9 additions & 9 deletions library/include/modules/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,26 @@ namespace DFHack
// NOTE: LuaApi.cpp assumes this struct is plain data and has empty destructor

Pen(char ch = 0, int8_t fg = 7, int8_t bg = 0, int tile = 0, bool color_tile = false)
: ch(ch), fg(fg&7), bg(bg), bold(!!(fg&8)),
: ch(ch), fg(fg&7), bg(bg&15), bold(!!(fg&8)),
tile(tile), tile_mode(color_tile ? CharColor : AsIs), tile_fg(0), tile_bg(0)
{}
Pen(char ch, int8_t fg, int8_t bg, bool bold, int tile = 0, bool color_tile = false)
: ch(ch), fg(fg), bg(bg), bold(bold),
: ch(ch), fg(fg&15), bg(bg&15), bold(bold),
tile(tile), tile_mode(color_tile ? CharColor : AsIs), tile_fg(0), tile_bg(0)
{}
Pen(char ch, int8_t fg, int8_t bg, int tile, int8_t tile_fg, int8_t tile_bg)
: ch(ch), fg(fg&7), bg(bg), bold(!!(fg&8)),
tile(tile), tile_mode(TileColor), tile_fg(tile_fg), tile_bg(tile_bg)
: ch(ch), fg(fg&7), bg(bg&15), bold(!!(fg&8)),
tile(tile), tile_mode(TileColor), tile_fg(tile_fg&15), tile_bg(tile_bg&15)
{}
Pen(char ch, int8_t fg, int8_t bg, bool bold, int tile, int8_t tile_fg, int8_t tile_bg)
: ch(ch), fg(fg), bg(bg), bold(bold),
tile(tile), tile_mode(TileColor), tile_fg(tile_fg), tile_bg(tile_bg)
: ch(ch), fg(fg&15), bg(bg&15), bold(bold),
tile(tile), tile_mode(TileColor), tile_fg(tile_fg&15), tile_bg(tile_bg&15)
{}

void adjust(int8_t nfg) { fg = nfg&7; bold = !!(nfg&8); }
void adjust(int8_t nfg, bool nbold) { fg = nfg; bold = nbold; }
void adjust(int8_t nfg, int8_t nbg) { adjust(nfg); bg = nbg; }
void adjust(int8_t nfg, bool nbold, int8_t nbg) { adjust(nfg, nbold); bg = nbg; }
void adjust(int8_t nfg, bool nbold) { fg = nfg&15; bold = nbold; }
void adjust(int8_t nfg, int8_t nbg) { adjust(nfg); bg = nbg&15; }
void adjust(int8_t nfg, bool nbold, int8_t nbg) { adjust(nfg, nbold); bg = nbg&15; }

Pen color(int8_t nfg) const { Pen cp(*this); cp.adjust(nfg); return cp; }
Pen color(int8_t nfg, bool nbold) const { Pen cp(*this); cp.adjust(nfg, nbold); return cp; }
Expand Down
10 changes: 5 additions & 5 deletions library/modules/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ bool Screen::drawBorder(const std::string &title)
if (!gps) return false;

auto dim = getWindowSize();
Pen border('\xDB', 8);
Pen text(0, 0, 7);
Pen signature(0, 0, 8);
Pen border('\xDB', COLOR_DARKGREY);
Pen text(0, COLOR_BLACK, COLOR_GREY);
Pen signature(0, COLOR_BLACK, COLOR_DARKGREY);

for (int x = 0; x < dim.x; x++)
{
Expand Down Expand Up @@ -516,8 +516,8 @@ bool Screen::invalidate()
return true;
}

const Pen Screen::Painter::default_pen(0,COLOR_GREY,0);
const Pen Screen::Painter::default_key_pen(0,COLOR_LIGHTGREEN,0);
const Pen Screen::Painter::default_pen(0, COLOR_GREY, COLOR_BLACK);
const Pen Screen::Painter::default_key_pen(0, COLOR_LIGHTGREEN, COLOR_BLACK);

void Screen::Painter::do_paint_string(const std::string &str, const Pen &pen, bool map)
{
Expand Down
Loading
Loading