bevy
bevy copied to clipboard
TextBundle missing `with_visibility` function
What problem does this solve or what need does it fill?
Many other fields on TextBundle can be built using a with_x
type pattern, e.g with_style, with_background_color, etc. However the "visibility" field cannot be built this way.
What solution would you like?
A simple "with_visibility" function on TextBundle.
What alternative(s) have you considered?
The workaround is to bind the TextBundle to a variable and then set the visibility field, which is what I'm doing now, but it would be convenient if that wasn't required for this field.
Additional context
Sorry if this issue is in the wrong place, it's an issue with the bevy_ui sub-project, but I wasn't sure how issue tracking is laid out. Just let me know if you want me to move it to a different repo on github.
Also, I'd be happy to make a PR for this myself - just wanted to confirm I'm not missing something obvious before I put that work in.
The workaround is to bind the TextBundle to a variable and then set the visibility field, which is what I'm doing now, but it would be convenient if that wasn't required for this field.
You could write a with_visibility
extension trait to add this? You can also set Display::None
on the Style
component to hide UI elements.
Intuitively I'm not sure I like this as a feature, but I'm not sure why exactly.
I'm not sure I explained the problem sufficiently - the with_visibility is part of the builder pattern for the TextBundle, so you'd do something like this:
TextBundle::from_section("Game Over", TextStyle {
font: asset_server.load(GAME_OVER_FONT),
font_size: 72.0,
..default()
})
.with_visibility(Visibility::Hidden);
And that would create a text node in your UI that is hidden by default, and then a system can come along and show it when it needs to be shown.
The same way you can set up a default background color and all the other properties on the TextBundle struct. The only property you can't control in this way is the visibility, and I'm not sure why that's been missed other than as an oversight?
You're right I could add an extension trait that adds this functionality, but I kind of don't see the point - it's not that often that you need to do it, and it's easy enough to just assign the TextBundle to a mutable variable and then mutate it before passing it to spawn instead (which is how I have worked around it).
If you don't think it should be there in the base struct implementation that's fair enough, but some comment or other doc explaining the reasoning might help prevent future programmers from having the same "huh?" moment I had when I saw there was every other field represented as a with_X "builder" function, except the one I wanted at the time :joy:
edit: looking back at the code again it seems there are a few others that aren't available as with_ functions, but they generally have an obvious reason behind why not - e.g calculated_size doesn't make sense to be able to set a "default" for in this way.