From c59ad78f40ab72dedd699c10cd8e0cc2e34d4906 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Thu, 16 Feb 2023 23:02:34 -0800 Subject: [PATCH] more tokens, textures, and colors --- plugins/buildingplan/buildingplan.cpp | 53 +++++++++++++-------------- plugins/lua/buildingplan.lua | 52 +++++++++++++++++++------- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/plugins/buildingplan/buildingplan.cpp b/plugins/buildingplan/buildingplan.cpp index e31914372..687f46705 100644 --- a/plugins/buildingplan/buildingplan.cpp +++ b/plugins/buildingplan/buildingplan.cpp @@ -148,7 +148,30 @@ static void validate_config(color_ostream &out, bool verbose = false) { set_config_bool(config, CONFIG_BARS, false); } -static void clear_job_item_repo() { +static bool call_buildingplan_lua(color_ostream *out, const char *fn_name, + int nargs = 0, int nres = 0, + Lua::LuaLambda && args_lambda = Lua::DEFAULT_LUA_LAMBDA, + Lua::LuaLambda && res_lambda = Lua::DEFAULT_LUA_LAMBDA) { + DEBUG(status).print("calling buildingplan lua function: '%s'\n", fn_name); + + CoreSuspender guard; + + auto L = Lua::Core::State; + Lua::StackUnwinder top(L); + + if (!out) + out = &Core::getInstance().getConsole(); + + return Lua::CallLuaModuleFunction(*out, L, "plugins.buildingplan", fn_name, + nargs, nres, + std::forward(args_lambda), + std::forward(res_lambda)); +} + +static void clear_state(color_ostream &out) { + call_buildingplan_lua(&out, "signal_reset"); + planned_buildings.clear(); + tasks.clear(); for (auto &entry : job_item_repo) { for (auto &jitem : entry.second) { delete jitem; @@ -168,9 +191,7 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) { validate_config(out); DEBUG(status,out).print("loading persisted state\n"); - planned_buildings.clear(); - tasks.clear(); - clear_job_item_repo(); + clear_state(out); vector building_configs; World::GetPersistentData(&building_configs, BLD_CONFIG_KEY); const size_t num_building_configs = building_configs.size(); @@ -185,33 +206,11 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) { DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { if (event == SC_WORLD_UNLOADED) { DEBUG(status,out).print("world unloaded; clearing state for %s\n", plugin_name); - planned_buildings.clear(); - tasks.clear(); - clear_job_item_repo(); + clear_state(out); } return CR_OK; } -static bool call_buildingplan_lua(color_ostream *out, const char *fn_name, - int nargs = 0, int nres = 0, - Lua::LuaLambda && args_lambda = Lua::DEFAULT_LUA_LAMBDA, - Lua::LuaLambda && res_lambda = Lua::DEFAULT_LUA_LAMBDA) { - DEBUG(status).print("calling buildingplan lua function: '%s'\n", fn_name); - - CoreSuspender guard; - - auto L = Lua::Core::State; - Lua::StackUnwinder top(L); - - if (!out) - out = &Core::getInstance().getConsole(); - - return Lua::CallLuaModuleFunction(*out, L, "plugins.buildingplan", fn_name, - nargs, nres, - std::forward(args_lambda), - std::forward(res_lambda)); -} - static bool cycle_requested = false; static void do_cycle(color_ostream &out) { diff --git a/plugins/lua/buildingplan.lua b/plugins/lua/buildingplan.lua index 7f8d996c8..6b1abb84e 100644 --- a/plugins/lua/buildingplan.lua +++ b/plugins/lua/buildingplan.lua @@ -64,15 +64,34 @@ function get_job_item(btype, subtype, custom, index) return obj end -local texpos_base = -1 +local BUTTON_START_PEN, BUTTON_END_PEN = nil, nil local reset_counts_flag = false local reset_inspector_flag = false function signal_reset() - texpos_base = dfhack.textures.getControlPanelTexposStart() + BUTTON_START_PEN = nil + BUTTON_END_PEN = nil reset_counts_flag = true reset_inspector_flag = true end +local to_pen = dfhack.pen.parse +local function get_button_start_pen() + if not BUTTON_START_PEN then + local texpos_base = dfhack.textures.getControlPanelTexposStart() + BUTTON_START_PEN = to_pen{ch='[', fg=COLOR_YELLOW, + tile=texpos_base > 0 and texpos_base + 13 or nil} + end + return BUTTON_START_PEN +end +local function get_button_end_pen() + if not BUTTON_END_PEN then + local texpos_base = dfhack.textures.getControlPanelTexposStart() + BUTTON_END_PEN = to_pen{ch=']', fg=COLOR_YELLOW, + tile=texpos_base > 0 and texpos_base + 15 or nil} + end + return BUTTON_END_PEN +end + -------------------------------- -- PlannerOverlay -- @@ -172,19 +191,20 @@ function ItemLine:init() widgets.Label{ frame={t=0, l=23}, text={ - {tile=2600}, - {gap=6, tile=2602}, - {tile=2600}, - {gap=1, tile=2602}, + {tile=get_button_start_pen}, + {gap=6, tile=get_button_end_pen}, + {tile=get_button_start_pen}, + {gap=1, tile=get_button_end_pen}, }, }, widgets.Label{ frame={t=0, l=0}, text={ - {width=21, text=function() return self:get_item_line_text() end}, - {gap=3, text='filter'}, - {gap=2, text='x'}, - {gap=3, text=function() return self.note end}, + {width=21, text=self:callback('get_item_line_text')}, + {gap=3, text='filter', pen=COLOR_GREEN}, + {gap=2, text='x', pen=COLOR_GREEN}, + {gap=3, text=function() return self.note end, + pen=function() return self.note_pen end}, }, }, } @@ -246,8 +266,13 @@ function ItemLine:get_item_line_text() self.available = self.available or countAvailableItems(uibs.building_type, uibs.building_subtype, uibs.custom_type, idx - 1) - self.note = self.available >= quantity and - 'Can build now' or 'Will build later' + if self.available >= quantity then + self.note_pen = COLOR_GREEN + self.note = 'Available now' + else + self.note_pen = COLOR_YELLOW + self.note = 'Will link later' + end return ('%d %s%s'):format(quantity, self.desc, quantity == 1 and '' or 's') end @@ -298,7 +323,7 @@ function PlannerOverlay:init() view_id='stairs_top_subtype', frame={t=4, l=4}, key='CUSTOM_R', - label='Top Stair Type: ', + label='Top Stair Type: ', visible=is_stairs, options={ {label='Auto', value='auto'}, @@ -444,7 +469,6 @@ function PlannerOverlay:render(dc) PlannerOverlay.super.render(self, dc) end -local to_pen = dfhack.pen.parse local GOOD_PEN = to_pen{ch='o', fg=COLOR_GREEN, tile=dfhack.screen.findGraphicsTile('CURSORS', 1, 2)} local BAD_PEN = to_pen{ch='X', fg=COLOR_RED,