libgdxtexturesspriteimagebuttonspritebatch

How to create an ImageButton with multiple Sprite/Texture layers in LibGDX?


I'm designing a game that requires generating a vast array of buttons, with different combinations of background colors and outlines. I already have a Sprite for the background and a Sprite for the outline and apply tints to each.

I've already tried to join both on a SpriteBatch but have had no luck converting it to a structure that ImageButton supports.

Thanks in advance.


Solution

  • You could make your own version of an ImageButton by extending the Actor class and implementing your own methods for drawing. The example below isn't tested but should give you and idea on how to make your own Custom button:

    private class LayeredButton extends Actor{
        private int width = 100;
        private int height = 75;
        private Texture backGround;
        private Texture foreGround;
        private Texture outline;
    
        public LayeredButton(Texture bg, Texture fg, Texture ol){
            setBounds(this.getX(),this.getY(),this.width,this.height);
            backGround = bg;
            foreGround = fg;
            outline = ol;
            addListener(new InputListener(){
                public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                    // do something on click here
                    return true;
                }
            });
        }
        @Override
        public void draw(Batch batch, float alpha){
            // draw from back to front
            batch.draw(backGround,this.getX(),this.getY());
            batch.draw(foreGround,this.getX(),this.getY());
            batch.draw(outline,this.getX(),this.getY());
        }
    
        @Override
        public void act(float delta){
            // do stuff to button
        }
    }