I'm trying to create a set of selectable labels, New Game, Load Game, Options, and Exit. I created a MenuItem class which extends Label, and set up the constructor to use the standard stuff expected from a Label. Here is the class.

package com.irridium.echo.menu;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.ui.Label;

/**
 * @author GeoDoX
 *
 * MenuItem.java
 */
public class MenuItem extends Label
{
    private boolean selected = false;
    private LabelStyle menuOption;

    public MenuItem(CharSequence text, LabelStyle menuOption)
    {
        super(text, menuOption);

        this.menuOption = menuOption;

        menuOption.fontColor = Color.WHITE;
    }

    public void select()
    {
        if(selected == true)
        {
            selected = false;
            menuOption.fontColor = Color.WHITE;
        }
        else
        {
            selected = true;
            menuOption.fontColor = Color.CYAN;
        }
    }

}

Here is where I am creating and instatiating them.

        @Override
        public void resize(int width, int height)
        {
            menuOption = new LabelStyle(white, Color.WHITE);
            newGame = new MenuItem("New Game", menuOption);
            newGame.setX(0);
            newGame.setY(Gdx.graphics.getHeight() / 2 + 50);
            newGame.setWidth(width);
            newGame.setAlignment(Align.center);

            loadGame = new MenuItem("Load Game", menuOption);
            loadGame.setX(0);
            loadGame.setY(Gdx.graphics.getHeight() / 2 + 25);
            loadGame.setWidth(width);
            loadGame.setAlignment(Align.center);

            options = new MenuItem("Options", menuOption);
            options.setX(0);
            options.setY(Gdx.graphics.getHeight() / 2 + 0);
            options.setWidth(width);
            options.setAlignment(Align.center);

            exit = new MenuItem("Exit", menuOption);
            exit.setX(0);
            exit.setY(Gdx.graphics.getHeight() / 2 - 25);
            exit.setWidth(width);
            exit.setAlignment(Align.center);

            stage.addActor(newGame);
            stage.addActor(loadGame);
            stage.addActor(options);
            stage.addActor(exit);
        }

Here is where I am getting the input and changing them.

if(selectedIndex == 0)
        {
            newGame.select();
            selectedIndex = 1;
        }

        if(Gdx.input.isKeyPressed(Input.Keys.DOWN))
        {
            switch(selectedIndex)
            {
                case 1:
                    newGame.select();
                    selectedIndex++;
                    loadGame.select();
                    break;
                case 2:
                    loadGame.select();
                    selectedIndex++;
                    options.select();
                    break;
                case 3:
                    options.select();
                    selectedIndex++;
                    exit.select();
                    break;
                case 4:
                    exit.select();
                    selectedIndex = 1;
                    newGame.select();
            }
        }

        if(Gdx.input.isKeyPressed(Input.Keys.UP))
        {
            switch(selectedIndex)
            {
                case 4:
                    exit.select();
                    selectedIndex--;
                    options.select();
                    break;
                case 3:
                    options.select();
                    selectedIndex--;
                    loadGame.select();
                    break;
                case 2:
                    loadGame.select();
                    selectedIndex--;
                    newGame.select();
                    break;
                case 1:
                    newGame.select();
                    selectedIndex = 4;
                    exit.select();
                    break;
            }
        }

And of course, declaring them.

    MenuItem newGame;
    MenuItem loadGame;
    MenuItem options;
    MenuItem exit;
    int selectedIndex = 0;

The problem is that all the labels are always selected. Input does nothing, and resizing doesn't either.
If you need the full class, just ask. If you dont have any expeirence with LibGDX then it would still be appreciated if you checked over the code to see if there is any logical errors.

Much appreiciated.

I don't think I could use a TextButton because I want the labels to change with the up and down keys and with a hover effect with the mouse.

I'm just a beginner with libGDX so I don't know most of the stuff.

How about ImageTextButton? https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ImageTextButton.java
You can actually do the theming that you so dersire since it actually contains these methods:

public ImageTextButtonStyle (ImageTextButtonStyle style) {
            super(style);
            if (style.imageUp != null) this.imageUp = style.imageUp;
            if (style.imageDown != null) this.imageDown = style.imageDown;
            if (style.imageOver != null) this.imageOver = style.imageOver;
            if (style.imageChecked != null) this.imageChecked = style.imageChecked;
            if (style.imageCheckedOver != null) this.imageCheckedOver = style.imageCheckedOver;
            if (style.imageDisabled != null) this.imageDisabled = style.imageDisabled;
        }

        public ImageTextButtonStyle (TextButtonStyle style) {
            super(style);
        }

Is there anyway to do an isOver or checked with the up and down arrow keys?

Is there any tutorials on how to use ImageTextButton? I'm having alot of trouble with ImageTextButtonStyle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.