From 242545b007aca8a02618dda8cc9d0ffd8048c734 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Sun, 26 Jan 2025 01:54:19 +0000 Subject: [PATCH] Convert tasks, production, tools, and landfill --- exp_gui/module/elements.lua | 4 +- exp_legacy/module/config/_file_loader.lua | 12 +- exp_legacy/module/expcore/datastore.lua | 4 + exp_legacy/module/modules/gui/landfill.lua | 39 +-- exp_legacy/module/modules/gui/production.lua | 42 +-- exp_legacy/module/modules/gui/task-list.lua | 271 ++++++++++--------- exp_legacy/module/modules/gui/tool.lua | 119 ++++---- exp_util/module/storage.lua | 9 +- 8 files changed, 276 insertions(+), 224 deletions(-) diff --git a/exp_gui/module/elements.lua b/exp_gui/module/elements.lua index ebc96819..4360fc3f 100644 --- a/exp_gui/module/elements.lua +++ b/exp_gui/module/elements.lua @@ -145,7 +145,9 @@ elements.subframe_base = ExpGui.element("container_subframe") style = ExpGui.property_from_arg(1), } :style{ - padding = { 2, 4 }, + height = 0, + minimal_height = 36, + padding = { 3, 4 }, use_header_filler = false, horizontally_stretchable = true, } diff --git a/exp_legacy/module/config/_file_loader.lua b/exp_legacy/module/config/_file_loader.lua index 425f422d..c8ceed82 100644 --- a/exp_legacy/module/config/_file_loader.lua +++ b/exp_legacy/module/config/_file_loader.lua @@ -47,7 +47,7 @@ return { "modules.gui.rocket-info", "modules.gui.science-info", "modules.gui.autofill", - --"modules.gui.task-list", + "modules.gui.task-list", --"modules.gui.warp-list", --"modules.gui.player-list", --"modules.gui.server-ups", @@ -55,14 +55,14 @@ return { --"modules.gui.vlayer", --"modules.gui.research", --"modules.gui.module", - --"modules.gui.landfill", - --"modules.gui.tool", - --"modules.gui.production", + "modules.gui.landfill", + "modules.gui.tool", + "modules.gui.production", --"modules.gui.playerdata", --"modules.gui.surveillance", - --"modules.graftorio.require", -- graftorio --"modules.gui.toolbar", -- must be loaded last to register toolbar handlers - + + "modules.graftorio.require", -- graftorio --- Config Files "config.expcore.permission_groups", -- loads some predefined permission groups "config.expcore.roles", -- loads some predefined roles diff --git a/exp_legacy/module/expcore/datastore.lua b/exp_legacy/module/expcore/datastore.lua index 9e97f23e..9d498787 100644 --- a/exp_legacy/module/expcore/datastore.lua +++ b/exp_legacy/module/expcore/datastore.lua @@ -164,6 +164,10 @@ Storage.register(Data, function(tbl) for name, datastore in pairs(Datastores) do datastore.data = Data[name] end +end, function(tbl) + for name in pairs(Datastores) do + tbl[name] = tbl[name] or {} + end end) ----- Datastore Manager diff --git a/exp_legacy/module/modules/gui/landfill.lua b/exp_legacy/module/modules/gui/landfill.lua index 0e6415ab..35643061 100644 --- a/exp_legacy/module/modules/gui/landfill.lua +++ b/exp_legacy/module/modules/gui/landfill.lua @@ -4,7 +4,7 @@ @alias landfill_container ]] -local Gui = require("modules.exp_legacy.expcore.gui") --- @dep expcore.gui +local Gui = require("modules/exp_gui") local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles @@ -93,8 +93,10 @@ for i, map in ipairs(curves) do end end +--- @param blueprint LuaItemStack +--- @return table local function landfill_gui_add_landfill(blueprint) - local entities = blueprint.get_blueprint_entities() + local entities = assert(blueprint.get_blueprint_entities()) local tile_index = 0 local new_tiles = {} @@ -164,22 +166,27 @@ local function landfill_gui_add_landfill(blueprint) return { tiles = new_tiles } end --- @element toolbar_button -Gui.toolbar_button("item/landfill", { "landfill.main-tooltip" }, function(player) - return Roles.player_allowed(player, "gui/landfill") -end) - :on_click(function(player, _, _) - if player.cursor_stack and player.cursor_stack.valid_for_read then - if player.cursor_stack.type == "blueprint" and player.cursor_stack.is_blueprint_setup() then - local modified = landfill_gui_add_landfill(player.cursor_stack) +--- Add the toolbar button +Gui.create_toolbar_button{ + name = "landfill", + sprite = "item/landfill", + tooltip = { "landfill.main-tooltip" }, + visible = function(player, element) + return Roles.player_allowed(player, "gui/landfill") + end +}:on_click(function(def, event, element) + local player = Gui.get_player(event) + if player.cursor_stack and player.cursor_stack.valid_for_read then + if player.cursor_stack.type == "blueprint" and player.cursor_stack.is_blueprint_setup() then + local modified = landfill_gui_add_landfill(player.cursor_stack) - if modified and next(modified.tiles) then - player.cursor_stack.set_blueprint_tiles(modified.tiles) - end + if modified and next(modified.tiles) then + player.cursor_stack.set_blueprint_tiles(modified.tiles) end - else - player.print{ "landfill.cursor-none" } end - end) + else + player.print{ "landfill.cursor-none" } + end +end) Event.add(defines.events.on_player_joined_game, landfill_init) diff --git a/exp_legacy/module/modules/gui/production.lua b/exp_legacy/module/modules/gui/production.lua index 5b3cf828..f957f6d0 100644 --- a/exp_legacy/module/modules/gui/production.lua +++ b/exp_legacy/module/modules/gui/production.lua @@ -1,7 +1,7 @@ ---- Production Data -- @gui Production -local Gui = require("modules.exp_legacy.expcore.gui") --- @dep expcore.gui +local Gui = require("modules/exp_gui") local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles @@ -35,8 +35,8 @@ end --- Display group -- @element production_data_group -local production_data_group = - Gui.element(function(_definition, parent, i) +local production_data_group = Gui.element("production_data_group") + :draw(function(_def, parent, i) local item if i == 0 then @@ -93,10 +93,10 @@ local production_data_group = --- A vertical flow containing all the production data -- @element production_data_set -local production_data_set = - Gui.element(function(_, parent, name) +local production_data_set = Gui.element("production_data_set") + :draw(function(_, parent, name) local production_set = parent.add{ type = "flow", direction = "vertical", name = name } - local disp = Gui.scroll_table(production_set, 350, 4, "disp") + local disp = Gui.elements.scroll_table(production_set, 350, 4, "disp") production_data_group(disp, 0) @@ -111,31 +111,37 @@ local production_data_set = return production_set end) -production_container = - Gui.element(function(definition, parent) - local container = Gui.container(parent, definition.name, 350) +production_container = Gui.element("production_container") + :draw(function(def, parent) + local container = Gui.elements.container(parent, 350) production_data_set(container, "production_st") return container.parent end) - :static_name(Gui.unique_static_name) - :add_to_left_flow() -Gui.left_toolbar_button("entity/assembling-machine-3", { "production.main-tooltip" }, production_container, function(player) - return Roles.player_allowed(player, "gui/production") -end) +--- Add the element to the left flow with a toolbar button +Gui.add_left_element(production_container, false) +Gui.create_toolbar_button{ + name = "production_toggle", + left_element = production_container, + sprite = "entity/assembling-machine-3", + tooltip = { "production.main-tooltip" }, + visible = function(player, element) + return Roles.player_allowed(player, "gui/production") + end +} Event.on_nth_tick(60, function() for _, player in pairs(game.connected_players) do - local frame = Gui.get_left_element(player, production_container) + local container = Gui.get_left_element(production_container, player) local stat = player.force.get_item_production_statistics(player.surface) -- Allow remote view - local precision_value = precision[frame.container["production_st"].disp.table["production_0_e"].selected_index] - local table = frame.container["production_st"].disp.table + local precision_value = precision[container.frame["production_st"].disp.table["production_0_e"].selected_index] + local table = container.frame["production_st"].disp.table for i = 1, 8 do local production_prefix = "production_" .. i - local item = table[production_prefix .. "_e"].elem_value + local item = table[production_prefix .. "_e"].elem_value --[[ @as string ]] if item then local add = math.floor(stat.get_flow_count{ name = item, category = "input", precision_index = precision_value, count = false } / 6) / 10 diff --git a/exp_legacy/module/modules/gui/task-list.lua b/exp_legacy/module/modules/gui/task-list.lua index 5ee310a4..d92167f9 100644 --- a/exp_legacy/module/modules/gui/task-list.lua +++ b/exp_legacy/module/modules/gui/task-list.lua @@ -5,7 +5,7 @@ ]] local ExpUtil = require("modules/exp_util") -local Gui = require("modules.exp_legacy.expcore.gui") --- @dep expcore.gui +local Gui = require("modules/exp_gui") local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles local Datastore = require("modules.exp_legacy.expcore.datastore") --- @dep expcore.datastore @@ -83,15 +83,18 @@ end --- Button displayed in the header bar, used to add a new task -- @element add_new_task -local add_new_task = - Gui.element{ +local add_new_task = Gui.element("add_new_task") + :draw{ type = "sprite-button", sprite = "utility/add", tooltip = { "task-list.add-tooltip" }, style = "tool_button", - name = Gui.unique_static_name, - }:style(Styles.sprite22):on_click( - function(player, _, _) + name = Gui.property_from_name, + } + :style(Styles.sprite22) + :on_click( + function(def, event, element) + local player = Gui.get_player(event) -- Disable editing PlayerIsEditing:set(player, false) -- Clear selected @@ -103,8 +106,8 @@ local add_new_task = --- Header displayed when no tasks are in the task list -- @element no_tasks_found -local no_tasks_found = - Gui.element( +local no_tasks_found = Gui.element("no_tasks_found") + :draw( function(_, parent) local header = parent.add{ @@ -113,6 +116,7 @@ local no_tasks_found = style = "negative_subheader_frame", } header.style.horizontally_stretchable = true + header.style.bottom_margin = 0 -- Flow used for centering the content in the subheader local center = header.add{ @@ -134,50 +138,43 @@ local no_tasks_found = --- Frame element with the right styling -- @element subfooter_frame -local subfooter_frame = - Gui.element( - function(_, parent, name) - return parent.add{ - type = "frame", - name = name, - direction = "vertical", - style = "subfooter_frame", - } - end - ):style{ +local subfooter_frame = Gui.element("task_list_subfooter_frame") + :draw{ + type = "frame", + name = Gui.property_from_arg(1), + direction = "vertical", + style = "subfooter_frame", + } + :style{ + height = 0, padding = 5, use_header_filler = false, - horizontally_stretchable = true, } --- Label element preset -- @element subfooter_label -local subfooter_label = - Gui.element( - function(_, parent, caption) - return parent.add{ - name = "footer_label", - type = "label", - style = "frame_title", - caption = caption, - } - end - ) +local subfooter_label = Gui.element("task_list_subfooter_label") + :draw{ + name = "footer_label", + type = "label", + style = "frame_title", + caption = Gui.property_from_arg(1), + } --- Action flow that contains action buttons -- @element subfooter_actions -local subfooter_actions = - Gui.element{ +local subfooter_actions = Gui.element("task_list_subfooter_actions") + :draw{ type = "flow", name = "actions", } --- Button element with a flow around it to fix duplicate name inside of the scroll flow -- @element task_list_item -local task_list_item = - Gui.element( - function(definition, parent, task) +local task_list_item = Gui.element("task_list_item") + :draw( + function(def, parent, task) local flow = parent.add{ type = "flow", name = "task-" .. task.task_id, @@ -187,7 +184,7 @@ local task_list_item = flow.style.horizontally_stretchable = true local button = flow.add{ - name = definition.name, + name = def.name, type = "button", style = "list_box_item", caption = task.title, @@ -199,17 +196,19 @@ local task_list_item = return button end - ):on_click( - function(player, element, _) + ) + :on_click( + function(def, event, element) + local player = Gui.get_player(event) local task_id = element.parent.caption PlayerSelected:set(player, task_id) end - ):static_name(Gui.unique_static_name) + ) --- Scrollable list of all tasks -- @element task_list -local task_list = - Gui.element( +local task_list = Gui.element("task_list") + :draw( function(_, parent) local scroll_pane = parent.add{ @@ -239,15 +238,16 @@ local task_list = --- Button element inside the task view footer to start editing a task -- @element task_view_edit_button -local task_view_edit_button = - Gui.element{ +local task_view_edit_button = Gui.element("task_view_edit_button") + :draw{ type = "button", - name = Gui.unique_static_name, + name = Gui.property_from_name, caption = { "", "[img=utility/rename_icon] ", { "task-list.edit" } }, tooltip = { "task-list.edit-tooltip" }, style = "shortcut_bar_button", }:style(Styles.footer_button):on_click( - function(player, _, _) + function(def, event, element) + local player = Gui.get_player(event) local selected = PlayerSelected:get(player) PlayerIsEditing:set(player, true) @@ -257,30 +257,35 @@ local task_view_edit_button = --- Button to close the task view footer -- @element task_view_close_button -local task_view_close_button = - Gui.element{ +local task_view_close_button = Gui.element("task_view_close_button") + :draw{ type = "sprite-button", sprite = "utility/collapse", style = "frame_action_button", tooltip = { "task-list.close-tooltip" }, } - :style(Styles.sprite22):on_click( - function(player, _, _) + :style(Styles.sprite22) + :on_click( + function(def, event, element) + local player = Gui.get_player(event) PlayerSelected:set(player, nil) end ) --- Button to delete the task inside the task view footer -- @element task_view_delete_button -local task_view_delete_button = - Gui.element{ +local task_view_delete_button = Gui.element("task_view_delete_button") + :draw{ type = "button", - name = Gui.unique_static_name, + name = Gui.property_from_name, caption = { "", "[img=utility/trash] ", { "task-list.delete" } }, tooltip = { "task-list.delete-tooltip" }, style = "shortcut_bar_button_red", - }:style(Styles.footer_button):on_click( - function(player, _, _) + } + :style(Styles.footer_button) + :on_click( + function(def, event, element) + local player = Gui.get_player(event) local selected = PlayerSelected:get(player) PlayerSelected:set(player, nil) Tasks.remove_task(selected) @@ -289,13 +294,13 @@ local task_view_delete_button = --- Subfooter inside the tasklist container that holds all the elements for viewing a task -- @element task_view_footer -local task_view_footer = - Gui.element( +local task_view_footer = Gui.element("task_view_footer") + :draw( function(_, parent) local footer = subfooter_frame(parent, "view") local flow = footer.add{ type = "flow" } subfooter_label(flow, { "task-list.view-footer-header" }) - local alignment = Gui.alignment(flow) + local alignment = Gui.elements.aligned_flow(flow) task_view_close_button(alignment) local title_label = footer.add{ @@ -345,9 +350,9 @@ local task_create_confirm_button --- Textfield element used in both the task create and edit footers -- @element task_message_textfield -local task_message_textfield = - Gui.element{ - name = Gui.unique_static_name, +local task_message_textfield = Gui.element("task_message_textfield") + :draw{ + name = Gui.property_from_name, type = "text-box", text = "", }:style{ @@ -356,7 +361,8 @@ local task_message_textfield = horizontally_stretchable = true, } :on_text_changed( - function(player, element, _) + function(def, event, element) + local player = Gui.get_player(event) local is_editing = PlayerIsEditing:get(player) local is_creating = PlayerIsCreating:get(player) @@ -372,15 +378,18 @@ local task_message_textfield = --- Button to confirm the changes inside the task edit footer -- @element task_edit_confirm_button -task_edit_confirm_button = - Gui.element{ +task_edit_confirm_button = Gui.element("task_edit_confirm_button") + :draw{ type = "button", - name = Gui.unique_static_name, + name = Gui.property_from_name, caption = { "", "[img=utility/check_mark] ", { "task-list.confirm" } }, tooltip = { "task-list.confirm-tooltip" }, style = "shortcut_bar_button_green", - }:style(Styles.footer_button):on_click( - function(player, element, _) + } + :style(Styles.footer_button) + :on_click( + function(def, event, element) + local player = Gui.get_player(event) local selected = PlayerSelected:get(player) PlayerIsEditing:set(player, false) local new_message = element.parent.parent[task_message_textfield.name].text @@ -392,14 +401,17 @@ task_edit_confirm_button = --- Button to discard the changes inside the task edit footer -- @element edit_task_discard_button -local edit_task_discard_button = - Gui.element{ +local edit_task_discard_button = Gui.element("edit_task_discard_button") + :draw{ type = "button", caption = { "", "[img=utility/close_black] ", { "task-list.discard" } }, tooltip = { "task-list.discard-tooltip" }, style = "shortcut_bar_button_red", - }:style(Styles.footer_button):on_click( - function(player, _, _) + } + :style(Styles.footer_button) + :on_click( + function(def, event, element) + local player = Gui.get_player(event) local selected = PlayerSelected:get(player) Tasks.set_editing(selected, player.name, nil) PlayerIsEditing:set(player, false) @@ -408,8 +420,8 @@ local edit_task_discard_button = --- Subfooter inside the tasklist container that holds all the elements for editing a task -- @element task_edit_footer -local task_edit_footer = - Gui.element( +local task_edit_footer = Gui.element("task_edit_footer") + :draw( function(_, parent) local footer = subfooter_frame(parent, "edit") subfooter_label(footer, { "task-list.edit-footer-header" }) @@ -427,16 +439,19 @@ local task_edit_footer = --- Button to confirm the changes inside the task create footer -- @element task_create_confirm_button -task_create_confirm_button = - Gui.element{ +task_create_confirm_button = Gui.element("task_create_confirm_button") + :draw{ type = "button", - name = Gui.unique_static_name, + name = Gui.property_from_name, caption = { "", "[img=utility/check_mark] ", { "task-list.confirm" } }, tooltip = { "task-list.confirm-tooltip" }, style = "shortcut_bar_button_green", enabled = false, - }:style(Styles.footer_button):on_click( - function(player, element, _) + } + :style(Styles.footer_button) + :on_click( + function(def, event, element) + local player = Gui.get_player(event) local message = element.parent.parent[task_message_textfield.name].text PlayerIsCreating:set(player, false) local parsed = parse_message(message) @@ -447,22 +462,25 @@ task_create_confirm_button = --- Button to discard the changes inside the task create footer -- @element task_create_discard_button -local task_create_discard_button = - Gui.element{ +local task_create_discard_button = Gui.element("task_create_discard_button") + :draw{ type = "button", caption = { "", "[img=utility/close_black] ", { "task-list.discard" } }, tooltip = { "task-list.discard-tooltip" }, style = "shortcut_bar_button_red", - }:style(Styles.footer_button):on_click( - function(player, _, _) + } + :style(Styles.footer_button) + :on_click( + function(def, event, element) + local player = Gui.get_player(event) PlayerIsCreating:set(player, false) end ) --- Subfooter inside the tasklist container that holds all the elements to create a new task -- @element task_create_footer -local task_create_footer = - Gui.element( +local task_create_footer = Gui.element("task_create_footer") + :draw( function(_, parent) local footer = subfooter_frame(parent, "create") subfooter_label(footer, { "task-list.create-footer-header" }) @@ -480,7 +498,7 @@ local task_create_footer = --- Clear and repopulate the task list with all current tasks local repopulate_task_list = function(task_list_element) - local force = Gui.get_player_from_element(task_list_element).force + local force = Gui.get_player(task_list_element).force local task_ids = Tasks.get_force_task_ids(force.name) task_list_element.clear() @@ -497,19 +515,21 @@ end --- Main task list container for the left flow -- @element task_list_container -local task_list_container = - Gui.element( - function(definition, parent) +local task_list_container = Gui.element("task_list_container") + :draw( + function(def, parent) -- Draw the internal container - local container = Gui.container(parent, definition.name, 268) + local container = Gui.elements.container(parent, 268) container.style.maximal_width = 268 - container.style.minimal_width = 268 -- Draw the header - local header = Gui.header(container, { "task-list.main-caption" }, { "task-list.sub-tooltip" }, true) + local header = Gui.elements.header(container, { + caption = { "task-list.main-caption" }, + tooltip = { "task-list.sub-tooltip" }, + }) -- Draw the new task button - local player = Gui.get_player_from_element(parent) + local player = Gui.get_player(parent) local add_new_task_element = add_new_task(header) add_new_task_element.visible = check_player_permissions(player) @@ -529,23 +549,22 @@ local task_list_container = -- Return the external container return container.parent end - ):static_name(Gui.unique_static_name):add_to_left_flow( - function(player) - local task_ids = Tasks.get_force_task_ids(player.force.name) - return #task_ids > 0 - end ) ---- Button on the top flow used to toggle the task list container --- @element toggle_left_element -Gui.left_toolbar_button( - "utility/not_enough_repair_packs_icon", - { "task-list.main-tooltip" }, - task_list_container, - function(player) +--- Add the element to the left flow with a toolbar button +Gui.add_left_element(task_list_container, function(player) + local task_ids = Tasks.get_force_task_ids(player.force.name) + return #task_ids > 0 +end) +Gui.create_toolbar_button{ + name = "task_list_toggle", + left_element = task_list_container, + sprite = "utility/not_enough_repair_packs_icon", + tooltip = { "task-list.main-tooltip" }, + visible = function(player, element) return Roles.player_allowed(player, "gui/task-list") end -) +} -- Function to update a single task and some of the elements inside the container local update_task = function(player, task_list_element, task_id) @@ -575,8 +594,8 @@ end -- Update the footer task edit view local update_task_edit_footer = function(player, task_id) local task = Tasks.get_task(task_id) - local frame = Gui.get_left_element(player, task_list_container) - local edit_flow = frame.container.edit + local container = Gui.get_left_element(task_list_container, player) + local edit_flow = container.frame.edit local message_element = edit_flow[task_message_textfield.name] @@ -587,8 +606,8 @@ end -- Update the footer task view local update_task_view_footer = function(player, task_id) local task = Tasks.get_task(task_id) - local frame = Gui.get_left_element(player, task_list_container) - local view_flow = frame.container.view + local container = Gui.get_left_element(task_list_container, player) + local view_flow = container.frame.view local has_permission = check_player_permissions(player, task) local title_element = view_flow.title @@ -633,8 +652,8 @@ Tasks.on_update( end end - local frame = Gui.get_left_element(player, task_list_container) - local task_list_element = frame.container.scroll.task_list + local container = Gui.get_left_element(task_list_container, player) + local task_list_element = container.frame.scroll.task_list -- Update the task that was changed update_task(player, task_list_element, task_id) @@ -647,12 +666,12 @@ PlayerIsCreating:on_update( function(player_name, curr_state, _) local player = game.players[player_name] - local frame = Gui.get_left_element(player, task_list_container) - local create = frame.container.create + local container = Gui.get_left_element(task_list_container, player) + local create = container.frame.create -- Clear the textfield - local message_element = frame.container.create[task_message_textfield.name] - local confirm_button_element = frame.container.create.actions[task_create_confirm_button.name] + local message_element = container.frame.create[task_message_textfield.name] + local confirm_button_element = container.frame.create.actions[task_create_confirm_button.name] message_element.focus() message_element.text = "" confirm_button_element.enabled = false @@ -670,10 +689,10 @@ PlayerSelected:on_update( function(player_name, curr_state, prev_state) local player = game.players[player_name] - local frame = Gui.get_left_element(player, task_list_container) - local task_list_element = frame.container.scroll.task_list - local view_flow = frame.container.view - local edit_flow = frame.container.edit + local container = Gui.get_left_element(task_list_container, player) + local task_list_element = container.frame.scroll.task_list + local view_flow = container.frame.view + local edit_flow = container.frame.edit local is_editing = PlayerIsEditing:get(player) local is_creating = PlayerIsCreating:get(player) @@ -721,9 +740,9 @@ PlayerIsEditing:on_update( function(player_name, curr_state, _) local player = game.players[player_name] - local frame = Gui.get_left_element(player, task_list_container) - local view_flow = frame.container.view - local edit_flow = frame.container.edit + local container = Gui.get_left_element(task_list_container, player) + local view_flow = container.frame.view + local edit_flow = container.frame.edit local selected = PlayerSelected:get(player) if curr_state then @@ -740,7 +759,7 @@ PlayerIsEditing:on_update( --- Makes sure the right buttons are present when roles change local function role_update_event(event) local player = game.players[event.player_index] - local container = Gui.get_left_element(player, task_list_container).container + local frame = Gui.get_left_element(task_list_container, player).frame -- Update the view task local selected = PlayerSelected:get(player) if selected then @@ -752,7 +771,7 @@ local function role_update_event(event) -- Update the new task button and create footer in case the user can now add them local has_permission = check_player_permissions(player) - local add_new_task_element = container.header.alignment[add_new_task.name] + local add_new_task_element = frame.header.alignment[add_new_task.name] add_new_task_element.visible = has_permission local is_creating = PlayerIsCreating:get(player) if is_creating and not has_permission then @@ -767,8 +786,8 @@ Event.add(Roles.events.on_role_unassigned, role_update_event) local function reset_task_list(event) -- Repopulate the task list local player = game.players[event.player_index] - local frame = Gui.get_left_element(player, task_list_container) - local task_list_element = frame.container.scroll.task_list + local container = Gui.get_left_element(task_list_container, player) + local task_list_element = container.frame.scroll.task_list repopulate_task_list(task_list_element) -- Check if the selected task is still valid diff --git a/exp_legacy/module/modules/gui/tool.lua b/exp_legacy/module/modules/gui/tool.lua index be36d435..ef453c7e 100644 --- a/exp_legacy/module/modules/gui/tool.lua +++ b/exp_legacy/module/modules/gui/tool.lua @@ -4,7 +4,7 @@ ]] local ExpUtil = require("modules/exp_util") -local Gui = require("modules/exp_legacy/expcore/gui") --- @dep expcore.gui +local Gui = require("modules/exp_gui") local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event local Selection = require("modules/exp_legacy/modules/control/selection") --- @dep modules.control.selection @@ -27,10 +27,10 @@ local style = { --- Arty label -- @element tool_gui_arty_l -local tool_gui_arty_l = - Gui.element{ +local tool_gui_arty_l = Gui.element("tool_gui_arty_l") + :draw{ type = "label", - name = "tool_arty_l", + name = Gui.property_from_name, caption = { "tool.artillery" }, tooltip = { "tool.artillery-tooltip" }, style = "heading_2_label" @@ -40,14 +40,15 @@ local tool_gui_arty_l = --- Arty button -- @element tool_gui_arty_b -local tool_gui_arty_b = - Gui.element{ +local tool_gui_arty_b = Gui.element("tool_gui_arty_b") + :draw{ type = "button", - name = "tool_arty_b", + name = Gui.property_from_name, caption = { "tool.apply" } }:style( style.button - ):on_click(function(player, _, _) + ):on_click(function(def, event, element) + local player = Gui.get_player(event) if Selection.is_selecting(player, SelectionArtyArea) then Selection.stop(player) @@ -59,10 +60,10 @@ local tool_gui_arty_b = --- Waterfill label -- @element tool_gui_waterfill_l -local tool_gui_waterfill_l = - Gui.element{ +local tool_gui_waterfill_l = Gui.element("tool_gui_waterfill_l") + :draw{ type = "label", - name = "tool_waterfill_l", + name = Gui.property_from_name, caption = { "tool.waterfill" }, tooltip = { "tool.waterfill-tooltip" }, style = "heading_2_label" @@ -72,14 +73,15 @@ local tool_gui_waterfill_l = --- Waterfill button -- @element tool_gui_waterfill_b -local tool_gui_waterfill_b = - Gui.element{ +local tool_gui_waterfill_b = Gui.element("tool_gui_waterfill_b") + :draw{ type = "button", - name = "tool_waterfill_b", + name = Gui.property_from_name, caption = { "tool.apply" } }:style( style.button - ):on_click(function(player, _, _) + ):on_click(function(def, event, element) + local player = Gui.get_player(event) if Selection.is_selecting(player, SelectionWaterfillArea) then Selection.stop(player) return player.print{ "exp-commands_waterfill.exit" } @@ -93,10 +95,10 @@ local tool_gui_waterfill_b = --- Train label -- @element tool_gui_train_l -local tool_gui_train_l = - Gui.element{ +local tool_gui_train_l = Gui.element("tool_gui_train_l") + :draw{ type = "label", - name = "tool_train_l", + name = Gui.property_from_name, caption = { "tool.train" }, tooltip = { "tool.train-tooltip" }, style = "heading_2_label" @@ -106,23 +108,24 @@ local tool_gui_train_l = --- Train button -- @element tool_gui_train_b -local tool_gui_train_b = - Gui.element{ +local tool_gui_train_b = Gui.element("tool_gui_train_b") + :draw{ type = "button", - name = "tool_train_b", + name = Gui.property_from_name, caption = { "tool.apply" } }:style( style.button - ):on_click(function(player, _, _) + ):on_click(function(def, event, element) + local player = Gui.get_player(event) addon_train.manual(player) end) --- Research label -- @element tool_gui_research_l -local tool_gui_research_l = - Gui.element{ +local tool_gui_research_l = Gui.element("tool_gui_research_l") + :draw{ type = "label", - name = "tool_research_l", + name = Gui.property_from_name, caption = { "tool.research" }, tooltip = { "tool.research-tooltip" }, style = "heading_2_label" @@ -132,18 +135,19 @@ local tool_gui_research_l = --- Research button -- @element tool_gui_research_b -local tool_gui_research_b = - Gui.element{ +local tool_gui_research_b = Gui.element("tool_gui_research_b") + :draw{ type = "button", - name = "tool_research_b", + name = Gui.property_from_name, caption = { "tool.apply" } }:style( style.button - ):on_click(function(player, _, _) + ):on_click(function(def, event, element) + local player = Gui.get_player(event) local enabled = addon_research.set_auto_research() if enabled then - addon_research.res_queue(player.force, true) + addon_research.res_queue(player.force --[[ @as LuaForce ]], true) end local player_name = ExpUtil.format_player_name_locale(player) @@ -152,10 +156,10 @@ local tool_gui_research_b = --- Spawn label -- @element tool_gui_spawn_l -local tool_gui_spawn_l = - Gui.element{ +local tool_gui_spawn_l = Gui.element("tool_gui_spawn_l") + :draw{ type = "label", - name = "tool_spawn_l", + name = Gui.property_from_name, caption = { "tool.spawn" }, tooltip = { "tool.spawn-tooltip" }, style = "heading_2_label" @@ -165,14 +169,15 @@ local tool_gui_spawn_l = --- Spawn button -- @element tool_gui_spawn_b -local tool_gui_spawn_b = - Gui.element{ +local tool_gui_spawn_b = Gui.element("tool_gui_spawn_b") + :draw{ type = "button", - name = "tool_spawn_b", + name = Gui.property_from_name, caption = { "tool.apply" } }:style( style.button - ):on_click(function(player, _, _) + ):on_click(function(def, event, element) + local player = Gui.get_player(event) if not player.character or player.character.health <= 0 or not ExpUtil.teleport_player(player, game.surfaces.nauvis, { 0, 0 }, "dismount") then @@ -180,9 +185,9 @@ local tool_gui_spawn_b = end end) -local function tool_perm(player) - local frame = Gui.get_left_element(player, tool_container) - local disp = frame.container["tool_st"].disp.table +local function tool_perm(player, container) + container = container or Gui.get_left_element(tool_container, player) + local disp = container.frame.tool_st.disp.table local allowed allowed = Roles.player_allowed(player, "command/artillery") @@ -208,10 +213,10 @@ end --- A vertical flow containing all the tool -- @element tool_set -local tool_set = - Gui.element(function(_, parent, name) +local tool_set = Gui.element("tool_set") + :draw(function(_, parent, name) local tool_set = parent.add{ type = "flow", direction = "vertical", name = name } - local disp = Gui.scroll_table(tool_set, 240, 2, "disp") + local disp = Gui.elements.scroll_table(tool_set, 240, 2, "disp") tool_gui_arty_l(disp) tool_gui_arty_b(disp) @@ -233,25 +238,29 @@ local tool_set = --- The main container for the tool gui -- @element tool_container -tool_container = - Gui.element(function(definition, parent) - local player = Gui.get_player_from_element(parent) - local container = Gui.container(parent, definition.name, 240) +tool_container = Gui.element("tool_container") + :draw(function(def, parent) + local player = Gui.get_player(parent) + local container = Gui.elements.container(parent, 240) tool_set(container, "tool_st") - tool_perm(player) + tool_perm(player, container.parent) return container.parent end) - :static_name(Gui.unique_static_name) - :add_to_left_flow() ---- Button on the top flow used to toggle the tool container --- @element toggle_left_element -Gui.left_toolbar_button("item/repair-pack", { "tool.main-tooltip" }, tool_container, function(player) - return Roles.player_allowed(player, "gui/tool") -end) +--- Add the element to the left flow with a toolbar button +Gui.add_left_element(tool_container, false) +Gui.create_toolbar_button{ + name = "tool_toggle", + left_element = tool_container, + sprite = "item/repair-pack", + tooltip = { "tool.main-tooltip" }, + visible = function(player, element) + return Roles.player_allowed(player, "gui/tool") + end +} Event.add(Roles.events.on_role_assigned, function(event) tool_perm(game.players[event.player_index]) diff --git a/exp_util/module/storage.lua b/exp_util/module/storage.lua index f064040c..5ec30318 100644 --- a/exp_util/module/storage.lua +++ b/exp_util/module/storage.lua @@ -49,15 +49,16 @@ local ExpUtil = require("modules/exp_util") --- @class ExpUtil_Storage local Storage = { - _registered = {}, --- @type table Map of all registered values and their initial values + _registered = {}, --- @type table } --- Register a new table to be stored in storage, can only be called once per file, can not be called during runtime --- @generic T:table --- @param tbl T The initial value for the table you are registering, this should be a local variable --- @param callback fun(tbl: T) The callback used to replace local references and metatables +--- @param on_init fun(tbl: T)? The callback used to setup/validate storage if a static value is not enough -- This function does not return the table because the callback can't access the local it would be assigned to -function Storage.register(tbl, callback) +function Storage.register(tbl, callback, on_init) ExpUtil.assert_not_runtime() ExpUtil.assert_argument_type(tbl, "table", 1, "tbl") ExpUtil.assert_argument_type(callback, "function", 2, "callback") @@ -70,6 +71,7 @@ function Storage.register(tbl, callback) Storage._registered[name] = { init = tbl, callback = callback, + on_init = on_init, } end @@ -110,6 +112,9 @@ function Storage.on_init() if exp_storage[name] == nil then exp_storage[name] = info.init end + if info.on_init then + info.on_init(exp_storage[name]) + end info.callback(exp_storage[name]) end end