From e0a034e0d7fcc495fac02050ab88dc31d4cfc641 Mon Sep 17 00:00:00 2001
From: Thorsten Goetzke
Date: Sun, 7 Jan 2024 21:19:53 +0100
Subject: [PATCH] status icon
---
scripts/caravan/caravan-global-gui.lua | 36 +++++++++++++++--------
scripts/caravan/caravan-gui-shared.lua | 37 ++++++++++++++++++++++++
scripts/caravan/caravan-gui.lua | 23 +++------------
scripts/caravan/caravan-outpost-gui.lua | 38 ++++++++++---------------
4 files changed, 80 insertions(+), 54 deletions(-)
create mode 100644 scripts/caravan/caravan-gui-shared.lua
diff --git a/scripts/caravan/caravan-global-gui.lua b/scripts/caravan/caravan-global-gui.lua
index 9939ecb3..936a4fd4 100644
--- a/scripts/caravan/caravan-global-gui.lua
+++ b/scripts/caravan/caravan-global-gui.lua
@@ -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
@@ -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
diff --git a/scripts/caravan/caravan-gui-shared.lua b/scripts/caravan/caravan-gui-shared.lua
new file mode 100644
index 00000000..96ba7845
--- /dev/null
+++ b/scripts/caravan/caravan-gui-shared.lua
@@ -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
\ No newline at end of file
diff --git a/scripts/caravan/caravan-gui.lua b/scripts/caravan/caravan-gui.lua
index 401ed7a4..7ba4f97d 100644
--- a/scripts/caravan/caravan-gui.lua
+++ b/scripts/caravan/caravan-gui.lua
@@ -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'
@@ -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]
diff --git a/scripts/caravan/caravan-outpost-gui.lua b/scripts/caravan/caravan-outpost-gui.lua
index 8c0e7989..f8e5e4f3 100644
--- a/scripts/caravan/caravan-outpost-gui.lua
+++ b/scripts/caravan/caravan-outpost-gui.lua
@@ -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
@@ -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
@@ -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,
@@ -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'
@@ -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)