From 3cb39ad94acbdcd14c65e9f6cb3a93fd978f62ca Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Fri, 17 Nov 2023 00:17:00 +0100 Subject: [PATCH 1/2] Changes Adds a new gui button at top that allows players to toggle all the top buttons Minor changes to fish defender Minor changes to Mtn v3 --- maps/fish_defender_v2/main.lua | 1 + maps/fish_defender_v2/market.lua | 1 + maps/fish_defender_v2/terrain.lua | 3 - maps/mountain_fortress_v3/gui.lua | 18 ++++ maps/mountain_fortress_v3/locomotive.lua | 2 +- maps/mountain_fortress_v3/main.lua | 1 + maps/scrap_towny_ffa/info.lua | 4 +- maps/scrap_towny_ffa/main.lua | 4 + modules/flashlight_toggle_button.lua | 3 +- modules/global_chat_toggle.lua | 4 +- utils/gui.lua | 104 +++++++++++++++++++---- 11 files changed, 122 insertions(+), 23 deletions(-) diff --git a/maps/fish_defender_v2/main.lua b/maps/fish_defender_v2/main.lua index 6061494b3..e1d6402ee 100644 --- a/maps/fish_defender_v2/main.lua +++ b/maps/fish_defender_v2/main.lua @@ -26,6 +26,7 @@ local enable_start_grace_period = true Gui.mod_gui_button_enabled = true Gui.button_style = 'mod_gui_button' +Gui.set_toggle_button(true) local starting_items = { ['pistol'] = 1, diff --git a/maps/fish_defender_v2/market.lua b/maps/fish_defender_v2/market.lua index c27ef1cd8..d5724b8e8 100644 --- a/maps/fish_defender_v2/market.lua +++ b/maps/fish_defender_v2/market.lua @@ -101,6 +101,7 @@ local function refresh_market_offers() {price = {{'coin', 200}}, offer = {type = 'give-item', item = 'belt-immunity-equipment', count = 1}}, {price = {{'coin', 250}}, offer = {type = 'give-item', item = 'personal-roboport-equipment', count = 1}}, {price = {{'coin', 350}}, offer = {type = 'give-item', item = 'roboport', count = 1}}, + {price = {{'coin', 50}}, offer = {type = 'give-item', item = 'logistic-chest-storage', count = 1}}, {price = {{'coin', 35}}, offer = {type = 'give-item', item = 'construction-robot', count = 1}}, {price = {{'coin', 25}}, offer = {type = 'give-item', item = 'cliff-explosives', count = 1}} } diff --git a/maps/fish_defender_v2/terrain.lua b/maps/fish_defender_v2/terrain.lua index a5681a7d8..cab10b7c0 100644 --- a/maps/fish_defender_v2/terrain.lua +++ b/maps/fish_defender_v2/terrain.lua @@ -270,9 +270,6 @@ local function initial_cargo_boxes() {name = 'gun-turret', count = 1}, {name = 'gun-turret', count = 1}, {name = 'gun-turret', count = 1}, - {name = 'logistic-chest-storage', count = 1}, - {name = 'logistic-chest-storage', count = 1}, - {name = 'logistic-chest-storage', count = 1}, {name = 'shotgun-shell', count = random(4, 5)}, {name = 'shotgun-shell', count = random(4, 5)}, {name = 'shotgun-shell', count = random(4, 5)}, diff --git a/maps/mountain_fortress_v3/gui.lua b/maps/mountain_fortress_v3/gui.lua index 759b0ed7c..4f43bf4bf 100644 --- a/maps/mountain_fortress_v3/gui.lua +++ b/maps/mountain_fortress_v3/gui.lua @@ -196,6 +196,7 @@ local function on_player_joined_game(event) end local function changed_surface(player) + local main_toggle_button_name = Gui.main_toggle_button_name local poll_button = Polls.main_button_name local rpg_button = RPG.draw_main_frame_name local rpg_frame = RPG.main_frame_name @@ -203,6 +204,7 @@ local function changed_surface(player) local main = Public.get('locomotive') local icw_locomotive = Public.get('icw_locomotive') local wagon_surface = icw_locomotive.surface + local main_toggle_button = player.gui.top[main_toggle_button_name] local info = player.gui.top[main_button_name] local wd = player.gui.top['wave_defense'] local spectate = player.gui.top[spectate_button_name] @@ -238,6 +240,9 @@ local function changed_surface(player) if player.surface == main.surface then local minimap = player.gui.left.icw_main_frame + if main_toggle_button and not main_toggle_button.visible then + main_toggle_button.visible = true + end if minimap and minimap.visible then minimap.visible = false end @@ -271,6 +276,9 @@ local function changed_surface(player) info.visible = true end elseif player.surface == wagon_surface then + if main_toggle_button and main_toggle_button.visible then + main_toggle_button.visible = false + end if wd then wd.visible = false end @@ -313,6 +321,9 @@ local function changed_surface(player) end end else + if main_toggle_button and main_toggle_button.visible then + main_toggle_button.visible = false + end if poll_b then poll_b.visible = false end @@ -429,6 +440,8 @@ local function enable_guis(event) return end + local main_toggle_button_name = Gui.main_toggle_button_name + local main_toggle_button = player.gui.top[main_toggle_button_name] local rpg_button = RPG.draw_main_frame_name local info = player.gui.top[main_button_name] local wd = player.gui.top['wave_defense'] @@ -445,10 +458,15 @@ local function enable_guis(event) info.sprite = 'item/dummy-steel-axe' end + if main_toggle_button and not main_toggle_button.visible then + main_toggle_button.visible = false + end + local minimap = player.gui.left.icw_main_frame if minimap and minimap.visible then minimap.visible = false end + if rpg_b and not rpg_b.visible then rpg_b.visible = true end diff --git a/maps/mountain_fortress_v3/locomotive.lua b/maps/mountain_fortress_v3/locomotive.lua index 5ded80ef7..52789e60d 100644 --- a/maps/mountain_fortress_v3/locomotive.lua +++ b/maps/mountain_fortress_v3/locomotive.lua @@ -285,7 +285,7 @@ local function give_passive_xp(data) end end end - elseif player.afk_time > 200 and player.character and player.surface.index == loco_surface.index then + elseif player.afk_time > 600 and player.character and player.surface.index == loco_surface.index then player.character_personal_logistic_requests_enabled = false end ::pre_exit:: diff --git a/maps/mountain_fortress_v3/main.lua b/maps/mountain_fortress_v3/main.lua index 8a903b3b3..44e4163c7 100644 --- a/maps/mountain_fortress_v3/main.lua +++ b/maps/mountain_fortress_v3/main.lua @@ -60,6 +60,7 @@ local remove = table.remove RPG.disable_cooldowns_on_spells() Gui.mod_gui_button_enabled = true Gui.button_style = 'mod_gui_button' +Gui.set_toggle_button(true) local collapse_kill = { entities = { diff --git a/maps/scrap_towny_ffa/info.lua b/maps/scrap_towny_ffa/info.lua index b2a52a936..7e8bea7a8 100644 --- a/maps/scrap_towny_ffa/info.lua +++ b/maps/scrap_towny_ffa/info.lua @@ -1,4 +1,5 @@ local Event = require 'utils.event' +local Gui = require 'utils.gui' local Public = {} @@ -53,10 +54,11 @@ function Public.toggle_button(player) if player.gui.top['towny_map_intro_button'] then return end - local b = player.gui.top.add({type = 'sprite-button', caption = 'Info', name = 'towny_map_intro_button', tooltip = 'Show Info'}) + local b = player.gui.top.add({type = 'sprite-button', caption = 'Info', name = 'towny_map_intro_button', tooltip = 'Show Info', style = Gui.button_style}) b.style.font_color = {r = 0.5, g = 0.3, b = 0.99} b.style.font = 'heading-1' b.style.minimal_height = 38 + b.style.maximal_height = 38 b.style.minimal_width = 80 b.style.top_padding = 1 b.style.left_padding = 1 diff --git a/maps/scrap_towny_ffa/main.lua b/maps/scrap_towny_ffa/main.lua index 4cb224046..83f6b4051 100644 --- a/maps/scrap_towny_ffa/main.lua +++ b/maps/scrap_towny_ffa/main.lua @@ -41,6 +41,10 @@ local Where = require 'utils.commands.where' local Inventory = require 'modules.show_inventory' local JailData = require 'utils.datastore.jail_data' +Gui.mod_gui_button_enabled = true +Gui.button_style = 'mod_gui_button' +Gui.set_toggle_button(true) + local function spairs(t) local keys = {} for k in pairs(t) do diff --git a/modules/flashlight_toggle_button.lua b/modules/flashlight_toggle_button.lua index 276c2190d..a37bbb592 100644 --- a/modules/flashlight_toggle_button.lua +++ b/modules/flashlight_toggle_button.lua @@ -1,6 +1,7 @@ -- toggle your flashlight -- by mewmew local Event = require 'utils.event' +local Gui = require 'utils.gui' local message_color = {r = 200, g = 200, b = 0} local function on_gui_click(event) @@ -54,7 +55,7 @@ local function on_player_joined_game(event) if player.gui.top['flashlight_toggle'] then return end - local b = player.gui.top.add({type = 'sprite-button', name = 'flashlight_toggle', sprite = 'item/small-lamp', tooltip = 'Toggle flashlight'}) + local b = player.gui.top.add({type = 'sprite-button', name = 'flashlight_toggle', sprite = 'item/small-lamp', tooltip = 'Toggle flashlight', style = Gui.button_style}) b.style.minimal_height = 38 b.style.maximal_height = 38 b.style.minimal_width = 38 diff --git a/modules/global_chat_toggle.lua b/modules/global_chat_toggle.lua index 94f9b3885..89fa51c00 100644 --- a/modules/global_chat_toggle.lua +++ b/modules/global_chat_toggle.lua @@ -1,3 +1,4 @@ +local Gui = require 'utils.gui' local function toggle(player) if not player.gui.top.global_chat_toggle then return @@ -26,7 +27,8 @@ local function create_gui_button(player) { type = 'sprite-button', name = 'global_chat_toggle', - caption = '' + caption = '', + style = Gui.button_style } ) b.style.font = 'heading-2' diff --git a/utils/gui.lua b/utils/gui.lua index ca5b98328..a48e9ae48 100644 --- a/utils/gui.lua +++ b/utils/gui.lua @@ -13,10 +13,11 @@ local Public = {} Public.events = {on_gui_removal = Event.generate_event_name('on_gui_removal')} -- local to this file +local local_settings = { + toggle_button = false +} local main_gui_tabs = {} local screen_elements = {} -local on_visible_handlers = {} -local on_pre_hidden_handlers = {} local remove_data_recursively -- global @@ -58,6 +59,7 @@ function Public.uid() end local main_frame_name = Public.uid_name() +local main_toggle_button_name = Public.uid_name() local main_button_name = Public.uid_name() local close_button_name = Public.uid_name() @@ -71,6 +73,7 @@ Public.frame_style = 'non_draggable_frame' Public.top_main_gui_button = main_button_name Public.main_frame_name = main_frame_name +Public.main_toggle_button_name = main_toggle_button_name --- Verifies if a frame is valid and destroys it. ---@param align userdata @@ -585,9 +588,29 @@ function Public.get_mod_gui_top_frame() return settings.mod_gui_top_frame end +---@param state boolean +--- If we should show the toggle button or not +function Public.set_toggle_button(state) + if _LIFECYCLE == 8 then + error('Calling Gui.set_toggle_button after on_init() or on_load() has run is a desync risk.', 2) + end + local_settings.toggle_button = state or false +end + +--- Get toggle_button state +function Public.get_toggle_button() + if _LIFECYCLE == 8 then + error('Calling Gui.get_toggle_button after on_init() or on_load() has run is a desync risk.', 2) + end + return local_settings.toggle_button +end + --- This adds the given gui to the main gui. ---@param tbl table function Public.add_tab_to_gui(tbl) + if _LIFECYCLE == 8 then + error('Calling Gui.add_tab_to_gui after on_init() or on_load() has run is a desync risk.', 2) + end if not tbl then return end @@ -667,6 +690,10 @@ function Public.clear_all_active_frames(player) child.destroy() end end + for _, child in pairs(player.gui.center.children) do + remove_data_recursively(child) + child.destroy() + end end function Public.get_player_active_frame(player) @@ -748,6 +775,26 @@ local function top_button(player) end end +local function top_toggle_button(player) + if not player or not player.valid then + return + end + + 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!' + } + ) + b.style.padding = 2 + b.style.width = 20 + b.style.maximal_height = 38 +end + local function draw_main_frame(player) local tabs = main_gui_tabs @@ -884,20 +931,6 @@ Public.on_text_changed = handler_factory(defines.events.on_gui_text_changed) -- Adds a player field to the event table. Public.on_value_changed = handler_factory(defines.events.on_gui_value_changed) --- Register a handler for when the player shows the top LuaGuiElements with element_name. --- Assuming the element_name has been added with Public.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. -Public.on_player_show_top = custom_handler_factory(on_visible_handlers) - --- Register a handler for when the player hides the top LuaGuiElements with element_name. --- Assuming the element_name has been added with Public.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. -Public.on_pre_player_hide_top = custom_handler_factory(on_pre_hidden_handlers) - Public.on_click( main_button_name, function(event) @@ -933,6 +966,42 @@ Public.on_click( end ) +Public.on_click( + main_toggle_button_name, + function(event) + local button = event.element + local player = event.player + local top = player.gui.top + + if button.sprite == 'utility/preset' then + for _, ele in pairs(top.children) do + if ele and ele.valid and ele.name ~= main_toggle_button_name then + ele.visible = false + end + end + + Public.clear_all_active_frames(player) + + local main_frame = Public.get_main_frame(player) + if main_frame then + main_frame.destroy() + end + + button.sprite = 'utility/expand_dots_white' + button.tooltip = 'Click to show top buttons!' + else + for _, ele in pairs(top.children) do + if ele and ele.valid and ele.name ~= main_toggle_button_name then + ele.visible = true + end + end + + button.sprite = 'utility/preset' + button.tooltip = 'Click to hide top buttons!' + end + end +) + Event.add( defines.events.on_gui_click, function(event) @@ -971,6 +1040,9 @@ Event.add( defines.events.on_player_created, function(event) local player = game.get_player(event.player_index) + if local_settings.toggle_button then + top_toggle_button(player) + end top_button(player) end ) From dfcf21d5c34353535b4183d19891f4fb2b9f8722 Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Fri, 17 Nov 2023 00:17:18 +0100 Subject: [PATCH 2/2] Fix luacheck --- utils/gui.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/utils/gui.lua b/utils/gui.lua index a48e9ae48..1dfef73dd 100644 --- a/utils/gui.lua +++ b/utils/gui.lua @@ -485,12 +485,6 @@ local function handler_factory(event_id) end end -local function custom_handler_factory(handlers) - return function(element_name, handler) - handlers[element_name] = handler - end -end - --luacheck: ignore custom_raise ---@diagnostic disable-next-line: unused-function, unused-local local function custom_raise(handlers, element, player)