1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/utils/gui.lua

116 lines
3.0 KiB
Lua
Raw Normal View History

2018-05-20 17:28:54 +02:00
local Token = require 'utils.global_token'
local Event = require 'utils.event'
local Gui = {}
global.Gui_data = {}
local click_handlers
2018-05-21 19:22:07 +02:00
local close_handlers
2018-05-20 17:28:54 +02:00
function Gui.uid_name()
2018-05-22 02:03:04 +02:00
if _DEBUG then
-- https://stackoverflow.com/questions/48402876/getting-current-file-name-in-lua
local filename = debug.getinfo(2, 'S').source:match('^.+/(.+)$'):sub(1, -5)
return filename .. ',' .. Token.uid()
else
return tostring(Token.uid())
end
2018-05-20 17:28:54 +02:00
end
-- Associates data with the LuaGuiElement. If data is nil then removes the data
function Gui.set_data(element, data)
global.Gui_data[element.player_index .. ',' .. element.index] = data
end
-- Gets the Associated data with this LuaGuiElement if any.
function Gui.get_data(element)
return global.Gui_data[element.player_index .. ',' .. element.index]
end
-- Removes data associated with LuaGuiElement and its children recursivly.
function Gui.remove_data_recursivly(element)
Gui.set_data(element, nil)
local children = element.children
if not children then
return
end
for _, child in ipairs(children) do
if child.valid then
Gui.remove_data_recursivly(child)
end
end
end
local function on_click(event)
local element = event.element
if not element or not element.valid then
return
end
local player = game.players[event.player_index]
if not player or not player.valid then
return
end
event.player = player
local handler = click_handlers[element.name]
if not handler then
return
end
handler(event)
end
-- Register a handler for the on_gui_click event for LuaGuiElements with element_name.
-- Can only have one handler per element name.
-- Guarantees that the element and the player are valid when calling the handler.
-- Adds a player field to the event table.
function Gui.on_click(element_name, handler)
if not click_handlers then
click_handlers = {}
Event.add(defines.events.on_gui_click, on_click)
end
click_handlers[element_name] = handler
end
2018-05-21 19:22:07 +02:00
local function on_close(event)
2018-05-22 00:48:50 +02:00
-- element is only set on the envent for custom gui elements.
2018-05-21 19:22:07 +02:00
local element = event.element
if not element or not element.valid then
return
end
local player = game.players[event.player_index]
if not player or not player.valid then
return
end
event.player = player
local handler = close_handlers[element.name]
if not handler then
return
end
handler(event)
end
-- Register a handler for the on_gui_closed event for a custom LuaGuiElements with element_name.
-- Can only have one handler per element name.
-- Guarantees that the element and the player are valid when calling the handler.
-- Adds a player field to the event table.
function Gui.on_custom_close(element_name, handler)
if not close_handlers then
close_handlers = {}
Event.add(defines.events.on_gui_closed, on_close)
end
close_handlers[element_name] = handler
end
2018-05-20 17:28:54 +02:00
return Gui