Search in sources :

Example 21 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class RelativeLayout method getRegion.

private Rect2i getRegion(WidgetInfo element, Canvas canvas) {
    Rect2i cachedRegion = cachedRegions.get(element);
    if (cachedRegion != null) {
        return cachedRegion;
    }
    int left = 0;
    int right = canvas.size().x;
    int center = canvas.size().x / 2;
    if (element.layoutHint.getPositionCenterHorizontal() != null) {
        HorizontalInfo info = element.layoutHint.getPositionCenterHorizontal();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        HorizontalAlign align = (info.getTarget() != null) ? info.getTarget() : HorizontalAlign.CENTER;
        center = align.getStart(targetRegion) + info.getOffset();
    }
    if (element.layoutHint.getPositionLeft() != null) {
        HorizontalInfo info = element.layoutHint.getPositionLeft();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        HorizontalAlign align = (info.getTarget() != null) ? info.getTarget() : HorizontalAlign.LEFT;
        left = align.getStart(targetRegion) + info.getOffset();
    }
    if (element.layoutHint.getPositionRight() != null) {
        HorizontalInfo info = element.layoutHint.getPositionRight();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        HorizontalAlign align = (info.getTarget() != null) ? info.getTarget() : HorizontalAlign.RIGHT;
        right = align.getStart(targetRegion) - info.getOffset();
    }
    int top = 0;
    int bottom = canvas.size().y;
    int vcenter = canvas.size().y / 2;
    if (element.layoutHint.getPositionCenterVertical() != null) {
        VerticalInfo info = element.layoutHint.getPositionCenterVertical();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        VerticalAlign align = (info.getTarget() != null) ? info.getTarget() : VerticalAlign.MIDDLE;
        vcenter = align.getStart(targetRegion) + info.getOffset();
    }
    if (element.layoutHint.getPositionTop() != null) {
        VerticalInfo info = element.layoutHint.getPositionTop();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        VerticalAlign align = (info.getTarget() != null) ? info.getTarget() : VerticalAlign.TOP;
        top = align.getStart(targetRegion) + info.getOffset();
    }
    if (element.layoutHint.getPositionBottom() != null) {
        VerticalInfo info = element.layoutHint.getPositionBottom();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        VerticalAlign align = (info.getTarget() != null) ? info.getTarget() : VerticalAlign.BOTTOM;
        bottom = align.getStart(targetRegion) - info.getOffset();
    }
    int width = element.layoutHint.getWidth();
    if (width == 0 && element.layoutHint.isUsingContentWidth()) {
        width = canvas.calculateRestrictedSize(element.widget, new Vector2i(right - left, bottom - top)).x;
    }
    if (width == 0) {
        width = right - left;
    } else {
        if (element.layoutHint.getPositionCenterHorizontal() != null) {
            left = center - width / 2;
        } else if (element.layoutHint.getPositionRight() != null) {
            if (element.layoutHint.getPositionLeft() != null) {
                center = left + (right - left) / 2;
                left = center - width / 2;
            } else {
                left = right - width;
            }
        }
    }
    int height = element.layoutHint.getHeight();
    if (height == 0 && element.layoutHint.isUsingContentHeight()) {
        height = canvas.calculateRestrictedSize(element.widget, new Vector2i(width, bottom - top)).y;
    }
    if (height == 0) {
        height = bottom - top;
    } else {
        if (element.layoutHint.getPositionCenterVertical() != null) {
            top = vcenter - height / 2;
        } else if (element.layoutHint.getPositionBottom() != null) {
            if (element.layoutHint.getPositionTop() != null) {
                vcenter = top + (bottom - top) / 2;
                top = vcenter - height / 2;
            } else {
                top = bottom - height;
            }
        }
    }
    Rect2i region = Rect2i.createFromMinAndSize(left, top, width, height);
    cachedRegions.put(element, region);
    return region;
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) VerticalAlign(org.terasology.rendering.nui.VerticalAlign) HorizontalAlign(org.terasology.rendering.nui.HorizontalAlign) Vector2i(org.terasology.math.geom.Vector2i)

Example 22 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class CursorAttachment method onDraw.

@Override
public void onDraw(Canvas canvas) {
    UIStyle style = canvas.getCurrentStyle();
    Vector2i attachmentSize = canvas.calculatePreferredSize(attachment);
    attachmentSize.add(style.getMargin().getTotals());
    // TODO get rid of CoreRegistry. e.g. by updatin a mousePos field with an InteractionListener
    MouseDevice mouse = CoreRegistry.get(InputSystem.class).getMouseDevice();
    int top;
    switch(style.getVerticalAlignment()) {
        case TOP:
            top = mouse.getPosition().y - attachmentSize.y;
            break;
        case MIDDLE:
            top = mouse.getPosition().y - attachmentSize.y / 2;
            break;
        default:
            top = mouse.getPosition().y + MOUSE_CURSOR_HEIGHT;
            break;
    }
    top = TeraMath.clamp(top, 0, canvas.size().y - attachmentSize.y);
    int left;
    switch(style.getHorizontalAlignment()) {
        case RIGHT:
            left = mouse.getPosition().x - attachmentSize.x;
            break;
        case CENTER:
            left = mouse.getPosition().x - attachmentSize.x / 2;
            break;
        default:
            left = mouse.getPosition().x;
            break;
    }
    left = TeraMath.clamp(left, 0, canvas.size().x - attachmentSize.x);
    try (SubRegion ignored = canvas.subRegion(Rect2i.createFromMinAndSize(left, top, attachmentSize.x, attachmentSize.y), false)) {
        canvas.drawBackground();
        canvas.drawWidget(attachment, style.getBackgroundBorder().shrink(canvas.getRegion()));
    }
}
Also used : UIStyle(org.terasology.rendering.nui.skin.UIStyle) InputSystem(org.terasology.input.InputSystem) Vector2i(org.terasology.math.geom.Vector2i) SubRegion(org.terasology.rendering.nui.SubRegion) MouseDevice(org.terasology.input.device.MouseDevice)

Example 23 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class UIDoubleSlider method getPreferredContentSize.

@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i areaHint) {
    Vector2i result = new Vector2i();
    canvas.setPart(SLIDER_PART);
    result.x = canvas.getCurrentStyle().getFixedWidth();
    if (result.x == 0) {
        result.x = canvas.getCurrentStyle().getMinWidth();
    }
    result.y = canvas.getCurrentStyle().getFixedHeight();
    if (result.y == 0) {
        result.y = canvas.getCurrentStyle().getMinHeight();
    }
    Vector2i left = getTickerPreferredContentSize(canvas, TICKER_LEFT_PART);
    Vector2i right = getTickerPreferredContentSize(canvas, TICKER_RIGHT_PART);
    result.y = Math.max(result.y, Math.max(left.y, right.y));
    result.x = Math.max(result.x, left.x + left.y);
    return result;
}
Also used : Vector2i(org.terasology.math.geom.Vector2i)

Example 24 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class UIIconBar method getPreferredContentSize.

@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
    canvas.setPart(ICON_PART);
    if (icon != null) {
        Vector2i iconSize = getIconSize(canvas);
        int maxHorizontalIcons = sizeHint.x / iconSize.x;
        int rows = ((maxIcons - 1) / maxHorizontalIcons) + 1;
        int columns = Math.min(maxIcons, maxHorizontalIcons);
        return new Vector2i(columns * iconSize.x + (columns - 1) * spacing, rows * iconSize.y + (rows - 1) * spacing);
    } else {
        return Vector2i.zero();
    }
}
Also used : Vector2i(org.terasology.math.geom.Vector2i)

Example 25 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class UIList method getPreferredContentSize.

@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i areaHint) {
    canvas.setPart("item");
    Vector2i result = new Vector2i();
    for (T item : list.get()) {
        Vector2i preferredSize = canvas.getCurrentStyle().getMargin().grow(itemRenderer.getPreferredSize(item, canvas));
        result.x = Math.max(result.x, preferredSize.x);
        result.y += preferredSize.y;
    }
    return result;
}
Also used : Vector2i(org.terasology.math.geom.Vector2i)

Aggregations

Vector2i (org.terasology.math.geom.Vector2i)76 UIWidget (org.terasology.rendering.nui.UIWidget)17 Rect2i (org.terasology.math.geom.Rect2i)14 BaseVector2i (org.terasology.math.geom.BaseVector2i)13 LayoutHint (org.terasology.rendering.nui.LayoutHint)13 List (java.util.List)9 Test (org.junit.Test)9 Vector2f (org.terasology.math.geom.Vector2f)6 Canvas (org.terasology.rendering.nui.Canvas)5 SubRegion (org.terasology.rendering.nui.SubRegion)5 UIStyle (org.terasology.rendering.nui.skin.UIStyle)5 UILabel (org.terasology.rendering.nui.widgets.UILabel)5 Before (org.junit.Before)4 Font (org.terasology.rendering.assets.font.Font)4 Color (org.terasology.rendering.nui.Color)4 Border (org.terasology.math.Border)3 Vector3f (org.terasology.math.geom.Vector3f)3 Mesh (org.terasology.rendering.assets.mesh.Mesh)3 HorizontalAlign (org.terasology.rendering.nui.HorizontalAlign)3 Sets (com.google.common.collect.Sets)2