Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slider always has default width in table

I created a slider inside of a table like shown in the following code example as I know that the minimum width of the background is used for the slider width:

public OptionScreen(MainClass game) {
    super(game);
    preference = new PreferencesHelper();
    font = this.getDefaultFont(25);
    this.table = new Table();
    if (Config.DEBUG)
        this.table.debug();

    // add volumenlabel
    LabelStyle style = new LabelStyle(font, Color.WHITE);
    volumenLabel = new Label(Config.VOLUMEN_LABLE, style);
    table.add(volumenLabel).colspan(2);
    table.row();
    // add slider
    Skin skin = new Skin();
    skin.add("sliderbackground",
            this.game.manager.get("data/sliderbackground.png"));
    skin.add("sliderknob", this.game.manager.get("data/sliderknob.png"));

    SliderStyle sliderStyle = new SliderStyle();
    sliderStyle.background = skin.getDrawable("sliderbackground");
    sliderStyle.background.setMinWidth(600f);
    sliderStyle.knob = skin.getDrawable("sliderknob");

    volumenSlider = new Slider(0f, 1f, 0.1f, false, sliderStyle);
    volumenSlider.setValue(preference.getVolumen()); // load current volumen
    volumenSlider.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            volumeValue.setText(String.format("%.01f",
                    volumenSlider.getValue()));
            // sett the preference
            preference.setVolumen(volumenSlider.getValue());
        }
    });
    // add volslider to stage
    table.add(volumenSlider);
    volumenLabel.invalidate();

    // table
    style = new LabelStyle(font, Color.WHITE);
    // set current volumen
    volumeValue = new Label(
            String.format("%.01f", volumenSlider.getValue()), style);
    volumenLabel.setAlignment(2);
    table.add(volumeValue).width(50f);
    table.row();

    initBackButton();

    // init table
    table.setPosition(Config.VIRTUAL_VIEW_WIDTH / 2,
            Config.VIRTUAL_VIEW_HEIGHT / 2 - Config.BLOCK_SIZE * 10);

    // add a nice fadeIn to the whole table :)
    table.setColor(0, 0, 0, 0f);
    table.addAction(Actions.fadeIn(2f)); // alpha fade
    table.addAction(Actions.moveTo(Config.VIRTUAL_VIEW_WIDTH / 2,
            Config.VIRTUAL_VIEW_HEIGHT / 2, 2f)); // move to center of the
                                                    // screen
    // add to stage
    this.stage.addActor(table);
}

The slide is inside a table with no width set. I already took a look if the width is set and if the calculation for the prefWidth of the slider does uses the set 600f.

Math.max(style.knob == null ? 0 : style.knob.getMinWidth(), style.background.getMinWidth())

Is the calculation for the width of the slider inside the Sliderclass. If I calculate that and log it, it loggs the desired 600f.

Everything seems right to me but the slider is rendered way to small for the 600 I set.

The background and knobtextures are 24x24.

So I hope you guys can tell me what I am doing wrong.

silder

like image 570
BennX Avatar asked Apr 09 '13 19:04

BennX


1 Answers

The folution is, that it's inside an table so the width is defined by the table width attibut for the spec. col and row.

So the fix is pretty short:

table.add(volumenSlider).width(600).height(60);

And its 600width and 60 height.


The wiki got edited to be more clear about this:

UI widgets do not set their own size and position. Instead, the parent widget sets the size and position of each child. Widgets provide a minimum, preferred, and maximum size that the parent can use as hints. Some parent widgets, such as Table, can be given constraints on how to size and position the children. To give a widget a specific size in a layout, the widget's minimum, preferred, and maximum size are left alone and size constraints are set in the parent.

Layout at Wiki

like image 190
BennX Avatar answered Oct 02 '22 13:10

BennX