Skip to content

Commit d5e1bbc

Browse files
committed
Add option to configure all listed siege engines at once
1 parent e56f4c2 commit d5e1bbc

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

gui/siegemanager.lua

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,36 @@ local function get_siege_engines()
202202
return siege_list
203203
end
204204

205+
local function item_in_list(item, list)
206+
for _, v in ipairs(list) do
207+
if v == item then
208+
return true
209+
end
210+
end
211+
return false
212+
end
213+
205214
-- Set siegeengine action, returning false if the building isn't found
206-
local function set_siege_engine_action(id, action)
215+
local function set_siege_engine_action(id_list, action)
216+
local count = 0
207217
for _, building in ipairs(df.global.world.buildings.other.IN_PLAY) do
208-
if building.id == id then
218+
if item_in_list(building.id, id_list) then
209219
if not df.building_siegeenginest:is_instance(building) then return false end
210220
building.action = action
211-
return true
221+
count = count + 1
222+
if count == #id_list then
223+
return count
224+
end
212225
end
213226
end
214-
return false
227+
return count
215228
end
216229

217230
-- SiegeEngineList
218231
SiegeEngineList = defclass(SiegeEngineList, widgets.Panel)
219232
SiegeEngineList.ATTRS = {
220233
view_id='list',
221-
frame={l=0, r=0, t=1, b=5},
234+
frame={l=0, r=0, t=1, b=7},
222235
frame_style=gui.FRAME_INTERIOR,
223236

224237
-- Filters by siegeengine_type, -1 being all
@@ -383,10 +396,27 @@ function SiegeEngineList:reveal_selected()
383396
end
384397
end
385398

399+
function SiegeEngineList:set_all_action(action)
400+
local listed = {}
401+
for key, _ in pairs(self.engines) do
402+
listed[#listed+1] = key
403+
end
404+
405+
local count = set_siege_engine_action(listed, action)
406+
if count ~= #listed then
407+
self:refresh_view(true)
408+
return
409+
end
410+
411+
for _, engine in ipairs(self.engines) do
412+
engine.action = action
413+
end
414+
end
415+
386416
function SiegeEngineList:set_selected_action(action)
387417
local _, selected = self.subviews.list:getSelected()
388418

389-
local successful = set_siege_engine_action(selected.data, action)
419+
local successful = set_siege_engine_action({selected.data}, action)
390420
if not successful then
391421
self:refresh_view(true)
392422
return
@@ -449,7 +479,7 @@ function SiegeManager:init()
449479
self:addviews({
450480
SiegeEngineList {},
451481
widgets.CycleHotkeyLabel {
452-
frame={b=3},
482+
frame={b=6},
453483
key='CUSTOM_T',
454484
on_change=self:callback('set_type_filter'),
455485
label='Show Types:',
@@ -461,6 +491,14 @@ function SiegeManager:init()
461491
},
462492
initial_option=1,
463493
},
494+
widgets.ToggleHotkeyLabel {
495+
view_id = 'configure_all',
496+
frame={b=2},
497+
key = 'CUSTOM_SHIFT_A',
498+
key_sep = ': ',
499+
label = 'Configure All',
500+
initial_option=2,
501+
},
464502
widgets.HotkeyLabel {
465503
frame={b=0},
466504
key='CUSTOM_CTRL_C',
@@ -472,7 +510,7 @@ function SiegeManager:init()
472510
for i, action_button in ipairs(action_button_order) do
473511
self:addviews({
474512
widgets.HotkeyLabel {
475-
frame = {b=1, l=(i - 1)*2},
513+
frame = {b=3, l=(i - 1)*2},
476514
key = action_button_keybinds[i],
477515
key_sep = i == #action_button_order and ': ' or '',
478516
label = i == #action_button_order and 'Set Action' or '',
@@ -492,7 +530,11 @@ function SiegeManager:reveal_selected()
492530
end
493531

494532
function SiegeManager:set_action(action)
495-
self.subviews.list:set_selected_action(action)
533+
if self.subviews.configure_all:getOptionValue() then
534+
self.subviews.list:set_all_action(action)
535+
else
536+
self.subviews.list:set_selected_action(action)
537+
end
496538
end
497539

498540
-- SiegeManagerScreen

0 commit comments

Comments
 (0)