mirror of
https://github.com/Xevion/project-cybersyn.git
synced 2025-12-09 16:08:11 -06:00
refactored stations
This commit is contained in:
@@ -1,58 +1,56 @@
|
|||||||
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 = {}
|
||||||
|
|
||||||
function stations_tab.build(widths)
|
function stations_tab.build(widths)
|
||||||
return {
|
return {
|
||||||
tab = {
|
tab = {
|
||||||
type = "tab",
|
type = "tab",
|
||||||
caption = { "gui.ltnm-stations" },
|
caption = { "gui.ltnm-stations" },
|
||||||
ref = { "stations", "tab" },
|
ref = { "stations", "tab" },
|
||||||
actions = {
|
actions = {
|
||||||
on_click = { gui = "main", action = "change_tab", tab = "stations" },
|
on_click = { gui = "main", action = "change_tab", tab = "stations" },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
content = {
|
content = {
|
||||||
type = "frame",
|
type = "frame",
|
||||||
style = "ltnm_main_content_frame",
|
style = "ltnm_main_content_frame",
|
||||||
direction = "vertical",
|
direction = "vertical",
|
||||||
ref = { "stations", "content_frame" },
|
ref = { "stations", "content_frame" },
|
||||||
{
|
{
|
||||||
type = "frame",
|
type = "frame",
|
||||||
style = "ltnm_table_toolbar_frame",
|
style = "ltnm_table_toolbar_frame",
|
||||||
templates.sort_checkbox(widths, "stations", "name", true),
|
templates.sort_checkbox(widths, "stations", "name", true),
|
||||||
templates.sort_checkbox(widths, "stations", "status", false, { "gui.ltnm-status-description" }),
|
templates.sort_checkbox(widths, "stations", "status", false, { "gui.ltnm-status-description" }),
|
||||||
templates.sort_checkbox(widths, "stations", "network_id", false),
|
templates.sort_checkbox(widths, "stations", "network_id", false),
|
||||||
templates.sort_checkbox(
|
templates.sort_checkbox(
|
||||||
widths,
|
widths,
|
||||||
"stations",
|
"stations",
|
||||||
"provided_requested",
|
"provided_requested",
|
||||||
false,
|
false,
|
||||||
{ "gui.ltnm-provided-requested-description" }
|
{ "gui.ltnm-provided-requested-description" }
|
||||||
),
|
),
|
||||||
templates.sort_checkbox(widths, "stations", "shipments", false, { "gui.ltnm-shipments-description" }),
|
templates.sort_checkbox(widths, "stations", "shipments", false, { "gui.ltnm-shipments-description" }),
|
||||||
templates.sort_checkbox(widths, "stations", "control_signals", false),
|
templates.sort_checkbox(widths, "stations", "control_signals", false),
|
||||||
},
|
},
|
||||||
{ type = "scroll-pane", style = "ltnm_table_scroll_pane", ref = { "stations", "scroll_pane" } },
|
{ type = "scroll-pane", style = "ltnm_table_scroll_pane", ref = { "stations", "scroll_pane" } },
|
||||||
{
|
{
|
||||||
type = "flow",
|
type = "flow",
|
||||||
style = "ltnm_warning_flow",
|
style = "ltnm_warning_flow",
|
||||||
visible = false,
|
visible = false,
|
||||||
ref = { "stations", "warning_flow" },
|
ref = { "stations", "warning_flow" },
|
||||||
{
|
{
|
||||||
type = "label",
|
type = "label",
|
||||||
style = "ltnm_semibold_label",
|
style = "ltnm_semibold_label",
|
||||||
caption = { "gui.ltnm-no-stations" },
|
caption = { "gui.ltnm-no-stations" },
|
||||||
ref = { "stations", "warning_label" },
|
ref = { "stations", "warning_label" },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -61,257 +59,195 @@ end
|
|||||||
--- @return GuiElemDef
|
--- @return GuiElemDef
|
||||||
function stations_tab.build(map_data, player_data)
|
function stations_tab.build(map_data, player_data)
|
||||||
|
|
||||||
local widths = constants.gui["en"]
|
local widths = constants.gui["en"]
|
||||||
|
|
||||||
local search_item = player_data.search_item
|
local search_item = player_data.search_item
|
||||||
local search_network_name = player_data.search_network_name
|
local search_network_name = player_data.search_network_name
|
||||||
local search_network_mask = player_data.search_network_mask
|
local search_network_mask = player_data.search_network_mask
|
||||||
local search_surface_idx = player_data.search_surface_idx
|
local search_surface_idx = player_data.search_surface_idx
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
if search_network_name then
|
local entity = station.entity_stop
|
||||||
if search_network_name ~= station.network_name then
|
if not entity.valid then
|
||||||
goto continue
|
goto continue
|
||||||
end
|
end
|
||||||
local train_flag = get_network_flag(station, search_network_name)
|
|
||||||
if not bit32.btest(search_network_mask, train_flag) then
|
|
||||||
goto continue
|
|
||||||
end
|
|
||||||
elseif search_network_mask ~= -1 then
|
|
||||||
if station.network_name == NETWORK_EACH then
|
|
||||||
local masks = station.network_flag--[[@as {}]]
|
|
||||||
for _, network_flag in pairs(masks) do
|
|
||||||
if bit32.btest(search_network_mask, network_flag) then
|
|
||||||
goto has_match
|
|
||||||
end
|
|
||||||
end
|
|
||||||
goto continue
|
|
||||||
::has_match::
|
|
||||||
elseif not bit32.btest(search_network_mask, station.network_flag) then
|
|
||||||
goto continue
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if search_surface_idx then
|
if search_network_name then
|
||||||
local entity = station.entity_stop
|
if search_network_name ~= station.network_name then
|
||||||
if not entity.valid then
|
goto continue
|
||||||
goto continue
|
end
|
||||||
end
|
local train_flag = get_network_flag(station, search_network_name)
|
||||||
if entity.surface.index ~= search_surface_idx then
|
if not bit32.btest(search_network_mask, train_flag) then
|
||||||
goto continue
|
goto continue
|
||||||
end
|
end
|
||||||
end
|
elseif search_network_mask ~= -1 then
|
||||||
|
if station.network_name == NETWORK_EACH then
|
||||||
|
local masks = station.network_flag--[[@as {}]]
|
||||||
|
for _, network_flag in pairs(masks) do
|
||||||
|
if bit32.btest(search_network_mask, network_flag) then
|
||||||
|
goto has_match
|
||||||
|
end
|
||||||
|
end
|
||||||
|
goto continue
|
||||||
|
::has_match::
|
||||||
|
elseif not bit32.btest(search_network_mask, station.network_flag) then
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if search_item then
|
if search_surface_idx then
|
||||||
if not station.deliveries then
|
if entity.surface.index ~= search_surface_idx then
|
||||||
goto continue
|
goto continue
|
||||||
end
|
end
|
||||||
for item_name, _ in pairs(station.deliveries) do
|
end
|
||||||
if item_name == search_item then
|
|
||||||
goto has_match
|
|
||||||
end
|
|
||||||
end
|
|
||||||
goto continue
|
|
||||||
::has_match::
|
|
||||||
end
|
|
||||||
|
|
||||||
stations_sorted[#stations_sorted + 1] = id
|
if search_item then
|
||||||
--insertion sort
|
if not station.deliveries then
|
||||||
local manifest = {}
|
goto continue
|
||||||
local manifest_type = {}
|
end
|
||||||
for name, _ in pairs(station.deliveries) do
|
for item_name, _ in pairs(station.deliveries) do
|
||||||
local is_fluid = get_is_fluid(name)
|
if item_name == search_item then
|
||||||
local i = 1
|
goto has_match
|
||||||
while i <= #manifest do
|
end
|
||||||
if (not is_fluid and manifest_type[i]) or (is_fluid == manifest_type[i] and name < manifest[i]) then
|
end
|
||||||
break
|
goto continue
|
||||||
end
|
::has_match::
|
||||||
i = i + 1
|
end
|
||||||
end
|
|
||||||
table.insert(manifest, i, name)
|
stations_sorted[#stations_sorted + 1] = id
|
||||||
table.insert(manifest_type, i, is_fluid)
|
::continue::
|
||||||
end
|
end
|
||||||
to_sorted_manifest[id] = manifest
|
|
||||||
::continue::
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
table.sort(stations_sorted, function(a, b)
|
table.sort(stations_sorted, function(a, b)
|
||||||
local station1 = map_data.stations[a]
|
local station1 = map_data.stations[a]
|
||||||
local station2 = map_data.stations[b]
|
local station2 = map_data.stations[b]
|
||||||
for i, v in ipairs(player_data.trains_orderings) do
|
for i, v in ipairs(player_data.trains_orderings) do
|
||||||
local invert = player_data.trains_orderings_invert[i]
|
local invert = player_data.trains_orderings_invert[i]
|
||||||
if v == ORDER_LAYOUT then
|
if v == ORDER_LAYOUT then
|
||||||
if not station1.allows_all_trains and not station2.allows_all_trains then
|
if not station1.allows_all_trains and not station2.allows_all_trains then
|
||||||
local layout1 = station1.layout_pattern--[[@as uint[] ]]
|
local layout1 = station1.layout_pattern--[[@as uint[] ]]
|
||||||
local layout2 = station2.layout_pattern--[[@as uint[] ]]
|
local layout2 = station2.layout_pattern--[[@as uint[] ]]
|
||||||
for j, c1 in ipairs(layout1) do
|
for j, c1 in ipairs(layout1) do
|
||||||
local c2 = layout2[j]
|
local c2 = layout2[j]
|
||||||
if c1 ~= c2 then
|
if c1 ~= c2 then
|
||||||
return invert ~= (c2 and c1 < c2)
|
return invert ~= (c2 and c1 < c2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if layout2[#layout1 + 1] then
|
if layout2[#layout1 + 1] then
|
||||||
return invert ~= true
|
return invert ~= true
|
||||||
end
|
end
|
||||||
elseif station1.allows_all_trains ~= station2.allows_all_trains then
|
elseif station1.allows_all_trains ~= station2.allows_all_trains then
|
||||||
return invert ~= station2.allows_all_trains
|
return invert ~= station2.allows_all_trains
|
||||||
end
|
end
|
||||||
elseif v == ORDER_NAME then
|
elseif v == ORDER_NAME then
|
||||||
local name1 = station1.entity_stop.valid and station1.entity_stop.backer_name
|
local name1 = station1.entity_stop.valid and station1.entity_stop.backer_name
|
||||||
local name2 = station2.entity_stop.valid and station2.entity_stop.backer_name
|
local name2 = station2.entity_stop.valid and station2.entity_stop.backer_name
|
||||||
if name1 ~= name2 then
|
if name1 ~= name2 then
|
||||||
return invert ~= (name1 and (name2 and name1 < name2 or true) or false)
|
return invert ~= (name1 and (name2 and name1 < name2 or true) or false)
|
||||||
end
|
end
|
||||||
elseif v == ORDER_TOTAL_TRAINS then
|
elseif v == ORDER_TOTAL_TRAINS then
|
||||||
if station1.deliveries_total ~= station2.deliveries_total then
|
if station1.deliveries_total ~= station2.deliveries_total then
|
||||||
return invert ~= (station1.deliveries_total < station2.deliveries_total)
|
return invert ~= (station1.deliveries_total < station2.deliveries_total)
|
||||||
end
|
end
|
||||||
elseif v == ORDER_MANIFEST then
|
elseif v == ORDER_MANIFEST then
|
||||||
if not next(station1.deliveries) then
|
if not next(station1.deliveries) then
|
||||||
if next(station2.deliveries) then
|
if next(station2.deliveries) then
|
||||||
return invert ~= true
|
return invert ~= true
|
||||||
end
|
end
|
||||||
elseif not next(station2.deliveries) then
|
elseif not next(station2.deliveries) then
|
||||||
return invert ~= false
|
return invert ~= false
|
||||||
else
|
else
|
||||||
local first_item = nil
|
local first_item = nil
|
||||||
local first_direction = nil
|
local first_direction = nil
|
||||||
for item_name in dual_pairs(station1.deliveries, station2.deliveries) do
|
for item_name in dual_pairs(station1.deliveries, station2.deliveries) do
|
||||||
if not first_item or item_lt(map_data.manager, item_name, first_item) then
|
if not first_item or item_lt(map_data.manager, item_name, first_item) then
|
||||||
local count1 = station1.deliveries[item_name] or 0
|
local count1 = station1.deliveries[item_name] or 0
|
||||||
local count2 = station2.deliveries[item_name] or 0
|
local count2 = station2.deliveries[item_name] or 0
|
||||||
if count1 ~= count2 then
|
if count1 ~= count2 then
|
||||||
first_item = item_name
|
first_item = item_name
|
||||||
first_direction = count1 < count2
|
first_direction = count1 < count2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if first_direction ~= nil then
|
if first_direction ~= nil then
|
||||||
return invert ~= first_direction
|
return invert ~= first_direction
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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)
|
||||||
|
|
||||||
|
local scroll_pane = refs.scroll_pane
|
||||||
|
|
||||||
|
|
||||||
|
for i, station_id in pairs(stations_sorted) do
|
||||||
|
local station = map_data.stations[station_id]
|
||||||
|
|
||||||
|
local color = i % 2 == 0 and "dark" or "light"
|
||||||
|
local row = gui.add(scroll_pane, {
|
||||||
|
type = "frame",
|
||||||
|
style = "ltnm_table_row_frame_" .. color,
|
||||||
|
{
|
||||||
|
type = "label",
|
||||||
|
style = "ltnm_clickable_semibold_label",
|
||||||
|
style_mods = { width = widths.name },
|
||||||
|
tooltip = constants.open_station_gui_tooltip,
|
||||||
|
},
|
||||||
|
templates.status_indicator(widths.status, true),
|
||||||
|
{ type = "label", style_mods = { width = widths.network_id, horizontal_align = "center" } },
|
||||||
|
templates.small_slot_table(widths, color, "provided_requested"),
|
||||||
|
templates.small_slot_table(widths, color, "shipments"),
|
||||||
|
templates.small_slot_table(widths, color, "control_signals"),
|
||||||
|
})
|
||||||
|
|
||||||
|
gui.add(row, {
|
||||||
|
{
|
||||||
|
elem_mods = { caption = station.entity_stop.name },
|
||||||
|
handler = stations_tab.hande.open_station_gui,
|
||||||
|
tags = station_id,
|
||||||
|
},
|
||||||
|
{ elem_mods = { caption = station.network_name } },
|
||||||
|
{ elem_mods = { caption = station.network_flag } },
|
||||||
|
})
|
||||||
|
|
||||||
|
util.slot_table_update(row.provided_requested_frame.provided_requested_table, {
|
||||||
|
{ color = "green", entries = station.provided, translations = dictionaries.materials },
|
||||||
|
{ color = "red", entries = station.requested, translations = dictionaries.materials },
|
||||||
|
})
|
||||||
|
util.slot_table_update(row.shipments_frame.shipments_table, {
|
||||||
|
{ color = "green", entries = station.inbound, translations = dictionaries.materials },
|
||||||
|
{ color = "blue", entries = station.outbound, translations = dictionaries.materials },
|
||||||
|
})
|
||||||
|
util.slot_table_update(row.control_signals_frame.control_signals_table, {
|
||||||
|
{
|
||||||
|
color = "default",
|
||||||
|
entries = station.control_signals,
|
||||||
|
translations = dictionaries.virtual_signals,
|
||||||
|
type = "virtual-signal",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
if #stations_sorted == 0 then
|
||||||
|
refs.warning_flow.visible = true
|
||||||
|
scroll_pane.visible = false
|
||||||
|
refs.content_frame.style = "ltnm_main_warning_frame"
|
||||||
|
else
|
||||||
|
refs.warning_flow.visible = false
|
||||||
|
scroll_pane.visible = true
|
||||||
|
refs.content_frame.style = "ltnm_main_content_frame"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function stations_tab.update(self)
|
|
||||||
local refs = self.refs.stations
|
|
||||||
local widths = self.widths.stations
|
|
||||||
|
|
||||||
local search_query = state.search_query
|
stations_tab.hande = {}
|
||||||
local search_network_id = state.network_id
|
|
||||||
local search_surface = state.surface
|
|
||||||
|
|
||||||
local ltn_stations = state.ltn_data.stations
|
function stations_tab.hande.open_station_gui()
|
||||||
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
|
|
||||||
|
|
||||||
-- False = ascending (arrow down), True = descending (arrow up)
|
|
||||||
local start, finish, step
|
|
||||||
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",
|
|
||||||
style = "ltnm_table_row_frame_" .. color,
|
|
||||||
{
|
|
||||||
type = "label",
|
|
||||||
style = "ltnm_clickable_semibold_label",
|
|
||||||
style_mods = { width = widths.name },
|
|
||||||
tooltip = constants.open_station_gui_tooltip,
|
|
||||||
},
|
|
||||||
templates.status_indicator(widths.status, true),
|
|
||||||
{ type = "label", style_mods = { width = widths.network_id, horizontal_align = "center" } },
|
|
||||||
templates.small_slot_table(widths, color, "provided_requested"),
|
|
||||||
templates.small_slot_table(widths, color, "shipments"),
|
|
||||||
templates.small_slot_table(widths, color, "control_signals"),
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
gui.update(row, {
|
|
||||||
{
|
|
||||||
elem_mods = { caption = station_data.name },
|
|
||||||
actions = {
|
|
||||||
on_click = { gui = "main", action = "open_station_gui", station_id = station_id },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
{ 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, {
|
|
||||||
{ color = "green", entries = station_data.provided, translations = dictionaries.materials },
|
|
||||||
{ color = "red", entries = station_data.requested, translations = dictionaries.materials },
|
|
||||||
})
|
|
||||||
util.slot_table_update(row.shipments_frame.shipments_table, {
|
|
||||||
{ color = "green", entries = station_data.inbound, translations = dictionaries.materials },
|
|
||||||
{ color = "blue", entries = station_data.outbound, translations = dictionaries.materials },
|
|
||||||
})
|
|
||||||
util.slot_table_update(row.control_signals_frame.control_signals_table, {
|
|
||||||
{
|
|
||||||
color = "default",
|
|
||||||
entries = station_data.control_signals,
|
|
||||||
translations = dictionaries.virtual_signals,
|
|
||||||
type = "virtual-signal",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for child_index = table_index + 1, #children do
|
|
||||||
children[child_index].destroy()
|
|
||||||
end
|
|
||||||
|
|
||||||
if table_index == 0 then
|
|
||||||
refs.warning_flow.visible = true
|
|
||||||
scroll_pane.visible = false
|
|
||||||
refs.content_frame.style = "ltnm_main_warning_frame"
|
|
||||||
else
|
|
||||||
refs.warning_flow.visible = false
|
|
||||||
scroll_pane.visible = true
|
|
||||||
refs.content_frame.style = "ltnm_main_content_frame"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return stations_tab
|
return stations_tab
|
||||||
|
|||||||
Reference in New Issue
Block a user