diff --git a/expcore/gui/left_flow.lua b/expcore/gui/left_flow.lua index e21a10c1..64243932 100644 --- a/expcore/gui/left_flow.lua +++ b/expcore/gui/left_flow.lua @@ -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,34 @@ 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 not element_define.toolbar_button then + goto hide_left_flow_end + end + + -- Check if the topflow contains the button + local button = top_flow[element_define.toolbar_button] + if not button then + goto hide_left_flow_end + end + + -- 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 + } + + -- Label for the end of the loop + ::hide_left_flow_end:: end end diff --git a/expcore/gui/prototype.lua b/expcore/gui/prototype.lua index 21f26033..aedcf736 100644 --- a/expcore/gui/prototype.lua +++ b/expcore/gui/prototype.lua @@ -350,5 +350,12 @@ 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) +--- Element Events. +-- @section customEvents +Gui.events = { + -- Triggered when a user changed the visibility of a left flow element by clicking a button + on_visibility_changed_by_click = 'on_visibility_changed_by_click' +} + -- Module return return Gui \ No newline at end of file diff --git a/modules/gui/warp-list.lua b/modules/gui/warp-list.lua index 66626780..7f63395c 100644 --- a/modules/gui/warp-list.lua +++ b/modules/gui/warp-list.lua @@ -439,9 +439,10 @@ 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) + local state = event.state -- true if visible, false if invisible + -- Set gui keep open state for player that clicked the button + keep_gui_open[player.name] = state end) --- When the name of a warp is updated this is triggered