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;
This commit is contained in:
badgamernl
2020-08-19 01:35:03 +02:00
parent 0900fe13ea
commit c2ea0bf590
3 changed files with 150 additions and 17 deletions

View File

@@ -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
}
}
}

View File

@@ -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

View File

@@ -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)