refactored stations

This commit is contained in:
monica
2023-01-14 23:13:02 -05:00
parent 0d829257f0
commit 90dc69bd02

View File

@@ -1,10 +1,8 @@
local gui = require("__flib__.gui") local gui = require("__flib__.gui-lite")
local constants = require("constants") local constants = require("scripts.gui.constants")
local util = require("scripts.gui.util")
local util = require("scripts.util") local templates = require("scripts.gui.templates")
local templates = require("templates")
local stations_tab = {} local stations_tab = {}
@@ -72,6 +70,11 @@ function stations_tab.build(map_data, player_data)
local stations_sorted = {} local stations_sorted = {}
local to_sorted_manifest = {} local to_sorted_manifest = {}
for id, station in pairs(map_data.stations) do for id, station in pairs(map_data.stations) do
local entity = station.entity_stop
if not entity.valid then
goto continue
end
if search_network_name then if search_network_name then
if search_network_name ~= station.network_name then if search_network_name ~= station.network_name then
goto continue goto continue
@@ -96,10 +99,6 @@ function stations_tab.build(map_data, player_data)
end end
if search_surface_idx then if search_surface_idx then
local entity = station.entity_stop
if not entity.valid then
goto continue
end
if entity.surface.index ~= search_surface_idx then if entity.surface.index ~= search_surface_idx then
goto continue goto continue
end end
@@ -119,22 +118,6 @@ function stations_tab.build(map_data, player_data)
end end
stations_sorted[#stations_sorted + 1] = id stations_sorted[#stations_sorted + 1] = id
--insertion sort
local manifest = {}
local manifest_type = {}
for name, _ in pairs(station.deliveries) do
local is_fluid = get_is_fluid(name)
local i = 1
while i <= #manifest do
if (not is_fluid and manifest_type[i]) or (is_fluid == manifest_type[i] and name < manifest[i]) then
break
end
i = i + 1
end
table.insert(manifest, i, name)
table.insert(manifest_type, i, is_fluid)
end
to_sorted_manifest[id] = manifest
::continue:: ::continue::
end end
@@ -199,56 +182,14 @@ function stations_tab.build(map_data, player_data)
return (not player_data.trains_orderings_invert[#player_data.trains_orderings_invert]) == (a < b) return (not player_data.trains_orderings_invert[#player_data.trains_orderings_invert]) == (a < b)
end) end)
end
function stations_tab.update(self)
local refs = self.refs.stations
local widths = self.widths.stations
local search_query = state.search_query
local search_network_id = state.network_id
local search_surface = state.surface
local ltn_stations = state.ltn_data.stations
local scroll_pane = refs.scroll_pane local scroll_pane = refs.scroll_pane
local children = scroll_pane.children
local sorts = state.sorts.stations
local active_sort = sorts._active
local sorted_stations = state.ltn_data.sorted_stations[active_sort]
local table_index = 0 for i, station_id in pairs(stations_sorted) do
local station = map_data.stations[station_id]
-- False = ascending (arrow down), True = descending (arrow up) local color = i % 2 == 0 and "dark" or "light"
local start, finish, step local row = gui.add(scroll_pane, {
if sorts[active_sort] then
start = #sorted_stations
finish = 1
step = -1
else
start = 1
finish = #sorted_stations
step = 1
end
for sorted_index = start, finish, step do
local station_id = sorted_stations[sorted_index]
local station_data = ltn_stations[station_id]
if station_data.entity.valid then
if
(search_surface == -1 or station_data.entity.surface.index == search_surface)
and bit32.btest(station_data.network_id, search_network_id)
and (
#search_query == 0 or string.find(station_data.search_strings[self.player.index], string.lower(search_query))
)
then
table_index = table_index + 1
local row = children[table_index]
local color = table_index % 2 == 0 and "dark" or "light"
if not row then
row = gui.add(scroll_pane, {
type = "frame", type = "frame",
style = "ltnm_table_row_frame_" .. color, style = "ltnm_table_row_frame_" .. color,
{ {
@@ -263,47 +204,36 @@ function stations_tab.update(self)
templates.small_slot_table(widths, color, "shipments"), templates.small_slot_table(widths, color, "shipments"),
templates.small_slot_table(widths, color, "control_signals"), templates.small_slot_table(widths, color, "control_signals"),
}) })
end
gui.update(row, { gui.add(row, {
{ {
elem_mods = { caption = station_data.name }, elem_mods = { caption = station.entity_stop.name },
actions = { handler = stations_tab.hande.open_station_gui,
on_click = { gui = "main", action = "open_station_gui", station_id = station_id }, tags = station_id,
}, },
}, { elem_mods = { caption = station.network_name } },
{ { elem_mods = { caption = station.network_flag } },
{ elem_mods = { sprite = "flib_indicator_" .. station_data.status.color } },
{ elem_mods = { caption = station_data.status.count } },
},
{ elem_mods = { caption = station_data.network_id } },
}) })
util.slot_table_update(row.provided_requested_frame.provided_requested_table, { util.slot_table_update(row.provided_requested_frame.provided_requested_table, {
{ color = "green", entries = station_data.provided, translations = dictionaries.materials }, { color = "green", entries = station.provided, translations = dictionaries.materials },
{ color = "red", entries = station_data.requested, translations = dictionaries.materials }, { color = "red", entries = station.requested, translations = dictionaries.materials },
}) })
util.slot_table_update(row.shipments_frame.shipments_table, { util.slot_table_update(row.shipments_frame.shipments_table, {
{ color = "green", entries = station_data.inbound, translations = dictionaries.materials }, { color = "green", entries = station.inbound, translations = dictionaries.materials },
{ color = "blue", entries = station_data.outbound, translations = dictionaries.materials }, { color = "blue", entries = station.outbound, translations = dictionaries.materials },
}) })
util.slot_table_update(row.control_signals_frame.control_signals_table, { util.slot_table_update(row.control_signals_frame.control_signals_table, {
{ {
color = "default", color = "default",
entries = station_data.control_signals, entries = station.control_signals,
translations = dictionaries.virtual_signals, translations = dictionaries.virtual_signals,
type = "virtual-signal", type = "virtual-signal",
}, },
}) })
end end
end
end
for child_index = table_index + 1, #children do if #stations_sorted == 0 then
children[child_index].destroy()
end
if table_index == 0 then
refs.warning_flow.visible = true refs.warning_flow.visible = true
scroll_pane.visible = false scroll_pane.visible = false
refs.content_frame.style = "ltnm_main_warning_frame" refs.content_frame.style = "ltnm_main_warning_frame"
@@ -314,4 +244,10 @@ else
end end
end end
stations_tab.hande = {}
function stations_tab.hande.open_station_gui()
end
return stations_tab return stations_tab