Skip to content

Commit

Permalink
Streamline the amount of code needed to handle GUI closing events
Browse files Browse the repository at this point in the history
  • Loading branch information
notnotmelon committed Jan 22, 2024
1 parent 8728a4f commit aaf0b03
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 109 deletions.
13 changes: 1 addition & 12 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,13 @@ script.on_event(defines.events.on_gui_opened, function(event)
Biofluid.events.on_gui_opened(event)
end)

script.on_event(defines.events.on_gui_closed, function(event)
script.on_event({defines.events.on_gui_closed, defines.events.on_player_changed_surface}, function(event)
Caravan.events.close_gui(event)
Caravan.events.on_close_global_gui(event)
Caravan.events.on_close_connected_gui(event)
Digosaurus.events.close_gui(event)
Slaughterhouse.events.on_gui_closed(event)
Biofluid.events.on_gui_closed(event)
end)

script.on_event(defines.events.on_player_changed_surface, function(event)
Caravan.events.close_gui(event)
Caravan.events.on_close_connected_gui(event)
Digosaurus.events.close_gui(event)
Slaughterhouse.events.on_gui_closed(event)
Biofluid.events.on_gui_closed(event)
end)


script.on_event(defines.events.on_rocket_launched, Smart_Farm.events.on_rocket_launched)

script.on_event(defines.events.on_entity_destroyed, function(event)
Expand Down
59 changes: 18 additions & 41 deletions scripts/caravan/caravan-connected-gui.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'caravan-gui-shared'
--local Table = require('__stdlib__/stdlib/utils/table')

local function guess(event)
local name = event.entity.name
Expand All @@ -17,52 +16,43 @@ end
--anchor is optional
local function instantiate_main_frame(gui, anchor)
if anchor then
return gui.relative.add {
return gui.relative.add{
type = 'frame',
name = 'connected_caravan_gui',
caption = { 'caravan-global-gui.caption' },
name = 'py_global_caravan_gui',
caption = {'caravan-global-gui.caption'},
direction = 'vertical',
anchor = anchor
}
end
if not gui.relative.caravan_flow then
return
end
return gui.relative.caravan_flow.add {
if not gui.relative.caravan_flow then return end
return gui.relative.caravan_flow.add{
type = 'frame',
name = 'connected_caravan_gui',
caption = { 'caravan-global-gui.caption' },
name = 'py_global_caravan_gui',
caption = {'caravan-global-gui.caption'},
direction = 'vertical',
}
end

--anchor is optional
Caravan.build_gui_connected = function(player, entity, anchor)
if not entity then
return
end
if not Caravan.has_any_caravan(entity) then
return
end
if not entity then return end
if not Caravan.has_any_caravan(entity) then return end
local main_frame = instantiate_main_frame(player.gui, anchor)
if not main_frame then
return
end
if not main_frame then return end
main_frame.style.width = 336
main_frame.style.minimal_height = 710
main_frame.tags = { unit_number = entity.unit_number }
main_frame.tags = {unit_number = entity.unit_number}

local content_frame = main_frame.add { type = 'frame', direction = 'vertical',
style = 'inside_shallow_frame_with_padding' }
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' }
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.margin = {-4, 0, -4, 0}
content_flow.style.vertical_align = 'center'
local scroll_pane = content_flow.add {
local scroll_pane = content_flow.add{
type = 'scroll-pane',
}
local table = scroll_pane.add {
local table = scroll_pane.add{
type = 'table',
column_count = 2
}
Expand All @@ -71,29 +61,16 @@ Caravan.build_gui_connected = function(player, entity, anchor)
Caravan.add_gui_row(caravan_data, key, table)
end
end

end

Caravan.events.on_gui_opened_connected = function(event)
local player = game.get_player(event.player_index)
local entity = event.entity
if not entity then
return
end
Caravan.events.on_close_connected_gui(event)
if not entity then return end
if player.gui.relative.connected_caravan_gui then return end
local anchor = {
gui = guess(event),
position = defines.relative_gui_position.right
}
Caravan.build_gui_connected(player, entity, anchor)

end

Caravan.events.on_close_connected_gui = function(event)
local player = game.get_player(event.player_index)
if not player.gui.relative.connected_caravan_gui then
return
end
player.gui.relative.connected_caravan_gui.destroy()
return true
end
38 changes: 10 additions & 28 deletions scripts/caravan/caravan-global-gui.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'caravan-gui-shared'
local close_button_name = 'click_close_global_gui'

local function add_titlebar(gui, caption, close_button_name)
local function add_titlebar(gui, caption)
local titlebar = gui.add{type = 'flow'}
titlebar.drag_target = gui
titlebar.add{
Expand All @@ -19,7 +18,7 @@ local function add_titlebar(gui, caption, close_button_name)
filler.style.horizontally_stretchable = true
titlebar.add{
type = 'sprite-button',
name = close_button_name,
name = 'py_close_global_caravan_gui',
style = 'frame_action_button',
sprite = 'utility/close_white',
hovered_sprite = 'utility/close_black',
Expand All @@ -36,7 +35,8 @@ function Caravan.has_any_caravan_at_all()
end

local function instantiate_main_frame(player)
local main_frame = player.gui.screen.add{type = 'frame', name = 'caravan_gui_global', direction = 'vertical'}
player.opened = nil
local main_frame = player.gui.screen.add{type = 'frame', name = 'py_global_caravan_gui', direction = 'vertical'}
add_titlebar(main_frame, {'caravan-global-gui.caption'}, 'click_close_global_gui')
main_frame.style.width = 336
main_frame.style.minimal_height = 710
Expand All @@ -47,7 +47,8 @@ end

Caravan.events.on_open_global_gui = function(event)
local player = game.get_player(event.player_index)
if Caravan.close_global_gui(event) then
if player.gui.screen.py_global_caravan_gui then
player.gui.screen.py_global_caravan_gui.destroy()
return
end
local main_frame = instantiate_main_frame(player)
Expand Down Expand Up @@ -78,7 +79,7 @@ Caravan.events.on_open_global_gui = function(event)
end
end

gui_events[defines.events.on_gui_click]['click_caravan_.'] = function(event)
gui_events[defines.events.on_gui_click]['py_click_caravan_.'] = function(event)
local player = game.get_player(event.player_index)
local element = event.element
local tags = element.tags
Expand All @@ -88,25 +89,6 @@ gui_events[defines.events.on_gui_click]['click_caravan_.'] = function(event)
end
end

local function is_our_event(event)
local player = game.get_player(event.player_index)
if not event.element then return end
return event.element.name == close_button_name or event.element == player.gui.screen.caravan_gui_global
end

Caravan.events.on_close_global_gui = function(event)
if is_our_event(event) then
Caravan.close_global_gui(event)
end
end

Caravan.close_global_gui = function(event)
local player = game.get_player(event.player_index)
if player.gui.screen.caravan_gui_global then
player.gui.screen.caravan_gui_global.destroy()
return true
end
return false
end

gui_events[defines.events.on_gui_click]['click_close_global_gui'] = Caravan.events.on_close_global_gui
gui_events[defines.events.on_gui_click]['py_close_global_caravan_gui'] = function(event)
event.element.parent.parent.destroy()
end
54 changes: 26 additions & 28 deletions scripts/caravan/caravan-gui-shared.lua
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
local Table = require('__stdlib__/stdlib/utils/table')
function has_schedule(caravan_data, entity)
if not Caravan.validity_check(caravan_data) then
return
if not Caravan.validity_check(caravan_data) then return end
if not caravan_data.schedule then return end
for _, schedule in pairs(caravan_data.schedule) do
if schedule.entity == entity then return true end
end
if not caravan_data.schedule then
return
end
return Table.any(caravan_data.schedule, function(schedule)
return schedule.entity == entity
end)
return false
end

-- probably bad if you have 10 k caravans... but is that even remotely relevant?
function Caravan.has_any_caravan(entity)
return Table.any(global.caravans, function(caravan_data)
return has_schedule(caravan_data, entity)
end)
for _, caravan_data in pairs(global.caravans) do
if has_schedule(caravan_data, entity) then return true end
end
return false
end

function Caravan.status_img(caravan_data)
local entity = caravan_data.entity
if caravan_data.is_aerial then
return { 'entity-status.working' }, 'utility/status_working'
return {'entity-status.working'}, 'utility/status_working'
elseif caravan_data.fuel_bar == 0 and caravan_data.fuel_inventory.is_empty() then
return { 'entity-status.starved' }, 'utility/status_not_working'
return {'entity-status.starved'}, 'utility/status_not_working'
elseif entity.health ~= entity.prototype.max_health then
return { 'entity-status.wounded' }, 'utility/status_yellow'
return {'entity-status.wounded'}, 'utility/status_yellow'
elseif not Caravan.is_automated(caravan_data) then
return { 'entity-status.idle' }, 'utility/status_yellow'
return {'entity-status.idle'}, 'utility/status_yellow'
else
return { 'entity-status.healthy' }, 'utility/status_working'
return {'entity-status.healthy'}, 'utility/status_working'
end
end

function Caravan.add_gui_row(caravan_data, key, table)
local button= table.add{type = 'button',
name = 'click_caravan_.' .. tostring(key),
-- style = 'train_schedule_add_station_button',
caption = {'caravan-shared.open',tostring(caravan_data.entity.name) },
tags = { unit_number = key, entity = caravan_data }}
button.style.maximal_height=24
local button = table.add{type = 'button',
name = 'py_click_caravan_.' .. tostring(key),
-- style = 'train_schedule_add_station_button',
caption = {'caravan-shared.open', tostring(caravan_data.entity.name)},
tags = {unit_number = key, entity = caravan_data}
}
button.style.maximal_height = 24

local status_flow = table.add { type = 'flow', direction = 'horizontal' }
local status_flow = table.add{type = 'flow', direction = 'horizontal'}
status_flow.style.vertical_align = 'center'
local status_sprite = status_flow.add { type = 'sprite' }
local status_sprite = status_flow.add{type = 'sprite'}
status_sprite.resize_to_sprite = false
status_sprite.style.size = { 16, 16 }
local status_text = status_flow.add { type = 'label' }
status_flow.add { type = 'empty-widget', style = 'py_empty_widget' }
status_sprite.style.size = {16, 16}
local status_text = status_flow.add{type = 'label'}
status_flow.add{type = 'empty-widget', style = 'py_empty_widget'}
local state, img = Caravan.status_img(caravan_data)
status_text.caption = state
status_sprite.sprite = img
Expand Down
8 changes: 8 additions & 0 deletions scripts/caravan/caravan-gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ end

Caravan.events.close_gui = function(event)
local player = game.get_player(event.player_index)

if player.gui.relative.py_global_caravan_gui then
player.gui.relative.py_global_caravan_gui.destroy()
end
if player.gui.screen.py_global_caravan_gui then
player.gui.screen.py_global_caravan_gui.destroy()
end

if event.gui_type == defines.gui_type.script_inventory or event.gui_type == defines.gui_type.custom then
local gui = Caravan.get_caravan_gui(player)
if gui then gui.destroy() end
Expand Down

0 comments on commit aaf0b03

Please sign in to comment.