From af9b0ffbcb97b0b20af7716c1af1034e1e9c34c2 Mon Sep 17 00:00:00 2001
From: Thorsten Goetzke
Date: Sat, 20 Jan 2024 19:16:22 +0100
Subject: [PATCH] cleanup code a bit
---
scripts/caravan/caravan-connected-gui.lua | 5 ---
scripts/caravan/caravan-global-gui.lua | 38 +++++++++++++----------
2 files changed, 21 insertions(+), 22 deletions(-)
diff --git a/scripts/caravan/caravan-connected-gui.lua b/scripts/caravan/caravan-connected-gui.lua
index b827e9af..7582a9cb 100644
--- a/scripts/caravan/caravan-connected-gui.lua
+++ b/scripts/caravan/caravan-connected-gui.lua
@@ -62,16 +62,11 @@ Caravan.build_gui_connected = function(player, entity, anchor)
content_flow.style.vertical_align = 'center'
local scroll_pane = content_flow.add {
type = 'scroll-pane',
- name = 'connected-caravan-pane'
}
local table = scroll_pane.add {
type = 'table',
- name = 'my_table',
column_count = 3
}
- table.add { type = 'label', caption = 'Key' }
- table.add { type = 'label', caption = 'Value' }
- table.add { type = 'label', caption = 'Value' }
for key, caravan_data in pairs(global.caravans) do
if has_schedule(caravan_data, entity) then
Caravan.add_gui_row(caravan_data, key, table)
diff --git a/scripts/caravan/caravan-global-gui.lua b/scripts/caravan/caravan-global-gui.lua
index d905f66e..85ebe526 100644
--- a/scripts/caravan/caravan-global-gui.lua
+++ b/scripts/caravan/caravan-global-gui.lua
@@ -1,4 +1,5 @@
require 'caravan-gui-shared'
+local Table = require('__stdlib__/stdlib/utils/table')
function add_titlebar(gui, caption, close_button_name)
local titlebar = gui.add{type = "flow"}
@@ -27,17 +28,25 @@ function add_titlebar(gui, caption, close_button_name)
}
end
+function Caravan.has_any_caravan_at_all()
+ return Table.any(global.caravans, Caravan.validity_check)
+end
-Caravan.events.on_open_global_gui = function(event)
- local player = game.get_player(event.player_index)
- if Caravan.events.on_close_global_gui(event) then
- return
- end
+local function instantiate_main_frame(player)
local main_frame = player.gui.screen.add { type = 'frame', name = 'caravan_gui_global', direction = 'vertical' }
add_titlebar(main_frame,{ 'caravan-global-gui.caption' },'click_close_global_gui')
main_frame.style.width = 436
main_frame.style.minimal_height = 710
+ main_frame.auto_center = true
+ return main_frame;
+end
+Caravan.events.on_open_global_gui = function(event)
+ local player = game.get_player(event.player_index)
+ if Caravan.events.on_close_global_gui(event) then
+ return
+ end
+ local main_frame=instantiate_main_frame(player)
local content_frame = main_frame.add { type = 'frame', direction = 'vertical',
style = 'inside_shallow_frame_with_padding' }
content_frame.style.vertically_stretchable = true
@@ -45,19 +54,18 @@ Caravan.events.on_open_global_gui = function(event)
content_flow.style.vertical_spacing = 8
content_flow.style.margin = { -4, 0, -4, 0 }
content_flow.style.vertical_align = 'center'
- main_frame.auto_center = true
+
local scroll_pane = content_flow.add {
type = 'scroll-pane',
- name = 'global-caravan-pane'
}
+ if not Caravan.has_any_caravan_at_all() then
+ scroll_pane.add{type= 'label', caption='No caravans found'}
+ return
+ end
local table = scroll_pane.add {
type = 'table',
- name = 'my_table',
column_count = 3
}
- table.add { type = 'label', caption = 'Key' }
- table.add { type = 'label', caption = 'Value' }
- table.add { type = 'label', caption = 'Value' }
for key, caravan_data in pairs(global.caravans) do
if Caravan.validity_check(caravan_data) then
Caravan.add_gui_row(caravan_data, key, table)
@@ -65,11 +73,6 @@ Caravan.events.on_open_global_gui = function(event)
end
end
-gui_events[defines.events.on_gui_click]['click_close_global_gui'] = function(event)
- Caravan.events.on_close_global_gui(event)
-end
-
-
gui_events[defines.events.on_gui_click]['click_caravan_.'] = function(event)
local player = game.get_player(event.player_index)
local element = event.element
@@ -87,4 +90,5 @@ Caravan.events.on_close_global_gui = function(event)
return true
end
return false
-end
\ No newline at end of file
+end
+gui_events[defines.events.on_gui_click]['click_close_global_gui'] = Caravan.events.on_close_global_gui