From d04b116626177a9d7ae50f6894e24abd7f16a9c9 Mon Sep 17 00:00:00 2001 From: pajawojciech Date: Tue, 6 Jan 2026 20:35:23 +0100 Subject: [PATCH 1/2] Add death cause button to dead/missing tab --- docs/changelog.txt | 1 + plugins/lua/sort.lua | 1 + plugins/lua/sort/deathcause_button.lua | 78 ++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 plugins/lua/sort/deathcause_button.lua diff --git a/docs/changelog.txt b/docs/changelog.txt index 2eb584644f..4dfb1419fe 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -57,6 +57,7 @@ Template for new versions: ## New Tools ## New Features +- `sort`: Add death cause button to dead/missing tab in the creatures screen ## Fixes diff --git a/plugins/lua/sort.lua b/plugins/lua/sort.lua index bdad4ae90e..a81bff5beb 100644 --- a/plugins/lua/sort.lua +++ b/plugins/lua/sort.lua @@ -1294,6 +1294,7 @@ OVERLAY_WIDGETS = { candidates=require('plugins.sort.info').CandidatesOverlay, interrogation=require('plugins.sort.info').InterrogationOverlay, conviction=require('plugins.sort.info').ConvictionOverlay, + deathcause_button=require('plugins.sort.deathcause_button').DeathCauseOverlay, location_selector=require('plugins.sort.locationselector').LocationSelectorOverlay, -- TODO: maybe rewrite for 50.12 -- burrow_assignment=require('plugins.sort.unitselector').BurrowAssignmentOverlay, diff --git a/plugins/lua/sort/deathcause_button.lua b/plugins/lua/sort/deathcause_button.lua new file mode 100644 index 0000000000..e5f424c2a9 --- /dev/null +++ b/plugins/lua/sort/deathcause_button.lua @@ -0,0 +1,78 @@ +local _ENV = mkmodule('plugins.sort.deathcause_button') + +local dialogs = require('gui.dialogs') +local overlay = require('plugins.overlay') +local widgets = require('gui.widgets') + +DeathCauseOverlay = defclass(DeathCauseOverlay, overlay.OverlayWidget) +DeathCauseOverlay.ATTRS{ + desc='Adds a button to view death cause on the dead/missing tab.', + default_pos={x=50, y=-7}, + default_enabled=true, + viewscreens='dwarfmode/Info/CREATURES/DECEASED', + frame={w=21, h=1}, +} + +function DeathCauseOverlay:init() + local deathcause = reqscript('deathcause') + + local function get_selected_unit() + -- Navigate to the creatures/deceased widget hierarchy: + -- list_widget - the main deceased list + -- ├─ children[0]: scrollbar widget + -- └─ children[1]: container widget (list_container) + -- ├─ grandchildren[0]: header + -- ├─ grandchildren[1]: header or other UI + -- └─ grandchildren[2]: scrollable rows container (scrollable_list) + -- └─ rows: row widgets (each row = one unit in the list) + -- └─ row.children[x]: unit widget + -- └─ unit_widget.u: pointer to the df.unit object + + local creatures = df.global.game.main_interface.info.creatures + local list_widget = dfhack.gui.getWidget(creatures, 'Tabs', 'Dead/Missing') + if not list_widget then return nil end + + local children = dfhack.gui.getWidgetChildren(list_widget) + local list_container = children[1] + local grandchildren = dfhack.gui.getWidgetChildren(list_container) + + local scrollable_list = grandchildren[2] + if not scrollable_list then + return nil + end + + local rows = dfhack.gui.getWidgetChildren(scrollable_list) + + local cursor_idx = list_widget.cursor_idx or 0 + + if cursor_idx >= 0 and cursor_idx < #rows then + local row = rows[cursor_idx + 1] + + local ok, unit = pcall(function() return dfhack.gui.getWidget(row, 0).u end) + if ok and unit then + return unit + end + end + + return nil + end + + self:addviews{ + widgets.TextButton{ + frame={t=0, l=0}, + label='Show death cause', + key='CUSTOM_D', + on_activate=function() + local unit = get_selected_unit() + if not unit then + dialogs.showMessage('Death Cause', 'No unit selected.') + return + end + local cause = deathcause.getDeathCause(unit) + dialogs.showMessage('Death Cause', dfhack.df2console(cause)) + end, + }, + } +end + +return _ENV From 9e916367761ca33cc6a1eabe08997710888a949c Mon Sep 17 00:00:00 2001 From: pajawojciech Date: Tue, 6 Jan 2026 20:54:12 +0100 Subject: [PATCH 2/2] Trim trailing whitespace --- plugins/lua/sort/deathcause_button.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/lua/sort/deathcause_button.lua b/plugins/lua/sort/deathcause_button.lua index e5f424c2a9..b149601a7a 100644 --- a/plugins/lua/sort/deathcause_button.lua +++ b/plugins/lua/sort/deathcause_button.lua @@ -31,20 +31,20 @@ function DeathCauseOverlay:init() local creatures = df.global.game.main_interface.info.creatures local list_widget = dfhack.gui.getWidget(creatures, 'Tabs', 'Dead/Missing') if not list_widget then return nil end - + local children = dfhack.gui.getWidgetChildren(list_widget) local list_container = children[1] local grandchildren = dfhack.gui.getWidgetChildren(list_container) - + local scrollable_list = grandchildren[2] if not scrollable_list then return nil end - + local rows = dfhack.gui.getWidgetChildren(scrollable_list) - + local cursor_idx = list_widget.cursor_idx or 0 - + if cursor_idx >= 0 and cursor_idx < #rows then local row = rows[cursor_idx + 1]