Page 1 of 1

Tabbed pane child element visibility/position weirdness?

Posted: Thu Mar 19, 2020 1:49 pm
by Deadlock989
Can't work out what I'm missing or doing wrong. I feel like this is something I'm not understanding rather than a bug.

I'm trying to replicate a crafting grid menu search function. (I know that I could be using something like an elem-chooser to get the base game one, so you don't need to tell me - just assume for now that I don't want to use that for valid reasons). However the following could have any application, it doesn't have to be about replicating any existing gui, it could be tabbed panes of something completely original.

Tabbed panes are structured as an array of "tabs", each of which is a table of tables representing the tab's properties and the content elements. So it's surprisingly easy to filter out particular child elements on the on_gui_text_changed event. If the child elements are themselves children of a table, you just make each of them visible or non-visible on some match against the filter text, and the tables automatically realign themselves:

display1.png
display1.png (114.73 KiB) Viewed 627 times
display2.png
display2.png (57.03 KiB) Viewed 627 times

However, if you do this to all the tabs at once (including the unselected tabs), and then switch to a different tab, the child elements in the other tabs have been hidden but their position has not "re-flowed":

display3.png
display3.png (69.33 KiB) Viewed 627 times

Changing the text field again while the tab is selected causes them to reflow correctly:

display4.png
display4.png (72.55 KiB) Viewed 627 times

To work around this, you can run the filtering every time a tab pane is clicked. However, there are two big drawbacks to that:

1. You occasionally see a flicker of the unfiltered tab content for an instant, presumably because the game renders the unfiltered tab content first and then processes the custom gui click event in the next tick.

2. It would be much preferred to filter all the tabs at once, because the next step is to disable any tab that now has zero visible elements in it, so I have to run through them all on a textfield change anyway.

Anyone run into this kind of thing?

Re: Tabbed pane child element visibility/position weirdness?

Posted: Thu Mar 19, 2020 5:43 pm
by Deadlock989
So, I found a workaround. The issue seems to be that table flows, and some other things to do with layout and child visibility e.g. hoverability, are not recalculated for anything but the selected tab if you change the properties of child elements in multiple tabs across a tabbed pane all at once. So the trick is ... to select the tab, as you step through the tabs doing your business. Because it all happens within a tick, the user doesn't see anything unusual.

A side effect of this is that if you are doing this in response to a textfield change event, changing the selected tab pulls focus away from the textfield even if you put it back as it was afterwards, so you need to reset the focus to the textfield again when you're done.

The outline is therefore something like:

Code: Select all

	local textfield = player.gui[whatever][whatever][your_textfield_element]
	local tabs = player.gui[whatever][whatever][your_tabbed_pane_element]
	local selected = tabs.selected_tab_index
	for index,tab in pairs(tabs.tabs) do
		tabs.selected_tab_index = index
		local count = 0
		for _,child in pairs(get_the_child_elements_required(tab)) do
			if meets_the_criteria(child) then
				count = count + 1
				child.visible_or_whatever = true
			else
				child.visible_or_whatever = false
			end
		end
		tab.tab.enabled = (count > 0)
	end
	tabs.selected_tab_index = selected
	textfield.focus()
This appears to work well. Shenanigans, but not too painful.

Re: Tabbed pane child element visibility/position weirdness?

Posted: Thu Mar 19, 2020 7:47 pm
by Rseding91
Deadlock989 wrote: Thu Mar 19, 2020 5:43 pm This appears to work well. Shenanigans, but not too painful.
That's the entirety of GUI work.