Skip to content

Commit

Permalink
status icon
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGoesen committed Jan 7, 2024
1 parent 40e68c6 commit e0a034e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 54 deletions.
36 changes: 24 additions & 12 deletions scripts/caravan/caravan-global-gui.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
require 'caravan-gui-shared'

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 frame = player.gui.screen.add { type = 'frame', name = 'caravan_gui_global', caption = { 'caravan-global-gui.caption' }, direction = 'vertical' }
frame.auto_center = true
local scroll_pane = frame.add {
local main_frame = player.gui.screen.add { type = 'frame', name = 'caravan_gui_global', caption = { 'caravan-global-gui.caption' }, direction = 'vertical' }
main_frame.style.width = 436
main_frame.style.minimal_height = 710

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'
}
local table = scroll_pane.add {
type = 'table',
name = 'my_table',
column_count = 2
column_count = 3
}
table.add { type = 'label', caption = 'Key' }
table.add { type = 'label', caption = 'Value' }
for key, value in pairs(global.caravans) do
if Caravan.validity_check(value) then
table.add { type = 'label', name = 'click_caravan_.' .. tostring(key),
style = 'clickable_squashable_label',
tags = { unit_number = key, entity = value },
caption = tostring(key) }
table.add { type = 'label', caption = tostring(value.entity.name) }
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
Expand All @@ -32,9 +41,12 @@ gui_events[defines.events.on_gui_click]['click_caravan_.'] = function(event)
local element = event.element
local tags = element.tags
local caravan_data = global.caravans[tags.unit_number]
if caravan_data then
game.print('1')
if Caravan.validity_check(caravan_data) then
game.print('2')
Caravan.build_gui(player, caravan_data.entity)
end
game.print('3')

end

Expand Down
37 changes: 37 additions & 0 deletions scripts/caravan/caravan-gui-shared.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function Caravan.status_img(caravan_data)
local status, img
local entity = caravan_data.entity
if caravan_data.is_aerial then
status = { 'entity-status.working' }
img = 'utility/status_working'
elseif caravan_data.fuel_bar == 0 and caravan_data.fuel_inventory.is_empty() then
status = { 'entity-status.starved' }
img = 'utility/status_not_working'
elseif entity.health ~= entity.prototype.max_health then
status = { 'entity-status.wounded' }
img = 'utility/status_yellow'
else
status = { 'entity-status.healthy' }
img = 'utility/status_working'
end
return { status = status, img = img }
end

function Caravan.add_gui_row(caravan_data, key, table)
table.add { type = 'label',
style = 'clickable_squashable_label',
name = 'click_caravan_.' .. tostring(key),
tags = { unit_number = key, entity = caravan_data },
caption = tostring(key) }
table.add { type = 'label', caption = tostring(caravan_data.entity.name) }
local status_flow = table.add { type = 'flow', direction = 'horizontal' }
status_flow.style.vertical_align = 'center'
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' }
local state = Caravan.status_img(caravan_data)
status_text.caption = state.status
status_sprite.sprite = state.img
end
23 changes: 4 additions & 19 deletions scripts/caravan/caravan-gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local prototypes = require 'caravan-prototypes'
local Table = require('__stdlib__/stdlib/utils/table')
local Position = require('__stdlib__/stdlib/area/position')
local FUN = require '__pycoalprocessing__/prototypes/functions/functions'
require 'caravan-gui-shared'

local function generate_button_status(caravan_data, schedule_id, action_id)
local style = 'train_schedule_action_button'
Expand Down Expand Up @@ -229,25 +230,9 @@ function Caravan.update_gui(gui, weak)
local caravan_data = global.caravans[gui.tags.unit_number]
if not Caravan.validity_check(caravan_data) then gui.destroy() return end
local content_flow = gui.content_frame.content_flow
local entity = caravan_data.entity

local status, img
if caravan_data.is_aerial then
status = {'entity-status.working'}
img = 'utility/status_working'
elseif caravan_data.fuel_bar == 0 and caravan_data.fuel_inventory.is_empty() then
status = {'entity-status.starved'}
img = 'utility/status_not_working'
elseif entity.health ~= entity.prototype.max_health then
status = {'entity-status.wounded'}
img = 'utility/status_yellow'
else
status = {'entity-status.healthy'}
img = 'utility/status_working'
end
content_flow.status_flow.status_text.caption = status
content_flow.status_flow.status_sprite.sprite = img

local state = Caravan.status_img(caravan_data)
content_flow.status_flow.status_text.caption = state.status
content_flow.status_flow.status_sprite.sprite = state.img
if caravan_data.fuel_inventory then
for i = 1, #caravan_data.fuel_inventory do
local stack = caravan_data.fuel_inventory[i]
Expand Down
38 changes: 15 additions & 23 deletions scripts/caravan/caravan-outpost-gui.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local function hasSchedule(caravan_data, entity)
require 'caravan-gui-shared'

local function has_schedule(caravan_data, entity)
if not Caravan.validity_check(caravan_data) then
return false
end
Expand All @@ -13,9 +15,10 @@ local function hasSchedule(caravan_data, entity)
return false
end

local function any_caravan(entity)
-- probably bad if you have 10 k caravans... but is that even remotely relevant?
local function has_any_caravan(entity)
for key, value in pairs(global.caravans) do
if hasSchedule(value, entity) then
if has_schedule(value, entity) then
return true
end
end
Expand All @@ -32,16 +35,10 @@ Caravan.events.on_gui_opened_outpost = function(event)
return
end
Caravan.events.on_close_outpost_gui(event)
if not any_caravan(entity) then
if not has_any_caravan(entity) then
return
end
local main_frame
if false then
main_frame = player.gui.screen.add { type = 'frame', name = 'caravan_gui', caption = entity.prototype.localised_name, direction = 'vertical' }
main_frame.auto_center = true
player.opened = main_frame
else
main_frame = player.gui.relative.add {
local main_frame = player.gui.relative.add {
type = 'frame',
name = 'outpost_caravan_gui',
caption = entity.prototype.localised_name,
Expand All @@ -51,15 +48,14 @@ Caravan.events.on_gui_opened_outpost = function(event)
position = defines.relative_gui_position.right
}
}
end
main_frame.style.width = 436
main_frame.style.minimal_height = 710
main_frame.tags = { unit_number = entity.unit_number }

local content_frame = main_frame.add { type = 'frame', name = 'content_frame_outpost', direction = 'vertical',
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', name = 'content_flow_outpost', 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.vertical_align = 'center'
Expand All @@ -70,20 +66,16 @@ Caravan.events.on_gui_opened_outpost = function(event)
local table = scroll_pane.add {
type = 'table',
name = 'my_table',
column_count = 2
column_count = 3
}
table.add { type = 'label', caption = 'Key' }
table.add { type = 'label', caption = 'Value' }
for key, value in pairs(global.caravans) do
if hasSchedule(value, entity) then
table.add { type = 'label', name = 'click_caravan_.' .. tostring(key),
style = 'clickable_squashable_label',
tags = { unit_number = key, entity = value },
caption = tostring(key) }
table.add { type = 'label', caption = tostring(value.entity.name) }
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)
end
end
game.print('it shoudl')
end

Caravan.events.on_close_outpost_gui = function(event)
Expand Down

0 comments on commit e0a034e

Please sign in to comment.