Merge pull request #3 from bbassie/feature/gui-v5

Feature/gui v5
This commit is contained in:
Cooldude2606
2020-02-25 23:54:39 +00:00
committed by GitHub
3 changed files with 54 additions and 9 deletions

View File

@@ -52,7 +52,7 @@ end)
]]
function Gui.left_toolbar_button(sprite,tooltip,element_define,authenticator)
return Gui.element{
local button = Gui.element{
type = 'sprite-button',
sprite = sprite,
tooltip = tooltip,
@@ -62,9 +62,26 @@ function Gui.left_toolbar_button(sprite,tooltip,element_define,authenticator)
padding = -2
}
:add_to_top_flow(authenticator)
:on_click(function(player,_,_)
Gui.toggle_left_element(player, element_define)
-- Add on_click handler to handle click events comming from the player
button:on_click(function(player,_,_)
local top_flow = Gui.get_top_flow(player)
local element = top_flow[button.name]
local visibility_state = Gui.toggle_left_element(player, element_define)
-- Raise custom event that tells listening elements if the element has changed visibility by a player clicking
-- Used in warp gui to handle the keep open logic
button:raise_custom_event{
name = Gui.events.on_visibility_changed_by_click,
element = element,
state = visibility_state
}
end)
-- Add property to the left flow element with the name of the button
-- This is for the ability to reverse lookup the button from the left flow element
element_define.toolbar_button = button.name
return button
end
--[[-- Draw all the left elements onto the left flow, internal use only
@@ -137,6 +154,26 @@ function Gui.hide_left_flow(player)
hide_button.visible = false
for name,_ in pairs(Gui.left_elements) do
left_flow[name].visible = false
-- Get the assosiated element define
local element_define = Gui.defines[name]
local top_flow = Gui.get_top_flow(player)
-- Check if the the element has a button attached
if element_define.toolbar_button then
-- Check if the topflow contains the button
local button = top_flow[element_define.toolbar_button]
if button then
-- Get the button define from the reverse lookup on the element
local button_define = Gui.defines[element_define.toolbar_button]
-- Raise the custom event if all of the top checks have passed
button_define:raise_custom_event{
name = Gui.events.on_visibility_changed_by_click,
element = button,
state = false
}
end
end
end
end

View File

@@ -246,6 +246,7 @@ end
--[[-- Raise the handler which is attached to any event; external use should be limited to custom events
@tparam table event the event table bassed to the handler, must include fields: name, element
@treturn table the element define so more events can be raised
@usage Raising a custom event
element_define:raise_custom_event{
@@ -258,20 +259,20 @@ function Gui._prototype_element:raise_custom_event(event)
-- Check the element is valid
local element = event.element
if not element or not element.valid then
return
return self
end
-- Get the event handler for this element
local handler = self[event.name]
if not handler then
return
return self
end
-- Get the player for this event
local player_index = event.player_index or element.player_index
local player = game.players[player_index]
if not player or not player.valid then
return
return self
end
event.player = player
@@ -279,6 +280,7 @@ function Gui._prototype_element:raise_custom_event(event)
if not success then
error('There as been an error with an event handler for a gui element:\n\t'..err)
end
return self
end
-- This function is used to register a link between element define events and the events in the factorio api
@@ -348,5 +350,11 @@ Gui._prototype_element.on_text_changed = event_handler_factory(defines.events.on
-- @tparam function handler the event handler which will be called
Gui._prototype_element.on_value_changed = event_handler_factory(defines.events.on_gui_value_changed)
--- Custom element events.
-- @section customEvents
-- Triggered when a user changed the visibility of a left flow element by clicking a button
Gui.events.on_visibility_changed_by_click = 'on_visibility_changed_by_click'
-- Module return
return Gui

View File

@@ -439,9 +439,9 @@ end)
Gui.left_toolbar_button('item/'..config.default_icon,{'warp-list.main-tooltip',config.standard_proximity_radius},warp_list_container, function(player)
return Roles.player_allowed(player,'gui/warp-list')
end)
:on_click(function(player,_,_)
local visible_state = Gui.toggle_left_element(player, warp_list_container)
keep_gui_open[player.name] = visible_state
:on_custom_event(Gui.events.on_visibility_changed_by_click, function(player,_,event)
-- Set gui keep open state for player that clicked the button: true if visible, false if invisible
keep_gui_open[player.name] = event.state
end)
--- When the name of a warp is updated this is triggered