You've already forked ComfyFactorio
mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-11-29 22:47:52 +02:00
Small commands refactor
This commit is contained in:
307
utils/gui.lua
307
utils/gui.lua
@@ -22,10 +22,10 @@ local local_settings = {
|
||||
local main_gui_tabs = {}
|
||||
local screen_elements = {}
|
||||
local remove_data_recursively
|
||||
|
||||
local concat = table.concat
|
||||
local names = {}
|
||||
-- global
|
||||
local data = {}
|
||||
local element_map = {}
|
||||
local settings = {
|
||||
mod_gui_top_frame = false,
|
||||
disabled_tabs = {},
|
||||
@@ -34,13 +34,14 @@ local settings = {
|
||||
|
||||
Public.token =
|
||||
Global.register(
|
||||
{data = data, element_map = element_map, settings = settings},
|
||||
function(tbl)
|
||||
data = tbl.data
|
||||
element_map = tbl.element_map
|
||||
settings = tbl.settings
|
||||
end
|
||||
)
|
||||
{ data = data, settings = settings },
|
||||
function (tbl)
|
||||
data = tbl.data
|
||||
settings = tbl.settings
|
||||
end
|
||||
)
|
||||
|
||||
Public.names = names
|
||||
|
||||
Public.beam = 'file/utils/files/beam.png'
|
||||
Public.beam_1 = 'file/utils/files/beam_1.png'
|
||||
@@ -61,7 +62,20 @@ Public.info_icon = 'file/utils/files/info.png'
|
||||
Public.mod_gui_button_enabled = false
|
||||
|
||||
function Public.uid_name()
|
||||
return tostring(Token.uid())
|
||||
if game then
|
||||
return error('This function is not allowed to be called in this context.', 2)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
function Public.uid()
|
||||
@@ -121,9 +135,10 @@ function Public.set_data(element, value)
|
||||
data[player_index] = values
|
||||
end
|
||||
|
||||
values[element.index] = value
|
||||
values[element.index] = { value = value, name = element.name }
|
||||
end
|
||||
end
|
||||
|
||||
local set_data = Public.set_data
|
||||
|
||||
-- Associates data with the LuaGuiElement. If data is nil then removes the data
|
||||
@@ -151,58 +166,7 @@ function Public.set_data_parent(parent, element, value)
|
||||
values[parent.index] = {}
|
||||
end
|
||||
|
||||
values[parent.index][element.index] = value
|
||||
end
|
||||
end
|
||||
|
||||
-- Associates data with the LuaGuiElement along with the tag. If data is nil then removes the data
|
||||
function Public.set_data_custom(tag, element, value)
|
||||
if not tag then
|
||||
return error('A tag is required', 2)
|
||||
end
|
||||
|
||||
local player_index = element.player_index
|
||||
local values = data[player_index]
|
||||
|
||||
if value == nil then
|
||||
if not values then
|
||||
return
|
||||
end
|
||||
|
||||
local tags = values[tag]
|
||||
if not tags then
|
||||
if next(values) == nil then
|
||||
data[player_index] = nil
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
if element.remove then
|
||||
values[tag] = nil
|
||||
return
|
||||
end
|
||||
|
||||
tags[element.index] = nil
|
||||
|
||||
if next(tags) == nil then
|
||||
values[tag] = nil
|
||||
end
|
||||
else
|
||||
if not values then
|
||||
values = {
|
||||
[tag] = {}
|
||||
}
|
||||
data[player_index] = values
|
||||
end
|
||||
|
||||
local tags = values[tag]
|
||||
|
||||
if not tags then
|
||||
values[tag] = {}
|
||||
tags = values[tag]
|
||||
end
|
||||
|
||||
tags[element.index] = value
|
||||
values[parent.index][element.index] = { value = value, name = element.name }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -219,7 +183,7 @@ function Public.get_data(element)
|
||||
return nil
|
||||
end
|
||||
|
||||
return values[element.index]
|
||||
return values[element.index].value
|
||||
end
|
||||
|
||||
-- Gets the Associated data with this LuaGuiElement if any.
|
||||
@@ -243,51 +207,28 @@ function Public.get_data_parent(parent, element)
|
||||
return nil
|
||||
end
|
||||
|
||||
return values[element.index]
|
||||
end
|
||||
|
||||
-- Gets the Associated data with this LuaGuiElement if any.
|
||||
function Public.get_data_custom(tag, element)
|
||||
if not tag then
|
||||
return error('A tag is required', 2)
|
||||
end
|
||||
if not element then
|
||||
return error('An element is required', 2)
|
||||
end
|
||||
|
||||
local player_index = element.player_index
|
||||
|
||||
local values = data[player_index]
|
||||
if not values then
|
||||
return nil
|
||||
end
|
||||
|
||||
values = values[tag]
|
||||
if not values then
|
||||
return nil
|
||||
end
|
||||
|
||||
return values[element.index]
|
||||
return values[element.index].value
|
||||
end
|
||||
|
||||
-- Adds a gui that is alike the factorio native gui.
|
||||
function Public.add_main_frame_with_toolbar(player, align, set_frame_name, set_settings_button_name, close_main_frame_name, name, info, inside_table_count)
|
||||
function Public.add_main_frame_with_toolbar(player, align, set_frame_name, set_settings_button_name,
|
||||
close_main_frame_name, name, info, inside_table_count)
|
||||
if not align then
|
||||
return
|
||||
end
|
||||
local main_frame
|
||||
if align == 'left' then
|
||||
validate_frame_and_destroy(player.gui.left, set_frame_name)
|
||||
main_frame = player.gui.left.add {type = 'frame', name = set_frame_name, direction = 'vertical'}
|
||||
main_frame = player.gui.left.add { type = 'frame', name = set_frame_name, direction = 'vertical' }
|
||||
elseif align == 'center' then
|
||||
validate_frame_and_destroy(player.gui.center, set_frame_name)
|
||||
main_frame = player.gui.center.add {type = 'frame', name = set_frame_name, direction = 'vertical'}
|
||||
main_frame = player.gui.center.add { type = 'frame', name = set_frame_name, direction = 'vertical' }
|
||||
elseif align == 'screen' then
|
||||
validate_frame_and_destroy(player.gui.screen, set_frame_name)
|
||||
main_frame = player.gui.screen.add {type = 'frame', name = set_frame_name, direction = 'vertical'}
|
||||
main_frame = player.gui.screen.add { type = 'frame', name = set_frame_name, direction = 'vertical' }
|
||||
end
|
||||
|
||||
local titlebar = main_frame.add {type = 'flow', name = 'titlebar', direction = 'horizontal'}
|
||||
local titlebar = main_frame.add { type = 'flow', name = 'titlebar', direction = 'horizontal' } --[[@as LuaGuiElement]]
|
||||
titlebar.style.horizontal_spacing = 8
|
||||
titlebar.style = 'horizontal_flow'
|
||||
|
||||
@@ -302,7 +243,7 @@ function Public.add_main_frame_with_toolbar(player, align, set_frame_name, set_s
|
||||
caption = name,
|
||||
ignored_by_interaction = true
|
||||
}
|
||||
local widget = titlebar.add {type = 'empty-widget', style = 'draggable_space', ignored_by_interaction = true}
|
||||
local widget = titlebar.add { type = 'empty-widget', style = 'draggable_space', ignored_by_interaction = true }
|
||||
widget.style.left_margin = 4
|
||||
widget.style.right_margin = 4
|
||||
widget.style.height = 24
|
||||
@@ -315,7 +256,7 @@ function Public.add_main_frame_with_toolbar(player, align, set_frame_name, set_s
|
||||
name = set_settings_button_name,
|
||||
style = 'frame_action_button',
|
||||
sprite = Public.settings_white_icon,
|
||||
mouse_button_filter = {'left'},
|
||||
mouse_button_filter = { 'left' },
|
||||
hovered_sprite = Public.settings_black_icon,
|
||||
clicked_sprite = Public.settings_black_icon,
|
||||
tooltip = 'Settings',
|
||||
@@ -329,7 +270,7 @@ function Public.add_main_frame_with_toolbar(player, align, set_frame_name, set_s
|
||||
name = set_settings_button_name,
|
||||
style = 'frame_action_button',
|
||||
sprite = Public.info_icon,
|
||||
mouse_button_filter = {'left'},
|
||||
mouse_button_filter = { 'left' },
|
||||
hovered_sprite = Public.info_icon,
|
||||
clicked_sprite = Public.info_icon,
|
||||
tooltip = 'Info',
|
||||
@@ -345,7 +286,7 @@ function Public.add_main_frame_with_toolbar(player, align, set_frame_name, set_s
|
||||
type = 'sprite-button',
|
||||
name = close_main_frame_name,
|
||||
style = 'frame_action_button',
|
||||
mouse_button_filter = {'left'},
|
||||
mouse_button_filter = { 'left' },
|
||||
sprite = 'utility/close_white',
|
||||
hovered_sprite = 'utility/close_black',
|
||||
clicked_sprite = 'utility/close_black',
|
||||
@@ -358,10 +299,10 @@ function Public.add_main_frame_with_toolbar(player, align, set_frame_name, set_s
|
||||
|
||||
local inside_frame =
|
||||
main_frame.add {
|
||||
type = 'table',
|
||||
column_count = 1 or inside_table_count,
|
||||
name = 'inside_frame'
|
||||
}
|
||||
type = 'table',
|
||||
column_count = 1 or inside_table_count,
|
||||
name = 'inside_frame'
|
||||
}
|
||||
|
||||
return main_frame, inside_frame
|
||||
end
|
||||
@@ -382,6 +323,7 @@ function Public.remove_data_recursively(element)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
remove_data_recursively = Public.remove_data_recursively
|
||||
|
||||
local remove_children_data
|
||||
@@ -399,6 +341,7 @@ function Public.remove_children_data(element)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
remove_children_data = Public.remove_children_data
|
||||
|
||||
function Public.destroy(element)
|
||||
@@ -475,12 +418,13 @@ local function handler_factory(event_id)
|
||||
end
|
||||
end
|
||||
|
||||
return function(element_name, handler)
|
||||
return function (element_name, handler)
|
||||
if not element_name then
|
||||
return error('Element name is required when passing it onto the handler_factory.', 2)
|
||||
end
|
||||
if not handler or not type(handler) == 'function' then
|
||||
return error('Handler is required when passing it onto the handler_factory and needs to be of type function.', 2)
|
||||
return error(
|
||||
'Handler is required when passing it onto the handler_factory and needs to be of type function.', 2)
|
||||
end
|
||||
|
||||
if not handlers then
|
||||
@@ -507,7 +451,7 @@ local function custom_raise(handlers, element, player)
|
||||
return
|
||||
end
|
||||
|
||||
handler({element = element, player = player})
|
||||
handler({ element = element, player = player })
|
||||
end
|
||||
|
||||
-- Disabled the handler so it does not clean then data table of invalid data.
|
||||
@@ -639,7 +583,7 @@ function Public.add_tab_to_gui(tbl)
|
||||
local only_server_sided = tbl.only_server_sided or false
|
||||
|
||||
if not main_gui_tabs[tbl.caption] then
|
||||
main_gui_tabs[tbl.caption] = {id = tbl.id, name = tbl.name, admin = admin, only_server_sided = only_server_sided}
|
||||
main_gui_tabs[tbl.caption] = { id = tbl.id, name = tbl.name, admin = admin, only_server_sided = only_server_sided }
|
||||
else
|
||||
error('Given name: ' .. tbl.caption .. ' already exists in table.')
|
||||
end
|
||||
@@ -688,7 +632,7 @@ function Public.clear_all_screen_frames(player)
|
||||
end
|
||||
|
||||
function Public.clear_all_active_frames(player)
|
||||
Event.raise(Public.events.on_gui_closed_main_frame, {player_index = player.index})
|
||||
Event.raise(Public.events.on_gui_closed_main_frame, { player_index = player.index })
|
||||
for _, child in pairs(player.gui.left.children) do
|
||||
remove_data_recursively(child)
|
||||
child.destroy()
|
||||
@@ -771,7 +715,8 @@ end
|
||||
|
||||
local function top_button(player)
|
||||
if settings.mod_gui_top_frame then
|
||||
local button = Public.add_mod_button(player, {type = 'sprite-button', name = main_button_name, sprite = 'item/raw-fish', style = Public.button_style})
|
||||
local button = Public.add_mod_button(player,
|
||||
{ type = 'sprite-button', name = main_button_name, sprite = 'item/raw-fish', style = Public.button_style })
|
||||
if button then
|
||||
button.style.minimal_height = 36
|
||||
button.style.maximal_height = 36
|
||||
@@ -782,7 +727,12 @@ local function top_button(player)
|
||||
if player.gui.top[main_button_name] then
|
||||
return
|
||||
end
|
||||
local button = player.gui.top.add({type = 'sprite-button', name = main_button_name, sprite = 'item/raw-fish', style = Public.button_style})
|
||||
local button = player.gui.top.add({
|
||||
type = 'sprite-button',
|
||||
name = main_button_name,
|
||||
sprite = 'item/raw-fish',
|
||||
style = Public.button_style
|
||||
})
|
||||
button.style.minimal_height = 38
|
||||
button.style.maximal_height = 38
|
||||
button.style.minimal_width = 40
|
||||
@@ -798,17 +748,17 @@ local function top_toggle_button(player)
|
||||
if Public.get_mod_gui_top_frame() then
|
||||
local b =
|
||||
Public.add_mod_button(
|
||||
player,
|
||||
{
|
||||
type = 'sprite-button',
|
||||
name = main_toggle_button_name,
|
||||
sprite = 'utility/preset',
|
||||
tooltip = 'Click to hide top buttons!',
|
||||
style = Public.button_style
|
||||
}
|
||||
)
|
||||
player,
|
||||
{
|
||||
type = 'sprite-button',
|
||||
name = main_toggle_button_name,
|
||||
sprite = 'utility/preset',
|
||||
tooltip = 'Click to hide top buttons!',
|
||||
style = Public.button_style
|
||||
}
|
||||
)
|
||||
if b then
|
||||
b.style.font_color = {165, 165, 165}
|
||||
b.style.font_color = { 165, 165, 165 }
|
||||
b.style.font = 'heading-3'
|
||||
b.style.minimal_height = 36
|
||||
b.style.maximal_height = 36
|
||||
@@ -819,14 +769,14 @@ local function top_toggle_button(player)
|
||||
else
|
||||
local b =
|
||||
player.gui.top.add(
|
||||
{
|
||||
type = 'sprite-button',
|
||||
name = main_toggle_button_name,
|
||||
sprite = 'utility/preset',
|
||||
style = Public.button_style,
|
||||
tooltip = 'Click to hide top buttons!'
|
||||
}
|
||||
)
|
||||
{
|
||||
type = 'sprite-button',
|
||||
name = main_toggle_button_name,
|
||||
sprite = 'utility/preset',
|
||||
style = Public.button_style,
|
||||
tooltip = 'Click to hide top buttons!'
|
||||
}
|
||||
)
|
||||
b.style.padding = 2
|
||||
b.style.width = 20
|
||||
b.style.maximal_height = 38
|
||||
@@ -845,32 +795,33 @@ local function draw_main_frame(player)
|
||||
|
||||
local admins = Server.get_admins_data()
|
||||
|
||||
local frame, inside_frame = Public.add_main_frame_with_toolbar(player, 'left', main_frame_name, nil, close_button_name, 'Comfy Factorio')
|
||||
local tabbed_pane = inside_frame.add({type = 'tabbed-pane', name = 'tabbed_pane'})
|
||||
local frame, inside_frame = Public.add_main_frame_with_toolbar(player, 'left', main_frame_name, nil,
|
||||
close_button_name, 'Comfy Factorio')
|
||||
local tabbed_pane = inside_frame.add({ type = 'tabbed-pane', name = 'tabbed_pane' })
|
||||
for name, callback in pairs(tabs) do
|
||||
if not settings.disabled_tabs[name] then
|
||||
local secs = Server.get_current_time()
|
||||
if callback.only_server_sided then
|
||||
if secs then
|
||||
local tab = tabbed_pane.add({type = 'tab', caption = name, name = callback.name})
|
||||
local name_frame = tabbed_pane.add({type = 'frame', name = name, direction = 'vertical'})
|
||||
local tab = tabbed_pane.add({ type = 'tab', caption = name, name = callback.name })
|
||||
local name_frame = tabbed_pane.add({ type = 'frame', name = name, direction = 'vertical' })
|
||||
tabbed_pane.add_tab(tab, name_frame)
|
||||
end
|
||||
elseif callback.admin == true then
|
||||
if player.admin then
|
||||
if not secs then
|
||||
local tab = tabbed_pane.add({type = 'tab', caption = name, name = callback.name})
|
||||
local name_frame = tabbed_pane.add({type = 'frame', name = name, direction = 'vertical'})
|
||||
local tab = tabbed_pane.add({ type = 'tab', caption = name, name = callback.name })
|
||||
local name_frame = tabbed_pane.add({ type = 'frame', name = name, direction = 'vertical' })
|
||||
tabbed_pane.add_tab(tab, name_frame)
|
||||
elseif secs and admins[player.name] then
|
||||
local tab = tabbed_pane.add({type = 'tab', caption = name, name = callback.name})
|
||||
local name_frame = tabbed_pane.add({type = 'frame', name = name, direction = 'vertical'})
|
||||
local tab = tabbed_pane.add({ type = 'tab', caption = name, name = callback.name })
|
||||
local name_frame = tabbed_pane.add({ type = 'frame', name = name, direction = 'vertical' })
|
||||
tabbed_pane.add_tab(tab, name_frame)
|
||||
end
|
||||
end
|
||||
else
|
||||
local tab = tabbed_pane.add({type = 'tab', caption = name, name = callback.name})
|
||||
local name_frame = tabbed_pane.add({type = 'frame', name = name, direction = 'vertical'})
|
||||
local tab = tabbed_pane.add({ type = 'tab', caption = name, name = callback.name })
|
||||
local name_frame = tabbed_pane.add({ type = 'frame', name = name, direction = 'vertical' })
|
||||
tabbed_pane.add_tab(tab, name_frame)
|
||||
end
|
||||
end
|
||||
@@ -907,7 +858,7 @@ function Public.refresh(player)
|
||||
for _, tab in pairs(tabbed_pane.tabs) do
|
||||
if tab.content.name ~= frame.name then
|
||||
tab.content.clear()
|
||||
Event.raise(Public.events.on_gui_removal, {player_index = player.index})
|
||||
Event.raise(Public.events.on_gui_removal, { player_index = player.index })
|
||||
end
|
||||
end
|
||||
|
||||
@@ -980,7 +931,7 @@ Public.on_value_changed = handler_factory(defines.events.on_gui_value_changed)
|
||||
|
||||
Public.on_click(
|
||||
main_button_name,
|
||||
function(event)
|
||||
function (event)
|
||||
local is_spamming = SpamProtection.is_spamming(event.player, nil, 'Main button')
|
||||
if is_spamming then
|
||||
return
|
||||
@@ -991,9 +942,10 @@ Public.on_click(
|
||||
if frame then
|
||||
remove_data_recursively(frame)
|
||||
frame.destroy()
|
||||
Event.raise(Public.events.on_gui_removal, {player_index = player.index})
|
||||
Event.raise(Public.events.on_gui_removal, { player_index = player.index })
|
||||
local active_frame = Public.get_player_active_frame(player)
|
||||
Event.raise(Public.events.on_gui_closed_main_frame, {player_index = player.index, element = active_frame or nil})
|
||||
Event.raise(Public.events.on_gui_closed_main_frame,
|
||||
{ player_index = player.index, element = active_frame or nil })
|
||||
else
|
||||
draw_main_frame(player)
|
||||
end
|
||||
@@ -1002,11 +954,11 @@ Public.on_click(
|
||||
|
||||
Public.on_click(
|
||||
close_button_name,
|
||||
function(event)
|
||||
function (event)
|
||||
local player = event.player
|
||||
local frame = Public.get_parent_frame(player)
|
||||
local active_frame = Public.get_player_active_frame(player)
|
||||
Event.raise(Public.events.on_gui_closed_main_frame, {player_index = player.index, element = active_frame or nil})
|
||||
Event.raise(Public.events.on_gui_closed_main_frame, { player_index = player.index, element = active_frame or nil })
|
||||
if frame then
|
||||
remove_data_recursively(frame)
|
||||
frame.destroy()
|
||||
@@ -1016,10 +968,10 @@ Public.on_click(
|
||||
|
||||
Public.on_custom_close(
|
||||
main_frame_name,
|
||||
function(event)
|
||||
function (event)
|
||||
local player = event.player
|
||||
local active_frame = Public.get_player_active_frame(player)
|
||||
Event.raise(Public.events.on_gui_closed_main_frame, {player_index = player.index, element = active_frame or nil})
|
||||
Event.raise(Public.events.on_gui_closed_main_frame, { player_index = player.index, element = active_frame or nil })
|
||||
local frame = Public.get_parent_frame(player)
|
||||
if frame then
|
||||
remove_data_recursively(frame)
|
||||
@@ -1030,7 +982,7 @@ Public.on_custom_close(
|
||||
|
||||
Public.on_click(
|
||||
main_toggle_button_name,
|
||||
function(event)
|
||||
function (event)
|
||||
local button = event.element
|
||||
local player = event.player
|
||||
local top = player.gui.top
|
||||
@@ -1082,7 +1034,7 @@ Public.on_click(
|
||||
|
||||
Event.add(
|
||||
defines.events.on_gui_click,
|
||||
function(event)
|
||||
function (event)
|
||||
local element = event.element
|
||||
if not element or not element.valid then
|
||||
return
|
||||
@@ -1116,7 +1068,7 @@ Event.add(
|
||||
|
||||
Event.add(
|
||||
defines.events.on_player_created,
|
||||
function(event)
|
||||
function (event)
|
||||
local player = game.get_player(event.player_index)
|
||||
if local_settings.toggle_button then
|
||||
top_toggle_button(player)
|
||||
@@ -1127,67 +1079,14 @@ Event.add(
|
||||
|
||||
Event.add(
|
||||
defines.events.on_player_joined_game,
|
||||
function(event)
|
||||
function (event)
|
||||
local player = game.get_player(event.player_index)
|
||||
top_button(player)
|
||||
end
|
||||
)
|
||||
|
||||
if _DEBUG then
|
||||
local concat = table.concat
|
||||
|
||||
local names = {}
|
||||
Public.names = names
|
||||
|
||||
function Public.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
|
||||
|
||||
function Public.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 = Public.set_data
|
||||
|
||||
function Public.data()
|
||||
return data
|
||||
end
|
||||
|
||||
function Public.element_map()
|
||||
return element_map
|
||||
end
|
||||
function Public.data()
|
||||
return data
|
||||
end
|
||||
|
||||
return Public
|
||||
|
||||
Reference in New Issue
Block a user