Skip to content

Commit

Permalink
get network-tank autoconfig working
Browse files Browse the repository at this point in the history
  • Loading branch information
bengardner committed Nov 24, 2023
1 parent 5eee88c commit 10af41e
Show file tree
Hide file tree
Showing 9 changed files with 607 additions and 158 deletions.
1 change: 1 addition & 0 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ require "src.NetworkChest"
require "src.NetworkSensor"
require "src.NetworkViewUi"
require "src.NetworkViewUi_test"
require "src.NetworkTankGui"
2 changes: 1 addition & 1 deletion data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function M.add_network_tank()
},
se_allow_in_space = true,
allow_copy_paste = true,
additional_pastable_entities = { "network-tank" },
additional_pastable_entities = { name },
max_health = 200,
}

Expand Down
2 changes: 0 additions & 2 deletions src/GlobalState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,6 @@ function M.update_queue_lists(update_entity)
-- nil means entity is invalid.
if pri_adj ~= nil then
M.queue_insert(unit_number, old_pri + pri_adj)
else
clog("Dropped: G unum %s", unit_number)
end
end
end
Expand Down
182 changes: 182 additions & 0 deletions src/GuiManager.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
--[[
Some helpers for managing the UI persistant data.
At the top of the UI file, do something like:
M.mgr = GuiManager.new("my_unique_tag")
]]
local GlobalState = require "src.GlobalState"
local clog = require("src.log_console").log

local GuiManager = {}
function GuiManager.new(name)
local self = { name=name }
return setmetatable(self, { __index = GuiManager })
end

function GuiManager:get(player_index)
return GlobalState.get_ui_state(player_index)[self.name]
end

function GuiManager:set(player_index, value)
GlobalState.get_ui_state(player_index)[self.name] = value
end

function GuiManager:destroy(player_index)
local ui = GlobalState.get_ui_state(player_index)
local inst = ui[self.name]
if inst ~= nil then
-- break the link to prevent future events
ui[self.name] = nil

-- call destructor on any child classes
if type(inst.children) == 'table' then
for _, ch in pairs(inst.children) do
if type(ch.destroy) == "function" then
ch.destroy(ch)
end
end
end

if inst.elems ~= nil and inst.elems.main_window ~= nil then
-- remove player focus
local player = inst.player or game.get_player(player_index)
if player ~= nil and player.opened == inst.elems.main_window then
player.opened = nil
end

-- destroy the UI
inst.elems.main_window.destroy()
end
end
end

-- returns a function that can be attached to an event
function GuiManager:wrap(func)
-- callback takes an event, calls get() and passes that to the function
return function (event)
local inst = self:get(event.player_index)
--clog("wrap event %s", serpent.line(inst))
if inst ~= nil then
func(inst, event)
end
end
end

--[[
Creates a new top-level window and returns the table of elements.
This sets the window as the current
@player is the player that gets the GUI
@title is the name to put in the title bar
@opts selects the extra buttons to enable
'window_name' = unqiue name for the window (uses title if omitted)
'refresh_button' = unique name for the refresh button (not created if omitted)
'close_button' = unique name for the close button (not created if omitted)
'pin_button' = unique name for the pin button (not created if omitted)
retval.elems.body is set to a vertical flow where the GUI elements should be added.
]]
function GuiManager:create_window(player, title, opts)
local elems = {}

-- create the main window
local main_window = player.gui.screen.add({
type = "frame",
name = opts.window_name or title,
style = "inset_frame_container_frame",
})
main_window.auto_center = true
main_window.style.horizontally_stretchable = true
main_window.style.vertically_stretchable = true
elems.main_window = main_window

-- create a vertical flow to cover the entire window body
local vert_flow = main_window.add({
type = "flow",
direction = "vertical",
})
vert_flow.style.horizontally_stretchable = true
vert_flow.style.vertically_stretchable = true

-- add the header/toolbar flow
local title_flow = vert_flow.add({
type = "flow",
direction = "horizontal",
})
title_flow.drag_target = main_window
elems.title_flow = title_flow

-- add the window title
title_flow.add {
type = "label",
caption = title,
style = "frame_title",
ignored_by_interaction = true,
}

-- add the drag space
local header_drag = title_flow.add {
type = "empty-widget",
style = "draggable_space_header",
ignored_by_interaction = true,
}
header_drag.style.horizontally_stretchable = true
header_drag.style.vertically_stretchable = true
header_drag.style.height = 24

local name = opts.refresh_button
if name ~= nil then
elems.refresh_button = title_flow.add {
name = name,
type = "sprite-button",
sprite = "utility/refresh",
style = "frame_action_button",
tooltip = { "gui.refresh" },
}
end

name = opts.close_button
if name ~= nil then
elems.close_button = title_flow.add {
name = name,
type = "sprite-button",
sprite = "utility/close_white",
hovered_sprite = "utility/close_black",
clicked_sprite = "utility/close_black",
style = "close_button",
}
end

name = opts.pin_button
if name ~= nil then
elems.pin_button = title_flow.add {
name = name,
type = "sprite-button",
sprite = "flib_pin_white",
hovered_sprite = "flib_pin_black",
clicked_sprite = "flib_pin_black",
style = "frame_action_button",
}
end

-- create the window body flow
elems.body = vert_flow.add({
type = "flow",
direction = "vertical",
})

-- give focus to the window (make optional?)
player.opened = main_window

-- start the UI data
local inst = {
elems = elems,
player = player,
-- children = nil,
}

self:set(player.index, inst)

return inst
end

return GuiManager
33 changes: 10 additions & 23 deletions src/NetworkChest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local NetworkChestGui = require "src.NetworkChestGui"
local UiHandlers = require "src.UiHandlers"
local NetworkViewUi = require "src.NetworkViewUi"
local UiConstants = require "src.UiConstants"
local NetworkTankGui = require "src.NetworkTankGui"
local Event = require('__stdlib__/stdlib/event/event')
local util = require("util") -- from core/lualib
local clog = require("src.log_console").log
Expand Down Expand Up @@ -40,7 +39,7 @@ local function generic_create_handler(event)
M.on_create(event, entity)
elseif entity.name == "network-chest-requester" then
M.on_create(event, entity)
elseif entity.name == "network-tank" then
elseif constants.NETWORK_TANK_NAMES[entity.name] ~= nil then
local config = nil
if event.tags ~= nil then
local config_tag = event.tags.config
Expand Down Expand Up @@ -78,7 +77,7 @@ function M.on_entity_cloned(event)
if source_info ~= nil and dest_info ~= nil then
dest_info.requests = source_info.requests
end
elseif name == "network-tank" then
elseif constants.NETWORK_TANK_NAMES[name] ~= nil then
GlobalState.register_tank_entity(event.source)
GlobalState.register_tank_entity(event.destination)
GlobalState.copy_tank_config(
Expand Down Expand Up @@ -124,7 +123,8 @@ function M.generic_destroy_handler(event, opts)
global.mod.network_chest_gui.frame.destroy()
global.mod.network_chest_gui = nil
end
elseif entity.name == "network-tank" then

elseif constants.NETWORK_TANK_NAMES[entity.name] ~= nil then
GlobalState.put_tank_contents_in_network(entity)
if not opts.do_not_delete_entity then
GlobalState.delete_tank_entity(unit_number)
Expand Down Expand Up @@ -162,7 +162,7 @@ end
function M.on_marked_for_deconstruction(event)
if event.entity.name == "network-chest" then
GlobalState.put_chest_contents_in_network(event.entity)
elseif event.entity.name == "network-tank" then
elseif constants.NETWORK_TANK_NAMES[event.entity.name] ~= nil then
GlobalState.put_tank_contents_in_network(event.entity)
end
end
Expand Down Expand Up @@ -248,9 +248,9 @@ function M.on_player_setup_blueprint(event)
)
end
end
elseif entity.name == "network-tank" then
elseif constants.NETWORK_TANK_NAMES[entity.name] ~= nil then
local real_entity = event.surface.find_entity(
"network-tank",
entity.name,
entity.position
)
if real_entity ~= nil then
Expand Down Expand Up @@ -323,8 +323,8 @@ function M.on_entity_settings_pasted(event)
end
end

elseif dest.name == "network-tank" then
if source.name == "network-tank" then
elseif constants.NETWORK_TANK_NAMES[dest.name] ~= nil then
if source.name == dest.name then
GlobalState.copy_tank_config(source.unit_number, dest.unit_number)
end

Expand Down Expand Up @@ -1129,8 +1129,7 @@ local function update_entity(unit_number, priority)
return retval
end

-- unknown/invalid unit_number
clog("Dropped: unknown unum %s", unit_number)
-- unknown/invalid unit_number (probably already removed)
return nil
end

Expand Down Expand Up @@ -1345,25 +1344,13 @@ function M.on_gui_opened(event)
end

NetworkChestGui.on_gui_opened(player, entity)
elseif event.gui_type == defines.gui_type.entity and event.entity.name == "network-tank" then
local entity = event.entity
assert(GlobalState.get_tank_info(entity.unit_number) ~= nil)

local player = game.get_player(event.player_index)
if player == nil then
return
end

NetworkTankGui.on_gui_opened(player, entity)
end
end

function M.on_gui_closed(event)
local frame = event.element
if frame ~= nil and frame.name == UiConstants.NV_FRAME then
NetworkViewUi.on_gui_closed(event)
elseif frame ~= nil and frame.name == UiConstants.NT_MAIN_FRAME then
NetworkTankGui.on_gui_closed(event)
elseif frame ~= nil and (frame.name == UiConstants.MAIN_FRAME_NAME or frame.name == UiConstants.MODAL_FRAME_NAME) then
NetworkChestGui.on_gui_closed(event)
end
Expand Down
Loading

0 comments on commit 10af41e

Please sign in to comment.