Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextField should have a setFontScale method like Label #5719

Open
maltaisn opened this issue Jul 20, 2019 · 6 comments
Open

TextField should have a setFontScale method like Label #5719

maltaisn opened this issue Jul 20, 2019 · 6 comments

Comments

@maltaisn
Copy link
Contributor

The Label class has a setFontScale(scaleX, scaleY) method which allows to set the scale of the bitmap font used for layout calculations and drawing. This is very useful when using a distance field font as the font size can be set easily.

However, the TextField class lacks such a method. Furthermore, most of the methods doing the layout are package-private (e.g.: updateDisplayText), so the font scale cannot be set with bitmapFont.getData().setScale(...) by subclassing.

Unless I'm missing something, this situation makes it impossible to have two TextFields with different font sizes without creating two BitmapFonts when using a distance field font.

If there's no workaround, I can make a pull request.

@NathanSweet
Copy link
Member

I'm not a fan of Label setFontScale, it feels really hacky. If we add it to TextField, the next request will be TextButton, SelectBox, etc. Maybe a BitmapFont could be wrapped with something that does the scaling transparently, without littering in the widget API?

@maltaisn
Copy link
Contributor Author

Thank you for the response.

I thought about using a single texture with multiple BitmapFonts, but each instance had to parse the whole .fnt file, it doesn't feel like a good solution when working with 10+ font sizes. The BitmapFontData can't be shared since all fonts have a different scale. Each wrapper also creates a new font cache which is more or less lightweight.

The BitmapFontData can't be shared because the scale has to be set not only when drawing, but also on measurements, like lineHeight, ascent, descent, etc. Some of those values are available through the BitmapFont (eg: getLineHeight()) and can be scaled properly, since the BitmapFont wrapper has a scale property. But these values are also available on the BitmapFontData which has no scale property since it's used by all wrappers.

I don't know how you would make a wrapper, but I feel like the font classes would need a major refactoring for it to work efficiently. Unless again, I'm missing something.

@NathanSweet
Copy link
Member

I have not implemented a good solution for my apps, sorry. I just load multiple fonts, but I generally only need 4-5 max.

Maybe we could support creating a new, scaled BitmapFontData from an existing one? You'd load one, then copy and scale it. It still takes up the same memory for the glyphs, but at least you don't need to parse the font data file many times.

@MrStahlfelge
Copy link
Member

That would be a great addition. I usually end up setting transform and resizing the widgets this way, but it is... hacky.

@nhouser9
Copy link

nhouser9 commented Jul 29, 2022

+1 for this, it's very unfortunate that i can set the font scale on my entire UI including TextButton and Label, but not on TextField.

Edit: i was able to work around this by creating 5 pointers to the same font in my skin and scaling the 5 fonts differently. Maybe this workaround is acceptable, but i do feel that the inconsistency of the current API is not ideal. IMO either Label should not have a method for setting the font scale, or all text actors should have it. The alternative is quite confusing, and led me to spend a good amount of time going down a path that just doesn't work.

@auraxangelic
Copy link
Contributor

+1 for this. I've been doubling the font programmatically for all my UI elements, but can't call setFontScale for text fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants