Skip to content

Commit 10abfcb

Browse files
committed
add race filter on histfig page
and refactor exportlegends overlays into separate files and fix missed export start when Export XML button is clicked but the user has already navigated to a subpage
1 parent 0471d9e commit 10abfcb

File tree

4 files changed

+255
-103
lines changed

4 files changed

+255
-103
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Template for new versions:
3737
- `caravan`: If you have managed to select an item that is ethically unacceptable to the merchant, an "Ethics warning" badge will now appear next to the "Trade" button. Clicking on the badge will show you which items that you have selected are problematic. The dialog has a button that you can click to deselect the problematic items in the trade list.
3838
- `confirm`: If you have ethically unacceptable items selected for trade, the "Are you sure you want to trade" confirmation will warn you about them
3939
- `quickfort`: ``#zone`` blueprints now integrated with `preserve-rooms` so you can create a zone and automatically assign it to a noble or administrative role
40+
- `exportlegends`: option to filter by race on historical figures page
4041

4142
## Fixes
4243
- `timestream`: ensure child growth events (that is, a child's transition to adulthood) are not skipped; existing "overage" children will be automatically fixed within a year

exportlegends.lua

Lines changed: 20 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
--luacheck-flags: strictsubtype
33
--@ module=true
44

5-
local gui = require('gui')
6-
local overlay = require('plugins.overlay')
5+
local asyncexport = reqscript('internal/exportlegends/asyncexport')
6+
local racefilter = reqscript('internal/exportlegends/racefilter')
77
local script = require('gui.script')
8-
local widgets = require('gui.widgets')
8+
9+
local GLOBAL_KEY = 'exportlegends'
910

1011
-- Get the date of the world as a string
1112
-- Format: "YYYYY-MM-DD"
@@ -42,10 +43,7 @@ local function table_containskey(self, key)
4243
return false
4344
end
4445

45-
progress_item = progress_item or ''
46-
num_done = num_done or -1
47-
num_total = num_total or -1
48-
last_update_ms = 0
46+
local last_update_ms = 0
4947

5048
-- should be frequent enough so that user can still effectively use
5149
-- the vanilla legends UI to browse while export is in progress
@@ -62,12 +60,12 @@ end
6260
--luacheck: skip
6361
local function progress_ipairs(vector, desc, skip_count, interval)
6462
desc = desc or 'item'
65-
progress_item = desc
63+
asyncexport.progress_item = desc
6664
interval = interval or 10000
6765
local cb = ipairs(vector)
6866
return function(vector, k, ...)
6967
if not skip_count then
70-
num_done = num_done + 1
68+
asyncexport.num_done = asyncexport.num_done + 1
7169
end
7270
if k then
7371
if #vector >= interval and (k % interval == 0 or k == #vector - 1) then
@@ -80,7 +78,7 @@ local function progress_ipairs(vector, desc, skip_count, interval)
8078
end
8179

8280
local function make_chunk(name, vector, fn)
83-
num_total = num_total + #vector
81+
asyncexport.num_total = asyncexport.num_total + #vector
8482
return {
8583
name=name,
8684
vector=vector,
@@ -1016,113 +1014,32 @@ local function export_more_legends_xml()
10161014
end
10171015

10181016
local function wrap_export()
1019-
if num_total >= 0 then
1017+
if asyncexport.num_total >= 0 then
10201018
qerror('exportlegends already in progress')
10211019
end
1022-
num_total = 0
1023-
num_done = 0
1024-
progress_item = 'basic info'
1020+
asyncexport.num_total = 0
1021+
asyncexport.num_done = 0
1022+
asyncexport.progress_item = 'basic info'
10251023
yield_if_timeout()
10261024
local ok, err = pcall(export_more_legends_xml)
10271025
if not ok then
10281026
dfhack.printerr(err)
10291027
end
1030-
num_total = -1
1031-
num_done = -1
1032-
progress_item = ''
1033-
end
1034-
1035-
-- -------------------
1036-
-- LegendsOverlay
1037-
--
1038-
1039-
LegendsOverlay = defclass(LegendsOverlay, overlay.OverlayWidget)
1040-
LegendsOverlay.ATTRS{
1041-
desc='Adds extended export progress bar to the legends main screen.',
1042-
default_pos={x=2, y=2},
1043-
default_enabled=true,
1044-
viewscreens='legends/Default',
1045-
frame={w=55, h=5},
1046-
}
1047-
1048-
function LegendsOverlay:init()
1049-
self:addviews{
1050-
widgets.Panel{
1051-
view_id='button_mask',
1052-
frame={t=0, l=0, w=15, h=3},
1053-
},
1054-
widgets.BannerPanel{
1055-
frame={b=0, l=0, r=0, h=1},
1056-
subviews={
1057-
widgets.ToggleHotkeyLabel{
1058-
view_id='do_export',
1059-
frame={t=0, l=1, r=1},
1060-
label='Also export DFHack extended legends data:',
1061-
key='CUSTOM_CTRL_D',
1062-
visible=function() return num_total < 0 end,
1063-
},
1064-
widgets.Label{
1065-
frame={t=0, l=1},
1066-
text={
1067-
'Exporting ',
1068-
{width=27, text=function() return progress_item end},
1069-
' ',
1070-
{text=function() return ('%.2f'):format((num_done * 100) / num_total) end, pen=COLOR_YELLOW},
1071-
'% complete'
1072-
},
1073-
visible=function() return num_total >= 0 end,
1074-
},
1075-
},
1076-
},
1077-
}
1078-
end
1079-
1080-
function LegendsOverlay:onInput(keys)
1081-
if keys._MOUSE_L and num_total < 0 and
1082-
self.subviews.button_mask:getMousePos() and
1083-
self.subviews.do_export:getOptionValue()
1084-
then
1085-
script.start(wrap_export)
1086-
end
1087-
return LegendsOverlay.super.onInput(self, keys)
1028+
asyncexport.reset_state()
10881029
end
10891030

1090-
-- -------------------
1091-
-- DoneMaskOverlay
1092-
--
1093-
1094-
DoneMaskOverlay = defclass(DoneMaskOverlay, overlay.OverlayWidget)
1095-
DoneMaskOverlay.ATTRS{
1096-
desc='Prevents legends mode from being exited while an export is in progress.',
1097-
default_pos={x=-2, y=2},
1098-
default_enabled=true,
1099-
viewscreens='legends',
1100-
frame={w=9, h=3},
1031+
OVERLAY_WIDGETS = {
1032+
export=asyncexport.LegendsOverlay,
1033+
mask=asyncexport.DoneMaskOverlay,
1034+
histfigfilter=racefilter.RaceFilterOverlay,
11011035
}
11021036

1103-
function DoneMaskOverlay:init()
1104-
self:addviews{
1105-
widgets.Panel{
1106-
frame_background=gui.CLEAR_PEN,
1107-
visible=function() return num_total >= 0 end,
1108-
}
1109-
}
1110-
end
1111-
1112-
function DoneMaskOverlay:onInput(keys)
1113-
if num_total >= 0 then
1114-
if keys.LEAVESCREEN or (keys._MOUSE_L and self:getMousePos()) then
1115-
return true
1116-
end
1037+
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
1038+
if sc == SC_VIEWSCREEN_CHANGED and df.viewscreen_choose_game_typest:is_instance(dfhack.gui.getDFViewscreen(true)) then
1039+
asyncexport.reset_state()
11171040
end
1118-
return DoneMaskOverlay.super.onInput(self, keys)
11191041
end
11201042

1121-
OVERLAY_WIDGETS = {
1122-
export=LegendsOverlay,
1123-
mask=DoneMaskOverlay,
1124-
}
1125-
11261043
if dfhack_flags.module then
11271044
return
11281045
end
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
--@module = true
2+
3+
local gui = require('gui')
4+
local overlay = require('plugins.overlay')
5+
local widgets = require('gui.widgets')
6+
7+
progress_item = nil
8+
num_done = nil
9+
num_total = nil
10+
11+
function reset_state()
12+
progress_item = ''
13+
num_done = -1
14+
num_total = -1
15+
end
16+
reset_state()
17+
18+
-- -------------------
19+
-- LegendsOverlay
20+
--
21+
22+
LegendsOverlay = defclass(LegendsOverlay, overlay.OverlayWidget)
23+
LegendsOverlay.ATTRS{
24+
desc='Adds extended export progress bar to the legends main screen.',
25+
default_pos={x=2, y=2},
26+
default_enabled=true,
27+
viewscreens='legends',
28+
frame={w=55, h=5},
29+
}
30+
31+
function LegendsOverlay:init()
32+
self:addviews{
33+
widgets.Panel{
34+
view_id='button_mask',
35+
frame={t=0, l=0, w=15, h=3},
36+
},
37+
widgets.BannerPanel{
38+
frame={b=0, l=0, r=0, h=1},
39+
visible=function() return dfhack.gui.matchFocusString('legends/Default', dfhack.gui.getDFViewscreen(true)) end,
40+
subviews={
41+
widgets.ToggleHotkeyLabel{
42+
view_id='do_export',
43+
frame={t=0, l=1, r=1},
44+
label='Also export DFHack extended legends data:',
45+
key='CUSTOM_CTRL_D',
46+
visible=function() return num_total < 0 end,
47+
},
48+
widgets.Label{
49+
frame={t=0, l=1},
50+
text={
51+
'Exporting ',
52+
{width=27, text=function() return progress_item end},
53+
' ',
54+
{text=function() return ('%.2f'):format((num_done * 100) / num_total) end, pen=COLOR_YELLOW},
55+
'% complete'
56+
},
57+
visible=function() return num_total >= 0 end,
58+
},
59+
},
60+
},
61+
}
62+
end
63+
64+
function LegendsOverlay:onInput(keys)
65+
if keys._MOUSE_L and self.subviews.button_mask:getMousePos() and self.subviews.do_export:getOptionValue() then
66+
if num_total < 0 then
67+
dfhack.run_script('exportlegends')
68+
else
69+
return true
70+
end
71+
end
72+
return LegendsOverlay.super.onInput(self, keys)
73+
end
74+
75+
-- -------------------
76+
-- DoneMaskOverlay
77+
--
78+
79+
DoneMaskOverlay = defclass(DoneMaskOverlay, overlay.OverlayWidget)
80+
DoneMaskOverlay.ATTRS{
81+
desc='Prevents legends mode from being exited while an export is in progress.',
82+
default_pos={x=-2, y=2},
83+
default_enabled=true,
84+
viewscreens='legends',
85+
frame={w=9, h=3},
86+
}
87+
88+
function DoneMaskOverlay:init()
89+
self:addviews{
90+
widgets.Panel{
91+
frame_background=gui.CLEAR_PEN,
92+
visible=function() return num_total >= 0 end,
93+
}
94+
}
95+
end
96+
97+
function DoneMaskOverlay:onInput(keys)
98+
if num_total >= 0 then
99+
if keys.LEAVESCREEN or (keys._MOUSE_L and self:getMousePos()) then
100+
return true
101+
end
102+
end
103+
return DoneMaskOverlay.super.onInput(self, keys)
104+
end

0 commit comments

Comments
 (0)