Trains tab initial testing

This commit is contained in:
Will Berry
2023-03-09 16:45:14 -05:00
parent 118f8c9eb2
commit fd3d62ea9d
4 changed files with 122 additions and 79 deletions

View File

@@ -6,7 +6,7 @@ local constants = require("scripts.gui.constants")
local templates = require("scripts.gui.templates")
local stations_tab = require("scripts.gui.stations")
--local trains_tab = require("scripts.gui.trains")
local trains_tab = require("scripts.gui.trains")
--local depots_tab = require("scripts.gui.depots")
local inventory_tab = require("scripts.gui.inventory")
--local history_tab = require("scripts.gui.history")
@@ -103,6 +103,7 @@ function manager.create(player)
name = "manager_tabbed_pane",
type = "tabbed-pane",
style = "ltnm_tabbed_pane",
trains_tab.create(widths),
stations_tab.create(widths),
inventory_tab.create(),
selected_tab_index = 1,
@@ -162,6 +163,7 @@ function manager.update(map_data, player_data)
manager.build(player_data)
stations_tab.build(map_data, player_data)
inventory_tab.build(map_data, player_data)
trains_tab.build(map_data,player_data)
end

View File

@@ -99,10 +99,6 @@ function stations_tab.build(map_data, player_data)
end
if search_network_name then
--nil matches all?
if search_network_name == nil then
goto has_match
end
if search_network_name ~= station.network_name then
goto continue
end

View File

@@ -1,4 +1,4 @@
local format = require("__flib__.format")
local train_util = require("__flib__.train")
local gui = require("__flib__.gui-lite")
local constants = require("constants")
@@ -8,21 +8,59 @@ local templates = require("scripts.gui.templates")
local trains_tab = {}
function trains_tab.create(widths)
return {
tab = {
name = "manager_trains_tab",
type = "tab",
--caption = #trains_sorted == 0 and { "cybersyn-gui.trains" } or { "cybersyn-gui.trains", #train_list },
caption = { "cybersyn-gui.trains" },
--badge_text = format.number(#ltn_data.sorted_trains.composition),
--handler = trains_tab.handle.change_tab, --on_click
tags = { tab = "trains_tab" },
},
content = {
name = "manager_trains_tab_content_frame",
type = "frame",
style = "ltnm_main_content_frame",
direction = "vertical",
children = {
{
type = "frame",
style = "ltnm_table_toolbar_frame",
templates.sort_checkbox(widths, "trains", "status", false),
templates.sort_checkbox(widths, "trains", "layout", false, { "cybersyn-gui.composition-description" }),
templates.sort_checkbox(widths, "trains", "depot", false),
templates.sort_checkbox(widths, "trains", "shipment", false),
},
{ name = "manager_trains_tab_scroll_pane", type = "scroll-pane", style = "ltnm_table_scroll_pane" },
{
name = "trains_warning_flow",
type = "flow",
style = "ltnm_warning_flow",
},
},
},
}
end
--- @param map_data MapData
--- @param player_data PlayerData
--- @return GuiElemDef
function trains_tab.build(map_data, player_data)
local widths = constants.gui["en"]
local refs = player_data.refs
local search_query = player_data.search_query
local search_item = player_data.search_item
local search_network_name = player_data.search_network_name
local search_network_mask = player_data.search_network_mask
local search_surface_idx = player_data.search_surface_idx
local trains = map_data.trains
local trains_sorted = {}
for id, train in pairs(map_data.trains) do
for id, train in pairs(trains) do
if search_network_name then
if search_network_name ~= train.network_name then
goto continue
@@ -47,6 +85,9 @@ function trains_tab.build(map_data, player_data)
end
if search_surface_idx then
if search_surface_idx == -1 then
goto has_match
end
local entity = get_any_train_entity(train.entity)
if not entity then
goto continue
@@ -54,6 +95,7 @@ function trains_tab.build(map_data, player_data)
if entity.surface.index ~= search_surface_idx then
goto continue
end
::has_match::
end
if search_item then
@@ -126,106 +168,110 @@ function trains_tab.build(map_data, player_data)
return a < b
end)
local scroll_pane = refs.manager_trains_tab_scroll_pane
if next(scroll_pane.children) ~= nil then
refs.manager_trains_tab_scroll_pane.clear()
end
---@type GuiElemDef
local train_list = {}
if #trains_sorted == 0 then
train_list[1] = {
gui.add(scroll_pane, {
type = "label",
style = "ltnm_semibold_label",
caption = { "cybersyn-gui.no-trains" },
}
})
else
for idx, train_id in ipairs(trains_sorted) do
local train = map_data.trains[train_id]
local depot = map_data.depots[train.depot_id]
local depot_name = depot.entity_stop.valid and depot.entity_stop.backer_name or ""
local train_entity = train.entity
local locomotive
if train_entity.locomotives["front_movers"][1] then
locomotive = train_entity.locomotives["front_movers"][1]
else
locomotive = train_entity.locomotives["back_movers"][1]
end
local manifest = {}
if train.manifest ~= nil then
manifest = train.manifest
end
local color = idx % 2 == 0 and "dark" or "light"
train_list[idx] = {
gui.add(scroll_pane, {
type = "frame",
style = "ltnm_table_row_frame_" .. color,
children = {
{
type = "frame",
style = "ltnm_table_inset_frame_" .. color,
children = {
{
type = "minimap",
name = "train_minimap",
style = "ltnm_train_minimap",
{ type = "label", style = "ltnm_minimap_label" },
{
type = "button",
style = "ltnm_train_minimap_button",
tooltip = { "cybersyn-gui.open-train-gui" },
elem_mods = { entity = get_any_train_entity(train.entity) },
handler = trains_tab.handle.open_train_gui, --on_click
tags = { train_id = train_id },
handler = trains_tab.handle.open_train_gui, --on_click
},
},
},
{
type = "label",
style_mods = { width = widths.trains.composition },
elem_mods = { caption = train.layout_id },
caption = train.layout_id,
},
{
type = "label",
style_mods = { width = widths.trains.depot },
elem_mods = { caption = depot_name },
caption = depot_name,
},
{
type = "frame",
name = "shipment_frame",
style = "ltnm_small_slot_table_frame_" .. color,
style_mods = { width = widths.trains.shipment },
children = {
{
type = "table",
name = "shipment_table",
style = "slot_table",
column_count = widths.trains.shipment_columns,
children = util.slot_table_build(train.manifest, "default"),
{ },
},
},
},
},
}
}, refs)
refs.train_minimap.entity = locomotive
gui.add(refs.shipment_table, util.slot_table_build_from_manifest(manifest, "default"))
end
end
return {
tab = {
name = "trains_tab",
type = "tab",
caption = #trains_sorted == 0 and { "cybersyn-gui.trains" } or { "cybersyn-gui.trains", #train_list },
--badge_text = format.number(#ltn_data.sorted_trains.composition),
handler = trains_tab.handle.change_tab, --on_click
tags = { tab = "trains_tab" },
},
content = {
name = "trains_content_frame",
type = "frame",
style = "ltnm_main_content_frame",
direction = "vertical",
children = {
{
type = "frame",
style = "ltnm_table_toolbar_frame",
templates.sort_checkbox(widths, "trains", "status", false),
templates.sort_checkbox(widths, "trains", "layout", false, { "cybersyn-gui.composition-description" }),
templates.sort_checkbox(widths, "trains", "depot", false),
templates.sort_checkbox(widths, "trains", "shipment", false),
},
{ name = "trains_scroll_pane", type = "scroll-pane", style = "ltnm_table_scroll_pane" },
{
name = "trains_warning_flow",
type = "flow",
style = "ltnm_warning_flow",
children = train_list,
},
},
},
}
end
trains_tab.handle = {}
--- @param e {player_index: uint}
function trains_tab.wrapper(e, handler)
local player = game.get_player(e.player_index)
if not player then return end
local player_data = global.manager.players[e.player_index]
handler(player, player_data, player_data.refs, e)
end
--- @param e GuiEventData
--- @param player_data PlayerData
function trains_tab.handle.open_train_gui(player, player_data, refs, e)
local train_id = e.element.tags.train_id
--- @type Train
local train = global.trains[train_id]
local train_entity = train.entity
if not train_entity or not train_entity.valid then
util.error_flying_text(gui.player, { "message.ltnm-error-train-is-invalid" })
return
end
train_util.open_gui(player.index, train_entity)
end
gui.add_handlers(trains_tab.handle, trains_tab.wrapper)
return trains_tab

View File

@@ -64,27 +64,26 @@ end
function util.slot_table_build_from_manifest(manifest, color)
---@type GuiElemDef[]
local children = {}
for _, item in pairs(manifest) do
local name = item.name
local sprite
if item.type then
sprite = item.type .. "/" .. name
else
sprite = string.gsub(name, ",", "/")
end
if game.is_valid_sprite_path(sprite) then
children[#children + 1] = {
type = "sprite-button",
enabled = false,
style = "ltnm_small_slot_button_" .. color,
sprite = sprite,
tooltip = {
"",
"[img=" .. sprite .. "]",
{ "item-name." .. name },
"\n"..format.number(count),
},
}
if manifest then
for _, item in pairs(manifest) do
local name = item.name
local count = item.count
local sprite, img_path, item_string = util.generate_item_references(name)
if game.is_valid_sprite_path(sprite) then
children[#children + 1] = {
type = "sprite-button",
enabled = false,
style = "ltnm_small_slot_button_" .. color,
sprite = sprite,
number = count,
tooltip = {
"",
img_path,
{ item_string },
"\n"..format.number(count),
},
}
end
end
end
return children