Search in sources :

Example 11 with Actor

use of com.badlogic.gdx.scenes.scene2d.Actor in project libgdx by libgdx.

the class Table method layout.

/** Positions and sizes children of the table using the cell associated with each child. The values given are the position
	 * within the parent and size of the table. */
private void layout(float layoutX, float layoutY, float layoutWidth, float layoutHeight) {
    Array<Cell> cells = this.cells;
    int cellCount = cells.size;
    if (sizeInvalid)
        computeSize();
    float padLeft = this.padLeft.get(this);
    float hpadding = padLeft + padRight.get(this);
    float padTop = this.padTop.get(this);
    float vpadding = padTop + padBottom.get(this);
    int columns = this.columns, rows = this.rows;
    float[] expandWidth = this.expandWidth, expandHeight = this.expandHeight;
    float[] columnWidth = this.columnWidth, rowHeight = this.rowHeight;
    float totalExpandWidth = 0, totalExpandHeight = 0;
    for (int i = 0; i < columns; i++) totalExpandWidth += expandWidth[i];
    for (int i = 0; i < rows; i++) totalExpandHeight += expandHeight[i];
    // Size columns and rows between min and pref size using (preferred - min) size to weight distribution of extra space.
    float[] columnWeightedWidth;
    float totalGrowWidth = tablePrefWidth - tableMinWidth;
    if (totalGrowWidth == 0)
        columnWeightedWidth = columnMinWidth;
    else {
        float extraWidth = Math.min(totalGrowWidth, Math.max(0, layoutWidth - tableMinWidth));
        columnWeightedWidth = Table.columnWeightedWidth = ensureSize(Table.columnWeightedWidth, columns);
        float[] columnMinWidth = this.columnMinWidth, columnPrefWidth = this.columnPrefWidth;
        for (int i = 0; i < columns; i++) {
            float growWidth = columnPrefWidth[i] - columnMinWidth[i];
            float growRatio = growWidth / totalGrowWidth;
            columnWeightedWidth[i] = columnMinWidth[i] + extraWidth * growRatio;
        }
    }
    float[] rowWeightedHeight;
    float totalGrowHeight = tablePrefHeight - tableMinHeight;
    if (totalGrowHeight == 0)
        rowWeightedHeight = rowMinHeight;
    else {
        rowWeightedHeight = Table.rowWeightedHeight = ensureSize(Table.rowWeightedHeight, rows);
        float extraHeight = Math.min(totalGrowHeight, Math.max(0, layoutHeight - tableMinHeight));
        float[] rowMinHeight = this.rowMinHeight, rowPrefHeight = this.rowPrefHeight;
        for (int i = 0; i < rows; i++) {
            float growHeight = rowPrefHeight[i] - rowMinHeight[i];
            float growRatio = growHeight / totalGrowHeight;
            rowWeightedHeight[i] = rowMinHeight[i] + extraHeight * growRatio;
        }
    }
    // Determine actor and cell sizes (before expand or fill).
    for (int i = 0; i < cellCount; i++) {
        Cell c = cells.get(i);
        int column = c.column, row = c.row;
        Actor a = c.actor;
        float spannedWeightedWidth = 0;
        int colspan = c.colspan;
        for (int ii = column, nn = ii + colspan; ii < nn; ii++) spannedWeightedWidth += columnWeightedWidth[ii];
        float weightedHeight = rowWeightedHeight[row];
        float prefWidth = c.prefWidth.get(a);
        float prefHeight = c.prefHeight.get(a);
        float minWidth = c.minWidth.get(a);
        float minHeight = c.minHeight.get(a);
        float maxWidth = c.maxWidth.get(a);
        float maxHeight = c.maxHeight.get(a);
        if (prefWidth < minWidth)
            prefWidth = minWidth;
        if (prefHeight < minHeight)
            prefHeight = minHeight;
        if (maxWidth > 0 && prefWidth > maxWidth)
            prefWidth = maxWidth;
        if (maxHeight > 0 && prefHeight > maxHeight)
            prefHeight = maxHeight;
        c.actorWidth = Math.min(spannedWeightedWidth - c.computedPadLeft - c.computedPadRight, prefWidth);
        c.actorHeight = Math.min(weightedHeight - c.computedPadTop - c.computedPadBottom, prefHeight);
        if (colspan == 1)
            columnWidth[column] = Math.max(columnWidth[column], spannedWeightedWidth);
        rowHeight[row] = Math.max(rowHeight[row], weightedHeight);
    }
    // Distribute remaining space to any expanding columns/rows.
    if (totalExpandWidth > 0) {
        float extra = layoutWidth - hpadding;
        for (int i = 0; i < columns; i++) extra -= columnWidth[i];
        float used = 0;
        int lastIndex = 0;
        for (int i = 0; i < columns; i++) {
            if (expandWidth[i] == 0)
                continue;
            float amount = extra * expandWidth[i] / totalExpandWidth;
            columnWidth[i] += amount;
            used += amount;
            lastIndex = i;
        }
        columnWidth[lastIndex] += extra - used;
    }
    if (totalExpandHeight > 0) {
        float extra = layoutHeight - vpadding;
        for (int i = 0; i < rows; i++) extra -= rowHeight[i];
        float used = 0;
        int lastIndex = 0;
        for (int i = 0; i < rows; i++) {
            if (expandHeight[i] == 0)
                continue;
            float amount = extra * expandHeight[i] / totalExpandHeight;
            rowHeight[i] += amount;
            used += amount;
            lastIndex = i;
        }
        rowHeight[lastIndex] += extra - used;
    }
    // Distribute any additional width added by colspanned cells to the columns spanned.
    for (int i = 0; i < cellCount; i++) {
        Cell c = cells.get(i);
        int colspan = c.colspan;
        if (colspan == 1)
            continue;
        float extraWidth = 0;
        for (int column = c.column, nn = column + colspan; column < nn; column++) extraWidth += columnWeightedWidth[column] - columnWidth[column];
        extraWidth -= Math.max(0, c.computedPadLeft + c.computedPadRight);
        extraWidth /= colspan;
        if (extraWidth > 0) {
            for (int column = c.column, nn = column + colspan; column < nn; column++) columnWidth[column] += extraWidth;
        }
    }
    // Determine table size.
    float tableWidth = hpadding, tableHeight = vpadding;
    for (int i = 0; i < columns; i++) tableWidth += columnWidth[i];
    for (int i = 0; i < rows; i++) tableHeight += rowHeight[i];
    // Position table within the container.
    int align = this.align;
    float x = layoutX + padLeft;
    if ((align & Align.right) != 0)
        x += layoutWidth - tableWidth;
    else if (// Center
    (align & Align.left) == 0)
        x += (layoutWidth - tableWidth) / 2;
    float y = layoutY + padTop;
    if ((align & Align.bottom) != 0)
        y += layoutHeight - tableHeight;
    else if (// Center
    (align & Align.top) == 0)
        y += (layoutHeight - tableHeight) / 2;
    // Position actors within cells.
    float currentX = x, currentY = y;
    for (int i = 0; i < cellCount; i++) {
        Cell c = cells.get(i);
        float spannedCellWidth = 0;
        for (int column = c.column, nn = column + c.colspan; column < nn; column++) spannedCellWidth += columnWidth[column];
        spannedCellWidth -= c.computedPadLeft + c.computedPadRight;
        currentX += c.computedPadLeft;
        float fillX = c.fillX, fillY = c.fillY;
        if (fillX > 0) {
            c.actorWidth = Math.max(spannedCellWidth * fillX, c.minWidth.get(c.actor));
            float maxWidth = c.maxWidth.get(c.actor);
            if (maxWidth > 0)
                c.actorWidth = Math.min(c.actorWidth, maxWidth);
        }
        if (fillY > 0) {
            c.actorHeight = Math.max(rowHeight[c.row] * fillY - c.computedPadTop - c.computedPadBottom, c.minHeight.get(c.actor));
            float maxHeight = c.maxHeight.get(c.actor);
            if (maxHeight > 0)
                c.actorHeight = Math.min(c.actorHeight, maxHeight);
        }
        align = c.align;
        if ((align & Align.left) != 0)
            c.actorX = currentX;
        else if ((align & Align.right) != 0)
            c.actorX = currentX + spannedCellWidth - c.actorWidth;
        else
            c.actorX = currentX + (spannedCellWidth - c.actorWidth) / 2;
        if ((align & Align.top) != 0)
            c.actorY = currentY + c.computedPadTop;
        else if ((align & Align.bottom) != 0)
            c.actorY = currentY + rowHeight[c.row] - c.actorHeight - c.computedPadBottom;
        else
            c.actorY = currentY + (rowHeight[c.row] - c.actorHeight + c.computedPadTop - c.computedPadBottom) / 2;
        if (c.endRow) {
            currentX = x;
            currentY += rowHeight[c.row];
        } else
            currentX += spannedCellWidth + c.computedPadRight;
    }
    // Store debug rectangles.
    if (debug == Debug.none)
        return;
    clearDebugRects();
    currentX = x;
    currentY = y;
    if (debug == Debug.table || debug == Debug.all) {
        addDebugRect(layoutX, layoutY, layoutWidth, layoutHeight, debugTableColor);
        addDebugRect(x, y, tableWidth - hpadding, tableHeight - vpadding, debugTableColor);
    }
    for (int i = 0; i < cellCount; i++) {
        Cell c = cells.get(i);
        // Actor bounds.
        if (debug == Debug.actor || debug == Debug.all)
            addDebugRect(c.actorX, c.actorY, c.actorWidth, c.actorHeight, debugActorColor);
        // Cell bounds.
        float spannedCellWidth = 0;
        for (int column = c.column, nn = column + c.colspan; column < nn; column++) spannedCellWidth += columnWidth[column];
        spannedCellWidth -= c.computedPadLeft + c.computedPadRight;
        currentX += c.computedPadLeft;
        if (debug == Debug.cell || debug == Debug.all) {
            addDebugRect(currentX, currentY + c.computedPadTop, spannedCellWidth, rowHeight[c.row] - c.computedPadTop - c.computedPadBottom, debugCellColor);
        }
        if (c.endRow) {
            currentX = x;
            currentY += rowHeight[c.row];
        } else
            currentX += spannedCellWidth + c.computedPadRight;
    }
}
Also used : Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Example 12 with Actor

use of com.badlogic.gdx.scenes.scene2d.Actor in project libgdx by libgdx.

the class Button method draw.

public void draw(Batch batch, float parentAlpha) {
    validate();
    boolean isDisabled = isDisabled();
    boolean isPressed = isPressed();
    boolean isChecked = isChecked();
    boolean isOver = isOver();
    Drawable background = null;
    if (isDisabled && style.disabled != null)
        background = style.disabled;
    else if (isPressed && style.down != null)
        background = style.down;
    else if (isChecked && style.checked != null)
        background = (style.checkedOver != null && isOver) ? style.checkedOver : style.checked;
    else if (isOver && style.over != null)
        background = style.over;
    else if (//
    style.up != null)
        background = style.up;
    setBackground(background);
    float offsetX = 0, offsetY = 0;
    if (isPressed && !isDisabled) {
        offsetX = style.pressedOffsetX;
        offsetY = style.pressedOffsetY;
    } else if (isChecked && !isDisabled) {
        offsetX = style.checkedOffsetX;
        offsetY = style.checkedOffsetY;
    } else {
        offsetX = style.unpressedOffsetX;
        offsetY = style.unpressedOffsetY;
    }
    Array<Actor> children = getChildren();
    for (int i = 0; i < children.size; i++) children.get(i).moveBy(offsetX, offsetY);
    super.draw(batch, parentAlpha);
    for (int i = 0; i < children.size; i++) children.get(i).moveBy(-offsetX, -offsetY);
    Stage stage = getStage();
    if (stage != null && stage.getActionsRequestRendering() && isPressed != clickListener.isPressed())
        Gdx.graphics.requestRendering();
}
Also used : Actor(com.badlogic.gdx.scenes.scene2d.Actor) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) Stage(com.badlogic.gdx.scenes.scene2d.Stage)

Example 13 with Actor

use of com.badlogic.gdx.scenes.scene2d.Actor in project libgdx by libgdx.

the class Dialog method initialize.

private void initialize() {
    setModal(true);
    defaults().space(6);
    add(contentTable = new Table(skin)).expand().fill();
    row();
    add(buttonTable = new Table(skin)).fillX();
    contentTable.defaults().space(6);
    buttonTable.defaults().space(6);
    buttonTable.addListener(new ChangeListener() {

        public void changed(ChangeEvent event, Actor actor) {
            if (!values.containsKey(actor))
                return;
            while (actor.getParent() != buttonTable) actor = actor.getParent();
            result(values.get(actor));
            if (!cancelHide)
                hide();
            cancelHide = false;
        }
    });
    focusListener = new FocusListener() {

        public void keyboardFocusChanged(FocusEvent event, Actor actor, boolean focused) {
            if (!focused)
                focusChanged(event);
        }

        public void scrollFocusChanged(FocusEvent event, Actor actor, boolean focused) {
            if (!focused)
                focusChanged(event);
        }

        private void focusChanged(FocusEvent event) {
            Stage stage = getStage();
            if (isModal && stage != null && stage.getRoot().getChildren().size > 0 && stage.getRoot().getChildren().peek() == Dialog.this) {
                // Dialog is top most actor.
                Actor newFocusedActor = event.getRelatedActor();
                if (newFocusedActor != null && !newFocusedActor.isDescendantOf(Dialog.this) && !(newFocusedActor.equals(previousKeyboardFocus) || newFocusedActor.equals(previousScrollFocus)))
                    event.cancel();
            }
        }
    };
}
Also used : Actor(com.badlogic.gdx.scenes.scene2d.Actor) Stage(com.badlogic.gdx.scenes.scene2d.Stage) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) FocusListener(com.badlogic.gdx.scenes.scene2d.utils.FocusListener)

Example 14 with Actor

use of com.badlogic.gdx.scenes.scene2d.Actor in project libgdx by libgdx.

the class Dialog method hide.

/** Hides the dialog with the given action and then removes it from the stage. */
public void hide(Action action) {
    Stage stage = getStage();
    if (stage != null) {
        removeListener(focusListener);
        if (previousKeyboardFocus != null && previousKeyboardFocus.getStage() == null)
            previousKeyboardFocus = null;
        Actor actor = stage.getKeyboardFocus();
        if (actor == null || actor.isDescendantOf(this))
            stage.setKeyboardFocus(previousKeyboardFocus);
        if (previousScrollFocus != null && previousScrollFocus.getStage() == null)
            previousScrollFocus = null;
        actor = stage.getScrollFocus();
        if (actor == null || actor.isDescendantOf(this))
            stage.setScrollFocus(previousScrollFocus);
    }
    if (action != null) {
        addCaptureListener(ignoreTouchDown);
        addAction(sequence(action, Actions.removeListener(ignoreTouchDown, true), Actions.removeActor()));
    } else
        remove();
}
Also used : Actor(com.badlogic.gdx.scenes.scene2d.Actor) Stage(com.badlogic.gdx.scenes.scene2d.Stage)

Example 15 with Actor

use of com.badlogic.gdx.scenes.scene2d.Actor in project libgdx by libgdx.

the class HorizontalGroup method computeSize.

private void computeSize() {
    sizeInvalid = false;
    SnapshotArray<Actor> children = getChildren();
    int n = children.size;
    prefHeight = 0;
    if (wrap) {
        prefWidth = 0;
        if (rowSizes == null)
            rowSizes = new FloatArray();
        else
            rowSizes.clear();
        FloatArray rowSizes = this.rowSizes;
        float space = this.space, wrapSpace = this.wrapSpace;
        float pad = padLeft + padRight, groupWidth = getWidth() - pad, x = 0, y = 0, rowHeight = 0;
        int i = 0, incr = 1;
        if (reverse) {
            i = n - 1;
            n = -1;
            incr = -1;
        }
        for (; i != n; i += incr) {
            Actor child = children.get(i);
            float width, height;
            if (child instanceof Layout) {
                Layout layout = (Layout) child;
                width = layout.getPrefWidth();
                height = layout.getPrefHeight();
            } else {
                width = child.getWidth();
                height = child.getHeight();
            }
            float incrX = width + (x > 0 ? space : 0);
            if (x + incrX > groupWidth && x > 0) {
                rowSizes.add(x);
                rowSizes.add(rowHeight);
                prefWidth = Math.max(prefWidth, x + pad);
                if (y > 0)
                    y += wrapSpace;
                y += rowHeight;
                rowHeight = 0;
                x = 0;
                incrX = width;
            }
            x += incrX;
            rowHeight = Math.max(rowHeight, height);
        }
        rowSizes.add(x);
        rowSizes.add(rowHeight);
        prefWidth = Math.max(prefWidth, x + pad);
        if (y > 0)
            y += wrapSpace;
        prefHeight = Math.max(prefHeight, y + rowHeight);
    } else {
        prefWidth = padLeft + padRight + space * (n - 1);
        for (int i = 0; i < n; i++) {
            Actor child = children.get(i);
            if (child instanceof Layout) {
                Layout layout = (Layout) child;
                prefWidth += layout.getPrefWidth();
                prefHeight = Math.max(prefHeight, layout.getPrefHeight());
            } else {
                prefWidth += child.getWidth();
                prefHeight = Math.max(prefHeight, child.getHeight());
            }
        }
    }
    prefHeight += padTop + padBottom;
    if (round) {
        prefWidth = Math.round(prefWidth);
        prefHeight = Math.round(prefHeight);
    }
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) Layout(com.badlogic.gdx.scenes.scene2d.utils.Layout) Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Aggregations

Actor (com.badlogic.gdx.scenes.scene2d.Actor)67 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)26 Stage (com.badlogic.gdx.scenes.scene2d.Stage)19 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)19 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)18 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)15 Layout (com.badlogic.gdx.scenes.scene2d.utils.Layout)11 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)10 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)9 Texture (com.badlogic.gdx.graphics.Texture)8 Group (com.badlogic.gdx.scenes.scene2d.Group)8 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 InputListener (com.badlogic.gdx.scenes.scene2d.InputListener)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)6 Dialog (com.badlogic.gdx.scenes.scene2d.ui.Dialog)6 Image (com.badlogic.gdx.scenes.scene2d.ui.Image)6 ScreenViewport (com.badlogic.gdx.utils.viewport.ScreenViewport)6 CheckBox (com.badlogic.gdx.scenes.scene2d.ui.CheckBox)5 ScrollPane (com.badlogic.gdx.scenes.scene2d.ui.ScrollPane)5 Slider (com.badlogic.gdx.scenes.scene2d.ui.Slider)5