Show images in Libgdx / Scene2D SelectBox
I have a SelectBox that gets a list of objects that basically encapsulate images and a few more details. Instead of displaying text, I would like to display this TextureRegion.
How can i do this? As far as I can see, SelectBox displays toString () by default.
So my class looks like this (a little dumbfounded):
public class Image
{
private Vector2 position;
private TextureRegion sprite;
public Image(Vector2 position) {...}
public void render(SpriteBatch batch) { batch.draw{this.sprite, this.position.x ...}
public String toString() { return "Position: " + position; }
}
SelectBox<Image> selectBox = ... // fetch data
Do I need to create my own SelectBoxStyle? If so, how can I rewrite it to paint TextureRegion instead of Text?
source to share
SelectBox
uses List
to display its values. List
converts its children with this method:
protected String toString (T obj) {
return obj.toString();
}
It is not your regular actor that contains and manages its children: it basically has a list of data containers, converting them to a string (allowing you to override the conversion method by extending the class). The renderer does not expect the data containers to be subjects: it simply uses a font to render the text they were rendered into. You will basically have to implement a custom selection actor, perhaps using Table
(or another container) in ScrollPane
c TextButton
/ ImageTextButton
, keeping the current value and opening the menu.
However, you can use the extension VisUI
. Its menu widget has a similar dropdown behavior and allows you to add icons for each menu entry. In addition, VisUI 1.0.0
a powerful widget has been added ListView
that allows you to create personalized members to display a collection of values.
source to share