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

355 lines
10 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'
local Global = require 'utils.global'
2018-05-20 17:28:54 +02:00
2019-02-06 23:20:27 +02:00
local tostring = tostring
local next = next
2018-05-20 17:28:54 +02:00
local Gui = {}
local data = {}
2019-05-26 21:32:44 +02:00
local element_map = {}
2019-05-26 22:03:42 +02:00
Gui.token =
2019-05-26 21:32:44 +02:00
Global.register(
2019-05-26 22:03:42 +02:00
{data = data, element_map = element_map},
function(tbl)
data = tbl.data
element_map = tbl.element_map
end
)
2018-12-16 12:58:28 +02:00
local top_elements = {}
local on_visible_handlers = {}
local on_pre_hidden_handlers = {}
2018-05-20 17:28:54 +02:00
function Gui.uid_name()
2019-01-31 00:16:44 +02:00
return tostring(Token.uid())
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, value)
2019-02-06 23:20:27 +02:00
local player_index = element.player_index
local values = data[player_index]
if value == nil then
if not values then
return
end
values[element.index] = nil
if next(values) == nil then
data[player_index] = nil
end
else
if not values then
values = {}
data[player_index] = values
end
values[element.index] = value
end
2018-05-20 17:28:54 +02:00
end
2019-02-06 23:20:27 +02:00
local set_data = Gui.set_data
2018-05-20 17:28:54 +02:00
-- Gets the Associated data with this LuaGuiElement if any.
function Gui.get_data(element)
2019-02-06 23:20:27 +02:00
local player_index = element.player_index
local values = data[player_index]
if not values then
return nil
end
return values[element.index]
2018-05-20 17:28:54 +02:00
end
2019-02-06 23:20:27 +02:00
local remove_data_recursively
-- Removes data associated with LuaGuiElement and its children recursively.
2018-12-01 23:06:24 +02:00
function Gui.remove_data_recursively(element)
2019-02-06 23:20:27 +02:00
set_data(element, nil)
2018-05-20 17:28:54 +02:00
local children = element.children
if not children then
return
end
2019-02-06 23:20:27 +02:00
for _, child in next, children do
2018-05-20 17:28:54 +02:00
if child.valid then
2019-02-06 23:20:27 +02:00
remove_data_recursively(child)
2018-05-20 17:28:54 +02:00
end
end
end
2019-02-06 23:20:27 +02:00
remove_data_recursively = Gui.remove_data_recursively
2018-05-20 17:28:54 +02:00
2019-02-06 23:20:27 +02:00
local remove_children_data
2018-06-27 15:48:04 +02:00
function Gui.remove_children_data(element)
local children = element.children
if not children then
return
end
2019-02-06 23:20:27 +02:00
for _, child in next, children do
2018-06-27 15:48:04 +02:00
if child.valid then
2019-02-06 23:20:27 +02:00
set_data(child, nil)
remove_children_data(child)
2018-06-27 15:48:04 +02:00
end
end
end
2019-02-06 23:20:27 +02:00
remove_children_data = Gui.remove_children_data
2018-06-27 15:48:04 +02:00
function Gui.destroy(element)
2019-02-06 23:20:27 +02:00
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)
2019-02-06 23:20:27 +02:00
remove_children_data(element)
2018-06-27 15:48:04 +02:00
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(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
local function custom_handler_factory(handlers)
return function(element_name, handler)
handlers[element_name] = handler
end
end
local function custom_raise(handlers, element, player)
local handler = handlers[element.name]
if not handler then
return
end
handler({element = element, player = player})
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-12-16 03:39:19 +02:00
-- Register a handler for when the player shows the top LuaGuiElements with element_name.
-- Assuming the element_name has been added with Gui.allow_player_to_toggle_top_element_visibility.
-- 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_player_show_top = custom_handler_factory(on_visible_handlers)
2018-12-16 03:39:19 +02:00
-- Register a handler for when the player hides the top LuaGuiElements with element_name.
-- Assuming the element_name has been added with Gui.allow_player_to_toggle_top_element_visibility.
-- 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_pre_player_hide_top = custom_handler_factory(on_pre_hidden_handlers)
--- Allows the player to show / hide this element.
-- The element must be part in gui.top.
2018-12-16 12:58:28 +02:00
-- This function must be called in the control stage, i.e not inside an event.
-- @param element_name<string> This name must be globally unique.
function Gui.allow_player_to_toggle_top_element_visibility(element_name)
2019-02-18 08:43:59 +02:00
if _LIFECYCLE ~= _STAGE.control then
2019-02-11 05:36:44 +02:00
error('can only be called during the control stage', 2)
2019-02-04 23:17:00 +02:00
end
top_elements[#top_elements + 1] = element_name
end
local toggle_button_name = Gui.uid_name()
Event.add(
defines.events.on_player_created,
function(event)
local player = game.get_player(event.player_index)
if not player or not player.valid then
return
end
local b =
player.gui.top.add {
type = 'button',
name = toggle_button_name,
caption = '<',
2019-03-04 00:54:51 +02:00
tooltip = {'gui_util.button_tooltip'}
}
local style = b.style
style.width = 18
style.height = 38
style.left_padding = 0
style.top_padding = 0
style.right_padding = 0
style.bottom_padding = 0
style.font = 'default-small-bold'
end
)
Gui.on_click(
toggle_button_name,
function(event)
local button = event.element
local player = event.player
local top = player.gui.top
if button.caption == '<' then
for i = 1, #top_elements do
local name = top_elements[i]
local ele = top[name]
if ele and ele.valid then
2019-02-25 01:07:15 +02:00
if ele.visible then
custom_raise(on_pre_hidden_handlers, ele, player)
2019-02-25 01:07:15 +02:00
ele.visible = false
end
end
end
button.caption = '>'
button.style.height = 24
else
for i = 1, #top_elements do
local name = top_elements[i]
local ele = top[name]
if ele and ele.valid then
2019-02-25 01:07:15 +02:00
if not ele.visible then
ele.visible = true
custom_raise(on_visible_handlers, ele, player)
end
end
end
button.caption = '<'
button.style.height = 38
end
end
)
2019-01-31 00:16:44 +02:00
if _DEBUG then
local concat = table.concat
local names = {}
Gui.names = names
function Gui.uid_name()
local info = debug.getinfo(2, 'Sl')
local filepath = info.source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
local line = info.currentline
local token = tostring(Token.uid())
local name = concat {token, ' - ', filepath, ':line:', line}
names[token] = name
return token
end
2019-02-09 19:57:55 +02:00
2019-05-26 21:32:44 +02:00
function Gui.set_data(element, value)
local player_index = element.player_index
local values = data[player_index]
if value == nil then
if not values then
return
end
local index = element.index
values[index] = nil
element_map[index] = nil
if next(values) == nil then
data[player_index] = nil
end
else
if not values then
values = {}
data[player_index] = values
end
local index = element.index
values[index] = value
element_map[index] = element
end
end
set_data = Gui.set_data
2019-05-26 22:03:42 +02:00
function Gui.data()
return data
end
function Gui.element_map()
return element_map
end
2019-01-31 00:16:44 +02:00
end
2018-05-20 17:28:54 +02:00
return Gui