Widget Update/Override Request

With the issues WP8 has with displaying newText objects, I realized this is affecting widgets with text such as buttons and their labels. For example I have a overlay scene that has 12 button widgets, all with a single character label, and it takes upwards of 3 or 4 seconds for the overlay to come on screen!

I tried looking at possibly adding a new button type to the widget.button that would use bitmapText objects for the labels, or even overriding the 2 image button function (The one I make use of) but I think that’s a little over my head.

Has anyone else figured out another solution?

A widget button inherits from Corona’s “group” display object.  Meaning that a widget button can be a container for other display objects such as images, sprites, shapes, etc.  This means that a widget button has a :insert() function, just like a group.

So, what you can do is create a widget button with an empty label/string and then insert your bitmap font text inside of the widget button.

Here is a quick example:

-- Create a button without text. local myButton = widget.newButton { width = 128, height = 32, label = "", } -- Insert your bitmap font text inside of the button. myBitmapFontText.x = myButton.contentWidth / 2 -- Center your text. myBitmapFontText.y = myButton.contentHeight / 2 -- Center your text. myButton:insert(myBitmapFontText)

And an even simpler solution would be to not use a widget button and instead create a new PNG of a button with the text, all pre-drawn.  This means creating a PNG for every different button/text combination that you have.  This is simple, but much more wasteful in memory.

That’s great! Thank you Joshua. I thought about the other option of making individual buttons for each one, it’s easier… but I rather learn the more efficient ways for future reference :slight_smile:

Don’t bother creating an empty label if your going to add one on top anyway using a bitmap font. Your just creating another display object for nothing with that.

Best of luck

I’ve seen the widget button code.  It creates a display.newText() object regardless if you give it a label field or not.  If you don’t give it a label field, then by default it sets the text to an empty string “”.  On WP8, we’ve optimized it for the empty string case.

Hi Joshua, by optimized do you mean that an empty display.newText doesn’t have the adverse effect that a non-empty one does?

Correct.  If you pass an empty string or a string containing unprintable characters (such as a space) into display.newText(), then it is much much faster because in these cases there is no text to render.  Now for the case where you just have a space in the string " ", a completely transparent bitmap/texture will still be generated using the correct font/text metrics, but we skip the step where we have the operating system drawing text to the bitmap which avoids the huge performance penalty.

A widget button inherits from Corona’s “group” display object.  Meaning that a widget button can be a container for other display objects such as images, sprites, shapes, etc.  This means that a widget button has a :insert() function, just like a group.

So, what you can do is create a widget button with an empty label/string and then insert your bitmap font text inside of the widget button.

Here is a quick example:

-- Create a button without text. local myButton = widget.newButton { width = 128, height = 32, label = "", } -- Insert your bitmap font text inside of the button. myBitmapFontText.x = myButton.contentWidth / 2 -- Center your text. myBitmapFontText.y = myButton.contentHeight / 2 -- Center your text. myButton:insert(myBitmapFontText)

And an even simpler solution would be to not use a widget button and instead create a new PNG of a button with the text, all pre-drawn.  This means creating a PNG for every different button/text combination that you have.  This is simple, but much more wasteful in memory.

That’s great! Thank you Joshua. I thought about the other option of making individual buttons for each one, it’s easier… but I rather learn the more efficient ways for future reference :slight_smile:

Don’t bother creating an empty label if your going to add one on top anyway using a bitmap font. Your just creating another display object for nothing with that.

Best of luck

I’ve seen the widget button code.  It creates a display.newText() object regardless if you give it a label field or not.  If you don’t give it a label field, then by default it sets the text to an empty string “”.  On WP8, we’ve optimized it for the empty string case.

Hi Joshua, by optimized do you mean that an empty display.newText doesn’t have the adverse effect that a non-empty one does?

Correct.  If you pass an empty string or a string containing unprintable characters (such as a space) into display.newText(), then it is much much faster because in these cases there is no text to render.  Now for the case where you just have a space in the string " ", a completely transparent bitmap/texture will still be generated using the correct font/text metrics, but we skip the step where we have the operating system drawing text to the bitmap which avoids the huge performance penalty.