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

147 lines
4.8 KiB
Lua
Raw Normal View History

2018-11-26 03:07:03 +02:00
local Token = require 'utils.token'
2018-05-20 17:28:54 +02:00
local Event = require 'utils.event'
2018-09-23 00:25:13 +02:00
local Game = require 'utils.game'
2018-05-20 17:28:54 +02:00
local Gui = {}
global.Gui_data = {}
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)
2018-06-24 12:52:51 +02:00
global.Gui_data[element.player_index * 0x100000000 + element.index] = data
2018-05-20 17:28:54 +02:00
end
-- Gets the Associated data with this LuaGuiElement if any.
function Gui.get_data(element)
2018-06-24 12:52:51 +02:00
return global.Gui_data[element.player_index * 0x100000000 + element.index]
2018-05-20 17:28:54 +02:00
end
-- Removes data associated with LuaGuiElement and its children recursivly.
2018-12-01 23:06:24 +02:00
function Gui.remove_data_recursively(element)
2018-05-20 17:28:54 +02:00
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
2018-12-01 23:06:24 +02:00
Gui.remove_data_recursively(child)
2018-05-20 17:28:54 +02:00
end
end
end
2018-06-27 15:48:04 +02:00
function Gui.remove_children_data(element)
local children = element.children
if not children then
return
end
for _, child in ipairs(children) do
if child.valid then
Gui.set_data(child, nil)
Gui.remove_children_data(child)
end
end
end
function Gui.destroy(element)
2018-12-01 23:06:24 +02:00
Gui.remove_data_recursively(element)
2018-06-28 19:28:39 +02:00
element.destroy()
2018-06-27 15:48:04 +02:00
end
function Gui.clear(element)
Gui.remove_children_data(element)
element.clear()
end
2018-06-21 18:11:52 +02:00
local function handler_factory(event_id)
local handlers
2018-05-20 17:28:54 +02:00
2018-06-21 18:11:52 +02:00
local function on_event(event)
local element = event.element
if not element or not element.valid then
return
end
2018-05-20 17:28:54 +02:00
2018-06-21 18:11:52 +02:00
local handler = handlers[element.name]
if not handler then
return
end
2018-05-20 17:28:54 +02:00
local player = Game.get_player_by_index(event.player_index)
2018-06-21 18:11:52 +02:00
if not player or not player.valid then
return
end
event.player = player
2018-05-20 17:28:54 +02:00
2018-06-21 18:11:52 +02:00
handler(event)
2018-06-20 18:15:48 +02:00
end
2018-06-21 18:11:52 +02:00
return function(element_name, handler)
if not handlers then
handlers = {}
Event.add(event_id, on_event)
end
2018-06-20 18:15:48 +02:00
2018-06-21 18:11:52 +02:00
handlers[element_name] = handler
2018-06-20 18:15:48 +02:00
end
end
2018-06-21 18:11:52 +02:00
-- Register a handler for the on_gui_checked_state_changed event for LuaGuiElements with element_name.
2018-06-20 18:15:48 +02:00
-- 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.
2018-06-21 18:11:52 +02:00
Gui.on_checked_state_changed = handler_factory(defines.events.on_gui_checked_state_changed)
2018-06-20 18:15:48 +02:00
2018-06-21 18:11:52 +02:00
-- 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.
Gui.on_click = handler_factory(defines.events.on_gui_click)
2018-05-21 19:22:07 +02:00
2018-06-21 18:11:52 +02:00
-- 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.
Gui.on_custom_close = handler_factory(defines.events.on_gui_closed)
2018-05-21 19:22:07 +02:00
2018-06-21 18:11:52 +02:00
-- Register a handler for the on_gui_elem_changed 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.
Gui.on_elem_changed = handler_factory(defines.events.on_gui_elem_changed)
2018-05-21 19:22:07 +02:00
2018-06-21 18:11:52 +02:00
-- Register a handler for the on_gui_selection_state_changed 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.
Gui.on_selection_state_changed = handler_factory(defines.events.on_gui_selection_state_changed)
2018-05-21 19:22:07 +02:00
2018-06-21 18:11:52 +02:00
-- Register a handler for the on_gui_text_changed event for LuaGuiElements with element_name.
2018-05-21 19:22:07 +02:00
-- 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.
2018-06-21 18:11:52 +02:00
Gui.on_text_changed = handler_factory(defines.events.on_gui_text_changed)
2018-05-21 19:22:07 +02:00
2018-06-21 18:11:52 +02:00
-- Register a handler for the on_gui_value_changed 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.
Gui.on_value_changed = handler_factory(defines.events.on_gui_value_changed)
2018-05-21 19:22:07 +02:00
2018-05-20 17:28:54 +02:00
return Gui