From 91f4c3d561593f72839a7c63898929c06ded4f12 Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Mon, 14 Aug 2023 08:17:57 +0300 Subject: [PATCH] mirgate preloaded assets to lua --- library/LuaApi.cpp | 1 - library/include/modules/Textures.h | 5 -- library/lua/gui.lua | 117 +++++++++++++++++------------ library/modules/Textures.cpp | 36 --------- plugins/lua/buildingplan/pens.lua | 28 +++---- plugins/lua/hotkeys.lua | 5 +- plugins/pathable.cpp | 7 +- 7 files changed, 89 insertions(+), 110 deletions(-) diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 095d432ee..d61786feb 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1720,7 +1720,6 @@ static int textures_loadTileset(lua_State *state) } static const LuaWrapper::FunctionReg dfhack_textures_module[] = { - WRAPM(Textures, getAsset), WRAPM(Textures, getTexposByHandle), { NULL, NULL } }; diff --git a/library/include/modules/Textures.h b/library/include/modules/Textures.h index d8a256b2b..17cd6e2fc 100644 --- a/library/include/modules/Textures.h +++ b/library/include/modules/Textures.h @@ -43,11 +43,6 @@ DFHACK_EXPORT std::vector loadTileset(const std::string& file, */ DFHACK_EXPORT long getTexposByHandle(TexposHandle handle); -/** - * Get texpos for static asset with index in tileset. - */ -DFHACK_EXPORT long getAsset(const std::string asset, size_t index = 0); - /** * Call this on DFHack init just once to setup interposed handlers and * init static assets. diff --git a/library/lua/gui.lua b/library/lua/gui.lua index d9f826cd1..0176c998e 100644 --- a/library/lua/gui.lua +++ b/library/lua/gui.lua @@ -912,32 +912,77 @@ local BASE_FRAME = { paused_pen = to_pen{fg=COLOR_RED, bg=COLOR_BLACK}, } -local function make_frame(name, double_line) - local tp = function(offset) - local texpos = dfhack.textures.getAsset('hack/data/art/border-'..name:lower()..'.png', offset) - return texpos >= 0 and texpos or nil - end +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), + icons = dfhack.textures.loadTileset('hack/data/art/icons.png', 8, 12), + on_off = dfhack.textures.loadTileset('hack/data/art/on-off.png', 8, 12), + control_panel = dfhack.textures.loadTileset('hack/data/art/control-panel.png', 8, 12), + border_thin = dfhack.textures.loadTileset('hack/data/art/border-thin.png', 8, 12), + border_medium = dfhack.textures.loadTileset('hack/data/art/border-medium.png', 8, 12), + border_bold = dfhack.textures.loadTileset('hack/data/art/border-bold.png', 8, 12), + border_panel = dfhack.textures.loadTileset('hack/data/art/border-panel.png', 8, 12), + border_window = dfhack.textures.loadTileset('hack/data/art/border-window.png', 8, 12), +} +function tp_border_thin(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.border_thin[offset]) +end +function tp_border_medium(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.border_medium[offset]) +end +function tp_border_bold(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.border_bold[offset]) +end +function tp_border_panel(offset) + return dfhack.textures.getTexposByHandle(texpos_handles.border_panel[offset]) +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(1), ch=double_line and 205 or 196, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.l_frame_pen = to_pen{ tile=tp(7), ch=double_line and 186 or 179, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.b_frame_pen = to_pen{ tile=tp(15), ch=double_line and 205 or 196, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.r_frame_pen = to_pen{ tile=tp(9), ch=double_line and 186 or 179, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.lt_frame_pen = to_pen{ tile=tp(0), ch=double_line and 201 or 218, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.lb_frame_pen = to_pen{ tile=tp(14), ch=double_line and 200 or 192, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.rt_frame_pen = to_pen{ tile=tp(2), ch=double_line and 187 or 191, fg=COLOR_GREY, bg=COLOR_BLACK } - frame.rb_frame_pen = to_pen{ tile=tp(16), ch=double_line and 188 or 217, fg=COLOR_GREY, bg=COLOR_BLACK } + 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 } return frame end -FRAME_WINDOW = make_frame('Window', true) -FRAME_PANEL = make_frame('Panel', false) -FRAME_MEDIUM = make_frame('Medium', false) -FRAME_BOLD = make_frame('Bold', true) -FRAME_INTERIOR = make_frame('Thin', false) -FRAME_INTERIOR.signature_pen = false -FRAME_INTERIOR_MEDIUM = copyall(FRAME_MEDIUM) -FRAME_INTERIOR_MEDIUM.signature_pen = false +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 } + end + return frame +end +function FRAME_PANEL(resizable) + return make_frame(tp_border_panel, false) +end +function FRAME_MEDIUM(resizable) + return make_frame(tp_border_medium, false) +end +function FRAME_BOLD(resizable) + return make_frame(tp_border_bold, true) +end +function FRAME_INTERIOR(resizable) + local frame = make_frame(tp_border_thin, false) + frame.signature_pen = false + return frame +end +function FRAME_INTERIOR_MEDIUM(resizable) + local frame = make_frame(tp_border_medium, false) + frame.signature_pen = false + return frame +end -- for compatibility with pre-steam code GREY_LINE_FRAME = FRAME_PANEL @@ -950,38 +995,16 @@ BOLD_FRAME = FRAME_BOLD INTERIOR_FRAME = FRAME_INTERIOR INTERIOR_MEDIUM_FRAME = FRAME_INTERIOR_MEDIUM --- for compatibility with dynamic textures -local function choose_frame_style(style) - if style == FRAME_WINDOW or style == WINDOW_FRAME then return make_frame('Window', true) end - if style == FRAME_PANEL or style == GREY_LINE_FRAME or style == PANEL_FRAME then - return make_frame('Panel', true) - end - if style == FRAME_MEDIUM or style == MEDIUM_FRAME then return make_frame('Medium', true) end - if style == FRAME_BOLD then return make_frame('Bold', true) end - if style == FRAME_INTERIOR or style == INTERIOR_FRAME then - local frame = make_frame('Thin', true) - frame.signature_pen = false - return frame - end - if style == FRAME_INTERIOR_MEDIUM or style == INTERIOR_MEDIUM_FRAME then - local frame = make_frame('Medium', true) - frame.signature_pen = false - return frame - end -end - function paint_frame(dc, rect, style, title, inactive, pause_forced, resizable) - style = choose_frame_style(style) + if type(style) == 'function' then + style = style(resizable) + end local pen = style.frame_pen local x1,y1,x2,y2 = dc.x1+rect.x1, dc.y1+rect.y1, dc.x1+rect.x2, dc.y1+rect.y2 dscreen.paintTile(style.lt_frame_pen or pen, x1, y1) dscreen.paintTile(style.rt_frame_pen or pen, x2, y1) dscreen.paintTile(style.lb_frame_pen or pen, x1, y2) - local rb_frame_pen = style.rb_frame_pen - if rb_frame_pen == FRAME_WINDOW.rb_frame_pen and not resizable then - rb_frame_pen = FRAME_PANEL.rb_frame_pen - end - dscreen.paintTile(rb_frame_pen or pen, x2, y2) + dscreen.paintTile(style.rb_frame_pen or pen, x2, y2) dscreen.fillRect(style.t_frame_pen or style.h_frame_pen or pen,x1+1,y1,x2-1,y1) dscreen.fillRect(style.b_frame_pen or style.h_frame_pen or pen,x1+1,y2,x2-1,y2) dscreen.fillRect(style.l_frame_pen or style.v_frame_pen or pen,x1,y1+1,x1,y2-1) diff --git a/library/modules/Textures.cpp b/library/modules/Textures.cpp index 10ccbf59d..729bf0cae 100644 --- a/library/modules/Textures.cpp +++ b/library/modules/Textures.cpp @@ -30,24 +30,6 @@ static std::unordered_map g_handle_to_texpos; static std::unordered_map g_handle_to_surface; static std::mutex g_adding_mutex; -static std::vector empty{}; -// handle, tile width px, tile height px -static std::tuple, int, int> basic{empty, Textures::TILE_WIDTH_PX, - Textures::TILE_HEIGHT_PX}; -static std::unordered_map, int, int>> - g_static_assets{{"hack/data/art/green-pin.png", basic}, - {"hack/data/art/red-pin.png", basic}, - {"hack/data/art/icons.png", basic}, - {"hack/data/art/on-off.png", basic}, - {"hack/data/art/pathable.png", std::make_tuple(empty, 32, 32)}, - {"hack/data/art/unsuspend.png", std::make_tuple(empty, 32, 32)}, - {"hack/data/art/control-panel.png", basic}, - {"hack/data/art/border-thin.png", basic}, - {"hack/data/art/border-medium.png", basic}, - {"hack/data/art/border-bold.png", basic}, - {"hack/data/art/border-panel.png", basic}, - {"hack/data/art/border-window.png", basic}}; - // Converts an arbitrary Surface to something like the display format // (32-bit RGBA), and converts magenta to transparency if convert_magenta is set // and the source surface didn't already have an alpha channel. @@ -160,15 +142,6 @@ long Textures::getTexposByHandle(TexposHandle handle) { return -1; } -long Textures::getAsset(const std::string asset, size_t index) { - if (!g_static_assets.contains(asset)) - return -1; - auto handles = std::get<0>(g_static_assets[asset]); - if (handles.size() <= index) - return -1; - return Textures::getTexposByHandle(handles[index]); -} - static void reset_texpos() { g_handle_to_texpos.clear(); } @@ -269,15 +242,6 @@ static void uninstall_reset_point() { void Textures::init(color_ostream& out) { install_reset_point(); DEBUG(textures, out).print("dynamic texture loading ready"); - - for (auto& [key, value] : g_static_assets) { - auto tile_w = std::get<1>(value); - auto tile_h = std::get<2>(value); - g_static_assets[key] = - std::make_tuple(Textures::loadTileset(key, tile_w, tile_h), tile_w, tile_h); - } - - DEBUG(textures, out).print("assets loaded"); } void Textures::cleanup() { diff --git a/plugins/lua/buildingplan/pens.lua b/plugins/lua/buildingplan/pens.lua index 7e9b24300..d50ee4c75 100644 --- a/plugins/lua/buildingplan/pens.lua +++ b/plugins/lua/buildingplan/pens.lua @@ -1,5 +1,7 @@ local _ENV = mkmodule('plugins.buildingplan.pens') +local gui = require('gui') + GOOD_TILE_PEN, BAD_TILE_PEN = nil, nil VERT_TOP_PEN, VERT_MID_PEN, VERT_BOT_PEN = nil, nil, nil HORI_LEFT_PEN, HORI_MID_PEN, HORI_RIGHT_PEN = nil, nil, nil @@ -9,29 +11,21 @@ MINI_TEXT_PEN, MINI_TEXT_HPEN, MINI_BUTT_PEN, MINI_BUTT_HPEN = nil, nil, nil, ni local to_pen = dfhack.pen.parse -local tp = function(asset, offset) - local texpos = dfhack.textures.getAsset(asset, offset) - return texpos >= 0 and texpos or nil -end - 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)} - local tb = 'hack/data/art/border-thin.png' - VERT_TOP_PEN = to_pen { tile = tp(tb, 10), ch = 194, fg = COLOR_GREY, bg = COLOR_BLACK } - VERT_MID_PEN = to_pen { tile = tp(tb, 4), ch = 179, fg = COLOR_GREY, bg = COLOR_BLACK } - VERT_BOT_PEN = to_pen { tile = tp(tb, 11), ch = 193, fg = COLOR_GREY, bg = COLOR_BLACK } + 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 } - local mb = 'hack/data/art/border-medium.png' - HORI_LEFT_PEN = to_pen { tile = tp(mb, 12), ch = 195, fg = COLOR_GREY, bg = COLOR_BLACK } - HORI_MID_PEN = to_pen { tile = tp(mb, 5), ch = 196, fg = COLOR_GREY, bg = COLOR_BLACK } - HORI_RIGHT_PEN = to_pen { tile = tp(mb, 13), ch = 180, 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 } - local cp = 'hack/data/art/control-panel.png' - BUTTON_START_PEN = to_pen { tile = tp(cp, 13), ch = '[', fg = COLOR_YELLOW } - BUTTON_END_PEN = to_pen { tile = tp(cp, 15), ch = ']', fg = COLOR_YELLOW } - SELECTED_ITEM_PEN = to_pen { tile = tp(cp, 9), ch = string.char(251), fg = COLOR_YELLOW } + 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 } MINI_TEXT_PEN = to_pen{fg=COLOR_BLACK, bg=COLOR_GREY} MINI_TEXT_HPEN = to_pen{fg=COLOR_BLACK, bg=COLOR_WHITE} diff --git a/plugins/lua/hotkeys.lua b/plugins/lua/hotkeys.lua index ce4342ea0..728b8e9df 100644 --- a/plugins/lua/hotkeys.lua +++ b/plugins/lua/hotkeys.lua @@ -5,6 +5,8 @@ local helpdb = require('helpdb') local overlay = require('plugins.overlay') local widgets = require('gui.widgets') +local textures = dfhack.textures.loadTileset('hack/data/art/dfhack.png', 8, 12) + local function get_command(cmdline) local first_word = cmdline:trim():split(' +')[1] if first_word:startswith(':') then first_word = first_word:sub(2) end @@ -50,7 +52,6 @@ HotspotMenuWidget.ATTRS{ function HotspotMenuWidget:init() self.mouseover = false - self.textures = dfhack.textures.loadTileset('hack/data/art/dfhack.png', 8, 12) end function HotspotMenuWidget:overlay_onupdate() @@ -71,7 +72,7 @@ local dscreen = dfhack.screen function HotspotMenuWidget:onRenderBody(dc) local x, y = dc.x, dc.y local tp = function(offset) - return dfhack.textures.getTexposByHandle(self.textures[offset]) + return dfhack.textures.getTexposByHandle(textures[offset]) end if tp(1) == -1 then diff --git a/plugins/pathable.cpp b/plugins/pathable.cpp index c0c49c470..12a69dd31 100644 --- a/plugins/pathable.cpp +++ b/plugins/pathable.cpp @@ -26,7 +26,10 @@ namespace DFHack { DBG_DECLARE(pathable, log, DebugCategory::LINFO); } +static std::vector textures; + DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) { + textures = Textures::loadTileset("hack/data/art/pathable.png", 32, 32); return CR_OK; } @@ -44,10 +47,10 @@ static void paintScreenPathable(df::coord target, bool show_hidden = false) { long pathable_tile_texpos = init->load_bar_texpos[1]; long unpathable_tile_texpos = init->load_bar_texpos[4]; - long on_off_texpos = Textures::getAsset("hack/data/art/pathable.png", 0); + long on_off_texpos = Textures::getTexposByHandle(textures[0]); if (on_off_texpos > 0) { pathable_tile_texpos = on_off_texpos; - unpathable_tile_texpos = Textures::getAsset("hack/data/art/pathable.png", 1); + unpathable_tile_texpos = Textures::getTexposByHandle(textures[1]); } auto dims = Gui::getDwarfmodeViewDims().map();