Search in sources :

Example 11 with Vector2i

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

the class ScrollableArea method onDraw.

@Override
public void onDraw(Canvas canvas) {
    int availableWidth = canvas.size().x;
    int availableHeight = canvas.size().y;
    // First, try to layout it without any scroll bars
    Vector2i contentSize = canvas.calculateRestrictedSize(content, new Vector2i(availableWidth, availableHeight));
    if (contentSize.x <= availableWidth && contentSize.y <= availableHeight) {
        canvas.drawWidget(content, Rect2i.createFromMinAndSize(new Vector2i(0, 0), new Vector2i(availableWidth, availableHeight)));
        return;
    }
    // Second, try to layout it just with vertical bar (if supported)
    if (verticalScrollbar) {
        int scrollbarWidth = canvas.calculateRestrictedSize(verticalBar, new Vector2i(availableWidth, availableHeight)).x;
        int scrollbarHeight = canvas.calculateRestrictedSize(verticalBar, new Vector2i(availableWidth, availableHeight)).y;
        contentSize = canvas.calculateRestrictedSize(content, new Vector2i(availableWidth - scrollbarWidth, Integer.MAX_VALUE));
        if (horizontalScrollbar && contentSize.x > availableWidth - scrollbarWidth) {
            if (contentSize.y > availableHeight - scrollbarHeight) {
                layoutWithBothScrollbars(canvas, contentSize, availableWidth, availableHeight, scrollbarWidth, scrollbarHeight);
            } else {
                contentSize = canvas.calculateRestrictedSize(content, new Vector2i(availableWidth, availableHeight - scrollbarHeight));
                layoutWithJustHorizontal(canvas, contentSize, availableWidth, availableHeight, scrollbarHeight);
            }
        } else {
            layoutWithJustVertical(canvas, contentSize, availableWidth, availableHeight, scrollbarWidth);
        }
    } else if (horizontalScrollbar) {
        // Well we know that just horizontal is allowed
        int scrollbarHeight = canvas.calculateRestrictedSize(verticalBar, new Vector2i(availableWidth, availableHeight)).y;
        availableHeight -= scrollbarHeight;
        contentSize = canvas.calculateRestrictedSize(content, new Vector2i(Integer.MAX_VALUE, availableHeight - scrollbarHeight));
        layoutWithJustHorizontal(canvas, contentSize, availableWidth, availableHeight, scrollbarHeight);
    } else {
        throw new IllegalStateException("ScrollableArea without any scrollbar allowed, what's the point of that?!");
    }
}
Also used : Vector2i(org.terasology.math.geom.Vector2i) LayoutHint(org.terasology.rendering.nui.LayoutHint)

Example 12 with Vector2i

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

the class BlockSelectionRenderSystem method renderOverlay.

@Override
public void renderOverlay() {
    for (EntityRef entity : entityManager.getEntitiesWith(BlockSelectionComponent.class)) {
        BlockSelectionComponent blockSelectionComponent = entity.getComponent(BlockSelectionComponent.class);
        if (blockSelectionComponent.shouldRender) {
            Texture texture = blockSelectionComponent.texture;
            if (null == texture) {
                texture = Assets.getTexture("engine:selection").get();
            }
            Vector2i textureDimensions = new Vector2i(texture.getWidth(), texture.getHeight());
            BlockSelectionRenderer selectionRenderer = cachedBlockSelectionRendererByTextureDimensionsMap.get(textureDimensions);
            if (null == selectionRenderer) {
                selectionRenderer = new BlockSelectionRenderer(texture);
                cachedBlockSelectionRendererByTextureDimensionsMap.put(textureDimensions, selectionRenderer);
            } else {
                selectionRenderer.setEffectsTexture(texture);
            }
            renderOverlayForOneBlockSelection(blockSelectionComponent, selectionRenderer);
        }
    }
}
Also used : BlockSelectionComponent(org.terasology.world.selection.BlockSelectionComponent) Vector2i(org.terasology.math.geom.Vector2i) EntityRef(org.terasology.entitySystem.entity.EntityRef) Texture(org.terasology.rendering.assets.texture.Texture)

Example 13 with Vector2i

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

the class ContextMenuScreen method onDraw.

@Override
public void onDraw(Canvas canvas) {
    canvas.addInteractionRegion(mainListener);
    Vector2i currentPosition = null;
    int currentWidth = 0;
    for (UIList<AbstractContextMenuItem> level : menuWidgets) {
        if (level.isVisible()) {
            if (currentPosition == null) {
                currentPosition = new Vector2i(position);
            } else {
                currentPosition.addX(currentWidth);
            }
            Rect2i region = Rect2i.createFromMinAndSize(currentPosition, canvas.calculatePreferredSize(level));
            double percentageThreshold = 0.9;
            if (region.maxY() > canvas.getRegion().height() * percentageThreshold) {
                region = Rect2i.createFromMinAndMax(region.minX(), region.minY() - (region.maxY() - canvas.getRegion().height()) - (int) (canvas.getRegion().height() * (1 - percentageThreshold)), region.maxX(), canvas.getRegion().height());
            }
            currentWidth = canvas.calculatePreferredSize(level).getX() - 8;
            canvas.drawWidget(level, region);
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) Vector2i(org.terasology.math.geom.Vector2i)

Example 14 with Vector2i

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

the class CanvasImpl method processMouseClick.

@Override
public boolean processMouseClick(MouseInput button, Vector2i pos) {
    boolean possibleDoubleClick = lastClickPosition.gridDistance(pos) < MAX_DOUBLE_CLICK_DISTANCE && lastClickButton == button && time.getGameTimeInMs() - lastClickTime < DOUBLE_CLICK_TIME;
    lastClickPosition.set(pos);
    lastClickButton = button;
    lastClickTime = time.getGameTimeInMs();
    for (InteractionRegion next : mouseOverRegions) {
        if (next.region.contains(pos)) {
            Vector2i relPos = new Vector2i(pos);
            relPos.sub(next.offset);
            if (possibleDoubleClick && nuiManager.getFocus() == next.element) {
                if (next.listener.onMouseDoubleClick(createDoubleClickEvent(button, relPos))) {
                    clickedRegion = next;
                    return true;
                }
            } else if (next.listener.onMouseClick(createClickEvent(button, relPos))) {
                clickedRegion = next;
                nuiManager.setFocus(next.element);
                return true;
            }
        }
    }
    return false;
}
Also used : Vector2i(org.terasology.math.geom.Vector2i) BaseVector2i(org.terasology.math.geom.BaseVector2i)

Example 15 with Vector2i

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

the class CanvasImpl method addInteractionRegion.

@Override
public void addInteractionRegion(InteractionListener listener, UIWidget tooltip, Rect2i region) {
    Vector2i offset = state.drawRegion.min();
    Rect2i finalRegion = state.cropRegion.intersect(relativeToAbsolute(region));
    if (!finalRegion.isEmpty()) {
        listener.setFocusManager(nuiManager);
        if (state.drawOnTop) {
            drawOnTopOperations.add(new DrawInteractionRegionOperation(finalRegion, offset, listener, state.element, tooltip));
        } else {
            interactionRegions.addLast(new InteractionRegion(finalRegion, offset, listener, state.element, tooltip));
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) Vector2i(org.terasology.math.geom.Vector2i) BaseVector2i(org.terasology.math.geom.BaseVector2i)

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