From 0900fe13ea3f2eb4f3b6b5c12532ab4f909319bf Mon Sep 17 00:00:00 2001 From: badgamernl Date: Mon, 17 Aug 2020 23:04:20 +0200 Subject: [PATCH 01/28] Added Autofill toolbar button - May needs to be changed to have a left flow with option; --- config/_file_loader.lua | 1 + config/gui/autofill.lua | 7 +++++++ locale/en/gui.cfg | 3 +++ modules/gui/autofill.lua | 31 +++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 config/gui/autofill.lua create mode 100644 modules/gui/autofill.lua diff --git a/config/_file_loader.lua b/config/_file_loader.lua index be200bf3..12550b98 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -61,6 +61,7 @@ return { 'modules.gui.player-list', 'modules.gui.server-ups', 'modules.commands.debug', + 'modules.gui.autofill', --- Config Files 'config.expcore.command_auth_admin', -- commands tagged with admin_only are blocked for non admins diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua new file mode 100644 index 00000000..c75cc208 --- /dev/null +++ b/config/gui/autofill.lua @@ -0,0 +1,7 @@ +--- This file contains all the different settings for the autofill system and gui +-- @config Autofill + +return { + -- General config + icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar +} \ No newline at end of file diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index aaec09be..de1ff1e5 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -81,6 +81,9 @@ edit-tooltip=Currently being edited by: __1__ edit-tooltip-none=Currently being edited by: Nobody discard-tooltip=Remove task +[autofill] +main-tooltip=Autofill toggle + [warp-list] main-caption=Warp List main-tooltip=Warp List; Must be within __1__ tiles to use diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua new file mode 100644 index 00000000..ac49ac09 --- /dev/null +++ b/modules/gui/autofill.lua @@ -0,0 +1,31 @@ +--[[-- Gui Module - Autofill + - Adds a button to enable Autofill + @gui Autofill + @alias autofill +]] + +local Gui = require 'expcore.gui' --- @dep expcore.gui +local Global = require 'utils.global' --- @dep utils.global +local config = require 'config.gui.autofill' --- @dep config.gui.autofill + +--- Table that stores if autofill is enabled or not +local autofill_enabled = {} +Global.register(autofill_enabled, function(tbl) + autofill_enabled = tbl +end) + +--- Button on the top flow used to toggle autofill +local toolbar_autofill_toggle +toolbar_autofill_toggle = Gui.toolbar_button(config.icon, {'autofill.main-tooltip'}) +:on_click(function(player) + local top_flow = Gui.get_top_flow(player) + local element = top_flow[toolbar_autofill_toggle.name] + if not autofill_enabled[player.name] then + autofill_enabled[player.name] = true + player.print("true") + else + autofill_enabled[player.name] = false + player.print("false") + end + Gui.toolbar_button_style(element, autofill_enabled[player.name]) +end) \ No newline at end of file From c2ea0bf590fd3c3cd3e848defaa01d862de47c30 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Wed, 19 Aug 2020 01:35:03 +0200 Subject: [PATCH 02/28] Added setings left container - Changed toolbar button to left flow toggle; - Added Ammo section; - Added Fuel section; - Added default config settings; - Added autofill item toggle button and textboxes; --- config/gui/autofill.lua | 38 ++++++++++++ locale/en/gui.cfg | 8 ++- modules/gui/autofill.lua | 121 +++++++++++++++++++++++++++++++++------ 3 files changed, 150 insertions(+), 17 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index c75cc208..806aa9b0 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -4,4 +4,42 @@ return { -- General config icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar + default_settings = { + { + type = 'ammo', + item = 'uranium-rounds-magazine', + amount = 100, + enabled = false + }, + { + type = 'ammo', + item = 'piercing-rounds-magazine', + amount = 100, + enabled = false + }, + { + type = 'ammo', + item = 'firearm-magazine', + amount = 100, + enabled = false + }, + { + type = 'fuel', + item = 'nuclear-fuel', + amount = 100, + enabled = false + }, + { + type = 'fuel', + item = 'solid-fuel', + amount = 100, + enabled = false + }, + { + type = 'fuel', + item = 'coal', + amount = 100, + enabled = false + } + } } \ No newline at end of file diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index de1ff1e5..f7597dc4 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -82,7 +82,13 @@ edit-tooltip-none=Currently being edited by: Nobody discard-tooltip=Remove task [autofill] -main-tooltip=Autofill toggle +main-tooltip=Autofill settings +ammo-caption=Ammo Autofill +ammo-tooltip=Autofill settings when placing turrets +fuel-caption=Fuel Autofill +fuel-tooltip=Autofill settings when placing vehicles +toggle-tooltip=Enable or disable the autofill for the item +amount-tooltip=Amount of items it will try to take from your inventory and put in the turret [warp-list] main-caption=Warp List diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index ac49ac09..98fceb41 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -7,25 +7,114 @@ local Gui = require 'expcore.gui' --- @dep expcore.gui local Global = require 'utils.global' --- @dep utils.global local config = require 'config.gui.autofill' --- @dep config.gui.autofill +local Event = require 'utils.event' --- @dep utils.event --- Table that stores if autofill is enabled or not -local autofill_enabled = {} -Global.register(autofill_enabled, function(tbl) - autofill_enabled = tbl +local autofill_player_settings = {} +Global.register(autofill_player_settings, function(tbl) + autofill_player_settings = tbl end) ---- Button on the top flow used to toggle autofill -local toolbar_autofill_toggle -toolbar_autofill_toggle = Gui.toolbar_button(config.icon, {'autofill.main-tooltip'}) -:on_click(function(player) - local top_flow = Gui.get_top_flow(player) - local element = top_flow[toolbar_autofill_toggle.name] - if not autofill_enabled[player.name] then - autofill_enabled[player.name] = true - player.print("true") - else - autofill_enabled[player.name] = false - player.print("false") +local autofill_container + +--- Draw a section header and main scroll +-- @element autofill_section_container +local section = +Gui.element(function(_, parent, section_name, table_size) + -- Draw the header for the section + Gui.header( + parent, + {'autofill.'..section_name..'-caption'}, + {'autofill.'..section_name..'-tooltip'}, + true, + section_name..'-header' + ) + -- Table used to display the settings + local scroll_table = Gui.scroll_table(parent, 215, table_size, section_name..'-scroll-table') + return scroll_table +end) + +local toggle_item_button = +Gui.element(function(event_trigger, parent, item_name) + return parent.add{ + name = event_trigger, + type = 'sprite-button', + sprite = 'item/'..item_name, + tooltip = {'autofill.toggle-tooltip'}, + style = 'shortcut_bar_button_red' + } +end) +:style(Gui.sprite_style(32, nil, { right_margin = -3 })) +:on_click(function(player, element) + for _, setting in pairs(autofill_player_settings[player.name]) do + if 'item/'..setting.item == element.sprite then + if setting.enabled then + setting.enabled = false + element.style = 'shortcut_bar_button_red' + else + setting.enabled = true + element.style = 'shortcut_bar_button_green' + end + end + end +end) + +local amount_textfield = +Gui.element(function(event_trigger, parent, amount) + return parent.add{ + name = event_trigger, + type = 'textfield', + text = amount, + tooltip = {'autofill.amount-tooltip'}, + clear_and_focus_on_right_click = true + } +end) +:style{ + maximal_width = 90, + height = 28 +} + +local add_autofill_setting = +Gui.element(function(_, parent, item_name, amount) + local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-'..item_name } + local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-'..item_name } + toggle_flow.style.padding = 0 + amount_flow.style.padding = 0 + toggle_item_button(toggle_flow, item_name) + amount_textfield(amount_flow, amount) +end) + +--- Main gui container for the left flow +-- @element autofill_container +autofill_container = +Gui.element(function(event_trigger, parent) + -- Draw the internal container + local container = Gui.container(parent, event_trigger, 100) + -- Draw the header + local ammo_table = section(container, 'ammo', 2) + local fuel_table = section(container, 'fuel', 2) + + for _, setting in pairs(config.default_settings) do + if setting.type == 'ammo' then + add_autofill_setting(ammo_table, setting.item, setting.amount) + elseif setting.type == 'fuel' then + add_autofill_setting(fuel_table, setting.item, setting.amount) + end + end + + -- Return the external container + return container.parent +end) +:add_to_left_flow() + +--- Button on the top flow used to toggle autofill container +-- @element autofill_toggle +Gui.left_toolbar_button(config.icon, {'autofill.main-tooltip'}, autofill_container) + +--- When a player is created make sure they have the default autofill settings +Event.add(defines.events.on_player_created, function(event) + local player = game.players[event.player_index] + if not autofill_player_settings[player.name] then + autofill_player_settings[player.name] = config.default_settings end - Gui.toolbar_button_style(element, autofill_enabled[player.name]) end) \ No newline at end of file From 453b4f8357825204785d7e7b858c43502be5f7f6 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 20 Aug 2020 00:10:36 +0200 Subject: [PATCH 03/28] Added Entity build event autofill - Added autofill on entity placement; - Added nice flying text; - Fixed ability to insert more of a item than the stacksize allows; --- config/gui/autofill.lua | 128 +++++++++++++++++++++++++++------------ locale/en/gui.cfg | 3 +- modules/gui/autofill.lua | 76 ++++++++++++++++++++++- 3 files changed, 164 insertions(+), 43 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index 806aa9b0..7e1605cd 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -2,44 +2,92 @@ -- @config Autofill return { - -- General config - icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar - default_settings = { - { - type = 'ammo', - item = 'uranium-rounds-magazine', - amount = 100, - enabled = false - }, - { - type = 'ammo', - item = 'piercing-rounds-magazine', - amount = 100, - enabled = false - }, - { - type = 'ammo', - item = 'firearm-magazine', - amount = 100, - enabled = false - }, - { - type = 'fuel', - item = 'nuclear-fuel', - amount = 100, - enabled = false - }, - { - type = 'fuel', - item = 'solid-fuel', - amount = 100, - enabled = false - }, - { - type = 'fuel', - item = 'coal', - amount = 100, - enabled = false - } - } + -- General config + icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar + entities = { + ['car'] = { + { + type = 'fuel', + inventory = defines.inventory.fuel, + enabled = true + }, + { + type = 'ammo', + inventory = defines.inventory.car_ammo, + enabled = true + } + }, + ['locomotive'] = { + { + type = 'fuel', + inventory = defines.inventory.fuel, + enabled = true + } + }, + ['tank'] = { + { + type = 'fuel', + inventory = defines.inventory.fuel, + enabled = true + } + }, + ['gun-turret'] = { + { + type = 'ammo', + inventory = defines.inventory.turret_ammo, + enabled = true + } + } + }, + default_settings = { + { + type = 'ammo', + inventories = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, + item = 'uranium-rounds-magazine', + amount = 10, + enabled = false + }, + { + type = 'ammo', + inventories = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, + item = 'piercing-rounds-magazine', + amount = 10, + enabled = false + }, + { + type = 'ammo', + inventories = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, + item = 'firearm-magazine', + amount = 10, + enabled = false + }, + { + type = 'fuel', + inventories = {defines.inventory.fuel}, + item = 'nuclear-fuel', + amount = 1, + enabled = false + }, + { + type = 'fuel', + inventories = {defines.inventory.fuel}, + item = 'rocket-fuel', + amount = 10, + enabled = false + }, + { + type = 'fuel', + inventories = {defines.inventory.fuel}, + item = 'solid-fuel', + amount = 10, + enabled = false + }, + { + type = 'fuel', + inventories = {defines.inventory.fuel}, + item = 'coal', + amount = 10, + enabled = false + } + } } \ No newline at end of file diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index f7597dc4..2ae942f0 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -89,7 +89,8 @@ fuel-caption=Fuel Autofill fuel-tooltip=Autofill settings when placing vehicles toggle-tooltip=Enable or disable the autofill for the item amount-tooltip=Amount of items it will try to take from your inventory and put in the turret - +confirmed=Set autofill amount to __1__ for __2__ +filled=Autofilled the __1__ with __2__ __3__ [warp-list] main-caption=Warp List main-tooltip=Warp List; Must be within __1__ tiles to use diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 98fceb41..1d4169cb 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -4,11 +4,14 @@ @alias autofill ]] +local Game = require 'utils.game' --- @dep utils.game local Gui = require 'expcore.gui' --- @dep expcore.gui local Global = require 'utils.global' --- @dep utils.global local config = require 'config.gui.autofill' --- @dep config.gui.autofill local Event = require 'utils.event' --- @dep utils.event +local print_text = Game.print_floating_text -- (surface, position, text, color) + --- Table that stores if autofill is enabled or not local autofill_player_settings = {} Global.register(autofill_player_settings, function(tbl) @@ -71,8 +74,18 @@ Gui.element(function(event_trigger, parent, amount) end) :style{ maximal_width = 90, - height = 28 + height = 32, + padding = -2 } +:on_confirmed(function(player, element, _) + local parent_name = element.parent.name + for _, setting in pairs(autofill_player_settings[player.name]) do + if 'amount-setting-'..setting.item == parent_name then + setting.amount = tonumber(element.text) + player.print({'autofill.confirmed', setting.amount, '[img=item/'..setting.item..']'}) + end + end +end) local add_autofill_setting = Gui.element(function(_, parent, item_name, amount) @@ -117,4 +130,63 @@ Event.add(defines.events.on_player_created, function(event) if not autofill_player_settings[player.name] then autofill_player_settings[player.name] = config.default_settings end -end) \ No newline at end of file +end) + +local function inventories_contains_inventory(inventories, inventory) + for _, inv in pairs(inventories) do + if inv == inventory then + return true + end + end + return false +end + +local function entity_build(event) + -- Check if player exists + local player = game.players[event.player_index] + if not player then + return + end + -- Check if the entity is in the config and enabled + local entity = event.created_entity + local entity_configs = config.entities[entity.name] + if not entity_configs then + return + end + + -- Loop over each entity config and try to furfill the request amount + for _,entity_config in pairs(entity_configs) do + if not entity_config.enabled then + break + end + local player_inventory = player.get_main_inventory() + local entity_inventory = entity.get_inventory(entity_config.inventory) + for _, setting in pairs(autofill_player_settings[player.name]) do + if not setting.enabled or not inventories_contains_inventory(setting.inventories, entity_config.inventory) then + goto continue + end + local item = setting.item + local preferd_amount = setting.amount + local item_amount = player_inventory.get_item_count(item) + if item_amount ~= 0 then + local inserted + if item_amount >= preferd_amount then + if not entity_inventory.can_insert({name=item, count=preferd_amount}) then + goto continue + end + inserted = entity_inventory.insert({name=item, count=preferd_amount}) + player_inventory.remove({name=item, count=inserted}) + print_text(entity.surface, entity.position, {'autofill.filled', '[img=entity/'..entity.name..']', inserted, '[img=item/'..item..']' }, { r = 0, g = 255, b = 0, a = 1}) + else + inserted = entity_inventory.insert({name=item, count=item_amount}) + player_inventory.remove({name=item, count=inserted}) + print_text(entity.surface, entity.position, {'autofill.filled', '[img=entity/'..entity.name..']', inserted, '[img=item/'..item..']' }, { r = 255, g = 165, b = 0, a = 1}) + end + goto continue + end + ::continue:: + end + end +end + +Event.add(defines.events.on_built_entity, entity_build) \ No newline at end of file From 05153c7a6cd23171f3fcf3cec0d73b503a728aa0 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 20 Aug 2020 21:03:45 +0200 Subject: [PATCH 04/28] Fix desync settings table shared acros all players --- modules/gui/autofill.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 1d4169cb..78bf5192 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -9,6 +9,7 @@ local Gui = require 'expcore.gui' --- @dep expcore.gui local Global = require 'utils.global' --- @dep utils.global local config = require 'config.gui.autofill' --- @dep config.gui.autofill local Event = require 'utils.event' --- @dep utils.event +local table = require 'overrides.table' --- @dep overrides.table local print_text = Game.print_floating_text -- (surface, position, text, color) @@ -128,7 +129,7 @@ Gui.left_toolbar_button(config.icon, {'autofill.main-tooltip'}, autofill_contain Event.add(defines.events.on_player_created, function(event) local player = game.players[event.player_index] if not autofill_player_settings[player.name] then - autofill_player_settings[player.name] = config.default_settings + autofill_player_settings[player.name] = table.deep_copy(config.default_settings) end end) From 8efa1c09544a910c189a73341ff832bcba1be664 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 20 Aug 2020 21:45:35 +0200 Subject: [PATCH 05/28] Locale and code quality improvements - Added tank ammo autofill to config - Fixed locale to be fancier - Fixed some repatative code --- config/gui/autofill.lua | 5 +++++ locale/en/gui.cfg | 8 ++++---- modules/gui/autofill.lua | 37 +++++++++++++++++++++---------------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index 7e1605cd..d7069993 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -29,6 +29,11 @@ return { type = 'fuel', inventory = defines.inventory.fuel, enabled = true + }, + { + type = 'ammo', + inventory = defines.inventory.car_ammo, + enabled = true } }, ['gun-turret'] = { diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index 2ae942f0..4d6ab0be 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -84,11 +84,11 @@ discard-tooltip=Remove task [autofill] main-tooltip=Autofill settings ammo-caption=Ammo Autofill -ammo-tooltip=Autofill settings when placing turrets +ammo-tooltip=Autofill settings when playing entities with a ammo slot fuel-caption=Fuel Autofill -fuel-tooltip=Autofill settings when placing vehicles -toggle-tooltip=Enable or disable the autofill for the item -amount-tooltip=Amount of items it will try to take from your inventory and put in the turret +fuel-tooltip=Autofill settings when placing entities with a fuel slot +toggle-tooltip=Toggle the autofill of __1__ into the __2__ slot of entities +amount-tooltip=Amount of items it will try and put into the __1__ slot of entities confirmed=Set autofill amount to __1__ for __2__ filled=Autofilled the __1__ with __2__ __3__ [warp-list] diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 78bf5192..a4e7a635 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -21,11 +21,15 @@ end) local autofill_container +local function rich_img(type, value) + return '[img='..type..'/'..value..']' +end + --- Draw a section header and main scroll -- @element autofill_section_container local section = Gui.element(function(_, parent, section_name, table_size) - -- Draw the header for the section + -- Draw the header for the section Gui.header( parent, {'autofill.'..section_name..'-caption'}, @@ -39,12 +43,12 @@ Gui.element(function(_, parent, section_name, table_size) end) local toggle_item_button = -Gui.element(function(event_trigger, parent, item_name) +Gui.element(function(event_trigger, parent, setting) return parent.add{ name = event_trigger, type = 'sprite-button', - sprite = 'item/'..item_name, - tooltip = {'autofill.toggle-tooltip'}, + sprite = 'item/'..setting.item, + tooltip = {'autofill.toggle-tooltip', rich_img('item', setting.item), setting.type}, style = 'shortcut_bar_button_red' } end) @@ -64,12 +68,12 @@ end) end) local amount_textfield = -Gui.element(function(event_trigger, parent, amount) +Gui.element(function(event_trigger, parent, setting) return parent.add{ name = event_trigger, type = 'textfield', - text = amount, - tooltip = {'autofill.amount-tooltip'}, + text = setting.amount, + tooltip = {'autofill.amount-tooltip', setting.type}, clear_and_focus_on_right_click = true } end) @@ -89,13 +93,13 @@ end) end) local add_autofill_setting = -Gui.element(function(_, parent, item_name, amount) - local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-'..item_name } - local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-'..item_name } +Gui.element(function(_, parent, setting) + local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-'..setting.item } + local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-'..setting.item } toggle_flow.style.padding = 0 amount_flow.style.padding = 0 - toggle_item_button(toggle_flow, item_name) - amount_textfield(amount_flow, amount) + toggle_item_button(toggle_flow, setting) + amount_textfield(amount_flow, setting) end) --- Main gui container for the left flow @@ -104,15 +108,16 @@ autofill_container = Gui.element(function(event_trigger, parent) -- Draw the internal container local container = Gui.container(parent, event_trigger, 100) + -- Draw the header local ammo_table = section(container, 'ammo', 2) local fuel_table = section(container, 'fuel', 2) for _, setting in pairs(config.default_settings) do if setting.type == 'ammo' then - add_autofill_setting(ammo_table, setting.item, setting.amount) + add_autofill_setting(ammo_table, setting) elseif setting.type == 'fuel' then - add_autofill_setting(fuel_table, setting.item, setting.amount) + add_autofill_setting(fuel_table, setting) end end @@ -177,11 +182,11 @@ local function entity_build(event) end inserted = entity_inventory.insert({name=item, count=preferd_amount}) player_inventory.remove({name=item, count=inserted}) - print_text(entity.surface, entity.position, {'autofill.filled', '[img=entity/'..entity.name..']', inserted, '[img=item/'..item..']' }, { r = 0, g = 255, b = 0, a = 1}) + print_text(entity.surface, entity.position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item) }, { r = 0, g = 255, b = 0, a = 1}) else inserted = entity_inventory.insert({name=item, count=item_amount}) player_inventory.remove({name=item, count=inserted}) - print_text(entity.surface, entity.position, {'autofill.filled', '[img=entity/'..entity.name..']', inserted, '[img=item/'..item..']' }, { r = 255, g = 165, b = 0, a = 1}) + print_text(entity.surface, entity.position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item) }, { r = 255, g = 165, b = 0, a = 1}) end goto continue end From 9911a0e3d3b872d1d33cc2d55c89e53f48cb6a54 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 20 Aug 2020 21:48:53 +0200 Subject: [PATCH 06/28] Fixed redundand contains function --- modules/gui/autofill.lua | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index a4e7a635..d9419554 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -138,15 +138,6 @@ Event.add(defines.events.on_player_created, function(event) end end) -local function inventories_contains_inventory(inventories, inventory) - for _, inv in pairs(inventories) do - if inv == inventory then - return true - end - end - return false -end - local function entity_build(event) -- Check if player exists local player = game.players[event.player_index] @@ -168,7 +159,7 @@ local function entity_build(event) local player_inventory = player.get_main_inventory() local entity_inventory = entity.get_inventory(entity_config.inventory) for _, setting in pairs(autofill_player_settings[player.name]) do - if not setting.enabled or not inventories_contains_inventory(setting.inventories, entity_config.inventory) then + if not setting.enabled or not table.contains(setting.inventories, entity_config.inventory) then goto continue end local item = setting.item From dac4d40682d5b3c5f6a5a50b8cea900dd405c2af Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 20 Aug 2020 22:29:50 +0200 Subject: [PATCH 07/28] Changed config/settings loop - Fixed settings/entity loop should be faster now - Fixed floating text position overlapping - Added more comments --- config/gui/autofill.lua | 12 ++++++------ modules/gui/autofill.lua | 39 +++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index d7069993..60cc91a8 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -6,38 +6,38 @@ return { icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar entities = { ['car'] = { - { + [defines.inventory.fuel] = { type = 'fuel', inventory = defines.inventory.fuel, enabled = true }, - { + [defines.inventory.car_ammo] = { type = 'ammo', inventory = defines.inventory.car_ammo, enabled = true } }, ['locomotive'] = { - { + [defines.inventory.fuel] = { type = 'fuel', inventory = defines.inventory.fuel, enabled = true } }, ['tank'] = { - { + [defines.inventory.fuel] = { type = 'fuel', inventory = defines.inventory.fuel, enabled = true }, - { + [defines.inventory.car_ammo] = { type = 'ammo', inventory = defines.inventory.car_ammo, enabled = true } }, ['gun-turret'] = { - { + [defines.inventory.turret_ammo] = { type = 'ammo', inventory = defines.inventory.turret_ammo, enabled = true diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index d9419554..9fdc3736 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -150,39 +150,50 @@ local function entity_build(event) if not entity_configs then return end + -- Get the inventory of the player + local player_inventory = player.get_main_inventory() - -- Loop over each entity config and try to furfill the request amount - for _,entity_config in pairs(entity_configs) do - if not entity_config.enabled then - break + local text_position = { x = entity.position.x, y = entity.position.y } + -- Loop over all possible item settings to insert into the entity + for _, setting in pairs(autofill_player_settings[player.name]) do + if not setting.enabled then + goto end_setting end - local player_inventory = player.get_main_inventory() - local entity_inventory = entity.get_inventory(entity_config.inventory) - for _, setting in pairs(autofill_player_settings[player.name]) do - if not setting.enabled or not table.contains(setting.inventories, entity_config.inventory) then - goto continue + -- Loop over possible inventories for this setting to put into the vehicle + for _, inventory in pairs(setting.inventories) do + -- Check in the configs if the inventory type exists and is enabled for this vehicle + if not entity_configs[inventory] or not entity_configs[inventory].enabled then + goto end_inventory end + + -- Get the inventory of the entity + local entity_inventory = entity.get_inventory(inventory) + if not entity_inventory then + goto end_inventory + end + local item = setting.item local preferd_amount = setting.amount local item_amount = player_inventory.get_item_count(item) if item_amount ~= 0 then local inserted + text_position.y = text_position.y - 0.2 if item_amount >= preferd_amount then if not entity_inventory.can_insert({name=item, count=preferd_amount}) then - goto continue + goto end_inventory end inserted = entity_inventory.insert({name=item, count=preferd_amount}) player_inventory.remove({name=item, count=inserted}) - print_text(entity.surface, entity.position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item) }, { r = 0, g = 255, b = 0, a = 1}) + print_text(entity.surface, text_position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item) }, { r = 0, g = 255, b = 0, a = 1}) else inserted = entity_inventory.insert({name=item, count=item_amount}) player_inventory.remove({name=item, count=inserted}) - print_text(entity.surface, entity.position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item) }, { r = 255, g = 165, b = 0, a = 1}) + print_text(entity.surface, text_position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item) }, { r = 255, g = 165, b = 0, a = 1}) end - goto continue end - ::continue:: + ::end_inventory:: end + ::end_setting:: end end From f1537af711024c2064b55cd531ea8c8057cc98cf Mon Sep 17 00:00:00 2001 From: badgamernl Date: Tue, 1 Sep 2020 05:43:05 +0200 Subject: [PATCH 08/28] Major overhaul of inner workings: - Change config layout to entity based; - Change the gui layout to entity based; - Fix entity placed event to work with new system; - Change gui styles to fit other gui's more; - Added demo entities with items: * Car; * Locomotive; * Spidertron; * Gun Turret; * Stone furnace; * Steel furnace; --- config/gui/autofill.lua | 296 +++++++++++++++++++++++----------- locale/en/gui.cfg | 11 +- modules/gui/autofill.lua | 333 +++++++++++++++++++++++++++------------ 3 files changed, 440 insertions(+), 200 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index 60cc91a8..da9069f0 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -1,98 +1,212 @@ --- This file contains all the different settings for the autofill system and gui -- @config Autofill -return { +local table = require 'overrides.table' --- @dep overrides.table + +local ammo = 'ammo' +local fuel = 'fuel' +local shell = 'shell' + +local car = 'car' +local tank = 'tank' +local spidertron = 'spidertron' +local locomotive = 'locomotive' +local gun_turret = 'gun-turret' +local stone_furnace = 'stone-furnace' +local steel_furnace = 'steel-furnace' + +local config = { -- General config icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar - entities = { - ['car'] = { - [defines.inventory.fuel] = { - type = 'fuel', - inventory = defines.inventory.fuel, - enabled = true - }, - [defines.inventory.car_ammo] = { - type = 'ammo', - inventory = defines.inventory.car_ammo, - enabled = true - } - }, - ['locomotive'] = { - [defines.inventory.fuel] = { - type = 'fuel', - inventory = defines.inventory.fuel, - enabled = true - } - }, - ['tank'] = { - [defines.inventory.fuel] = { - type = 'fuel', - inventory = defines.inventory.fuel, - enabled = true - }, - [defines.inventory.car_ammo] = { - type = 'ammo', - inventory = defines.inventory.car_ammo, - enabled = true - } - }, - ['gun-turret'] = { - [defines.inventory.turret_ammo] = { - type = 'ammo', - inventory = defines.inventory.turret_ammo, - enabled = true - } - } + categories = { + ammo = ammo, + fuel = fuel, + shell = shell }, - default_settings = { - { - type = 'ammo', - inventories = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - item = 'uranium-rounds-magazine', - amount = 10, - enabled = false - }, - { - type = 'ammo', - inventories = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - item = 'piercing-rounds-magazine', - amount = 10, - enabled = false - }, - { - type = 'ammo', - inventories = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - item = 'firearm-magazine', - amount = 10, - enabled = false - }, - { - type = 'fuel', - inventories = {defines.inventory.fuel}, - item = 'nuclear-fuel', - amount = 1, - enabled = false - }, - { - type = 'fuel', - inventories = {defines.inventory.fuel}, - item = 'rocket-fuel', - amount = 10, - enabled = false - }, - { - type = 'fuel', - inventories = {defines.inventory.fuel}, - item = 'solid-fuel', - amount = 10, - enabled = false - }, - { - type = 'fuel', - inventories = {defines.inventory.fuel}, - item = 'coal', - amount = 10, - enabled = false - } + entities = { + car = car, + tank = tank, + spidertron = spidertron, + locomotive = locomotive, + gun_turret = gun_turret, + stone_furnace = stone_furnace, + steel_furnace = steel_furnace + }, + default_entities = {} +} + +local default_autofill_item_settings = { + { + category = config.categories.ammo, + entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, + type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, + name = 'uranium-rounds-magazine', + amount = 10, + enabled = false + }, + { + category = config.categories.ammo, + entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, + type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, + name = 'piercing-rounds-magazine', + amount = 10, + enabled = false + }, + { + category = config.categories.ammo, + entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, + type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, + name = 'firearm-magazine', + amount = 10, + enabled = false + }, + { + category = config.categories.ammo, + entity = {config.entities.tank}, + type = {defines.inventory.car_ammo}, + name = 'flamethrower-ammo', + amount = 10, + enabled = false + }, + { + category = config.categories.shell, + entity = {config.entities.tank}, + type = {defines.inventory.car_ammo}, + name = 'cannon-shell', + amount = 10, + enabled = false + }, + { + category = config.categories.shell, + entity = {config.entities.tank}, + type = {defines.inventory.car_ammo}, + name = 'explosive-cannon-shell', + amount = 10, + enabled = false + }, + { + category = config.categories.shell, + entity = {config.entities.tank}, + type = {defines.inventory.car_ammo}, + name = 'uranium-cannon-shell', + amount = 10, + enabled = false + }, + { + category = config.categories.shell, + entity = {config.entities.tank}, + type = {defines.inventory.car_ammo}, + name = 'explosive-uranium-cannon-shell', + amount = 10, + enabled = false + }, + { + category = config.categories.ammo, + entity = {config.entities.spidertron}, + type = {defines.inventory.car_ammo}, + name = 'rocket', + amount = 10, + enabled = false + }, + { + category = config.categories.ammo, + entity = {config.entities.spidertron}, + type = {defines.inventory.car_ammo}, + name = 'explosive-rocket', + amount = 10, + enabled = false + }, + { + category = config.categories.ammo, + entity = {config.entities.spidertron}, + type = {defines.inventory.car_ammo}, + name = 'atomic-bomb', + amount = 10, + enabled = false + }, + { + category = config.categories.fuel, + entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.stone_furnace, config.entities.steel_furnace}, + type = {defines.inventory.fuel}, + name = 'nuclear-fuel', + amount = 1, + enabled = false + }, + { + category = config.categories.fuel, + entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.stone_furnace, config.entities.steel_furnace}, + type = {defines.inventory.fuel}, + name = 'rocket-fuel', + amount = 10, + enabled = false + }, + { + category = config.categories.fuel, + entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.stone_furnace, config.entities.steel_furnace}, + type = {defines.inventory.fuel}, + name = 'solid-fuel', + amount = 10, + enabled = false + }, + { + category = config.categories.fuel, + entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.stone_furnace, config.entities.steel_furnace}, + type = {defines.inventory.fuel}, + name = 'coal', + amount = 10, + enabled = false } -} \ No newline at end of file +} + +local function get_items_by_type(entity, type) + local items = entity.items + for _, item in pairs(default_autofill_item_settings) do + if table.contains(item.entity, entity.entity) then + if table.contains(item.type, type) then + items[item.name] = { + entity = entity.entity, + category = item.category, + type = type, + name = item.name, + amount = item.amount, + enabled = item.enabled + } + end + end + end + return items +end + +local default_entities = config.default_entities + +local function generate_default_setting(entity_name, type, enabled) + if not default_entities[entity_name] then + default_entities[entity_name] = { + entity = entity_name, + enabled = enabled, + items = {} + } + end + get_items_by_type(default_entities[entity_name], type) +end + +generate_default_setting(config.entities.car, defines.inventory.fuel, true) +generate_default_setting(config.entities.car, defines.inventory.car_ammo, true) + +generate_default_setting(config.entities.locomotive, defines.inventory.fuel, true) + +generate_default_setting(config.entities.tank, defines.inventory.fuel, true) +generate_default_setting(config.entities.tank, defines.inventory.car_ammo, true) + +generate_default_setting(config.entities.spidertron, defines.inventory.car_ammo, true) + +generate_default_setting(config.entities.gun_turret, defines.inventory.turret_ammo, true) + +generate_default_setting(config.entities.stone_furnace, defines.inventory.fuel, true) + +generate_default_setting(config.entities.steel_furnace, defines.inventory.fuel, true) + +-- Cleanup temporary table +default_autofill_item_settings = nil + +return config \ No newline at end of file diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index 4d6ab0be..6cebaa17 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -83,13 +83,12 @@ discard-tooltip=Remove task [autofill] main-tooltip=Autofill settings -ammo-caption=Ammo Autofill -ammo-tooltip=Autofill settings when playing entities with a ammo slot -fuel-caption=Fuel Autofill -fuel-tooltip=Autofill settings when placing entities with a fuel slot +toggle-section-tooltip=Expand Section +toggle-section-collapse-tooltip=Collapse Section +toggle-entity-tooltip=Toggle the autofill of __1__ entity (overwrites autofill settings of the entity) toggle-tooltip=Toggle the autofill of __1__ into the __2__ slot of entities -amount-tooltip=Amount of items it will try and put into the __1__ slot of entities -confirmed=Set autofill amount to __1__ for __2__ +amount-tooltip=Amount of items it will try and put into the __1__ slot of __2__ +confirmed=Set autofill amount to __1__ __2__ for __3__ filled=Autofilled the __1__ with __2__ __3__ [warp-list] main-caption=Warp List diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 9fdc3736..a675e147 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -25,99 +25,232 @@ local function rich_img(type, value) return '[img='..type..'/'..value..']' end ---- Draw a section header and main scroll --- @element autofill_section_container -local section = -Gui.element(function(_, parent, section_name, table_size) - -- Draw the header for the section - Gui.header( - parent, - {'autofill.'..section_name..'-caption'}, - {'autofill.'..section_name..'-tooltip'}, - true, - section_name..'-header' - ) - -- Table used to display the settings - local scroll_table = Gui.scroll_table(parent, 215, table_size, section_name..'-scroll-table') - return scroll_table -end) - -local toggle_item_button = -Gui.element(function(event_trigger, parent, setting) - return parent.add{ - name = event_trigger, - type = 'sprite-button', - sprite = 'item/'..setting.item, - tooltip = {'autofill.toggle-tooltip', rich_img('item', setting.item), setting.type}, - style = 'shortcut_bar_button_red' - } -end) -:style(Gui.sprite_style(32, nil, { right_margin = -3 })) -:on_click(function(player, element) - for _, setting in pairs(autofill_player_settings[player.name]) do - if 'item/'..setting.item == element.sprite then - if setting.enabled then - setting.enabled = false - element.style = 'shortcut_bar_button_red' - else - setting.enabled = true - element.style = 'shortcut_bar_button_green' +-- Button to toggle a section dropdown +-- @element toggle_section +local toggle_section = +Gui.element{ + type = 'sprite-button', + sprite = 'utility/expand_dark', + hovered_sprite = 'utility/expand', + tooltip = {'autofill.toggle-section-tooltip'} +} +toggle_section +:style(Gui.sprite_style(20)) +:on_click(function(_, element, _) + local header_flow = element.parent + local flow_name = header_flow.caption + local flow = header_flow.parent.parent[flow_name] + if Gui.toggle_visible_state(flow) then + element.sprite = 'utility/collapse_dark' + element.hovered_sprite = 'utility/collapse' + element.tooltip = {'autofill.toggle-section-collapse-tooltip'} + else + element.sprite = 'utility/expand_dark' + element.hovered_sprite = 'utility/expand' + element.tooltip = {'autofill.toggle-section-tooltip'} + end + local event = {} + for _, child in pairs(element.parent.parent.children) do + if child.alignment and child.alignment[toggle_section.name] then + if element.parent.name ~= child.name then + event.element = child.alignment[toggle_section.name] + toggle_section:raise_custom_event(event) end end end end) +-- Used to assign an event to the header label to trigger a toggle +-- @element header_toggle +local header_toggle = Gui.element() +:on_click(function(_, element, event) + event.element = element.parent.alignment[toggle_section.name] + toggle_section:raise_custom_event(event) +end) +-- Used to assign an event to the header label to trigger a toggle +-- @element header_toggle +local entity_toggle = Gui.element(function(event_trigger, parent, entity_name) + return parent.add{ + name = event_trigger, + type = 'sprite-button', + sprite = 'item/'..entity_name, + tooltip = {'autofill.toggle-entity-tooltip', rich_img('item', entity_name)}, + style = 'shortcut_bar_button_green' + } +end) +:style(Gui.sprite_style(22)) +:on_click(function(player, element, _) + local entity_name = string.sub(element.parent.parent.name, 0, -(1 + string.len('-header'))) + if not autofill_player_settings[player.name] then return end + local setting = autofill_player_settings[player.name][entity_name] + if not setting then return end + if setting.enabled then + setting.enabled = false + element.style = 'shortcut_bar_button_red' + else + setting.enabled = true + element.style = 'shortcut_bar_button_green' + end +end) + +--- Draw a section header and main scroll +-- @element autofill_section_container +local section = +Gui.element(function(_, parent, section_name, table_size) + -- Draw the header for the section + local header = Gui.header( + parent, + {'entity-name.'..section_name}, + {'autofill.toggle-section-tooltip'}, + true, + section_name..'-header', + header_toggle.name + ) + -- Right aligned button to toggle the section + header.caption = section_name + entity_toggle(header, section_name) + toggle_section(header) + + -- Table used to display the settings + local scroll_table = Gui.scroll_table(parent, 999, table_size, section_name) + scroll_table.parent.visible = false + + return scroll_table +end) + + + +local toggle_item_button = +Gui.element(function(event_trigger, parent, item) + return parent.add{ + name = event_trigger, + type = 'sprite-button', + sprite = 'item/'..item.name, + tooltip = {'autofill.toggle-tooltip', rich_img('item', item.name), item.category}, + style = 'shortcut_bar_button_red' + } +end) +:style(Gui.sprite_style(32, nil, { right_margin = -3 })) +:on_click(function(player, element) + local item_name = string.sub(element.parent.name, 1 + string.len('toggle-setting-'), -1) + local entity_name = element.parent.parent.parent.parent.parent.parent.name + if not autofill_player_settings[player.name] then return end + local setting = autofill_player_settings[player.name][entity_name] + if not setting then return end + local item = setting.items[item_name] + if not item then return end + if item.enabled then + item.enabled = false + element.style = 'shortcut_bar_button_red' + else + item.enabled = true + element.style = 'shortcut_bar_button_green' + end +end) + local amount_textfield = -Gui.element(function(event_trigger, parent, setting) +Gui.element(function(event_trigger, parent, item) return parent.add{ name = event_trigger, type = 'textfield', - text = setting.amount, - tooltip = {'autofill.amount-tooltip', setting.type}, + text = item.amount, + tooltip = {'autofill.amount-tooltip', item.category, rich_img('item', item.entity) }, clear_and_focus_on_right_click = true } end) :style{ - maximal_width = 90, - height = 32, + maximal_width = 40, + height = 31, padding = -2 } :on_confirmed(function(player, element, _) - local parent_name = element.parent.name - for _, setting in pairs(autofill_player_settings[player.name]) do - if 'amount-setting-'..setting.item == parent_name then - setting.amount = tonumber(element.text) - player.print({'autofill.confirmed', setting.amount, '[img=item/'..setting.item..']'}) - end - end + local item_name = string.sub(element.parent.name, 1 + string.len('toggle-setting-'), -1) + local entity_name = element.parent.parent.parent.parent.parent.parent.name + if not autofill_player_settings[player.name] then return end + local setting = autofill_player_settings[player.name][entity_name] + if not setting then return end + local item = setting.items[item_name] + if not item then return end + item.amount = tonumber(element.text) + player.print({'autofill.confirmed', item.amount, rich_img('item', item.name), rich_img('entity', entity_name) }) end) local add_autofill_setting = -Gui.element(function(_, parent, setting) - local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-'..setting.item } - local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-'..setting.item } +Gui.element(function(_, parent, item) + local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-'..item.name } + local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-'..item.name } toggle_flow.style.padding = 0 amount_flow.style.padding = 0 - toggle_item_button(toggle_flow, setting) - amount_textfield(amount_flow, setting) + toggle_item_button(toggle_flow, item) + amount_textfield(amount_flow, item) end) +local toggle_item_button_empty = +Gui.element(function(_, parent, i) + return parent.add{ + name = 'toggle-setting-empty-frame-'..i, + type = 'sprite-button' + } +end) +:style(Gui.sprite_style(32, nil, { right_margin = -3 })) + + +local amount_textfield_empty = +Gui.element(function(_, parent, i) + return parent.add{ + name = 'amount-setting-empty-frame-'..i, + type = 'textfield' + } +end) +:style{ maximal_width = 40, height = 31, padding = -2 } + +local add_empty_autofill_setting = +Gui.element(function(_, parent, i) + local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-empty-'..i } + local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-empty-'..i } + toggle_flow.style.padding = 0 + amount_flow.style.padding = 0 + local toggle_element = toggle_item_button_empty(toggle_flow, i) + toggle_element.enabled = false + local amount_element = amount_textfield_empty(amount_flow, i) + amount_element.enabled = false +end) + + --- Main gui container for the left flow -- @element autofill_container autofill_container = Gui.element(function(event_trigger, parent) -- Draw the internal container - local container = Gui.container(parent, event_trigger, 100) - + local container = Gui.container(parent, event_trigger) + container.parent.style.minimal_width = 245 -- Draw the header - local ammo_table = section(container, 'ammo', 2) - local fuel_table = section(container, 'fuel', 2) + for _, setting in pairs(config.default_entities) do + local table_sizes = {} + local tables = {} + local entity_table = section(container, setting.entity, 3) + for _, category in pairs(config.categories) do + if not table_sizes[category] then table_sizes[category] = 0 end + local alignment = Gui.alignment(entity_table, category..'-'..setting.entity, 'center', 'top') + alignment.style.padding = 0 + local category_table = Gui.scroll_table(alignment, 999, 2, category) + category_table.parent.style.padding = 0 + tables[category] = category_table + for _, item in pairs(setting.items) do + if item.category == category then + add_autofill_setting(category_table, item) + table_sizes[category] = table_sizes[category] + 1 + end + end + end - for _, setting in pairs(config.default_settings) do - if setting.type == 'ammo' then - add_autofill_setting(ammo_table, setting) - elseif setting.type == 'fuel' then - add_autofill_setting(fuel_table, setting) + local t = table.get_values(table_sizes) + table.sort(t) + local biggest = t[#t] + for category, size in pairs(table_sizes) do + for i=biggest-size,1,-1 do + add_empty_autofill_setting(tables[category], i) + end end end @@ -134,8 +267,10 @@ Gui.left_toolbar_button(config.icon, {'autofill.main-tooltip'}, autofill_contain Event.add(defines.events.on_player_created, function(event) local player = game.players[event.player_index] if not autofill_player_settings[player.name] then - autofill_player_settings[player.name] = table.deep_copy(config.default_settings) + autofill_player_settings[player.name] = table.deep_copy(config.default_entities) end + + print(game.table_to_json(autofill_player_settings[player.name])) end) local function entity_build(event) @@ -146,54 +281,46 @@ local function entity_build(event) end -- Check if the entity is in the config and enabled local entity = event.created_entity - local entity_configs = config.entities[entity.name] - if not entity_configs then - return - end + + -- Check if player has settings + if not autofill_player_settings[player.name] then return end + + -- Check if autofill for the entity is enabled + if not autofill_player_settings[player.name][entity.name] then return end + -- Get the inventory of the player local player_inventory = player.get_main_inventory() local text_position = { x = entity.position.x, y = entity.position.y } - -- Loop over all possible item settings to insert into the entity - for _, setting in pairs(autofill_player_settings[player.name]) do - if not setting.enabled then - goto end_setting - end - -- Loop over possible inventories for this setting to put into the vehicle - for _, inventory in pairs(setting.inventories) do - -- Check in the configs if the inventory type exists and is enabled for this vehicle - if not entity_configs[inventory] or not entity_configs[inventory].enabled then - goto end_inventory - end + -- Loop over all possible items to insert into the entity + for _, item in pairs(autofill_player_settings[player.name][entity.name].items) do + -- Check if the item is enabled or goto next item + if not item.enabled then goto end_item end - -- Get the inventory of the entity - local entity_inventory = entity.get_inventory(inventory) - if not entity_inventory then - goto end_inventory - end + -- Get the inventory of the entity or goto next item + local entity_inventory = entity.get_inventory(item.type) + if not entity_inventory then goto end_item end - local item = setting.item - local preferd_amount = setting.amount - local item_amount = player_inventory.get_item_count(item) - if item_amount ~= 0 then - local inserted - text_position.y = text_position.y - 0.2 - if item_amount >= preferd_amount then - if not entity_inventory.can_insert({name=item, count=preferd_amount}) then - goto end_inventory - end - inserted = entity_inventory.insert({name=item, count=preferd_amount}) - player_inventory.remove({name=item, count=inserted}) - print_text(entity.surface, text_position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item) }, { r = 0, g = 255, b = 0, a = 1}) - else - inserted = entity_inventory.insert({name=item, count=item_amount}) - player_inventory.remove({name=item, count=inserted}) - print_text(entity.surface, text_position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item) }, { r = 255, g = 165, b = 0, a = 1}) + local preferd_amount = item.amount + local item_amount = player_inventory.get_item_count(item.name) + if item_amount ~= 0 then + local inserted + text_position.y = text_position.y - 0.2 + if item_amount >= preferd_amount then + -- Can item be inserted? no, goto next item! + if not entity_inventory.can_insert({name=item.name, count=preferd_amount}) then + goto end_item end + inserted = entity_inventory.insert({name=item.name, count=preferd_amount}) + player_inventory.remove({name=item.name, count=inserted}) + print_text(entity.surface, text_position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item.name) }, { r = 0, g = 255, b = 0, a = 1}) + else + inserted = entity_inventory.insert({name=item.name, count=item_amount}) + player_inventory.remove({name=item.name, count=inserted}) + print_text(entity.surface, text_position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item.name) }, { r = 255, g = 165, b = 0, a = 1}) end - ::end_inventory:: end - ::end_setting:: + ::end_item:: end end From 665ecfca7c7b222249cd2fd99bd01a8a221f46cd Mon Sep 17 00:00:00 2001 From: badgamernl Date: Wed, 2 Sep 2020 00:10:10 +0200 Subject: [PATCH 09/28] Change in look and feel: - Change the way the header text is displayed; - Change entity toggle to be checkmark and cross; - Added burner mining drill to autofill; - Added comments; - Changed use of some scroll tables to just normal tables; - Fixed entity toggle not working when placing down entities; --- config/gui/autofill.lua | 12 ++++-- locale/en/gui.cfg | 1 + modules/gui/autofill.lua | 87 ++++++++++++++++++++++++++-------------- 3 files changed, 65 insertions(+), 35 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index da9069f0..118e8ec4 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -12,6 +12,7 @@ local tank = 'tank' local spidertron = 'spidertron' local locomotive = 'locomotive' local gun_turret = 'gun-turret' +local burner_mining_drill = 'burner-mining-drill' local stone_furnace = 'stone-furnace' local steel_furnace = 'steel-furnace' @@ -29,6 +30,7 @@ local config = { spidertron = spidertron, locomotive = locomotive, gun_turret = gun_turret, + burner_mining_drill = burner_mining_drill, stone_furnace = stone_furnace, steel_furnace = steel_furnace }, @@ -126,7 +128,7 @@ local default_autofill_item_settings = { }, { category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.stone_furnace, config.entities.steel_furnace}, + entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, type = {defines.inventory.fuel}, name = 'nuclear-fuel', amount = 1, @@ -134,7 +136,7 @@ local default_autofill_item_settings = { }, { category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.stone_furnace, config.entities.steel_furnace}, + entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, type = {defines.inventory.fuel}, name = 'rocket-fuel', amount = 10, @@ -142,7 +144,7 @@ local default_autofill_item_settings = { }, { category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.stone_furnace, config.entities.steel_furnace}, + entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, type = {defines.inventory.fuel}, name = 'solid-fuel', amount = 10, @@ -150,7 +152,7 @@ local default_autofill_item_settings = { }, { category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.stone_furnace, config.entities.steel_furnace}, + entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, type = {defines.inventory.fuel}, name = 'coal', amount = 10, @@ -202,6 +204,8 @@ generate_default_setting(config.entities.spidertron, defines.inventory.car_ammo, generate_default_setting(config.entities.gun_turret, defines.inventory.turret_ammo, true) +generate_default_setting(config.entities.burner_mining_drill, defines.inventory.fuel, true) + generate_default_setting(config.entities.stone_furnace, defines.inventory.fuel, true) generate_default_setting(config.entities.steel_furnace, defines.inventory.fuel, true) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index 6cebaa17..ed99cd97 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -83,6 +83,7 @@ discard-tooltip=Remove task [autofill] main-tooltip=Autofill settings +toggle-section-caption=__1__ __2__ toggle-section-tooltip=Expand Section toggle-section-collapse-tooltip=Collapse Section toggle-entity-tooltip=Toggle the autofill of __1__ entity (overwrites autofill settings of the entity) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index a675e147..7e6dcf03 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -25,8 +25,9 @@ local function rich_img(type, value) return '[img='..type..'/'..value..']' end --- Button to toggle a section dropdown --- @element toggle_section +--- Toggle enitity button, used for toggling autofill for the specific entity +-- All entity autofill settings will be ignored if its disabled +-- @element toggle_item_button local toggle_section = Gui.element{ type = 'sprite-button', @@ -49,31 +50,23 @@ toggle_section element.hovered_sprite = 'utility/expand' element.tooltip = {'autofill.toggle-section-tooltip'} end - local event = {} - for _, child in pairs(element.parent.parent.children) do - if child.alignment and child.alignment[toggle_section.name] then - if element.parent.name ~= child.name then - event.element = child.alignment[toggle_section.name] - toggle_section:raise_custom_event(event) - end - end - end end) --- Used to assign an event to the header label to trigger a toggle +--- Used to assign an event to the header label to trigger a toggle -- @element header_toggle local header_toggle = Gui.element() :on_click(function(_, element, event) event.element = element.parent.alignment[toggle_section.name] toggle_section:raise_custom_event(event) end) --- Used to assign an event to the header label to trigger a toggle + +--- Used to assign an event to the header label to trigger a toggle -- @element header_toggle local entity_toggle = Gui.element(function(event_trigger, parent, entity_name) return parent.add{ name = event_trigger, type = 'sprite-button', - sprite = 'item/'..entity_name, + sprite = 'utility/confirm_slot', tooltip = {'autofill.toggle-entity-tooltip', rich_img('item', entity_name)}, style = 'shortcut_bar_button_green' } @@ -86,9 +79,11 @@ end) if not setting then return end if setting.enabled then setting.enabled = false + element.sprite = 'utility/close_black' element.style = 'shortcut_bar_button_red' else setting.enabled = true + element.sprite = 'utility/confirm_slot' element.style = 'shortcut_bar_button_green' end end) @@ -100,7 +95,7 @@ Gui.element(function(_, parent, section_name, table_size) -- Draw the header for the section local header = Gui.header( parent, - {'entity-name.'..section_name}, + {'autofill.toggle-section-caption', rich_img('item', section_name), {'entity-name.'..section_name}}, {'autofill.toggle-section-tooltip'}, true, section_name..'-header', @@ -111,15 +106,19 @@ Gui.element(function(_, parent, section_name, table_size) entity_toggle(header, section_name) toggle_section(header) - -- Table used to display the settings - local scroll_table = Gui.scroll_table(parent, 999, table_size, section_name) - scroll_table.parent.visible = false + local section_table = parent.add{ + type = 'table', + name = section_name, + column_count = table_size + } - return scroll_table + section_table.visible = false + + return section_table end) - - +--- Toggle item button, used for toggling autofill for the specific item +-- @element toggle_item_button local toggle_item_button = Gui.element(function(event_trigger, parent, item) return parent.add{ @@ -133,7 +132,7 @@ end) :style(Gui.sprite_style(32, nil, { right_margin = -3 })) :on_click(function(player, element) local item_name = string.sub(element.parent.name, 1 + string.len('toggle-setting-'), -1) - local entity_name = element.parent.parent.parent.parent.parent.parent.name + local entity_name = element.parent.parent.parent.parent.name if not autofill_player_settings[player.name] then return end local setting = autofill_player_settings[player.name][entity_name] if not setting then return end @@ -148,6 +147,9 @@ end) end end) + +--- Amount text field for a autofill item +-- @element amount_textfield local amount_textfield = Gui.element(function(event_trigger, parent, item) return parent.add{ @@ -165,7 +167,7 @@ end) } :on_confirmed(function(player, element, _) local item_name = string.sub(element.parent.name, 1 + string.len('toggle-setting-'), -1) - local entity_name = element.parent.parent.parent.parent.parent.parent.name + local entity_name = element.parent.parent.parent.parent.name if not autofill_player_settings[player.name] then return end local setting = autofill_player_settings[player.name][entity_name] if not setting then return end @@ -175,6 +177,8 @@ end) player.print({'autofill.confirmed', item.amount, rich_img('item', item.name), rich_img('entity', entity_name) }) end) +--- Autofill setting, contains a button and a textbox +-- @element add_autofill_setting local add_autofill_setting = Gui.element(function(_, parent, item) local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-'..item.name } @@ -185,6 +189,8 @@ Gui.element(function(_, parent, item) amount_textfield(amount_flow, item) end) +--- Toggle item button empty, just a filler gui element +-- @element toggle_item_button_empty local toggle_item_button_empty = Gui.element(function(_, parent, i) return parent.add{ @@ -195,6 +201,8 @@ end) :style(Gui.sprite_style(32, nil, { right_margin = -3 })) +--- Amount text field empty, just a filler gui element +-- @element amount_textfield_empty local amount_textfield_empty = Gui.element(function(_, parent, i) return parent.add{ @@ -204,6 +212,8 @@ Gui.element(function(_, parent, i) end) :style{ maximal_width = 40, height = 31, padding = -2 } +--- Autofill setting empty, contains filler button and textfield gui elements +-- @element add_empty_autofill_setting local add_empty_autofill_setting = Gui.element(function(_, parent, i) local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-empty-'..i } @@ -223,19 +233,33 @@ autofill_container = Gui.element(function(event_trigger, parent) -- Draw the internal container local container = Gui.container(parent, event_trigger) - container.parent.style.minimal_width = 245 - -- Draw the header + container.parent.style.minimal_width = 257 + -- Draw the scroll container + local scroll_table = Gui.scroll_table(container, 400, 1, 'autofill-scroll-table') + scroll_table.style.vertical_spacing = 0 + scroll_table.parent.vertical_scroll_policy = 'always' + -- Loop over each default entity config for _, setting in pairs(config.default_entities) do local table_sizes = {} local tables = {} - local entity_table = section(container, setting.entity, 3) + -- Draw a section for the element + local entity_table = section(scroll_table, setting.entity, 3) + -- Loop over each item category for _, category in pairs(config.categories) do if not table_sizes[category] then table_sizes[category] = 0 end + -- Draw a alignment gui to make sure the gui is floating to the top of the parent local alignment = Gui.alignment(entity_table, category..'-'..setting.entity, 'center', 'top') alignment.style.padding = 0 - local category_table = Gui.scroll_table(alignment, 999, 2, category) + -- Draw table + local category_table = alignment.add{ + type = 'table', + name = 'category-table', + column_count = 2 + } + category_table.style.vertical_spacing = 1 category_table.parent.style.padding = 0 tables[category] = category_table + -- Add item autofill setting gui elements to the table for _, item in pairs(setting.items) do if item.category == category then add_autofill_setting(category_table, item) @@ -244,6 +268,7 @@ Gui.element(function(event_trigger, parent) end end + -- Add empty gui elements for the categories with less items than the other categories local t = table.get_values(table_sizes) table.sort(t) local biggest = t[#t] @@ -269,8 +294,6 @@ Event.add(defines.events.on_player_created, function(event) if not autofill_player_settings[player.name] then autofill_player_settings[player.name] = table.deep_copy(config.default_entities) end - - print(game.table_to_json(autofill_player_settings[player.name])) end) local function entity_build(event) @@ -285,15 +308,17 @@ local function entity_build(event) -- Check if player has settings if not autofill_player_settings[player.name] then return end + local entity_settings = autofill_player_settings[player.name][entity.name] -- Check if autofill for the entity is enabled - if not autofill_player_settings[player.name][entity.name] then return end + if not entity_settings then return end + if not entity_settings.enabled then return end -- Get the inventory of the player local player_inventory = player.get_main_inventory() local text_position = { x = entity.position.x, y = entity.position.y } -- Loop over all possible items to insert into the entity - for _, item in pairs(autofill_player_settings[player.name][entity.name].items) do + for _, item in pairs(entity_settings.items) do -- Check if the item is enabled or goto next item if not item.enabled then goto end_item end From 58a23a901ac334697398db7bd514ec96a05bae20 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Wed, 2 Sep 2020 16:03:31 +0200 Subject: [PATCH 10/28] Fix amount textbox validation & event --- locale/en/gui.cfg | 3 ++- modules/gui/autofill.lua | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index ed99cd97..aac03bcf 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -89,8 +89,9 @@ toggle-section-collapse-tooltip=Collapse Section toggle-entity-tooltip=Toggle the autofill of __1__ entity (overwrites autofill settings of the entity) toggle-tooltip=Toggle the autofill of __1__ into the __2__ slot of entities amount-tooltip=Amount of items it will try and put into the __1__ slot of __2__ -confirmed=Set autofill amount to __1__ __2__ for __3__ +invalid=Set autofill amount to the maximum __1__ __2__ for __3__ filled=Autofilled the __1__ with __2__ __3__ + [warp-list] main-caption=Warp List main-tooltip=Warp List; Must be within __1__ tiles to use diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 7e6dcf03..cfbda6fe 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -157,7 +157,10 @@ Gui.element(function(event_trigger, parent, item) type = 'textfield', text = item.amount, tooltip = {'autofill.amount-tooltip', item.category, rich_img('item', item.entity) }, - clear_and_focus_on_right_click = true + clear_and_focus_on_right_click = true, + numeric = true, + allow_decimal = false, + allow_negative = false } end) :style{ @@ -165,7 +168,10 @@ end) height = 31, padding = -2 } -:on_confirmed(function(player, element, _) +:on_text_changed(function(player, element, _) + local value = tonumber(element.text) + if not value then value = 0 end + local clamped = math.clamp(value, 0, 1000) local item_name = string.sub(element.parent.name, 1 + string.len('toggle-setting-'), -1) local entity_name = element.parent.parent.parent.parent.name if not autofill_player_settings[player.name] then return end @@ -173,8 +179,12 @@ end) if not setting then return end local item = setting.items[item_name] if not item then return end - item.amount = tonumber(element.text) - player.print({'autofill.confirmed', item.amount, rich_img('item', item.name), rich_img('entity', entity_name) }) + item.amount = clamped + if clamped ~= value then + element.text = clamped + player.print({'autofill.invalid', item.amount, rich_img('item', item.name), rich_img('entity', entity_name) }) + return + end end) --- Autofill setting, contains a button and a textbox From ecadbd653419b2935b664574d46c40883a334ff7 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 02:04:39 +0200 Subject: [PATCH 11/28] Resolve redundant config locals --- config/gui/autofill.lua | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index 118e8ec4..e9d6c6da 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -3,36 +3,23 @@ local table = require 'overrides.table' --- @dep overrides.table -local ammo = 'ammo' -local fuel = 'fuel' -local shell = 'shell' - -local car = 'car' -local tank = 'tank' -local spidertron = 'spidertron' -local locomotive = 'locomotive' -local gun_turret = 'gun-turret' -local burner_mining_drill = 'burner-mining-drill' -local stone_furnace = 'stone-furnace' -local steel_furnace = 'steel-furnace' - local config = { -- General config icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar categories = { - ammo = ammo, - fuel = fuel, - shell = shell + ammo = 'ammo', + fuel = 'fuel', + shell = 'shell' }, entities = { - car = car, - tank = tank, - spidertron = spidertron, - locomotive = locomotive, - gun_turret = gun_turret, - burner_mining_drill = burner_mining_drill, - stone_furnace = stone_furnace, - steel_furnace = steel_furnace + car = 'car', + tank = 'tank', + spidertron = 'spidertron', + locomotive = 'locomotive', + gun_turret = 'gun-turret', + burner_mining_drill = 'burner-mining-drill', + stone_furnace = 'stone-furnace', + steel_furnace = 'steel-furnace' }, default_entities = {} } From f149e446a17fbc06b970c27e0d754fc9c99c10b4 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 03:33:39 +0200 Subject: [PATCH 12/28] Fixed heavy repitition - Introduced EmmyLua documentation (can be removed if needed) - Remamed type to inv --- config/gui/autofill.lua | 207 ++++++++++++++++----------------------- modules/gui/autofill.lua | 14 +-- 2 files changed, 90 insertions(+), 131 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index e9d6c6da..c2a892c6 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -1,11 +1,35 @@ --- This file contains all the different settings for the autofill system and gui -- @config Autofill -local table = require 'overrides.table' --- @dep overrides.table +local table = require 'overrides.table' -- @dep overrides.table + +---@class AutofillEntity +---@field entity string +---@field enabled boolean +---@field items AutofillItem[] + +---@class AutofillItem +---@field entity string +---@field category string +---@field inv number +---@field name string +---@field amount number +---@field enabled boolean + +---@class DefaultItem +---@field name string +---@field amount number +---@field enabled boolean + +---@class DefaultCategory +---@field category string +---@field entity string[] +---@field inv number[] +---@field items DefaultItem[] local config = { -- General config - icon = 'item/piercing-rounds-magazine', --- @setting icon that will be used for the toolbar + icon = 'item/piercing-rounds-magazine', -- @setting icon that will be used for the toolbar categories = { ammo = 'ammo', fuel = 'fuel', @@ -21,162 +45,100 @@ local config = { stone_furnace = 'stone-furnace', steel_furnace = 'steel-furnace' }, + ---@type AutofillEntity[] default_entities = {} } -local default_autofill_item_settings = { +---@type DefaultCategory[] +local default_categories = { { category = config.categories.ammo, entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, - type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - name = 'uranium-rounds-magazine', - amount = 10, - enabled = false - }, - { - category = config.categories.ammo, - entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, - type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - name = 'piercing-rounds-magazine', - amount = 10, - enabled = false - }, - { - category = config.categories.ammo, - entity = {config.entities.car, config.entities.tank, config.entities.gun_turret}, - type = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, - name = 'firearm-magazine', - amount = 10, - enabled = false + inv = {defines.inventory.car_ammo, defines.inventory.turret_ammo}, + items = { + { name = 'uranium-rounds-magazine', amount = 10, enabled = false }, + { name = 'piercing-rounds-magazine', amount = 10, enabled = false }, + { name = 'firearm-magazine', amount = 10, enabled = false }, + } }, { category = config.categories.ammo, entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'flamethrower-ammo', - amount = 10, - enabled = false + inv = {defines.inventory.car_ammo}, + items = { + { name = 'flamethrower-ammo', amount = 10, enabled = false }, + } }, { category = config.categories.shell, entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'cannon-shell', - amount = 10, - enabled = false - }, - { - category = config.categories.shell, - entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'explosive-cannon-shell', - amount = 10, - enabled = false - }, - { - category = config.categories.shell, - entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'uranium-cannon-shell', - amount = 10, - enabled = false - }, - { - category = config.categories.shell, - entity = {config.entities.tank}, - type = {defines.inventory.car_ammo}, - name = 'explosive-uranium-cannon-shell', - amount = 10, - enabled = false + inv = {defines.inventory.car_ammo}, + items = { + { name = 'cannon-shell', amount = 10, enabled = false }, + { name = 'explosive-cannon-shell', amount = 10, enabled = false }, + { name = 'uranium-cannon-shell', amount = 10, enabled = false }, + { name = 'explosive-uranium-cannon-shell', amount = 10, enabled = false }, + } }, { category = config.categories.ammo, entity = {config.entities.spidertron}, - type = {defines.inventory.car_ammo}, - name = 'rocket', - amount = 10, - enabled = false - }, - { - category = config.categories.ammo, - entity = {config.entities.spidertron}, - type = {defines.inventory.car_ammo}, - name = 'explosive-rocket', - amount = 10, - enabled = false - }, - { - category = config.categories.ammo, - entity = {config.entities.spidertron}, - type = {defines.inventory.car_ammo}, - name = 'atomic-bomb', - amount = 10, - enabled = false + inv = {defines.inventory.car_ammo}, + items = { + { name = 'rocket', amount = 10, enabled = false }, + { name = 'explosive-rocket', amount = 10, enabled = false }, + { name = 'atomic-bomb', amount = 10, enabled = false }, + } }, { category = config.categories.fuel, entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, - type = {defines.inventory.fuel}, - name = 'nuclear-fuel', - amount = 1, - enabled = false - }, - { - category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, - type = {defines.inventory.fuel}, - name = 'rocket-fuel', - amount = 10, - enabled = false - }, - { - category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, - type = {defines.inventory.fuel}, - name = 'solid-fuel', - amount = 10, - enabled = false - }, - { - category = config.categories.fuel, - entity = {config.entities.car, config.entities.tank, config.entities.locomotive, config.entities.burner_mining_drill, config.entities.stone_furnace, config.entities.steel_furnace}, - type = {defines.inventory.fuel}, - name = 'coal', - amount = 10, - enabled = false + inv = {defines.inventory.fuel}, + items = { + { name = 'nuclear-fuel', amount = 10, enabled = false }, + { name = 'rocket-fuel', amount = 10, enabled = false }, + { name = 'solid-fuel', amount = 10, enabled = false }, + { name = 'coal', amount = 10, enabled = false }, + } } } -local function get_items_by_type(entity, type) +---@param entity AutofillEntity +---@param inv string +---@return AutofillItem[] +local function get_items_by_inv(entity, inv) local items = entity.items - for _, item in pairs(default_autofill_item_settings) do - if table.contains(item.entity, entity.entity) then - if table.contains(item.type, type) then - items[item.name] = { - entity = entity.entity, - category = item.category, - type = type, - name = item.name, - amount = item.amount, - enabled = item.enabled - } + for _, category in pairs(default_categories) do + if table.contains(category.entity, entity.entity) then + if table.contains(category.inv, inv) then + for _, item in pairs(category.items) do + items[item.name] = { + entity = entity.entity, + category = category.category, + inv = inv, + name = item.name, + amount = item.amount, + enabled = item.enabled + } + end end end end return items end -local default_entities = config.default_entities - -local function generate_default_setting(entity_name, type, enabled) - if not default_entities[entity_name] then - default_entities[entity_name] = { +---@param entity_name string +---@param inv number +---@param enabled boolean +local function generate_default_setting(entity_name, inv, enabled) + if not config.default_entities[entity_name] then + config.default_entities[entity_name] = { entity = entity_name, enabled = enabled, items = {} } end - get_items_by_type(default_entities[entity_name], type) + get_items_by_inv(config.default_entities[entity_name], inv) end generate_default_setting(config.entities.car, defines.inventory.fuel, true) @@ -197,7 +159,4 @@ generate_default_setting(config.entities.stone_furnace, defines.inventory.fuel, generate_default_setting(config.entities.steel_furnace, defines.inventory.fuel, true) --- Cleanup temporary table -default_autofill_item_settings = nil - return config \ No newline at end of file diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index cfbda6fe..51ec376d 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -4,12 +4,12 @@ @alias autofill ]] -local Game = require 'utils.game' --- @dep utils.game -local Gui = require 'expcore.gui' --- @dep expcore.gui -local Global = require 'utils.global' --- @dep utils.global -local config = require 'config.gui.autofill' --- @dep config.gui.autofill -local Event = require 'utils.event' --- @dep utils.event -local table = require 'overrides.table' --- @dep overrides.table +local Game = require 'utils.game' -- @dep utils.game +local Gui = require 'expcore.gui' -- @dep expcore.gui +local Global = require 'utils.global' -- @dep utils.global +local config = require 'config.gui.autofill' -- @dep config.gui.autofill +local Event = require 'utils.event' -- @dep utils.event +local table = require 'overrides.table' -- @dep overrides.table local print_text = Game.print_floating_text -- (surface, position, text, color) @@ -333,7 +333,7 @@ local function entity_build(event) if not item.enabled then goto end_item end -- Get the inventory of the entity or goto next item - local entity_inventory = entity.get_inventory(item.type) + local entity_inventory = entity.get_inventory(item.inv) if not entity_inventory then goto end_item end local preferd_amount = item.amount From 620051ddac4f8a1f59cf1db35ac825ce6fe7a417 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 03:34:59 +0200 Subject: [PATCH 13/28] Fix function chain --- modules/gui/autofill.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 51ec376d..5f448775 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -35,7 +35,6 @@ Gui.element{ hovered_sprite = 'utility/expand', tooltip = {'autofill.toggle-section-tooltip'} } -toggle_section :style(Gui.sprite_style(20)) :on_click(function(_, element, _) local header_flow = element.parent From e23092aa777f1b033f3120eea163924917a597ec Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 03:37:10 +0200 Subject: [PATCH 14/28] Fixed mixup of docs --- modules/gui/autofill.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 5f448775..b3cfd874 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -25,8 +25,7 @@ local function rich_img(type, value) return '[img='..type..'/'..value..']' end ---- Toggle enitity button, used for toggling autofill for the specific entity --- All entity autofill settings will be ignored if its disabled +--- Toggle entity section visibility -- @element toggle_item_button local toggle_section = Gui.element{ @@ -59,8 +58,9 @@ local header_toggle = Gui.element() toggle_section:raise_custom_event(event) end) ---- Used to assign an event to the header label to trigger a toggle --- @element header_toggle +--- Toggle enitity button, used for toggling autofill for the specific entity +-- All entity autofill settings will be ignored if its disabled +-- @element entity_toggle local entity_toggle = Gui.element(function(event_trigger, parent, entity_name) return parent.add{ name = event_trigger, From e52f06ddf9bd68fbcbf68069f2380baf05e8b0f8 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 03:46:23 +0200 Subject: [PATCH 15/28] Fixed locale issues --- locale/en/gui.cfg | 10 +++++----- modules/gui/autofill.lua | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index aac03bcf..83416ba1 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -86,11 +86,11 @@ main-tooltip=Autofill settings toggle-section-caption=__1__ __2__ toggle-section-tooltip=Expand Section toggle-section-collapse-tooltip=Collapse Section -toggle-entity-tooltip=Toggle the autofill of __1__ entity (overwrites autofill settings of the entity) -toggle-tooltip=Toggle the autofill of __1__ into the __2__ slot of entities -amount-tooltip=Amount of items it will try and put into the __1__ slot of __2__ -invalid=Set autofill amount to the maximum __1__ __2__ for __3__ -filled=Autofilled the __1__ with __2__ __3__ +toggle-entity-tooltip=Toggle the autofill of __1__s +toggle-tooltip=Toggle the autofill of __1__ into __2__ slots +amount-tooltip=Amount of items to insert into the __1__ slots +invalid=Autofill set to maximum amount: __1__ __2__ for __3__ +inserted=Inserted __1__ __2__ into __3__ [warp-list] main-caption=Warp List diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index b3cfd874..4c94c32d 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -155,7 +155,7 @@ Gui.element(function(event_trigger, parent, item) name = event_trigger, type = 'textfield', text = item.amount, - tooltip = {'autofill.amount-tooltip', item.category, rich_img('item', item.entity) }, + tooltip = {'autofill.amount-tooltip', item.category }, clear_and_focus_on_right_click = true, numeric = true, allow_decimal = false, @@ -340,19 +340,19 @@ local function entity_build(event) if item_amount ~= 0 then local inserted text_position.y = text_position.y - 0.2 + local color = { r = 0, g = 255, b = 0, a = 1} if item_amount >= preferd_amount then -- Can item be inserted? no, goto next item! if not entity_inventory.can_insert({name=item.name, count=preferd_amount}) then goto end_item end inserted = entity_inventory.insert({name=item.name, count=preferd_amount}) - player_inventory.remove({name=item.name, count=inserted}) - print_text(entity.surface, text_position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item.name) }, { r = 0, g = 255, b = 0, a = 1}) else inserted = entity_inventory.insert({name=item.name, count=item_amount}) - player_inventory.remove({name=item.name, count=inserted}) - print_text(entity.surface, text_position, {'autofill.filled', rich_img('entity', entity.name), inserted, rich_img('item', item.name) }, { r = 255, g = 165, b = 0, a = 1}) + color = { r = 255, g = 165, b = 0, a = 1} end + player_inventory.remove({name=item.name, count=inserted}) + print_text(entity.surface, text_position, {'autofill.inserted', inserted, rich_img('item', item.name), rich_img('entity', entity.name) }, color) end ::end_item:: end From d19cb5e8d79a925c91d067ed2632a93aed2487d9 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 03:48:48 +0200 Subject: [PATCH 16/28] Removed EmmaLua because CI complained --- config/gui/autofill.lua | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/config/gui/autofill.lua b/config/gui/autofill.lua index c2a892c6..b717f97d 100644 --- a/config/gui/autofill.lua +++ b/config/gui/autofill.lua @@ -3,30 +3,6 @@ local table = require 'overrides.table' -- @dep overrides.table ----@class AutofillEntity ----@field entity string ----@field enabled boolean ----@field items AutofillItem[] - ----@class AutofillItem ----@field entity string ----@field category string ----@field inv number ----@field name string ----@field amount number ----@field enabled boolean - ----@class DefaultItem ----@field name string ----@field amount number ----@field enabled boolean - ----@class DefaultCategory ----@field category string ----@field entity string[] ----@field inv number[] ----@field items DefaultItem[] - local config = { -- General config icon = 'item/piercing-rounds-magazine', -- @setting icon that will be used for the toolbar @@ -45,11 +21,9 @@ local config = { stone_furnace = 'stone-furnace', steel_furnace = 'steel-furnace' }, - ---@type AutofillEntity[] default_entities = {} } ----@type DefaultCategory[] local default_categories = { { category = config.categories.ammo, @@ -103,9 +77,6 @@ local default_categories = { } } ----@param entity AutofillEntity ----@param inv string ----@return AutofillItem[] local function get_items_by_inv(entity, inv) local items = entity.items for _, category in pairs(default_categories) do @@ -127,9 +98,6 @@ local function get_items_by_inv(entity, inv) return items end ----@param entity_name string ----@param inv number ----@param enabled boolean local function generate_default_setting(entity_name, inv, enabled) if not config.default_entities[entity_name] then config.default_entities[entity_name] = { From b03dbf8674d3af5664fc18a381542cb4a839b73f Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 04:22:29 +0200 Subject: [PATCH 17/28] Fix player.print --- modules/gui/autofill.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 4c94c32d..7b066c1f 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -181,7 +181,7 @@ end) item.amount = clamped if clamped ~= value then element.text = clamped - player.print({'autofill.invalid', item.amount, rich_img('item', item.name), rich_img('entity', entity_name) }) + player.print{'autofill.invalid', item.amount, rich_img('item', item.name), rich_img('entity', entity_name) } return end end) From 4100ba9876d73c5f77fb5a2c3b163306d2527076 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Thu, 3 Sep 2020 04:27:28 +0200 Subject: [PATCH 18/28] Fix function calls --- modules/gui/autofill.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 7b066c1f..f95fd5aa 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -343,15 +343,15 @@ local function entity_build(event) local color = { r = 0, g = 255, b = 0, a = 1} if item_amount >= preferd_amount then -- Can item be inserted? no, goto next item! - if not entity_inventory.can_insert({name=item.name, count=preferd_amount}) then + if not entity_inventory.can_insert{name=item.name, count=preferd_amount} then goto end_item end - inserted = entity_inventory.insert({name=item.name, count=preferd_amount}) + inserted = entity_inventory.insert{name=item.name, count=preferd_amount} else - inserted = entity_inventory.insert({name=item.name, count=item_amount}) + inserted = entity_inventory.insert{name=item.name, count=item_amount} color = { r = 255, g = 165, b = 0, a = 1} end - player_inventory.remove({name=item.name, count=inserted}) + player_inventory.remove{name=item.name, count=inserted} print_text(entity.surface, text_position, {'autofill.inserted', inserted, rich_img('item', item.name), rich_img('entity', entity.name) }, color) end ::end_item:: From 00bb67e9a01972917d41e4a803f223183ee5d306 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Sat, 5 Sep 2020 00:32:21 +0200 Subject: [PATCH 19/28] Fix scroll panel padding issues --- modules/gui/autofill.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index f95fd5aa..8623eb3b 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -242,11 +242,14 @@ autofill_container = Gui.element(function(event_trigger, parent) -- Draw the internal container local container = Gui.container(parent, event_trigger) - container.parent.style.minimal_width = 257 -- Draw the scroll container local scroll_table = Gui.scroll_table(container, 400, 1, 'autofill-scroll-table') - scroll_table.style.vertical_spacing = 0 + -- Set the scroll panel to always show the scrollbar (not doing this will result in a changing gui size) scroll_table.parent.vertical_scroll_policy = 'always' + -- Scroll panel has by default padding + scroll_table.parent.style.padding = 0 + -- Remove the default gap that is added in a table between elements + scroll_table.style.vertical_spacing = 0 -- Loop over each default entity config for _, setting in pairs(config.default_entities) do local table_sizes = {} From ef823f2f69061cda4f96227020bc319ab93b2354 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Sat, 5 Sep 2020 00:44:11 +0200 Subject: [PATCH 20/28] Fix empty placeholder using unneeded gui element --- modules/gui/autofill.lua | 45 +++++++++++++--------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 8623eb3b..0dc52f9d 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -198,40 +198,23 @@ Gui.element(function(_, parent, item) amount_textfield(amount_flow, item) end) ---- Toggle item button empty, just a filler gui element --- @element toggle_item_button_empty -local toggle_item_button_empty = -Gui.element(function(_, parent, i) - return parent.add{ - name = 'toggle-setting-empty-frame-'..i, - type = 'sprite-button' - } -end) -:style(Gui.sprite_style(32, nil, { right_margin = -3 })) - - ---- Amount text field empty, just a filler gui element --- @element amount_textfield_empty -local amount_textfield_empty = -Gui.element(function(_, parent, i) - return parent.add{ - name = 'amount-setting-empty-frame-'..i, - type = 'textfield' - } -end) -:style{ maximal_width = 40, height = 31, padding = -2 } - --- Autofill setting empty, contains filler button and textfield gui elements -- @element add_empty_autofill_setting local add_empty_autofill_setting = -Gui.element(function(_, parent, i) - local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-empty-'..i } - local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-empty-'..i } - toggle_flow.style.padding = 0 - amount_flow.style.padding = 0 - local toggle_element = toggle_item_button_empty(toggle_flow, i) +Gui.element(function(_, parent) + local toggle_element = parent.add{ + type = 'sprite-button' + } + toggle_element.style.right_margin = -3 + toggle_element.style.width = 32 + toggle_element.style.height = 32 toggle_element.enabled = false - local amount_element = amount_textfield_empty(amount_flow, i) + local amount_element = parent.add{ + type = 'textfield' + } + amount_element.style.maximal_width = 40 + amount_element.style.height = 31 + amount_element.style.padding = -2 amount_element.enabled = false end) @@ -286,7 +269,7 @@ Gui.element(function(event_trigger, parent) local biggest = t[#t] for category, size in pairs(table_sizes) do for i=biggest-size,1,-1 do - add_empty_autofill_setting(tables[category], i) + add_empty_autofill_setting(tables[category]) end end end From ca2ff8075e867065f21d19da994bbdb960f9d716 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Sat, 5 Sep 2020 00:48:28 +0200 Subject: [PATCH 21/28] Extra space added to entity table (There was some extra horizontal space that was empty) --- modules/gui/autofill.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 0dc52f9d..74231084 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -239,6 +239,7 @@ Gui.element(function(event_trigger, parent) local tables = {} -- Draw a section for the element local entity_table = section(scroll_table, setting.entity, 3) + entity_table.style.padding = 5 -- Loop over each item category for _, category in pairs(config.categories) do if not table_sizes[category] then table_sizes[category] = 0 end From cbb925eb65b3759ca1d510e4bb12ca99a60d348b Mon Sep 17 00:00:00 2001 From: badgamernl Date: Mon, 7 Sep 2020 00:52:40 +0200 Subject: [PATCH 22/28] Fix item and entity name assingment - Replaced with a more readable version --- modules/gui/autofill.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 74231084..fcde8c32 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -72,7 +72,7 @@ local entity_toggle = Gui.element(function(event_trigger, parent, entity_name) end) :style(Gui.sprite_style(22)) :on_click(function(player, element, _) - local entity_name = string.sub(element.parent.parent.name, 0, -(1 + string.len('-header'))) + local entity_name = string.match(element.parent.parent.name,'(.*)%-header') if not autofill_player_settings[player.name] then return end local setting = autofill_player_settings[player.name][entity_name] if not setting then return end @@ -130,7 +130,7 @@ Gui.element(function(event_trigger, parent, item) end) :style(Gui.sprite_style(32, nil, { right_margin = -3 })) :on_click(function(player, element) - local item_name = string.sub(element.parent.name, 1 + string.len('toggle-setting-'), -1) + local item_name = string.match(element.parent.name,'toggle%-setting%-(.*)') local entity_name = element.parent.parent.parent.parent.name if not autofill_player_settings[player.name] then return end local setting = autofill_player_settings[player.name][entity_name] @@ -171,7 +171,7 @@ end) local value = tonumber(element.text) if not value then value = 0 end local clamped = math.clamp(value, 0, 1000) - local item_name = string.sub(element.parent.name, 1 + string.len('toggle-setting-'), -1) + local item_name = string.match(element.parent.name,'amount%-setting%-(.*)') local entity_name = element.parent.parent.parent.parent.name if not autofill_player_settings[player.name] then return end local setting = autofill_player_settings[player.name][entity_name] From f76d8b0a3a23cd66e00d7c8872c2ef32cd02dba2 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Mon, 7 Sep 2020 05:03:28 +0200 Subject: [PATCH 23/28] Fix unneeded string.match - Tip from cool to use tooltip on flow --- modules/gui/autofill.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index fcde8c32..12625886 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -130,7 +130,7 @@ Gui.element(function(event_trigger, parent, item) end) :style(Gui.sprite_style(32, nil, { right_margin = -3 })) :on_click(function(player, element) - local item_name = string.match(element.parent.name,'toggle%-setting%-(.*)') + local item_name = element.parent.tooltip local entity_name = element.parent.parent.parent.parent.name if not autofill_player_settings[player.name] then return end local setting = autofill_player_settings[player.name][entity_name] @@ -171,7 +171,7 @@ end) local value = tonumber(element.text) if not value then value = 0 end local clamped = math.clamp(value, 0, 1000) - local item_name = string.match(element.parent.name,'amount%-setting%-(.*)') + local item_name = element.parent.tooltip local entity_name = element.parent.parent.parent.parent.name if not autofill_player_settings[player.name] then return end local setting = autofill_player_settings[player.name][entity_name] @@ -190,8 +190,8 @@ end) -- @element add_autofill_setting local add_autofill_setting = Gui.element(function(_, parent, item) - local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-'..item.name } - local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-'..item.name } + local toggle_flow = parent.add{ type = 'flow', name = 'toggle-setting-'..item.name, tooltip = item.name } + local amount_flow = parent.add{ type = 'flow', name = 'amount-setting-'..item.name, tooltip = item.name } toggle_flow.style.padding = 0 amount_flow.style.padding = 0 toggle_item_button(toggle_flow, item) From a01ba807f5fa58fb8764dc6bf6c71d316546ab3d Mon Sep 17 00:00:00 2001 From: badgamernl Date: Mon, 7 Sep 2020 05:11:29 +0200 Subject: [PATCH 24/28] Change padding (Was causing the gui width to change when entity expanded) --- modules/gui/autofill.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 12625886..20d2ef49 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -239,7 +239,7 @@ Gui.element(function(event_trigger, parent) local tables = {} -- Draw a section for the element local entity_table = section(scroll_table, setting.entity, 3) - entity_table.style.padding = 5 + entity_table.style.padding = 3 -- Loop over each item category for _, category in pairs(config.categories) do if not table_sizes[category] then table_sizes[category] = 0 end From afcd16c56e30fcb3bc3bd3bf7935c4fc9b952ce4 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Tue, 8 Sep 2020 16:20:02 +0200 Subject: [PATCH 25/28] Fixed table center Fixed locale --- locale/en/gui.cfg | 2 +- modules/gui/autofill.lua | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index 83416ba1..e4fa8611 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -86,7 +86,7 @@ main-tooltip=Autofill settings toggle-section-caption=__1__ __2__ toggle-section-tooltip=Expand Section toggle-section-collapse-tooltip=Collapse Section -toggle-entity-tooltip=Toggle the autofill of __1__s +toggle-entity-tooltip=Toggle the autofill of __1__ toggle-tooltip=Toggle the autofill of __1__ into __2__ slots amount-tooltip=Amount of items to insert into the __1__ slots invalid=Autofill set to maximum amount: __1__ __2__ for __3__ diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 20d2ef49..37146dea 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -233,6 +233,8 @@ Gui.element(function(event_trigger, parent) scroll_table.parent.style.padding = 0 -- Remove the default gap that is added in a table between elements scroll_table.style.vertical_spacing = 0 + -- Center the first collumn in the table + scroll_table.style.column_alignments[1] = 'center' -- Loop over each default entity config for _, setting in pairs(config.default_entities) do local table_sizes = {} From 7449535aa7610f1801a5cf1cb9cdac5542202413 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Tue, 8 Sep 2020 17:13:18 +0200 Subject: [PATCH 26/28] Remove alignment flow - This should also help with longer entity names --- modules/gui/autofill.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 37146dea..5c4c93eb 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -241,21 +241,23 @@ Gui.element(function(event_trigger, parent) local tables = {} -- Draw a section for the element local entity_table = section(scroll_table, setting.entity, 3) + -- Add some padding around the table entity_table.style.padding = 3 + -- Make sure each collumn is alignment top center + entity_table.style.column_alignments[1] = 'top-center' + entity_table.style.column_alignments[2] = 'top-center' + entity_table.style.column_alignments[3] = 'top-center' -- Loop over each item category for _, category in pairs(config.categories) do if not table_sizes[category] then table_sizes[category] = 0 end - -- Draw a alignment gui to make sure the gui is floating to the top of the parent - local alignment = Gui.alignment(entity_table, category..'-'..setting.entity, 'center', 'top') - alignment.style.padding = 0 -- Draw table - local category_table = alignment.add{ + local category_table = entity_table.add{ type = 'table', - name = 'category-table', + name = category..'-category', column_count = 2 } + -- Add padding between each item category_table.style.vertical_spacing = 1 - category_table.parent.style.padding = 0 tables[category] = category_table -- Add item autofill setting gui elements to the table for _, item in pairs(setting.items) do From 72a3378bfad4e2f74f54e20cd1a98e76431d3ec8 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Tue, 8 Sep 2020 20:34:38 +0200 Subject: [PATCH 27/28] Fix toggle item button --- modules/gui/autofill.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 5c4c93eb..c80ea2d4 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -131,7 +131,7 @@ end) :style(Gui.sprite_style(32, nil, { right_margin = -3 })) :on_click(function(player, element) local item_name = element.parent.tooltip - local entity_name = element.parent.parent.parent.parent.name + local entity_name = element.parent.parent.parent.name if not autofill_player_settings[player.name] then return end local setting = autofill_player_settings[player.name][entity_name] if not setting then return end From c46244e7edf7adc3df449c12077380ce21405960 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Fri, 11 Sep 2020 20:48:35 +0200 Subject: [PATCH 28/28] Fix floating text position --- modules/gui/autofill.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index c80ea2d4..f89a8c98 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -330,7 +330,7 @@ local function entity_build(event) local item_amount = player_inventory.get_item_count(item.name) if item_amount ~= 0 then local inserted - text_position.y = text_position.y - 0.2 + text_position.y = text_position.y - 0.5 local color = { r = 0, g = 255, b = 0, a = 1} if item_amount >= preferd_amount then -- Can item be inserted? no, goto next item!