0

I want to give TextButton a basic backgroundColor to distinguish it from a regular label.

Right now I am just drawing a filled rectangle behind the TextButton that I create with color (r,g,b,a) via a TextureRegion (in scala):

val pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888)
pixmap.setColor(r, g, b, a)
pixmap.fillRectangle(0, 0, 1, 1)
val texture = new Texture(pixmap)
txr = new TextureRegion(texture)
pixmap.dispose

However, this means that every time I change the values of (r,g,b,a), a new texture is made, which is quite expensive. Therefore, I was wondering if there are better/easier ways to obtain a similar (visual) result.

Steven
  • 1,123
  • 5
  • 14
  • 31

1 Answers1

1

Certainly!

A. textButton.setColor(color) or textButton.setColor(r,g,b,a) is probably the simplest way to get what you want - it tints the existing textbutton texture to the specified color

B. If you do want to create a specific texture in-code, you can color that as well by wrapping the TextureRegion in a TextureRegionDrawable, e.g. trd = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap))), and then you can tint that using trd.tint(color) - that way you create the pixmap once, and can tint it differently for every TextButton

  • Thank you. For me A is not working. As B concerns; trd.tint(Color) returns a drawable, how should I set the texture region (that I'm drawing) to the new drawable ? – Steven May 21 '21 at 11:13
  • Since textbutton is a Table, it has a setBackground() function. Also, see https://stackoverflow.com/questions/39081993/libgdx-scene2d-set-background-color-of-table – Yair Morgenstern May 21 '21 at 14:59