Skip to content

Commit

Permalink
cleanup code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGoesen committed Jan 20, 2024
1 parent 98dc46b commit af9b0ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
5 changes: 0 additions & 5 deletions scripts/caravan/caravan-connected-gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 21 additions & 17 deletions scripts/caravan/caravan-global-gui.lua
Original file line number Diff line number Diff line change
@@ -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"}
Expand Down Expand Up @@ -27,49 +28,51 @@ 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
local content_flow = content_frame.add { type = 'flow', direction = 'vertical' }
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)
end
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
Expand All @@ -87,4 +90,5 @@ Caravan.events.on_close_global_gui = function(event)
return true
end
return false
end
end
gui_events[defines.events.on_gui_click]['click_close_global_gui'] = Caravan.events.on_close_global_gui

0 comments on commit af9b0ff

Please sign in to comment.