Search in sources :

Example 56 with Vector2i

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

the class Rect2iTypeHandler method deserialize.

@Override
public Rect2i deserialize(PersistedData data, DeserializationContext context) {
    if (!data.isNull() && data.isValueMap()) {
        PersistedDataMap map = data.getAsValueMap();
        Vector2i min = context.deserializeAs(map.get(MIN_FIELD), Vector2i.class);
        Vector2i size = context.deserializeAs(map.get(SIZE_FIELD), Vector2i.class);
        return Rect2i.createFromMinAndSize(min, size);
    }
    return null;
}
Also used : PersistedDataMap(org.terasology.persistence.typeHandling.PersistedDataMap) Vector2i(org.terasology.math.geom.Vector2i)

Example 57 with Vector2i

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

the class MigComponent method calculatePreferredSize.

public void calculatePreferredSize(Canvas canvas, Vector2i sizeHint) {
    minimumSize = canvas.calculatePreferredSize(widget);
    minimumSize.x = Math.min(sizeHint.x, minimumSize.x);
    minimumSize.y = Math.min(sizeHint.y, minimumSize.y);
    preferredSize = new Vector2i(Math.max(sizeHint.x, minimumSize.x), Math.max(sizeHint.y, minimumSize.y));
}
Also used : Vector2i(org.terasology.math.geom.Vector2i)

Example 58 with Vector2i

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

the class UIDoubleSlider method getTickerPreferredContentSize.

private Vector2i getTickerPreferredContentSize(Canvas canvas, String part) {
    Vector2i result = new Vector2i();
    canvas.setPart(part);
    int tickerWidth = canvas.getCurrentStyle().getFont().getWidth(formatString);
    tickerWidth += canvas.getCurrentStyle().getMargin().getTotalWidth();
    result.x = tickerWidth;
    if (canvas.getCurrentStyle().getFixedWidth() != 0) {
        result.x = Math.max(result.x, canvas.getCurrentStyle().getFixedWidth());
    } else {
        result.x = Math.max(result.x, canvas.getCurrentStyle().getMinWidth());
    }
    if (canvas.getCurrentStyle().getFixedHeight() != 0) {
        result.y = canvas.getCurrentStyle().getFixedHeight();
    } else {
        result.y = canvas.getCurrentStyle().getMinHeight();
    }
    return result;
}
Also used : Vector2i(org.terasology.math.geom.Vector2i)

Example 59 with Vector2i

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

the class UIDropdownScrollable method createScrollbarItems.

/**
 * Located in the onDraw method, this draws the menu items with a scrollbar.
 *
 * @param canvas     {@link Canvas} from the onDraw method.
 * @param frame      Menu frame.
 * @param font       {@link Font} used in the menu.
 * @param itemMargin Margin around every menu item.
 * @param height     Total menu height.
 * @param itemHeight Height per menu item.
 */
private void createScrollbarItems(Canvas canvas, Rect2i frame, Font font, Border itemMargin, int height, int itemHeight) {
    // Scrollable Area
    Rect2i scrollableArea = Rect2i.createFromMinAndSize(0, canvas.size().y, canvas.size().x, height - itemMargin.getBottom());
    // Scrollbar Measurement
    int scrollbarWidth = canvas.calculateRestrictedSize(verticalBar, new Vector2i(canvas.size().x, canvas.size().y)).x;
    int scrollbarHeight = frame.size().y - itemMargin.getTop();
    int availableWidth = frame.size().x - scrollbarWidth;
    int scrollbarXPos = availableWidth - itemMargin.getRight();
    int scrollbarYPos = itemMargin.getTotalHeight() * 2 + font.getLineHeight();
    // Draw Scrollbar
    Rect2i scrollbarRegion = Rect2i.createFromMinAndSize(scrollbarXPos, scrollbarYPos, scrollbarWidth, scrollbarHeight);
    canvas.drawWidget(verticalBar, scrollbarRegion);
    // Set the range of Scrollbar
    float maxVertBarDesired = itemHeight * (optionListeners.size() - visibleOptionsNum - 0.5f) + itemMargin.getBottom();
    verticalBar.setRange((int) maxVertBarDesired);
    for (int i = 0; i < optionListeners.size(); ++i) {
        readItemMouseOver(canvas, i);
        Rect2i itemRegion = Rect2i.createFromMinAndSize(0, itemHeight * i - verticalBar.getValue(), availableWidth, itemHeight);
        // If outside location, then hide
        try (SubRegion ignored = canvas.subRegion(scrollableArea, true)) {
            drawItem(canvas, itemMargin, i, itemRegion);
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) Vector2i(org.terasology.math.geom.Vector2i) SubRegion(org.terasology.rendering.nui.SubRegion)

Example 60 with Vector2i

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

the class UIIconBar method onDraw.

@Override
public void onDraw(Canvas canvas) {
    canvas.setPart(ICON_PART);
    if (icon != null && getMaxValue() > 0) {
        Vector2i iconSize = getIconSize(canvas);
        float ratio = maxIcons * getValue() / getMaxValue();
        int fullIcons = TeraMath.floorToInt(ratio);
        boolean halfIcon = false;
        if (ratio - fullIcons >= 0.5f) {
            fullIcons++;
        } else if (ratio - fullIcons > 0) {
            halfIcon = true;
        }
        Vector2i offset = new Vector2i();
        for (int i = 0; i < maxIcons; ++i) {
            Rect2i iconRegion = Rect2i.createFromMinAndSize(offset, iconSize);
            canvas.drawBackground(iconRegion);
            if (ratio - i >= 0.5f) {
                canvas.drawTexture(icon, iconRegion);
            } else if (ratio - i > 0f) {
                switch(halfIconMode) {
                    case SHRINK:
                        Vector2i halfSize = new Vector2i(iconSize);
                        halfSize.x /= 2;
                        halfSize.y /= 2;
                        canvas.drawTexture(icon, Rect2i.createFromMinAndSize(new Vector2i(offset.x + halfSize.x / 2, offset.y + halfSize.y / 2), halfSize));
                        break;
                    case SPLIT:
                        canvas.drawTextureRaw(icon, Rect2i.createFromMinAndSize(offset, new Vector2i(iconSize.x / 2, iconSize.y)), ScaleMode.STRETCH, 0f, 0f, (float) (iconSize.x / 2) / iconSize.x, 1.0f);
                        break;
                    default:
                        canvas.drawTexture(icon, iconRegion);
                        break;
                }
            }
            offset.x += iconSize.x + spacing;
            if (offset.x + iconSize.x > canvas.size().x) {
                offset.x = 0;
                offset.y += iconSize.y + spacing;
            }
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) 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