mirgate preloaded assets to lua

develop
shevernitskiy 2023-08-14 08:17:57 +03:00
parent 1409af67de
commit 91f4c3d561
7 changed files with 89 additions and 110 deletions

@ -1720,7 +1720,6 @@ static int textures_loadTileset(lua_State *state)
} }
static const LuaWrapper::FunctionReg dfhack_textures_module[] = { static const LuaWrapper::FunctionReg dfhack_textures_module[] = {
WRAPM(Textures, getAsset),
WRAPM(Textures, getTexposByHandle), WRAPM(Textures, getTexposByHandle),
{ NULL, NULL } { NULL, NULL }
}; };

@ -43,11 +43,6 @@ DFHACK_EXPORT std::vector<TexposHandle> loadTileset(const std::string& file,
*/ */
DFHACK_EXPORT long getTexposByHandle(TexposHandle handle); 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 * Call this on DFHack init just once to setup interposed handlers and
* init static assets. * init static assets.

@ -912,32 +912,77 @@ local BASE_FRAME = {
paused_pen = to_pen{fg=COLOR_RED, bg=COLOR_BLACK}, paused_pen = to_pen{fg=COLOR_RED, bg=COLOR_BLACK},
} }
local function make_frame(name, double_line) local texpos_handles = {
local tp = function(offset) green_pin = dfhack.textures.loadTileset('hack/data/art/green-pin.png', 8, 12),
local texpos = dfhack.textures.getAsset('hack/data/art/border-'..name:lower()..'.png', offset) red_pin = dfhack.textures.loadTileset('hack/data/art/red-pin.png', 8, 12),
return texpos >= 0 and texpos or nil 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 end
local function make_frame(tp, double_line)
local frame = copyall(BASE_FRAME) 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.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(7), ch=double_line and 186 or 179, 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(15), ch=double_line and 205 or 196, 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(9), ch=double_line and 186 or 179, 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(0), ch=double_line and 201 or 218, 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(14), ch=double_line and 200 or 192, 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(2), ch=double_line and 187 or 191, 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(16), ch=double_line and 188 or 217, 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 return frame
end end
FRAME_WINDOW = make_frame('Window', true) function FRAME_WINDOW(resizable)
FRAME_PANEL = make_frame('Panel', false) local frame = make_frame(tp_border_window, true)
FRAME_MEDIUM = make_frame('Medium', false) if not resizable then
FRAME_BOLD = make_frame('Bold', true) 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_INTERIOR = make_frame('Thin', false) end
FRAME_INTERIOR.signature_pen = false return frame
FRAME_INTERIOR_MEDIUM = copyall(FRAME_MEDIUM) end
FRAME_INTERIOR_MEDIUM.signature_pen = false 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 -- for compatibility with pre-steam code
GREY_LINE_FRAME = FRAME_PANEL GREY_LINE_FRAME = FRAME_PANEL
@ -950,38 +995,16 @@ BOLD_FRAME = FRAME_BOLD
INTERIOR_FRAME = FRAME_INTERIOR INTERIOR_FRAME = FRAME_INTERIOR
INTERIOR_MEDIUM_FRAME = FRAME_INTERIOR_MEDIUM 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) 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 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 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.lt_frame_pen or pen, x1, y1)
dscreen.paintTile(style.rt_frame_pen or pen, x2, y1) dscreen.paintTile(style.rt_frame_pen or pen, x2, y1)
dscreen.paintTile(style.lb_frame_pen or pen, x1, y2) dscreen.paintTile(style.lb_frame_pen or pen, x1, y2)
local rb_frame_pen = style.rb_frame_pen dscreen.paintTile(style.rb_frame_pen or pen, x2, y2)
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.fillRect(style.t_frame_pen or style.h_frame_pen or pen,x1+1,y1,x2-1,y1) 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.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) dscreen.fillRect(style.l_frame_pen or style.v_frame_pen or pen,x1,y1+1,x1,y2-1)

@ -30,24 +30,6 @@ static std::unordered_map<TexposHandle, long> g_handle_to_texpos;
static std::unordered_map<TexposHandle, SDL_Surface*> g_handle_to_surface; static std::unordered_map<TexposHandle, SDL_Surface*> g_handle_to_surface;
static std::mutex g_adding_mutex; static std::mutex g_adding_mutex;
static std::vector<TexposHandle> empty{};
// handle, tile width px, tile height px
static std::tuple<std::vector<TexposHandle>, int, int> basic{empty, Textures::TILE_WIDTH_PX,
Textures::TILE_HEIGHT_PX};
static std::unordered_map<std::string, std::tuple<std::vector<TexposHandle>, 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 // Converts an arbitrary Surface to something like the display format
// (32-bit RGBA), and converts magenta to transparency if convert_magenta is set // (32-bit RGBA), and converts magenta to transparency if convert_magenta is set
// and the source surface didn't already have an alpha channel. // and the source surface didn't already have an alpha channel.
@ -160,15 +142,6 @@ long Textures::getTexposByHandle(TexposHandle handle) {
return -1; 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() { static void reset_texpos() {
g_handle_to_texpos.clear(); g_handle_to_texpos.clear();
} }
@ -269,15 +242,6 @@ static void uninstall_reset_point() {
void Textures::init(color_ostream& out) { void Textures::init(color_ostream& out) {
install_reset_point(); install_reset_point();
DEBUG(textures, out).print("dynamic texture loading ready"); 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() { void Textures::cleanup() {

@ -1,5 +1,7 @@
local _ENV = mkmodule('plugins.buildingplan.pens') local _ENV = mkmodule('plugins.buildingplan.pens')
local gui = require('gui')
GOOD_TILE_PEN, BAD_TILE_PEN = nil, nil GOOD_TILE_PEN, BAD_TILE_PEN = nil, nil
VERT_TOP_PEN, VERT_MID_PEN, VERT_BOT_PEN = nil, 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 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 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() function reload_pens()
GOOD_TILE_PEN = to_pen{ch='o', fg=COLOR_GREEN, tile=dfhack.screen.findGraphicsTile('CURSORS', 1, 2)} 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)} 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 = gui.tp_border_thin(11), ch = 194, fg = COLOR_GREY, bg = COLOR_BLACK }
VERT_TOP_PEN = to_pen { tile = tp(tb, 10), 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_MID_PEN = to_pen { tile = tp(tb, 4), 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_BOT_PEN = to_pen { tile = tp(tb, 11), ch = 193, fg = COLOR_GREY, bg = COLOR_BLACK }
local mb = 'hack/data/art/border-medium.png' HORI_LEFT_PEN = to_pen { tile = gui.tp_border_medium(13), ch = 195, fg = COLOR_GREY, bg = COLOR_BLACK }
HORI_LEFT_PEN = to_pen { tile = tp(mb, 12), 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_MID_PEN = to_pen { tile = tp(mb, 5), 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_RIGHT_PEN = to_pen { tile = tp(mb, 13), ch = 180, fg = COLOR_GREY, bg = COLOR_BLACK }
local cp = 'hack/data/art/control-panel.png' BUTTON_START_PEN = to_pen { tile = gui.tp_control_panel(14), ch = '[', fg = COLOR_YELLOW }
BUTTON_START_PEN = to_pen { tile = tp(cp, 13), ch = '[', fg = COLOR_YELLOW } BUTTON_END_PEN = to_pen { tile = gui.tp_control_panel(16), ch = ']', fg = COLOR_YELLOW }
BUTTON_END_PEN = to_pen { tile = tp(cp, 15), ch = ']', fg = COLOR_YELLOW } SELECTED_ITEM_PEN = to_pen { tile = gui.tp_control_panel(10), ch = string.char(251), fg = COLOR_YELLOW }
SELECTED_ITEM_PEN = to_pen { tile = tp(cp, 9), ch = string.char(251), fg = COLOR_YELLOW }
MINI_TEXT_PEN = to_pen{fg=COLOR_BLACK, bg=COLOR_GREY} MINI_TEXT_PEN = to_pen{fg=COLOR_BLACK, bg=COLOR_GREY}
MINI_TEXT_HPEN = to_pen{fg=COLOR_BLACK, bg=COLOR_WHITE} MINI_TEXT_HPEN = to_pen{fg=COLOR_BLACK, bg=COLOR_WHITE}

@ -5,6 +5,8 @@ local helpdb = require('helpdb')
local overlay = require('plugins.overlay') local overlay = require('plugins.overlay')
local widgets = require('gui.widgets') local widgets = require('gui.widgets')
local textures = dfhack.textures.loadTileset('hack/data/art/dfhack.png', 8, 12)
local function get_command(cmdline) local function get_command(cmdline)
local first_word = cmdline:trim():split(' +')[1] local first_word = cmdline:trim():split(' +')[1]
if first_word:startswith(':') then first_word = first_word:sub(2) end if first_word:startswith(':') then first_word = first_word:sub(2) end
@ -50,7 +52,6 @@ HotspotMenuWidget.ATTRS{
function HotspotMenuWidget:init() function HotspotMenuWidget:init()
self.mouseover = false self.mouseover = false
self.textures = dfhack.textures.loadTileset('hack/data/art/dfhack.png', 8, 12)
end end
function HotspotMenuWidget:overlay_onupdate() function HotspotMenuWidget:overlay_onupdate()
@ -71,7 +72,7 @@ local dscreen = dfhack.screen
function HotspotMenuWidget:onRenderBody(dc) function HotspotMenuWidget:onRenderBody(dc)
local x, y = dc.x, dc.y local x, y = dc.x, dc.y
local tp = function(offset) local tp = function(offset)
return dfhack.textures.getTexposByHandle(self.textures[offset]) return dfhack.textures.getTexposByHandle(textures[offset])
end end
if tp(1) == -1 then if tp(1) == -1 then

@ -26,7 +26,10 @@ namespace DFHack {
DBG_DECLARE(pathable, log, DebugCategory::LINFO); DBG_DECLARE(pathable, log, DebugCategory::LINFO);
} }
static std::vector<TexposHandle> textures;
DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) { DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) {
textures = Textures::loadTileset("hack/data/art/pathable.png", 32, 32);
return CR_OK; 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 pathable_tile_texpos = init->load_bar_texpos[1];
long unpathable_tile_texpos = init->load_bar_texpos[4]; 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) { if (on_off_texpos > 0) {
pathable_tile_texpos = on_off_texpos; 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(); auto dims = Gui::getDwarfmodeViewDims().map();