mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-30 04:30:58 +02:00
allow player to toggle top buttons visibility
This commit is contained in:
parent
edf50d24f7
commit
f45d6d5111
@ -572,4 +572,6 @@ Gui.on_custom_close(
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
||||||
|
|
||||||
Event.add(defines.events.on_player_joined_game, player_joined)
|
Event.add(defines.events.on_player_joined_game, player_joined)
|
||||||
|
@ -675,6 +675,8 @@ Gui.on_custom_close(
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
||||||
|
|
||||||
local Public = {}
|
local Public = {}
|
||||||
|
|
||||||
function Public.show_info(player)
|
function Public.show_info(player)
|
||||||
|
@ -216,5 +216,7 @@ Gui.on_custom_close(
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
||||||
|
|
||||||
Event.add(defines.events.on_player_joined_game, player_joined)
|
Event.add(defines.events.on_player_joined_game, player_joined)
|
||||||
Event.add(defines.events.on_player_built_tile, player_build_tile)
|
Event.add(defines.events.on_player_built_tile, player_build_tile)
|
||||||
|
@ -758,3 +758,5 @@ Gui.on_click(
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
||||||
|
@ -1159,6 +1159,8 @@ Gui.on_click(
|
|||||||
|
|
||||||
Gui.on_click(poll_view_vote_name, vote)
|
Gui.on_click(poll_view_vote_name, vote)
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
||||||
|
|
||||||
local Class = {}
|
local Class = {}
|
||||||
|
|
||||||
function Class.validate(data)
|
function Class.validate(data)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
local Event = require 'utils.event'
|
local Event = require 'utils.event'
|
||||||
local Game = require 'utils.game'
|
local Game = require 'utils.game'
|
||||||
local PlayerStats = require 'features.player_stats'
|
local PlayerStats = require 'features.player_stats'
|
||||||
|
local Gui = require 'utils.gui'
|
||||||
|
|
||||||
if not global.score_rockets_launched then
|
if not global.score_rockets_launched then
|
||||||
global.score_rockets_launched = 0
|
global.score_rockets_launched = 0
|
||||||
@ -131,6 +132,8 @@ local function rocket_launched()
|
|||||||
refresh_score()
|
refresh_score()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility('score')
|
||||||
|
|
||||||
Event.add(defines.events.on_entity_died, refresh_score)
|
Event.add(defines.events.on_entity_died, refresh_score)
|
||||||
Event.add(defines.events.on_gui_click, on_gui_click)
|
Event.add(defines.events.on_gui_click, on_gui_click)
|
||||||
Event.add(defines.events.on_player_joined_game, create_score_gui)
|
Event.add(defines.events.on_player_joined_game, create_score_gui)
|
||||||
|
@ -656,6 +656,8 @@ Gui.on_custom_close(
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
||||||
|
|
||||||
Event.add(defines.events.on_player_joined_game, player_joined)
|
Event.add(defines.events.on_player_joined_game, player_joined)
|
||||||
|
|
||||||
local function tag_command(cmd)
|
local function tag_command(cmd)
|
||||||
|
@ -1065,6 +1065,8 @@ Gui.on_click(
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
||||||
|
|
||||||
commands.add_command(
|
commands.add_command(
|
||||||
'task',
|
'task',
|
||||||
'<task> - Creates a new task (Admins and regulars only).',
|
'<task> - Creates a new task (Admins and regulars only).',
|
||||||
|
@ -76,6 +76,8 @@ local function on_player_created(event)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility('Diggy.CoinGathering.Button')
|
||||||
|
|
||||||
Gui.on_click('Diggy.CoinGathering.Button', toggle)
|
Gui.on_click('Diggy.CoinGathering.Button', toggle)
|
||||||
Gui.on_custom_close('Diggy.CoinGathering.Frame', function (event)
|
Gui.on_custom_close('Diggy.CoinGathering.Frame', function (event)
|
||||||
event.element.destroy()
|
event.element.destroy()
|
||||||
|
@ -488,6 +488,8 @@ local function on_player_created(event)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Gui.allow_player_to_toggle_top_element_visibility('Diggy.Experience.Button')
|
||||||
|
|
||||||
Gui.on_click('Diggy.Experience.Button', toggle)
|
Gui.on_click('Diggy.Experience.Button', toggle)
|
||||||
Gui.on_custom_close('Diggy.Experience.Frame', function (event)
|
Gui.on_custom_close('Diggy.Experience.Frame', function (event)
|
||||||
event.element.destroy()
|
event.element.destroy()
|
||||||
|
139
utils/gui.lua
139
utils/gui.lua
@ -1,10 +1,30 @@
|
|||||||
local Token = require 'utils.token'
|
local Token = require 'utils.token'
|
||||||
local Event = require 'utils.event'
|
local Event = require 'utils.event'
|
||||||
local Game = require 'utils.game'
|
local Game = require 'utils.game'
|
||||||
|
local Global = require 'utils.global'
|
||||||
|
local table = require 'utils.table'
|
||||||
|
|
||||||
local Gui = {}
|
local Gui = {}
|
||||||
|
|
||||||
global.Gui_data = {}
|
local data = {}
|
||||||
|
local top_elements = {}
|
||||||
|
|
||||||
|
Global.register(
|
||||||
|
{
|
||||||
|
data = data,
|
||||||
|
top_elements = top_elements
|
||||||
|
},
|
||||||
|
function(tbl)
|
||||||
|
data = tbl.data
|
||||||
|
top_elements = tbl.top_elements
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
|
local on_visable_event_id = script.generate_event_name()
|
||||||
|
local on_pre_hidden_event_id = script.generate_event_name()
|
||||||
|
|
||||||
|
local on_visable_handlers = {}
|
||||||
|
local on_pre_hidden_handlers = {}
|
||||||
|
|
||||||
function Gui.uid_name()
|
function Gui.uid_name()
|
||||||
if _DEBUG then
|
if _DEBUG then
|
||||||
@ -17,13 +37,13 @@ function Gui.uid_name()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Associates data with the LuaGuiElement. If data is nil then removes the data
|
-- Associates data with the LuaGuiElement. If data is nil then removes the data
|
||||||
function Gui.set_data(element, data)
|
function Gui.set_data(element, value)
|
||||||
global.Gui_data[element.player_index * 0x100000000 + element.index] = data
|
data[element.player_index * 0x100000000 + element.index] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Gets the Associated data with this LuaGuiElement if any.
|
-- Gets the Associated data with this LuaGuiElement if any.
|
||||||
function Gui.get_data(element)
|
function Gui.get_data(element)
|
||||||
return global.Gui_data[element.player_index * 0x100000000 + element.index]
|
return data[element.player_index * 0x100000000 + element.index]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Removes data associated with LuaGuiElement and its children recursivly.
|
-- Removes data associated with LuaGuiElement and its children recursivly.
|
||||||
@ -101,6 +121,21 @@ local function handler_factory(event_id)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function custom_handler_factory(handlers)
|
||||||
|
return function(element_name, handler)
|
||||||
|
handlers[element_name] = handler
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function custom_raise(handlers, element, player)
|
||||||
|
local handler = handlers[element.name]
|
||||||
|
if not handler then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
handler({element = element, player = player})
|
||||||
|
end
|
||||||
|
|
||||||
-- Register a handler for the on_gui_checked_state_changed event for LuaGuiElements with element_name.
|
-- Register a handler for the on_gui_checked_state_changed event for LuaGuiElements with element_name.
|
||||||
-- Can only have one handler per element name.
|
-- Can only have one handler per element name.
|
||||||
-- Guarantees that the element and the player are valid when calling the handler.
|
-- Guarantees that the element and the player are valid when calling the handler.
|
||||||
@ -143,4 +178,100 @@ Gui.on_text_changed = handler_factory(defines.events.on_gui_text_changed)
|
|||||||
-- Adds a player field to the event table.
|
-- Adds a player field to the event table.
|
||||||
Gui.on_value_changed = handler_factory(defines.events.on_gui_value_changed)
|
Gui.on_value_changed = handler_factory(defines.events.on_gui_value_changed)
|
||||||
|
|
||||||
|
-- Register a handler for the when the player shows the top LuaGuiElements with element_name.
|
||||||
|
-- Assuming the element_name has been added with Gui.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.
|
||||||
|
Gui.on_player_show_top = custom_handler_factory(on_visable_handlers)
|
||||||
|
|
||||||
|
-- Register a handler for the when the player hides the top LuaGuiElements with element_name.
|
||||||
|
-- Assuming the element_name has been added with Gui.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.
|
||||||
|
Gui.on_pre_player_hide_top = custom_handler_factory(on_pre_hidden_handlers)
|
||||||
|
|
||||||
|
--- Allows the player to show / hide this element.
|
||||||
|
-- The element must be part in gui.top.
|
||||||
|
-- @param element_name<string> This name must be globally unique.
|
||||||
|
function Gui.allow_player_to_toggle_top_element_visibility(element_name)
|
||||||
|
top_elements[#top_elements + 1] = element_name
|
||||||
|
end
|
||||||
|
|
||||||
|
local toggle_button_name = Gui.uid_name()
|
||||||
|
|
||||||
|
Event.add(
|
||||||
|
defines.events.on_player_created,
|
||||||
|
function(event)
|
||||||
|
local player = Game.get_player_by_index(event.player_index)
|
||||||
|
|
||||||
|
if not player or not player.valid then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local b =
|
||||||
|
player.gui.top.add {
|
||||||
|
type = 'button',
|
||||||
|
name = toggle_button_name,
|
||||||
|
caption = '<',
|
||||||
|
tooltip = 'Shows / hides the Redmew Gui buttons.'
|
||||||
|
}
|
||||||
|
local style = b.style
|
||||||
|
style.width = 18
|
||||||
|
style.height = 38
|
||||||
|
style.left_padding = 0
|
||||||
|
style.top_padding = 0
|
||||||
|
style.right_padding = 0
|
||||||
|
style.bottom_padding = 0
|
||||||
|
style.font = 'default-small-bold'
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
|
Gui.on_click(
|
||||||
|
toggle_button_name,
|
||||||
|
function(event)
|
||||||
|
local button = event.element
|
||||||
|
local player = event.player
|
||||||
|
local top = player.gui.top
|
||||||
|
|
||||||
|
if button.caption == '<' then
|
||||||
|
for i = 1, #top_elements do
|
||||||
|
local name = top_elements[i]
|
||||||
|
local ele = top[name]
|
||||||
|
if ele and ele.valid then
|
||||||
|
local style = ele.style
|
||||||
|
|
||||||
|
-- if visible is not set it has the value of nil.
|
||||||
|
-- Hence nil is treated as is visible.
|
||||||
|
local v = style.visible
|
||||||
|
if v or v == nil then
|
||||||
|
custom_raise(on_pre_hidden_handlers, ele, player)
|
||||||
|
style.visible = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
button.caption = '>'
|
||||||
|
button.style.height = 24
|
||||||
|
else
|
||||||
|
for i = 1, #top_elements do
|
||||||
|
local name = top_elements[i]
|
||||||
|
local ele = top[name]
|
||||||
|
if ele and ele.valid then
|
||||||
|
local style = ele.style
|
||||||
|
|
||||||
|
if not style.visible then
|
||||||
|
style.visible = true
|
||||||
|
custom_raise(on_visable_handlers, ele, player)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
button.caption = '<'
|
||||||
|
button.style.height = 38
|
||||||
|
end
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
return Gui
|
return Gui
|
||||||
|
Loading…
x
Reference in New Issue
Block a user