|
| 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