From 0e725be04639db4f63c6e532fc70403d81f9b7da Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Mon, 14 Aug 2023 12:07:27 +0300 Subject: [PATCH] support closure as tile arg to get texpos --- library/LuaApi.cpp | 22 +++++++++++++++- library/lua/gui.lua | 43 ++++++++++++++++++++++--------- plugins/lua/buildingplan/pens.lua | 18 ++++++------- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 174bedb56..29d343c17 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -161,6 +161,26 @@ static bool get_int_field(lua_State *L, T *pf, int idx, const char *name, int de return !nil; } +template +static bool get_int_or_closure_field(lua_State *L, T *pf, int idx, const char *name, int defval) +{ + lua_getfield(L, idx, name); + bool nil = lua_isnil(L, -1); + if (nil) { + *pf = T(defval); + } else if (lua_isnumber(L, -1)) { + *pf = T(lua_tointeger(L, -1)); + } else if (lua_isfunction(L, -1)) { + lua_call(L, 0, 1); + *pf = T(lua_tointeger(L, -1)); + lua_pop(L, 1); + } else { + luaL_error(L, "Field %s is not a number or closure function.", name); + } + lua_pop(L, 1); + return !nil; +} + static bool get_char_field(lua_State *L, char *pf, int idx, const char *name, char defval) { lua_getfield(L, idx, name); @@ -207,7 +227,7 @@ static void decode_pen(lua_State *L, Pen &pen, int idx) else pen.bold = lua_toboolean(L, -1); lua_pop(L, 1); - get_int_field(L, &pen.tile, idx, "tile", 0); + 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; diff --git a/library/lua/gui.lua b/library/lua/gui.lua index 0176c998e..8c61da6fd 100644 --- a/library/lua/gui.lua +++ b/library/lua/gui.lua @@ -912,6 +912,11 @@ local BASE_FRAME = { paused_pen = to_pen{fg=COLOR_RED, bg=COLOR_BLACK}, } +-- DFHack textures +-------------------------- + +-- Preloaded DFHack Asset +-- Use this handles if you need to get dfhack standard textures local texpos_handles = { green_pin = dfhack.textures.loadTileset('hack/data/art/green-pin.png', 8, 12), red_pin = dfhack.textures.loadTileset('hack/data/art/red-pin.png', 8, 12), @@ -925,6 +930,22 @@ local texpos_handles = { border_window = dfhack.textures.loadTileset('hack/data/art/border-window.png', 8, 12), } +-- Mathods to obtain valid texposes by handles +function tp_green_pin(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.green_pin[offset]) +end +function tp_red_pin(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.red_pin[offset]) +end +function tp_icons(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.icons[offset]) +end +function tp_on_off(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.on_off[offset]) +end +function tp_control_panel(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.control_panel[offset]) +end function tp_border_thin(offset) return dfhack.textures.getTexposByHandle(texpos_handles.border_thin[offset]) end @@ -940,27 +961,25 @@ end function tp_border_window(offset) return dfhack.textures.getTexposByHandle(texpos_handles.border_window[offset]) end -function tp_control_panel(offset) - return dfhack.textures.getTexposByHandle(texpos_handles.control_panel[offset]) -end + local function make_frame(tp, double_line) local frame = copyall(BASE_FRAME) - frame.t_frame_pen = to_pen{ tile=tp(2), ch=double_line and 205 or 196, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.l_frame_pen = to_pen{ tile=tp(8), ch=double_line and 186 or 179, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.b_frame_pen = to_pen{ tile=tp(16), ch=double_line and 205 or 196, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.r_frame_pen = to_pen{ tile=tp(10), ch=double_line and 186 or 179, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.lt_frame_pen = to_pen{ tile=tp(1), ch=double_line and 201 or 218, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.lb_frame_pen = to_pen{ tile=tp(15), ch=double_line and 200 or 192, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.rt_frame_pen = to_pen{ tile=tp(3), ch=double_line and 187 or 191, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.rb_frame_pen = to_pen{ tile=tp(17), ch=double_line and 188 or 217, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.t_frame_pen = to_pen{ tile=curry(tp, 2), ch=double_line and 205 or 196, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.l_frame_pen = to_pen{ tile=curry(tp, 8), ch=double_line and 186 or 179, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.b_frame_pen = to_pen{ tile=curry(tp, 16), ch=double_line and 205 or 196, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.r_frame_pen = to_pen{ tile=curry(tp, 10), ch=double_line and 186 or 179, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.lt_frame_pen = to_pen{ tile=curry(tp, 1), ch=double_line and 201 or 218, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.lb_frame_pen = to_pen{ tile=curry(tp, 15), ch=double_line and 200 or 192, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.rt_frame_pen = to_pen{ tile=curry(tp, 3), ch=double_line and 187 or 191, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.rb_frame_pen = to_pen{ tile=curry(tp, 17), ch=double_line and 188 or 217, fg=COLOR_GREY, bg=COLOR_BLACK } return frame end function FRAME_WINDOW(resizable) local frame = make_frame(tp_border_window, true) if not resizable then - frame.rb_frame_pen = to_pen{ tile=tp_border_panel(17), ch=double_line and 188 or 217, fg=COLOR_GREY, bg=COLOR_BLACK } + frame.rb_frame_pen = to_pen{ tile=curry(tp_border_panel, 17), ch=double_line and 188 or 217, fg=COLOR_GREY, bg=COLOR_BLACK } end return frame end diff --git a/plugins/lua/buildingplan/pens.lua b/plugins/lua/buildingplan/pens.lua index d50ee4c75..817070b76 100644 --- a/plugins/lua/buildingplan/pens.lua +++ b/plugins/lua/buildingplan/pens.lua @@ -15,17 +15,17 @@ function reload_pens() GOOD_TILE_PEN = to_pen{ch='o', fg=COLOR_GREEN, tile=dfhack.screen.findGraphicsTile('CURSORS', 1, 2)} BAD_TILE_PEN = to_pen{ch='X', fg=COLOR_RED, tile=dfhack.screen.findGraphicsTile('CURSORS', 3, 0)} - VERT_TOP_PEN = to_pen { tile = gui.tp_border_thin(11), ch = 194, fg = COLOR_GREY, bg = COLOR_BLACK } - VERT_MID_PEN = to_pen { tile = gui.tp_border_thin(5), ch = 179, fg = COLOR_GREY, bg = COLOR_BLACK } - VERT_BOT_PEN = to_pen { tile = gui.tp_border_thin(12), ch = 193, fg = COLOR_GREY, bg = COLOR_BLACK } + VERT_TOP_PEN = to_pen { tile = curry(gui.tp_border_thin, 11), ch = 194, fg = COLOR_GREY, bg = COLOR_BLACK } + VERT_MID_PEN = to_pen { tile = curry(gui.tp_border_thin, 5), ch = 179, fg = COLOR_GREY, bg = COLOR_BLACK } + VERT_BOT_PEN = to_pen { tile = curry(gui.tp_border_thin, 12), ch = 193, fg = COLOR_GREY, bg = COLOR_BLACK } - HORI_LEFT_PEN = to_pen { tile = gui.tp_border_medium(13), ch = 195, fg = COLOR_GREY, bg = COLOR_BLACK } - HORI_MID_PEN = to_pen { tile = gui.tp_border_medium(6), ch = 196, fg = COLOR_GREY, bg = COLOR_BLACK } - HORI_RIGHT_PEN = to_pen { tile = gui.tp_border_medium(14), ch = 180, fg = COLOR_GREY, bg = COLOR_BLACK } + HORI_LEFT_PEN = to_pen { tile = curry(gui.tp_border_medium, 13), ch = 195, fg = COLOR_GREY, bg = COLOR_BLACK } + HORI_MID_PEN = to_pen { tile = curry(gui.tp_border_medium, 6), ch = 196, fg = COLOR_GREY, bg = COLOR_BLACK } + HORI_RIGHT_PEN = to_pen { tile = curry(gui.tp_border_medium, 14), ch = 180, fg = COLOR_GREY, bg = COLOR_BLACK } - BUTTON_START_PEN = to_pen { tile = gui.tp_control_panel(14), ch = '[', fg = COLOR_YELLOW } - BUTTON_END_PEN = to_pen { tile = gui.tp_control_panel(16), ch = ']', fg = COLOR_YELLOW } - SELECTED_ITEM_PEN = to_pen { tile = gui.tp_control_panel(10), ch = string.char(251), fg = COLOR_YELLOW } + BUTTON_START_PEN = to_pen { tile = curry(gui.tp_control_panel, 14), ch = '[', fg = COLOR_YELLOW } + BUTTON_END_PEN = to_pen { tile = curry(gui.tp_control_panel, 16), ch = ']', fg = COLOR_YELLOW } + SELECTED_ITEM_PEN = to_pen { tile = curry(gui.tp_control_panel, 10), ch = string.char(251), fg = COLOR_YELLOW } MINI_TEXT_PEN = to_pen{fg=COLOR_BLACK, bg=COLOR_GREY} MINI_TEXT_HPEN = to_pen{fg=COLOR_BLACK, bg=COLOR_WHITE}