Search in sources :

Example 6 with Layout

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

the class ScrollPane method layout.

public void layout() {
    final Drawable bg = style.background;
    final Drawable hScrollKnob = style.hScrollKnob;
    final Drawable vScrollKnob = style.vScrollKnob;
    float bgLeftWidth = 0, bgRightWidth = 0, bgTopHeight = 0, bgBottomHeight = 0;
    if (bg != null) {
        bgLeftWidth = bg.getLeftWidth();
        bgRightWidth = bg.getRightWidth();
        bgTopHeight = bg.getTopHeight();
        bgBottomHeight = bg.getBottomHeight();
    }
    float width = getWidth();
    float height = getHeight();
    float scrollbarHeight = 0;
    if (hScrollKnob != null)
        scrollbarHeight = hScrollKnob.getMinHeight();
    if (style.hScroll != null)
        scrollbarHeight = Math.max(scrollbarHeight, style.hScroll.getMinHeight());
    float scrollbarWidth = 0;
    if (vScrollKnob != null)
        scrollbarWidth = vScrollKnob.getMinWidth();
    if (style.vScroll != null)
        scrollbarWidth = Math.max(scrollbarWidth, style.vScroll.getMinWidth());
    // Get available space size by subtracting background's padded area.
    areaWidth = width - bgLeftWidth - bgRightWidth;
    areaHeight = height - bgTopHeight - bgBottomHeight;
    if (widget == null)
        return;
    // Get widget's desired width.
    float widgetWidth, widgetHeight;
    if (widget instanceof Layout) {
        Layout layout = (Layout) widget;
        widgetWidth = layout.getPrefWidth();
        widgetHeight = layout.getPrefHeight();
    } else {
        widgetWidth = widget.getWidth();
        widgetHeight = widget.getHeight();
    }
    // Determine if horizontal/vertical scrollbars are needed.
    scrollX = forceScrollX || (widgetWidth > areaWidth && !disableX);
    scrollY = forceScrollY || (widgetHeight > areaHeight && !disableY);
    boolean fade = fadeScrollBars;
    if (!fade) {
        // Check again, now taking into account the area that's taken up by any enabled scrollbars.
        if (scrollY) {
            areaWidth -= scrollbarWidth;
            if (!scrollX && widgetWidth > areaWidth && !disableX)
                scrollX = true;
        }
        if (scrollX) {
            areaHeight -= scrollbarHeight;
            if (!scrollY && widgetHeight > areaHeight && !disableY) {
                scrollY = true;
                areaWidth -= scrollbarWidth;
            }
        }
    }
    // The bounds of the scrollable area for the widget.
    widgetAreaBounds.set(bgLeftWidth, bgBottomHeight, areaWidth, areaHeight);
    if (fade) {
        // Make sure widget is drawn under fading scrollbars.
        if (scrollX && scrollY) {
            areaHeight -= scrollbarHeight;
            areaWidth -= scrollbarWidth;
        }
    } else {
        if (scrollbarsOnTop) {
            // Make sure widget is drawn under non-fading scrollbars.
            if (scrollX)
                widgetAreaBounds.height += scrollbarHeight;
            if (scrollY)
                widgetAreaBounds.width += scrollbarWidth;
        } else {
            // Offset widget area y for horizontal scrollbar at bottom.
            if (scrollX && hScrollOnBottom)
                widgetAreaBounds.y += scrollbarHeight;
            // Offset widget area x for vertical scrollbar at left.
            if (scrollY && !vScrollOnRight)
                widgetAreaBounds.x += scrollbarWidth;
        }
    }
    // If the widget is smaller than the available space, make it take up the available space.
    widgetWidth = disableX ? areaWidth : Math.max(areaWidth, widgetWidth);
    widgetHeight = disableY ? areaHeight : Math.max(areaHeight, widgetHeight);
    maxX = widgetWidth - areaWidth;
    maxY = widgetHeight - areaHeight;
    if (fade) {
        // Make sure widget is drawn under fading scrollbars.
        if (scrollX && scrollY) {
            maxY -= scrollbarHeight;
            maxX -= scrollbarWidth;
        }
    }
    scrollX(MathUtils.clamp(amountX, 0, maxX));
    scrollY(MathUtils.clamp(amountY, 0, maxY));
    // Set the bounds and scroll knob sizes if scrollbars are needed.
    if (scrollX) {
        if (hScrollKnob != null) {
            float hScrollHeight = style.hScroll != null ? style.hScroll.getMinHeight() : hScrollKnob.getMinHeight();
            // The corner gap where the two scroll bars intersect might have to flip from right to left.
            float boundsX = vScrollOnRight ? bgLeftWidth : bgLeftWidth + scrollbarWidth;
            // Scrollbar on the top or bottom.
            float boundsY = hScrollOnBottom ? bgBottomHeight : height - bgTopHeight - hScrollHeight;
            hScrollBounds.set(boundsX, boundsY, areaWidth, hScrollHeight);
            if (variableSizeKnobs)
                hKnobBounds.width = Math.max(hScrollKnob.getMinWidth(), (int) (hScrollBounds.width * areaWidth / widgetWidth));
            else
                hKnobBounds.width = hScrollKnob.getMinWidth();
            hKnobBounds.height = hScrollKnob.getMinHeight();
            hKnobBounds.x = hScrollBounds.x + (int) ((hScrollBounds.width - hKnobBounds.width) * getScrollPercentX());
            hKnobBounds.y = hScrollBounds.y;
        } else {
            hScrollBounds.set(0, 0, 0, 0);
            hKnobBounds.set(0, 0, 0, 0);
        }
    }
    if (scrollY) {
        if (vScrollKnob != null) {
            float vScrollWidth = style.vScroll != null ? style.vScroll.getMinWidth() : vScrollKnob.getMinWidth();
            // the small gap where the two scroll bars intersect might have to flip from bottom to top
            float boundsX, boundsY;
            if (hScrollOnBottom) {
                boundsY = height - bgTopHeight - areaHeight;
            } else {
                boundsY = bgBottomHeight;
            }
            // bar on the left or right
            if (vScrollOnRight) {
                boundsX = width - bgRightWidth - vScrollWidth;
            } else {
                boundsX = bgLeftWidth;
            }
            vScrollBounds.set(boundsX, boundsY, vScrollWidth, areaHeight);
            vKnobBounds.width = vScrollKnob.getMinWidth();
            if (variableSizeKnobs)
                vKnobBounds.height = Math.max(vScrollKnob.getMinHeight(), (int) (vScrollBounds.height * areaHeight / widgetHeight));
            else
                vKnobBounds.height = vScrollKnob.getMinHeight();
            if (vScrollOnRight) {
                vKnobBounds.x = width - bgRightWidth - vScrollKnob.getMinWidth();
            } else {
                vKnobBounds.x = bgLeftWidth;
            }
            vKnobBounds.y = vScrollBounds.y + (int) ((vScrollBounds.height - vKnobBounds.height) * (1 - getScrollPercentY()));
        } else {
            vScrollBounds.set(0, 0, 0, 0);
            vKnobBounds.set(0, 0, 0, 0);
        }
    }
    widget.setSize(widgetWidth, widgetHeight);
    if (widget instanceof Layout)
        ((Layout) widget).validate();
}
Also used : Layout(com.badlogic.gdx.scenes.scene2d.utils.Layout) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable)

Example 7 with Layout

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

the class Tree method computeSize.

private void computeSize(Array<Node> nodes, float indent) {
    float ySpacing = this.ySpacing;
    float spacing = iconSpacingLeft + iconSpacingRight;
    for (int i = 0, n = nodes.size; i < n; i++) {
        Node node = nodes.get(i);
        float rowWidth = indent + iconSpacingRight;
        Actor actor = node.actor;
        if (actor instanceof Layout) {
            Layout layout = (Layout) actor;
            rowWidth += layout.getPrefWidth();
            node.height = layout.getPrefHeight();
            layout.pack();
        } else {
            rowWidth += actor.getWidth();
            node.height = actor.getHeight();
        }
        if (node.icon != null) {
            rowWidth += spacing + node.icon.getMinWidth();
            node.height = Math.max(node.height, node.icon.getMinHeight());
        }
        prefWidth = Math.max(prefWidth, rowWidth);
        prefHeight -= node.height + ySpacing;
        if (node.expanded)
            computeSize(node.children, indent + indentSpacing);
    }
}
Also used : Layout(com.badlogic.gdx.scenes.scene2d.utils.Layout) Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Example 8 with Layout

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

the class VerticalGroup method computeSize.

private void computeSize() {
    sizeInvalid = false;
    SnapshotArray<Actor> children = getChildren();
    int n = children.size;
    prefWidth = 0;
    if (wrap) {
        prefHeight = 0;
        if (columnSizes == null)
            columnSizes = new FloatArray();
        else
            columnSizes.clear();
        FloatArray columnSizes = this.columnSizes;
        float space = this.space, wrapSpace = this.wrapSpace;
        float pad = padTop + padBottom, groupHeight = getHeight() - pad, x = 0, y = 0, columnWidth = 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 incrY = height + (y > 0 ? space : 0);
            if (y + incrY > groupHeight && y > 0) {
                columnSizes.add(y);
                columnSizes.add(columnWidth);
                prefHeight = Math.max(prefHeight, y + pad);
                if (x > 0)
                    x += wrapSpace;
                x += columnWidth;
                columnWidth = 0;
                y = 0;
                incrY = height;
            }
            y += incrY;
            columnWidth = Math.max(columnWidth, width);
        }
        columnSizes.add(y);
        columnSizes.add(columnWidth);
        prefHeight = Math.max(prefHeight, y + pad);
        if (x > 0)
            x += wrapSpace;
        prefWidth = Math.max(prefWidth, x + columnWidth);
    } else {
        prefHeight = padTop + padBottom + space * (n - 1);
        for (int i = 0; i < n; i++) {
            Actor child = children.get(i);
            if (child instanceof Layout) {
                Layout layout = (Layout) child;
                prefWidth = Math.max(prefWidth, layout.getPrefWidth());
                prefHeight += layout.getPrefHeight();
            } else {
                prefWidth = Math.max(prefWidth, child.getWidth());
                prefHeight += child.getHeight();
            }
        }
    }
    prefWidth += padLeft + padRight;
    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)

Example 9 with Layout

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

the class VerticalGroup method layoutWrapped.

private void layoutWrapped() {
    float prefWidth = getPrefWidth();
    if (prefWidth != lastPrefWidth) {
        lastPrefWidth = prefWidth;
        invalidateHierarchy();
    }
    int align = this.align;
    boolean round = this.round;
    float space = this.space, padLeft = this.padLeft, fill = this.fill, wrapSpace = this.wrapSpace;
    float maxHeight = prefHeight - padTop - padBottom;
    float columnX = padLeft, groupHeight = getHeight();
    float yStart = prefHeight - padTop + space, y = 0, columnWidth = 0;
    if ((align & Align.right) != 0)
        columnX += getWidth() - prefWidth;
    else if (// center
    (align & Align.left) == 0)
        columnX += (getWidth() - prefWidth) / 2;
    if ((align & Align.top) != 0)
        yStart += groupHeight - prefHeight;
    else if (// center
    (align & Align.bottom) == 0)
        yStart += (groupHeight - prefHeight) / 2;
    groupHeight -= padTop;
    align = columnAlign;
    FloatArray columnSizes = this.columnSizes;
    SnapshotArray<Actor> children = getChildren();
    int i = 0, n = children.size, incr = 1;
    if (reverse) {
        i = n - 1;
        n = -1;
        incr = -1;
    }
    for (int r = 0; i != n; i += incr) {
        Actor child = children.get(i);
        float width, height;
        Layout layout = null;
        if (child instanceof Layout) {
            layout = (Layout) child;
            width = layout.getPrefWidth();
            height = layout.getPrefHeight();
        } else {
            width = child.getWidth();
            height = child.getHeight();
        }
        if (y - height - space < padBottom || r == 0) {
            y = yStart;
            if ((align & Align.bottom) != 0)
                y -= maxHeight - columnSizes.get(r);
            else if (// center
            (align & Align.top) == 0)
                y -= (maxHeight - columnSizes.get(r)) / 2;
            if (r > 0) {
                columnX += wrapSpace;
                columnX += columnWidth;
            }
            columnWidth = columnSizes.get(r + 1);
            r += 2;
        }
        if (fill > 0)
            width = columnWidth * fill;
        if (layout != null) {
            width = Math.max(width, layout.getMinWidth());
            float maxWidth = layout.getMaxWidth();
            if (maxWidth > 0 && width > maxWidth)
                width = maxWidth;
        }
        float x = columnX;
        if ((align & Align.right) != 0)
            x += columnWidth - width;
        else if (// center
        (align & Align.left) == 0)
            x += (columnWidth - width) / 2;
        y -= height + space;
        if (round)
            child.setBounds(Math.round(x), Math.round(y), Math.round(width), Math.round(height));
        else
            child.setBounds(x, y, width, height);
        if (layout != null)
            layout.validate();
    }
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) Layout(com.badlogic.gdx.scenes.scene2d.utils.Layout) Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Example 10 with Layout

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

the class WidgetGroup method invalidateHierarchy.

public void invalidateHierarchy() {
    invalidate();
    Group parent = getParent();
    if (parent instanceof Layout)
        ((Layout) parent).invalidateHierarchy();
}
Also used : Group(com.badlogic.gdx.scenes.scene2d.Group) Layout(com.badlogic.gdx.scenes.scene2d.utils.Layout)

Aggregations

Layout (com.badlogic.gdx.scenes.scene2d.utils.Layout)14 Actor (com.badlogic.gdx.scenes.scene2d.Actor)11 FloatArray (com.badlogic.gdx.utils.FloatArray)4 Group (com.badlogic.gdx.scenes.scene2d.Group)2 Rectangle (com.badlogic.gdx.math.Rectangle)1 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)1