Merge pull request #42 from wdberry/gui-trains-tab

Migrate GUI development branch
This commit is contained in:
Will Berry
2023-03-13 19:19:09 -04:00
committed by GitHub
3 changed files with 133 additions and 76 deletions

View File

@@ -6,7 +6,7 @@ local constants = require("scripts.gui.constants")
local templates = require("scripts.gui.templates") local templates = require("scripts.gui.templates")
local stations_tab = require("scripts.gui.stations") 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 depots_tab = require("scripts.gui.depots")
local inventory_tab = require("scripts.gui.inventory") local inventory_tab = require("scripts.gui.inventory")
--local history_tab = require("scripts.gui.history") --local history_tab = require("scripts.gui.history")
@@ -98,6 +98,7 @@ function manager.create(player)
name = "manager_tabbed_pane", name = "manager_tabbed_pane",
type = "tabbed-pane", type = "tabbed-pane",
style = "ltnm_tabbed_pane", style = "ltnm_tabbed_pane",
trains_tab.create(widths),
stations_tab.create(widths), stations_tab.create(widths),
inventory_tab.create(), inventory_tab.create(),
selected_tab_index = 1, selected_tab_index = 1,
@@ -162,6 +163,8 @@ function manager.update(map_data, player_data, query_limit)
stations_tab.build(map_data, player_data, query_limit) stations_tab.build(map_data, player_data, query_limit)
elseif player_data.selected_tab == "inventory_tab" then elseif player_data.selected_tab == "inventory_tab" then
inventory_tab.build(map_data, player_data) inventory_tab.build(map_data, player_data)
elseif player_data.selected_tab == "trains_tab" then
trains_tab.build(map_data, player_data, query_limit)
end end
end 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 gui = require("__flib__.gui-lite")
local constants = require("constants") local constants = require("constants")
@@ -8,23 +8,64 @@ local templates = require("scripts.gui.templates")
local trains_tab = {} 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.on_trains_tab_selected, --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 map_data MapData
--- @param player_data PlayerData --- @param player_data PlayerData
--- @return GuiElemDef --- @return GuiElemDef
function trains_tab.build(map_data, player_data, query_limit) function trains_tab.build(map_data, player_data, query_limit)
local widths = constants.gui["en"] 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_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 trains = map_data.trains
local trains_sorted = {} local trains_sorted = {}
local i = 0 local i = 0
for id, train in pairs(map_data.trains) do for id, train in pairs(trains) do
if not train.entity.valid then
goto continue
end
if search_network_name then if search_network_name then
if search_network_name ~= train.network_name then if search_network_name ~= train.network_name then
goto continue goto continue
@@ -49,6 +90,9 @@ function trains_tab.build(map_data, player_data, query_limit)
end end
if search_surface_idx then if search_surface_idx then
if search_surface_idx == -1 then
goto has_match
end
local entity = get_any_train_entity(train.entity) local entity = get_any_train_entity(train.entity)
if not entity then if not entity then
goto continue goto continue
@@ -56,6 +100,7 @@ function trains_tab.build(map_data, player_data, query_limit)
if entity.surface.index ~= search_surface_idx then if entity.surface.index ~= search_surface_idx then
goto continue goto continue
end end
::has_match::
end end
if search_item then if search_item then
@@ -63,7 +108,7 @@ function trains_tab.build(map_data, player_data, query_limit)
goto continue goto continue
end end
for i, v in ipairs(train.manifest) do for i, v in ipairs(train.manifest) do
if v.name == search_item then if string.match(v.name, search_item) then
goto has_match goto has_match
end end
end end
@@ -132,106 +177,116 @@ function trains_tab.build(map_data, player_data, query_limit)
return a < b return a < b
end) 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 if #trains_sorted == 0 then
train_list[1] = { gui.add(scroll_pane, {
type = "label", type = "label",
style = "ltnm_semibold_label", style = "ltnm_semibold_label",
caption = { "cybersyn-gui.no-trains" }, caption = { "cybersyn-gui.no-trains" },
} })
else else
for idx, train_id in ipairs(trains_sorted) do for idx, train_id in ipairs(trains_sorted) do
local train = map_data.trains[train_id] local train = map_data.trains[train_id]
local depot = map_data.depots[train.depot_id] local depot = map_data.depots[train.depot_id]
local depot_name = depot.entity_stop.valid and depot.entity_stop.backer_name or "" 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" local color = idx % 2 == 0 and "dark" or "light"
train_list[idx] = { gui.add(scroll_pane, {
type = "frame", type = "frame",
style = "ltnm_table_row_frame_" .. color, style = "ltnm_table_row_frame_" .. color,
children = {
{ {
type = "frame", type = "frame",
style = "ltnm_table_inset_frame_" .. color, style = "ltnm_table_inset_frame_" .. color,
children = { {
type = "minimap", type = "minimap",
name = "train_minimap",
style = "ltnm_train_minimap", style = "ltnm_train_minimap",
{ type = "label", style = "ltnm_minimap_label" }, { type = "label", style = "ltnm_minimap_label" },
{ {
type = "button", type = "button",
style = "ltnm_train_minimap_button", style = "ltnm_train_minimap_button",
tooltip = { "cybersyn-gui.open-train-gui" }, 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 }, tags = { train_id = train_id },
handler = trains_tab.handle.open_train_gui, --on_click
}, },
}, },
}, },
{ {
type = "label", type = "label",
style_mods = { width = widths.trains.composition }, style_mods = { width = widths.trains.composition },
elem_mods = { caption = train.layout_id }, caption = train.layout_id,
}, },
{ {
type = "label", type = "label",
style_mods = { width = widths.trains.depot }, style_mods = { width = widths.trains.depot },
elem_mods = { caption = depot_name }, caption = depot_name,
}, },
{ {
type = "frame", type = "frame",
name = "shipment_frame", name = "shipment_frame",
style = "ltnm_small_slot_table_frame_" .. color, style = "ltnm_small_slot_table_frame_" .. color,
style_mods = { width = widths.trains.shipment }, style_mods = { width = widths.trains.shipment },
children = {
{ {
type = "table", type = "table",
name = "shipment_table", name = "shipment_table",
style = "slot_table", style = "slot_table",
column_count = widths.trains.shipment_columns, 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
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 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
---@param player LuaPlayer
---@param player_data PlayerData
function trains_tab.handle.on_trains_tab_selected(player, player_data)
player_data.selected_tab = "trains_tab"
end
gui.add_handlers(trains_tab.handle, trains_tab.wrapper)
return trains_tab return trains_tab

View File

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