Merge pull request #166 from Zoryn4163/main

Add allow-list rescan button and display the allow-list numbers on the UI
This commit is contained in:
Tetlanesh
2024-10-23 17:17:30 +02:00
committed by GitHub
4 changed files with 74 additions and 0 deletions

View File

@@ -10,3 +10,4 @@ require("scripts.gui")
require("scripts.migrations") require("scripts.migrations")
require("scripts.main") require("scripts.main")
require("scripts.remote-interface") require("scripts.remote-interface")
require("scripts.gui-events")

View File

@@ -88,6 +88,10 @@ network=Network
network-tooltip=A signal is used to identify which network this combinator is a member of. Trains will only be dispatched from depots to provider and requester stations if they are all identified with the same signal. network-tooltip=A signal is used to identify which network this combinator is a member of. Trains will only be dispatched from depots to provider and requester stations if they are all identified with the same signal.
allow-list-description=Automatic allow-list allow-list-description=Automatic allow-list
allow-list-tooltip=Normally when a delivery is requested, any train with a large enough cargo capacity may fulfill it. When this setting is checked trains must also be on this station's "allow-list". Trains are automatically added to the allow-list if each of its wagons are able to be loaded or unloaded by inserters or pumps along this station. allow-list-tooltip=Normally when a delivery is requested, any train with a large enough cargo capacity may fulfill it. When this setting is checked trains must also be on this station's "allow-list". Trains are automatically added to the allow-list if each of its wagons are able to be loaded or unloaded by inserters or pumps along this station.
allow-list-refresh-description=Refresh allow-list
allow-list-refresh-tooltip=The allow-list is defined upon placement of the combinator based on the entities around it. As this is an expensive operation, it is seldom performed outside of this event. This button will cause this combinator to immediately recalculate its allow-list.
allow-list-preview=Allow List
allow-list-preview-tooltip=This is the list describing what trains are allowed to dock at this station.\n0 = Locomotive\n1 = Item\n2 = Fluid\n3 = Both
is-stack-description=Stack thresholds is-stack-description=Stack thresholds
is-stack-tooltip=When checked all request thresholds for this station are interpreted as a count of item stacks rather than a count of total items. Thresholds for fluids are unaffected. is-stack-tooltip=When checked all request thresholds for this station are interpreted as a count of item stacks rather than a count of total items. Thresholds for fluids are unaffected.
enable-inactive-description=Inactivity condition enable-inactive-description=Inactivity condition

View File

@@ -0,0 +1,10 @@
script.on_event(defines.events.on_gui_click, function(event)
if event.element.name ~= "allow_list_refresh" then return end
--game.print(serpent.block(event.element.tags))
-- < function interface.reset_stop_layout(stop_id, forbidden_entity, force_update)
local combId = event.element.tags.id
local stopId = storage.to_stop[combId].unit_number
remote.call("cybersyn", "reset_stop_layout", stopId, nil, true)
update_allow_list_section(event.player_index, combId)
end)

View File

@@ -100,6 +100,8 @@ local function handle_drop_down(e)
end end
combinator_update(storage, comb) combinator_update(storage, comb)
update_allow_list_section(e.player_index, comb.unit_number)
end end
---@param e EventData.on_gui_switch_state_changed ---@param e EventData.on_gui_switch_state_changed
local function handle_pr_switch(e) local function handle_pr_switch(e)
@@ -150,6 +152,48 @@ local function handle_setting_flip(e)
set_comb_setting(comb, element.tags.bit--[[@as int]], not element.state) set_comb_setting(comb, element.tags.bit--[[@as int]], not element.state)
combinator_update(storage, comb) combinator_update(storage, comb)
update_allow_list_section(e.player_index, comb.unit_number)
end
local function generate_stop_layout(combId)
local targetStop = storage.to_stop[combId]
local stopLayout = ""
if targetStop ~= nil then
local station = storage.stations[targetStop.unit_number]
local refueler = storage.refuelers[targetStop.unit_number]
if station ~= nil then
stopLayout = station.layout_pattern
elseif refueler ~= nil then
stopLayout = refueler.layout_pattern
end
end
--TODO: improve readability
return serpent.line(stopLayout)
end
function get_allow_list_section(player_index)
local player = game.get_player(player_index)
if player.opened.name == "cybersyn-combinator" then
--this WILL crash if the order of elements in the UI is modified
--TODO: make this dynamic based on property names? or store a ref to the layout preview section?
return player.opened.children[2].children[1].children[8]
end
end
function update_allow_list_section(player_index, comb_unit_number)
local layoutSection = get_allow_list_section(player_index)
if layoutSection ~= nil then
local selected_index, signal, switch_state, bits = get_comb_gui_settings(storage.to_comb[comb_unit_number])
--only for Station (1) and Refueler (3)
if ((selected_index == 1 or selected_index == 3) and setting_flip(bits, SETTING_DISABLE_ALLOW_LIST)) then
layoutSection.visible = true
layoutSection.children[2].caption = generate_stop_layout(comb_unit_number)
else
layoutSection.visible = false
end
end
end end
local function on_gui_opened(event) local function on_gui_opened(event)
@@ -196,6 +240,14 @@ function gui_opened(comb, player)
local rootgui = player.gui.screen local rootgui = player.gui.screen
local selected_index, signal, switch_state, bits = get_comb_gui_settings(comb) local selected_index, signal, switch_state, bits = get_comb_gui_settings(comb)
local showLayout = false
local layoutText = ""
--only for Station (1) and Refueler (3)
if ((selected_index == 1 or selected_index == 3) and setting_flip(bits, SETTING_DISABLE_ALLOW_LIST)) then
showLayout = true
layoutText = generate_stop_layout(comb.unit_number)
end
local _, main_window = flib_gui.add(rootgui, { local _, main_window = flib_gui.add(rootgui, {
{type="frame", direction="vertical", name=COMBINATOR_NAME, children={ {type="frame", direction="vertical", name=COMBINATOR_NAME, children={
--title bar --title bar
@@ -242,6 +294,12 @@ function gui_opened(comb, player)
{type="checkbox", name="is_stack", state=setting(bits, SETTING_IS_STACK), handler=handle_setting, tags={id=comb.unit_number, bit=SETTING_IS_STACK}, tooltip={"cybersyn-gui.is-stack-tooltip"}, caption={"cybersyn-gui.is-stack-description"}}, {type="checkbox", name="is_stack", state=setting(bits, SETTING_IS_STACK), handler=handle_setting, tags={id=comb.unit_number, bit=SETTING_IS_STACK}, tooltip={"cybersyn-gui.is-stack-tooltip"}, caption={"cybersyn-gui.is-stack-description"}},
}}, }},
{type="checkbox", name="enable_inactive", state=setting(bits, SETTING_ENABLE_INACTIVE), handler=handle_setting, tags={id=comb.unit_number, bit=SETTING_ENABLE_INACTIVE}, tooltip={"cybersyn-gui.enable-inactive-tooltip"}, caption={"cybersyn-gui.enable-inactive-description"}}, {type="checkbox", name="enable_inactive", state=setting(bits, SETTING_ENABLE_INACTIVE), handler=handle_setting, tags={id=comb.unit_number, bit=SETTING_ENABLE_INACTIVE}, tooltip={"cybersyn-gui.enable-inactive-tooltip"}, caption={"cybersyn-gui.enable-inactive-description"}},
}},
--preview allow list
{type="flow", name="bottom-allowlist", direction="vertical", style_mods={vertical_align="top"}, visible=showLayout, children={
{type="label", name="allow_list_label_title", style="heading_2_label", caption={"cybersyn-gui.allow-list-preview"}, tooltip={"cybersyn-gui.allow-list-preview-tooltip"}, style_mods={top_padding=8}},
{type="label", name="allow_list_label", caption=layoutText, style_mods={top_padding=8}},
{type="button", name="allow_list_refresh", tags={id=comb.unit_number}, tooltip={"cybersyn-gui.allow-list-refresh-tooltip"}, caption={"cybersyn-gui.allow-list-refresh-description"}},
}} }}
}} }}
}} }}
@@ -255,3 +313,4 @@ function gui_opened(comb, player)
set_visibility(main_window, selected_index) set_visibility(main_window, selected_index)
player.opened = main_window player.opened = main_window
end end