gui-beta.lua example
local event = require("__flib__.event")
local gui = require("__flib__.gui-beta")
local migration = require("__flib__.migration")
local mod_gui = require("__core__.lualib.mod-gui")
local function create_guis(player)
local location_button = mod_gui.get_button_flow(player).add{
type = "button",
style = mod_gui.button_style,
caption = "0, 0",
tags = {
[script.mod_name] = {
flib = {
on_click = "recenter_inventory_gui"
}
}
}
}
local function frame_action_button(side)
return {
type = "sprite-button",
style = "frame_action_button",
tags = {side = side},
actions = {
on_click = "print_titlebar_click"
}
}
end
local inventory_refs = gui.build(player.gui.screen, {
{
type = "frame",
direction = "vertical",
ref = {"window"},
actions = {
on_location_changed = "update_location_string"
},
children = {
{type = "flow", ref = {"titlebar", "flow"}, children = {
{type = "label", style = "frame_title", caption = "Demo GUI", ignored_by_interaction = true},
{type = "empty-widget", style = "flib_titlebar_drag_handle", ignored_by_interaction = true},
frame_action_button("left"),
frame_action_button("center"),
frame_action_button("right")
}},
{type = "frame", style = "inside_shallow_frame_with_padding", style_mods = {padding = 12}, children = {
{type = "frame", style = "slot_button_deep_frame", children = {
{
type = "scroll-pane",
style = "flib_naked_scroll_pane_no_padding",
style_mods = {height = 200},
children = {
{
type = "table",
style = "slot_table",
style_mods = {width = 400},
column_count = 10,
ref = {"slot_table"}
}
}
}
}}
}}
}
}
})
inventory_refs.titlebar.flow.drag_target = inventory_refs.window
inventory_refs.window.force_auto_center()
global.players[player.index].guis = {inventory = inventory_refs, location = location_button}
end
local function destroy_guis(player_table)
player_table.guis.inventory.window.destroy()
player_table.guis.location.destroy()
player_table.guis = nil
end
local function handle_action(msg, e)
if msg == "print_titlebar_click" then
local side = gui.get_tags(e.element).side
game.get_player(e.player_index).print("You clicked the "..side.." titlebar button!")
elseif msg == "print_sprite_click" then
local _, _, name = string.find(e.element.sprite, "item/(.*)")
game.get_player(e.player_index).print("You clicked the "..name.." slot button!")
elseif msg == "update_location_string" then
local location = e.element.location
global.players[e.player_index].guis.location.caption = location.x..", "..location.y
elseif msg == "recenter_inventory_gui" then
global.players[e.player_index].guis.inventory.window.force_auto_center()
end
end
event.on_init(function()
global.players = {}
for i, player in pairs(game.players) do
global.players[i] = {}
create_guis(player)
end
end)
event.on_configuration_changed(function(e)
if migration.on_config_changed(e, {}) then
for i, player in pairs(game.players) do
destroy_guis(global.players[i])
create_guis(player)
end
end
end)
gui.hook_events(function(e)
local msg = gui.read_action(e)
if msg then
handle_action(msg, e)
elseif e.name == defines.events.on_gui_closed and e.gui_type == 16 then
end
end)
event.on_player_created(function(e)
global.players[e.player_index] = {}
local player = game.get_player(e.player_index)
create_guis(player)
end)
event.on_player_removed(function(e)
global.players[e.player_index] = nil
end)
event.on_player_main_inventory_changed(function(e)
local player = game.get_player(e.player_index)
local table = global.players[e.player_index].guis.inventory.slot_table
local children = table.children
local i = 0
for name, count in pairs(player.get_main_inventory().get_contents()) do
i = i + 1
local child = children[i]
if child then
child.sprite = "item/"..name
child.number = count
else
table.add{
type = "sprite-button",
style = "slot_button",
sprite = "item/"..name,
number = count,
tags = {
[script.mod_name] = {
flib = {
on_click = "print_sprite_click"
}
}
}
}
end
end
for j = i + 1, #children do
children[j].destroy()
end
end)