Skip to content

Commit a39e8ab

Browse files
authored
Merge pull request #5698 from pajawojciech/death-cause-button
Add death cause button to dead/missing tab
2 parents 5d56bdd + 9e91636 commit a39e8ab

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Template for new versions:
5757
## New Tools
5858

5959
## New Features
60+
- `sort`: Add death cause button to dead/missing tab in the creatures screen
6061

6162
## Fixes
6263

plugins/lua/sort.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,7 @@ OVERLAY_WIDGETS = {
12941294
candidates=require('plugins.sort.info').CandidatesOverlay,
12951295
interrogation=require('plugins.sort.info').InterrogationOverlay,
12961296
conviction=require('plugins.sort.info').ConvictionOverlay,
1297+
deathcause_button=require('plugins.sort.deathcause_button').DeathCauseOverlay,
12971298
location_selector=require('plugins.sort.locationselector').LocationSelectorOverlay,
12981299
-- TODO: maybe rewrite for 50.12
12991300
-- burrow_assignment=require('plugins.sort.unitselector').BurrowAssignmentOverlay,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
local _ENV = mkmodule('plugins.sort.deathcause_button')
2+
3+
local dialogs = require('gui.dialogs')
4+
local overlay = require('plugins.overlay')
5+
local widgets = require('gui.widgets')
6+
7+
DeathCauseOverlay = defclass(DeathCauseOverlay, overlay.OverlayWidget)
8+
DeathCauseOverlay.ATTRS{
9+
desc='Adds a button to view death cause on the dead/missing tab.',
10+
default_pos={x=50, y=-7},
11+
default_enabled=true,
12+
viewscreens='dwarfmode/Info/CREATURES/DECEASED',
13+
frame={w=21, h=1},
14+
}
15+
16+
function DeathCauseOverlay:init()
17+
local deathcause = reqscript('deathcause')
18+
19+
local function get_selected_unit()
20+
-- Navigate to the creatures/deceased widget hierarchy:
21+
-- list_widget - the main deceased list
22+
-- ├─ children[0]: scrollbar widget
23+
-- └─ children[1]: container widget (list_container)
24+
-- ├─ grandchildren[0]: header
25+
-- ├─ grandchildren[1]: header or other UI
26+
-- └─ grandchildren[2]: scrollable rows container (scrollable_list)
27+
-- └─ rows: row widgets (each row = one unit in the list)
28+
-- └─ row.children[x]: unit widget
29+
-- └─ unit_widget.u: pointer to the df.unit object
30+
31+
local creatures = df.global.game.main_interface.info.creatures
32+
local list_widget = dfhack.gui.getWidget(creatures, 'Tabs', 'Dead/Missing')
33+
if not list_widget then return nil end
34+
35+
local children = dfhack.gui.getWidgetChildren(list_widget)
36+
local list_container = children[1]
37+
local grandchildren = dfhack.gui.getWidgetChildren(list_container)
38+
39+
local scrollable_list = grandchildren[2]
40+
if not scrollable_list then
41+
return nil
42+
end
43+
44+
local rows = dfhack.gui.getWidgetChildren(scrollable_list)
45+
46+
local cursor_idx = list_widget.cursor_idx or 0
47+
48+
if cursor_idx >= 0 and cursor_idx < #rows then
49+
local row = rows[cursor_idx + 1]
50+
51+
local ok, unit = pcall(function() return dfhack.gui.getWidget(row, 0).u end)
52+
if ok and unit then
53+
return unit
54+
end
55+
end
56+
57+
return nil
58+
end
59+
60+
self:addviews{
61+
widgets.TextButton{
62+
frame={t=0, l=0},
63+
label='Show death cause',
64+
key='CUSTOM_D',
65+
on_activate=function()
66+
local unit = get_selected_unit()
67+
if not unit then
68+
dialogs.showMessage('Death Cause', 'No unit selected.')
69+
return
70+
end
71+
local cause = deathcause.getDeathCause(unit)
72+
dialogs.showMessage('Death Cause', dfhack.df2console(cause))
73+
end,
74+
},
75+
}
76+
end
77+
78+
return _ENV

0 commit comments

Comments
 (0)