Search in sources :

Example 26 with Vector2i

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

the class UIList method onDraw.

@Override
public void onDraw(Canvas canvas) {
    updateItemListeners();
    canvas.setPart("item");
    boolean enabled = isEnabled();
    Border margin = canvas.getCurrentStyle().getMargin();
    int yOffset = 0;
    for (int i = 0; i < list.get().size(); ++i) {
        T item = list.get().get(i);
        Vector2i preferredSize = margin.grow(itemRenderer.getPreferredSize(item, canvas));
        Rect2i itemRegion = Rect2i.createFromMinAndSize(0, yOffset, canvas.size().x, preferredSize.y);
        ItemInteractionListener listener = itemListeners.get(i);
        if (enabled) {
            if (Objects.equals(item, selection.get())) {
                canvas.setMode(ACTIVE_MODE);
            } else if (listener.isMouseOver()) {
                canvas.setMode(HOVER_MODE);
            } else {
                canvas.setMode(DEFAULT_MODE);
            }
            if (isInteractive()) {
                canvas.addInteractionRegion(listener, itemRenderer.getTooltip(item), itemRegion);
            }
        } else {
            canvas.setMode(DISABLED_MODE);
        }
        canvas.drawBackground(itemRegion);
        itemRenderer.draw(item, canvas, margin.shrink(itemRegion));
        yOffset += preferredSize.getY();
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) Vector2i(org.terasology.math.geom.Vector2i) Border(org.terasology.math.Border)

Example 27 with Vector2i

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

the class UIText method drawSelection.

/**
 * Draws the selection indication which indicates that a certain part of the text is selected.
 *
 * @param canvas The canvas on which the widget resides
 */
protected void drawSelection(Canvas canvas) {
    Font font = canvas.getCurrentStyle().getFont();
    String currentText = getText();
    int start = Math.min(getCursorPosition(), selectionStart);
    int end = Math.max(getCursorPosition(), selectionStart);
    Color textColor = canvas.getCurrentStyle().getTextColor();
    int canvasWidth = (multiline) ? canvas.size().x : Integer.MAX_VALUE;
    // TODO: Support different text alignments
    List<String> rawLinesAfterCursor = TextLineBuilder.getLines(font, currentText, Integer.MAX_VALUE);
    int currentChar = 0;
    int lineOffset = 0;
    for (int lineIndex = 0; lineIndex < rawLinesAfterCursor.size() && currentChar <= end; ++lineIndex) {
        String line = rawLinesAfterCursor.get(lineIndex);
        List<String> innerLines = TextLineBuilder.getLines(font, line, canvasWidth);
        for (int innerLineIndex = 0; innerLineIndex < innerLines.size() && currentChar <= end; ++innerLineIndex) {
            String innerLine = innerLines.get(innerLineIndex);
            String selectionString;
            int offsetX = 0;
            if (currentChar + innerLine.length() < start) {
                selectionString = "";
            } else if (currentChar < start) {
                offsetX = font.getWidth(innerLine.substring(0, start - currentChar));
                selectionString = innerLine.substring(start - currentChar, Math.min(end - currentChar, innerLine.length()));
            } else if (currentChar + innerLine.length() >= end) {
                selectionString = innerLine.substring(0, end - currentChar);
            } else {
                selectionString = innerLine;
            }
            if (!selectionString.isEmpty()) {
                int selectionWidth = font.getWidth(selectionString);
                Vector2i selectionTopLeft = new Vector2i(offsetX, (lineOffset) * font.getLineHeight());
                Rect2i region = Rect2i.createFromMinAndSize(selectionTopLeft.x, selectionTopLeft.y, selectionWidth, font.getLineHeight());
                canvas.drawTexture(cursorTexture, region, textColor);
                canvas.drawTextRaw(FontUnderline.strip(FontColor.stripColor(selectionString)), font, textColor.inverse(), region);
            }
            currentChar += innerLine.length();
            lineOffset++;
        }
        currentChar++;
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) FontColor(org.terasology.rendering.FontColor) Color(org.terasology.rendering.nui.Color) Vector2i(org.terasology.math.geom.Vector2i) Font(org.terasology.rendering.assets.font.Font)

Example 28 with Vector2i

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

the class UITreeView method getPreferredContentSize.

@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
    canvas.setPart(TREE_NODE);
    if (model.get().getNodeCount() == 0) {
        return new Vector2i();
    }
    model.get().setEnumerateExpandedOnly(false);
    Vector2i result = new Vector2i();
    for (int i = 0; i < model.get().getNodeCount(); i++) {
        Tree<T> node = model.get().getNode(i);
        Vector2i preferredSize = canvas.getCurrentStyle().getMargin().grow(itemRenderer.getPreferredSize(node.getValue(), canvas).addX(node.getDepth() * levelIndent.get()));
        result.x = Math.max(result.x, preferredSize.x);
        result.y += preferredSize.y;
    }
    model.get().setEnumerateExpandedOnly(true);
    // Account for the expand/contract button!
    result.addX(levelIndent.get());
    return result;
}
Also used : Vector2i(org.terasology.math.geom.Vector2i)

Example 29 with Vector2i

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

the class DocumentRenderer method getDocumentPreferredSize.

public static Vector2i getDocumentPreferredSize(DocumentData documentData, Font defaultFont, Color defaultColor, int availableWidth) {
    DefaultDocumentRenderStyle defaultDocumentRenderStyle = new DefaultDocumentRenderStyle(defaultFont, defaultColor);
    DocumentRenderStyle documentRenderStyle = getDocumentRenderStyle(defaultDocumentRenderStyle, documentData);
    Collection<ParagraphData> paragraphs = documentData.getParagraphs();
    int minParagraphsWidth = getParagraphsMinimumWidth(availableWidth, documentRenderStyle, paragraphs);
    int documentSideMargins = documentRenderStyle.getDocumentMarginLeft().getValue(availableWidth) + documentRenderStyle.getDocumentMarginRight().getValue(availableWidth);
    int documentWidth = Math.max(availableWidth, minParagraphsWidth + documentSideMargins);
    ContainerFlowContainerRenderSpace containerRenderSpace = new ContainerFlowContainerRenderSpace(documentWidth);
    int preferredHeight = Math.max(getParagraphsPreferredHeight(documentRenderStyle, paragraphs, containerRenderSpace, 0), containerRenderSpace.getNextClearYPosition(ParagraphRenderStyle.ClearStyle.BOTH));
    int documentVerticalMargins = documentRenderStyle.getDocumentMarginTop().getValue(documentWidth) + documentRenderStyle.getDocumentMarginBottom().getValue(documentWidth);
    // Bring back the document indents to sides
    return new Vector2i(documentWidth, preferredHeight + documentVerticalMargins);
}
Also used : DocumentRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.DocumentRenderStyle) DefaultDocumentRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.DefaultDocumentRenderStyle) FallbackDocumentRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.FallbackDocumentRenderStyle) ParagraphData(org.terasology.rendering.nui.widgets.browser.data.ParagraphData) Vector2i(org.terasology.math.geom.Vector2i) DefaultDocumentRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.DefaultDocumentRenderStyle)

Example 30 with Vector2i

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

the class SolidRasterizer method generateChunk.

@Override
public void generateChunk(CoreChunk chunk, Region chunkRegion) {
    LiquidData waterLiquid = new LiquidData(LiquidType.WATER, LiquidData.MAX_LIQUID_DEPTH);
    DensityFacet solidityFacet = chunkRegion.getFacet(DensityFacet.class);
    SurfaceHeightFacet surfaceFacet = chunkRegion.getFacet(SurfaceHeightFacet.class);
    SurfaceDepthFacet surfaceDepthFacet = chunkRegion.getFacet(SurfaceDepthFacet.class);
    BiomeFacet biomeFacet = chunkRegion.getFacet(BiomeFacet.class);
    SeaLevelFacet seaLevelFacet = chunkRegion.getFacet(SeaLevelFacet.class);
    int seaLevel = seaLevelFacet.getSeaLevel();
    Vector2i pos2d = new Vector2i();
    for (Vector3i pos : ChunkConstants.CHUNK_REGION) {
        pos2d.set(pos.x, pos.z);
        int posY = pos.y + chunk.getChunkWorldOffsetY();
        // Check for an optional depth for this layer - if defined stop generating below that level
        if (surfaceDepthFacet != null && posY < surfaceDepthFacet.get(pos2d)) {
            continue;
        }
        Biome biome = biomeFacet.get(pos2d);
        chunk.setBiome(pos.x, pos.y, pos.z, biome);
        float density = solidityFacet.get(pos);
        if (density >= 32) {
            chunk.setBlock(pos, stone);
        } else if (density >= 0) {
            int depth = TeraMath.floorToInt(surfaceFacet.get(pos2d)) - posY;
            Block block = getSurfaceBlock(depth, posY, biome, seaLevel);
            chunk.setBlock(pos, block);
        } else {
            // fill up terrain up to sealevel height with water or ice
            if (posY == seaLevel && CoreBiome.SNOW == biome) {
                chunk.setBlock(pos, ice);
            } else if (posY <= seaLevel) {
                // either OCEAN or SNOW
                chunk.setBlock(pos, water);
                chunk.setLiquid(pos, waterLiquid);
            }
        }
    }
}
Also used : SurfaceHeightFacet(org.terasology.world.generation.facets.SurfaceHeightFacet) Biome(org.terasology.world.biomes.Biome) CoreBiome(org.terasology.core.world.CoreBiome) DensityFacet(org.terasology.world.generation.facets.DensityFacet) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) SeaLevelFacet(org.terasology.world.generation.facets.SeaLevelFacet) Vector2i(org.terasology.math.geom.Vector2i) BiomeFacet(org.terasology.core.world.generator.facets.BiomeFacet) LiquidData(org.terasology.world.liquid.LiquidData) SurfaceDepthFacet(org.terasology.world.generation.facets.SurfaceDepthFacet)

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