BitmapFontCache color changes

Discussions for core devs and contributors. Read only for normal forum members to keep the noise low

BitmapFontCache color changes

Postby NateS » Fri Dec 27, 2013 2:35 pm

BitmapFontCache has setColor methods that change all cached glyphs. It also has a setColor that changes a range. If your use case is adding text with various colors to the cache, setColor using a range is very clunky.

1) I think it would be better if the API were similar to SpriteBatch/SpriteCache, where any text added after setColor gets that color, but setColor doesn't change text added before it. Probably we would still want methods to change the color of all glyphs, but what would be their names? This would be a breaking change.

2) We could try not to break the API by adding a method "setMulticolor". Default is false and setColor works as it always has (sets color for all glyphs). When true, setColor only changes the color for subsequent glyphs. Downside is it doesn't match SpriteBatch/SpriteCache.

Related to this is support for embedding color information in the text given to BitmapFontCache. It was discussed on GitHub and the consensus seems to be we want plain text markup. If we had this, setColor would be less of an issue, though I still think it is a bit clunky to add markup to the beginning of each piece of my text just because setColor doesn't work like I'd expect (ie like SpriteBatch/SpriteCache).

I vote for #1. What do you guys think? Any alternate ideas?
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: BitmapFontCache color changes

Postby jrenner » Fri Dec 27, 2013 2:46 pm

I vote #1, mainly because I want consistency with spritebatch.
Superior Tactics - tactical spaceship battles RTS
Google Play: https://play.google.com/store/apps/deta ... r.superior
jrenner
 
Posts: 520
Joined: Sun Apr 14, 2013 5:09 am

Re: BitmapFontCache color changes

Postby mzechner » Fri Dec 27, 2013 8:33 pm

i vote for #1 as well, but i have no idea what a good method name for setting all glyphs' colors would be.
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: BitmapFontCache color changes

Postby BurningHand » Fri Dec 27, 2013 8:38 pm

I vote for #1. Perhaps, the set all the colors method could be resetColors or something like that.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: BitmapFontCache color changes

Postby xoppa » Fri Dec 27, 2013 8:51 pm

I agree, #1.
xoppa
 
Posts: 689
Joined: Thu Aug 23, 2012 11:27 pm

Re: BitmapFontCache color changes

Postby noblemaster » Mon Dec 30, 2013 7:12 am

I messaged Mario via twitter because I wasn't on my computer. I guess he didn't like that too much :D

Anyhow, I got another suggestion: I noted BitmapFont(Cache) uses CharSequence (not String) which adds some interesting possibilities. I would think most people probably just pass in a String (which implements CharSequence). So, if a String is passed in, the renderer simply renders the String as is using the default/given color that is set.

Now, one could create a class "DecoratedString" for example which would implement CharSequence such as e.g.:
Code: Select all
public class DecoratedString implements CharSequence {
  public char charAt(int index) {
    return ...
  }
  public int colorAt(int index) {
    return ...
  } 
  ... implement other methods from CharSequence ...
}

Basically, you implement all the methods from CharSequence (which shouldn't be difficult). It could be backed by a String. Then you also implement colorAt(...) which gives the color information for each char. Additionally there could be a "underlineAt(int index):boolean" which would tell the renderer if a line should be drawn underneath (optional).

I haven't looked in detail at the renderer, but something like that could work:
Code: Select all
...
if (text instanceof DecoratedString) {
  ... render ditto now but use the color info provided by colorAt(int index) for each char
}
else {
  ... render ditto now
}
...


Feel free to tell me off if you think that's stupid. I think however this would (a) not require a refactoring and be backwards compatible and (b) allow for more than simple colored text (e.g. underline/strikethrough/bold text etc).
noblemaster
 
Posts: 121
Joined: Thu Dec 09, 2010 6:49 pm
Location: Tokyo, Japan

Re: BitmapFontCache color changes

Postby BurningHand » Mon Dec 30, 2013 1:39 pm

That's a bit like how Android handles text styling. They have a Spanned interface which decorates CharSequence with spans of style (and other metadata) information.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: BitmapFontCache color changes

Postby davedes » Mon Dec 30, 2013 4:32 pm

Decorated string is something I was playing with in a fork at one point. Typical things a decorated string might contain:

- Font family & style (bold/italics) -- chooses a different BitmapFont based on some map of names/styles to fonts
- Underline -- this would render a line, e.g. like this
- Size, scale, line-spacing, etc -- this really complicates the multi-line stuff

Adding anything other than colors really increases complexity and basically forces you to start thinking about a higher-level API. If we were to introduce this into LibGDX,maybe it would be best to KISS and have a separate renderer for "decorated strings" that doesn't try to optimize vertices as much as BitmapFontCache.
davedes
 
Posts: 434
Joined: Thu Oct 11, 2012 7:51 pm

Re: BitmapFontCache color changes

Postby NateS » Mon Dec 30, 2013 5:46 pm

A DecoratedString would separate how the colors, etc are described from BitmapFontCache. If you have string a string with markup, you'd go: mark up -> markup parser -> DecoratedString -> BitmapFontCache. This would probably be the most common usage, since using a Java API to configure a DecoratedString wouldn't be as nice to use.

As davedes mentioned, anything more complex than color and possibly underline probably needs a higher level API, something that does text layout using human readable mark up, multiple BitmapFonts, etc. I guess for now we can do #1 so BitmapFontCache has a sane API and we punt the more complex problems of markup and advanced layout until someone feels like tackling that. It's probably completely separate anyway.
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: BitmapFontCache color changes

Postby NateS » Tue Dec 31, 2013 12:03 am

Would "setColors" (plural) be a terrible name for the method that changes the color of all the glyphs? setGlyphColors? Halp!
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Next

Return to Libgdx Development

Who is online

Users browsing this forum: No registered users and 1 guest