From 1169ef9b7cc4bc0a24e8f960847ea57a0acdf869 Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Mon, 13 Nov 2023 23:58:43 +0100 Subject: [PATCH] Tweaks --- maps/fish_defender_v2/main.lua | 10 +- maps/mountain_fortress_v3/highscore.lua | 3 - modules/launch_fish_to_win.lua | 11 +- modules/show_inventory.lua | 71 +++++--- utils/commands/where.lua | 20 ++- utils/gui.lua | 211 +++++++++++++++--------- utils/gui/player_list.lua | 53 ++++-- utils/player_list_vars.lua | 6 +- 8 files changed, 253 insertions(+), 132 deletions(-) diff --git a/maps/fish_defender_v2/main.lua b/maps/fish_defender_v2/main.lua index c24f9c365..6061494b3 100644 --- a/maps/fish_defender_v2/main.lua +++ b/maps/fish_defender_v2/main.lua @@ -3,6 +3,7 @@ --require "modules.rpg" local Public = require 'maps.fish_defender_v2.core' +local Gui = require 'utils.gui' require 'modules.rocket_launch_always_yields_science' require 'modules.launch_fish_to_win' require 'modules.biters_yield_coins' @@ -23,6 +24,9 @@ local random = math.random local insert = table.insert local enable_start_grace_period = true +Gui.mod_gui_button_enabled = true +Gui.button_style = 'mod_gui_button' + local starting_items = { ['pistol'] = 1, ['firearm-magazine'] = 20, @@ -103,7 +107,8 @@ local function create_wave_gui(player) if player.gui.top['fish_defense_waves'] then player.gui.top['fish_defense_waves'].destroy() end - local frame = player.gui.top.add({type = 'frame', name = 'fish_defense_waves'}) + local frame = player.gui.top.add({type = 'frame', name = 'fish_defense_waves', style = 'finished_game_subheader_frame'}) + frame.style.minimal_height = 38 frame.style.maximal_height = 38 local wave_count = 0 @@ -215,7 +220,8 @@ local function add_fd_stats_button(player) player.gui.top.add { type = 'sprite-button', name = button_id, - sprite = 'item/submachine-gun' + sprite = 'item/submachine-gun', + style = Gui.button_style } b.style.maximal_height = 38 end diff --git a/maps/mountain_fortress_v3/highscore.lua b/maps/mountain_fortress_v3/highscore.lua index 46dc75d59..91cd29308 100644 --- a/maps/mountain_fortress_v3/highscore.lua +++ b/maps/mountain_fortress_v3/highscore.lua @@ -120,9 +120,6 @@ local function get_sorted_list(column_name, score_list) for key, player in ipairs(score_list) do sl[key] = player i = i + 1 - if i == 20 then - return sl - end end return sl end diff --git a/modules/launch_fish_to_win.lua b/modules/launch_fish_to_win.lua index f90569ac9..807f1addc 100644 --- a/modules/launch_fish_to_win.lua +++ b/modules/launch_fish_to_win.lua @@ -1,6 +1,7 @@ -- launch fish into space to win the game -- by mewmew local Event = require 'utils.event' +local Gui = require 'utils.gui' local function goals() if not global.catplanet_goals then @@ -98,14 +99,9 @@ local function fish_in_space_toggle_button(player) if player.gui.top['fish_in_space_toggle'] then return end - local button = player.gui.top.add {name = 'fish_in_space_toggle', type = 'sprite-button', sprite = 'item/raw-fish', tooltip = 'Fish in Space'} - button.style.font = 'default-bold' + local button = player.gui.top.add {name = 'fish_in_space_toggle', type = 'sprite-button', sprite = 'item/raw-fish', tooltip = 'Fish in Space', style = Gui.button_style} button.style.minimal_height = 38 - button.style.minimal_width = 38 - button.style.top_padding = 2 - button.style.left_padding = 4 - button.style.right_padding = 4 - button.style.bottom_padding = 2 + button.style.maximal_height = 38 end local function level_up_popup(player) @@ -138,6 +134,7 @@ local function fish_in_space_gui(player) local frame = player.gui.left.add({type = 'frame', name = 'fish_in_space'}) local label = frame.add({type = 'label', caption = 'Fish rescued: '}) label.style.font_color = {r = 0.11, g = 0.8, b = 0.44} + frame.style.bottom_padding = -2 local progress = global.fish_in_space / global.catplanet_goals[i + 1].goal if progress > 1 then diff --git a/modules/show_inventory.lua b/modules/show_inventory.lua index ef0d008a6..cc0eae99b 100644 --- a/modules/show_inventory.lua +++ b/modules/show_inventory.lua @@ -4,6 +4,7 @@ local Global = require 'utils.global' local Color = require 'utils.color_presets' local SpamProtection = require 'utils.spam_protection' local Event = require 'utils.event' +local Gui = require 'utils.gui' local this = { data = {}, @@ -18,6 +19,8 @@ Global.register( end ) +local main_frame_name = Gui.uid_name() + local space = { minimal_height = 10, top_padding = 0, @@ -143,7 +146,7 @@ local function close_player_inventory(player) return end - local element = gui.inventory_gui + local element = gui[main_frame_name] if not is_valid(element) then return @@ -186,7 +189,7 @@ local function redraw_inventory(gui, source, target, caption, panel_type) return end - local inventory_gui = screen.inventory_gui + local inventory_gui = screen[main_frame_name] inventory_gui.caption = 'Inventory of ' .. target.name for i = 1, #panel_type do @@ -271,7 +274,7 @@ local function open_inventory(source, target) return end - local inventory_gui = screen.inventory_gui + local inventory_gui = screen[main_frame_name] if inventory_gui then close_player_inventory(source) end @@ -282,7 +285,7 @@ local function open_inventory(source, target) type = 'frame', caption = 'Inventory', direction = 'vertical', - name = 'inventory_gui' + name = main_frame_name } ) @@ -383,22 +386,6 @@ local function on_gui_click(event) end end end -local function gui_closed(event) - local player = game.get_player(event.player_index) - if not this.data[player.index] then - return - end - - local type = event.gui_type - - if type == defines.gui_type.custom then - local data = get_player_data(player) - if not data then - return - end - close_player_inventory(player) - end -end local function on_pre_player_left_game(event) local player = game.get_player(event.player_index) @@ -466,11 +453,36 @@ commands.add_command( end ) +function Public.show_inventory(player, target_player) + if not player or not player.valid then + return false + end + if not target_player or not target_player.valid then + return false + end + + local valid, opened = player_opened(player) + if valid then + if target_player.index == opened then + player.print('You are already viewing this players inventory.', Color.warning) + return false + end + end + + if validate_player(target_player) then + open_inventory(player, target_player) + return true + else + player.print('[Inventory] Please type a name of a player who is connected.', Color.warning) + return false + end +end + function Public.get_active_frame(player) - if not player.gui.screen.inventory_gui then + if not player.gui.screen[main_frame_name] then return false end - return player.gui.screen.inventory_gui.tabbed_pane.tabs[player.gui.screen.inventory_gui.tabbed_pane.selected_tab_index].content + return player.gui.screen[main_frame_name].tabbed_pane.tabs[player.gui.screen[main_frame_name].tabbed_pane.selected_tab_index].content end function Public.get(key) @@ -487,13 +499,26 @@ function Public.module_disabled(state) this.module_disabled = state or false end +Gui.on_custom_close( + main_frame_name, + function(event) + local player = game.get_player(event.player_index) + if not this.data[player.index] then + return + end + + close_player_inventory(player) + end +) + Event.add(defines.events.on_player_main_inventory_changed, update_gui) Event.add(defines.events.on_player_gun_inventory_changed, update_gui) Event.add(defines.events.on_player_ammo_inventory_changed, update_gui) Event.add(defines.events.on_player_armor_inventory_changed, update_gui) Event.add(defines.events.on_player_trash_inventory_changed, update_gui) -Event.add(defines.events.on_gui_closed, gui_closed) Event.add(defines.events.on_gui_click, on_gui_click) Event.add(defines.events.on_pre_player_left_game, on_pre_player_left_game) +Public.close_player_inventory = close_player_inventory + return Public diff --git a/utils/commands/where.lua b/utils/commands/where.lua index f2b357fc4..c138dd4f4 100644 --- a/utils/commands/where.lua +++ b/utils/commands/where.lua @@ -94,7 +94,7 @@ local function create_mini_camera_gui(player, target, zoom, render, tooltip) end local player_data - if validate_player(target) then + if validate_player(target) or (target and target.valid and player.admin) then player_data = create_player_data(player) player_data.target = target else @@ -107,6 +107,8 @@ local function create_mini_camera_gui(player, target, zoom, render, tooltip) frame = player.gui.screen.add({type = 'frame', name = locate_player_frame_name, caption = target.name}) end + player.opened = frame + frame.force_auto_center() local surface = tonumber(target.surface.index) @@ -219,6 +221,17 @@ Gui.on_click( end ) +Gui.on_custom_close( + locate_player_frame_name, + function(event) + local is_spamming = SpamProtection.is_spamming(event.player, nil, 'Where Locate Player') + if is_spamming then + return + end + remove_camera_frame(event.player) + end +) + Gui.on_click( player_frame_name, function(event) @@ -226,7 +239,10 @@ Gui.on_click( if is_spamming then return end - remove_camera_frame(event.player) + local type = event.gui_type + if type == defines.gui_type.custom then + remove_camera_frame(event.player) + end end ) diff --git a/utils/gui.lua b/utils/gui.lua index 8dfe3fd43..ca5b98328 100644 --- a/utils/gui.lua +++ b/utils/gui.lua @@ -138,6 +138,57 @@ function Public.set_data_parent(parent, element, 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 + end +end + -- Gets the Associated data with this LuaGuiElement if any. function Public.get_data(element) if not element then @@ -178,6 +229,30 @@ function Public.get_data_parent(parent, element) 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] +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) if not align then @@ -738,6 +813,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}) end end @@ -763,6 +839,9 @@ function Public.call_existing_tab(player, name) end end +Public.get_button_flow = mod_gui.get_button_flow +Public.mod_button = mod_gui.get_button_flow + -- Register a handler for the on_gui_checked_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. @@ -819,66 +898,6 @@ Public.on_player_show_top = custom_handler_factory(on_visible_handlers) -- Adds a player field to the event table. Public.on_pre_player_hide_top = custom_handler_factory(on_pre_hidden_handlers) -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 -end - -Public.get_button_flow = mod_gui.get_button_flow -Public.mod_button = mod_gui.get_button_flow - Public.on_click( main_button_name, function(event) @@ -891,6 +910,7 @@ Public.on_click( if frame then remove_data_recursively(frame) frame.destroy() + Event.raise(Public.events.on_gui_removal, {player_index = player.index}) else draw_main_frame(player) end @@ -963,22 +983,61 @@ Event.add( end ) -Event.add( - Public.events.on_gui_removal, - function(player) - local b = - Public.get_button_flow(player).add( - { - type = 'sprite-button', - name = main_button_name, - sprite = 'utility/expand_dots', - style = Public.button_style, - tooltip = 'The panel of all the goodies!' - } - ) - b.style.padding = 2 - b.style.width = 20 +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 +end return Public diff --git a/utils/gui/player_list.lua b/utils/gui/player_list.lua index f69f345b8..56e09eba8 100644 --- a/utils/gui/player_list.lua +++ b/utils/gui/player_list.lua @@ -12,6 +12,7 @@ local Token = require 'utils.token' local Vars = require 'utils.player_list_vars' local Utils = require 'utils.utils' local Core = require 'utils.core' +local Inventory = require 'modules.show_inventory' local Public = {} @@ -23,6 +24,7 @@ local ranks = Vars.ranks local pokemessages = Vars.pokemessages local get_formatted_playtime = Utils.get_formatted_playtime local get_comparator = Vars.get_comparator +local tag = 'Players' local this = { player_list = { @@ -248,23 +250,35 @@ Gui.on_click( local player = event.player local element = event.element - local data = Gui.get_data(element) + local button = event.button + local data = Gui.get_data_custom(tag, element) if not data then return end - local is_spamming = SpamProtection.is_spamming(player, nil, 'PlayerList Locate Player') - if is_spamming then - return - end + if button == defines.mouse_button_type.left then + local is_spamming = SpamProtection.is_spamming(player, nil, 'PlayerList Locate Player') + if is_spamming then + return + end - local target = game.get_player(data) - if not target or not target.valid then - Gui.set_data(element, nil) - return + local target = game.get_player(data) + if not target or not target.valid then + return + end + Where.create_mini_camera_gui(player, target) + elseif defines.mouse_button_type.right then + local is_spamming = SpamProtection.is_spamming(player, nil, 'PlayerList Show Inventory') + if is_spamming then + return + end + + local target = game.get_player(data) + if not target or not target.valid then + return + end + Inventory.show_inventory(player, target) end - Gui.set_data(element, nil) - Where.create_mini_camera_gui(player, target) end ) @@ -274,14 +288,13 @@ Gui.on_click( local player = event.player local element = event.element - local data = Gui.get_data(element) + local data = Gui.get_data_custom(tag, element) if not data then return end local target = game.get_player(data) if player.index == target.index then - Gui.set_data(element, nil) return end @@ -297,7 +310,6 @@ Gui.on_click( game.print(str) this.player_list.last_poke_tick[player.index] = game.tick this.player_list.pokes[target.index] = this.player_list.pokes[target.index] + 1 - Gui.set_data(element, nil) end end ) @@ -306,7 +318,7 @@ local function refresh() for _, player in pairs(game.connected_players) do local frame = Gui.get_player_active_frame(player) if frame then - if frame.name ~= 'Players' then + if frame.name ~= tag then return end local data = {player = player, frame = frame, sort_by = this.player_list.sorting_method[player.index]} @@ -342,12 +354,13 @@ function Public.rpg_enabled(value) return this.rpg_enabled end -Gui.add_tab_to_gui({name = module_name, caption = 'Players', id = player_list_show_token, admin = false}) +Gui.add_tab_to_gui({name = module_name, caption = tag, id = player_list_show_token, admin = false}) Gui.on_click( module_name, function(event) local player = event.player + Gui.set_data_custom(tag, {player_index = player.index, remove = true}, nil) Gui.reload_active_tab(player) end ) @@ -370,7 +383,7 @@ Gui.on_click( if not frame then return end - if frame.name ~= 'Players' then + if frame.name ~= tag then return end @@ -390,5 +403,11 @@ Gui.on_click( Event.add(defines.events.on_player_joined_game, on_player_joined_game) Event.add(defines.events.on_player_left_game, on_player_left_game) +Event.add( + Gui.events.on_gui_removal, + function(event) + Gui.set_data_custom(tag, {player_index = event.player_index, remove = true}, nil) + end +) return Public diff --git a/utils/player_list_vars.lua b/utils/player_list_vars.lua index 41e1281a9..ba3b6c106 100644 --- a/utils/player_list_vars.lua +++ b/utils/player_list_vars.lua @@ -413,6 +413,8 @@ Public.gui_data = function(data) tooltip = 'This player is not trusted.' .. minimap .. tooltip end + tooltip = tooltip .. '\nRight-click to view their inventory!' + local caption if show_roles_in_list or player.admin then caption = player.name .. ' ' .. trusted @@ -435,7 +437,7 @@ Public.gui_data = function(data) tooltip = tooltip } - Gui.set_data(name_label, player.index) + Gui.set_data_custom('Players', name_label, player.index) name_label.style.font = 'default' name_label.style.font_color = { @@ -589,7 +591,7 @@ Public.gui_data = function(data) poke_flow.style.right_padding = 20 local poke_label = poke_flow.add {type = 'label', name = 'button_spacer_' .. index, caption = ''} local poke_button = poke_flow.add {type = 'button', name = poke_player_frame_name, caption = player.pokes} - Gui.set_data(poke_button, player.index) + Gui.set_data_custom('Players', poke_button, player.index) poke_button.style.font = 'default' poke_button.tooltip = 'Poke ' .. player.name .. ' with a random message!\nDoes not work poking yourself :<' poke_label.style.font_color = {r = 0.83, g = 0.83, b = 0.83}