Skip to content

Commit

Permalink
Merge pull request #437 from ComfyFactory/changes
Browse files Browse the repository at this point in the history
Minor changes
  • Loading branch information
Gerkiz authored Nov 16, 2023
2 parents 86cdea7 + dfcf21d commit df0ade5
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 29 deletions.
1 change: 1 addition & 0 deletions maps/fish_defender_v2/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions maps/fish_defender_v2/market.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
Expand Down
3 changes: 0 additions & 3 deletions maps/fish_defender_v2/terrain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
18 changes: 18 additions & 0 deletions maps/mountain_fortress_v3/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ 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
local rpg_settings = RPG.settings_frame_name
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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion maps/mountain_fortress_v3/locomotive.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
1 change: 1 addition & 0 deletions maps/mountain_fortress_v3/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 3 additions & 1 deletion maps/scrap_towny_ffa/info.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Event = require 'utils.event'
local Gui = require 'utils.gui'

local Public = {}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions maps/scrap_towny_ffa/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/flashlight_toggle_button.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion modules/global_chat_toggle.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Gui = require 'utils.gui'
local function toggle(player)
if not player.gui.top.global_chat_toggle then
return
Expand Down Expand Up @@ -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'
Expand Down
110 changes: 88 additions & 22 deletions utils/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -482,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)
Expand Down Expand Up @@ -585,9 +582,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
Expand Down Expand Up @@ -667,6 +684,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)
Expand Down Expand Up @@ -748,6 +769,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

Expand Down Expand Up @@ -884,20 +925,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)
Expand Down Expand Up @@ -933,6 +960,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)
Expand Down Expand Up @@ -971,6 +1034,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
)
Expand Down

0 comments on commit df0ade5

Please sign in to comment.