Search in sources :

Example 6 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class DocumentRenderer method renderParagraph.

public static int renderParagraph(Canvas canvas, ParagraphRenderable.HyperlinkRegister register, ParagraphRenderStyle baseRenderStyle, int xShift, int leftIndent, int rightIndent, ContainerRenderSpace containerRenderSpace, int containerWidth, int startY, ParagraphData paragraphData) {
    int yShift = startY;
    ParagraphRenderable paragraphContents = paragraphData.getParagraphContents();
    ParagraphRenderStyle paragraphRenderStyle = getParagraphRenderStyle(baseRenderStyle, paragraphData);
    ParagraphRenderStyle.ClearStyle clearStyle = paragraphRenderStyle.getClearStyle();
    if (clearStyle != ParagraphRenderStyle.ClearStyle.NONE) {
        yShift = Math.max(yShift, containerRenderSpace.getNextClearYPosition(clearStyle));
    }
    ParagraphRenderStyle.FloatStyle floatStyle = paragraphRenderStyle.getFloatStyle();
    if (floatStyle == ParagraphRenderStyle.FloatStyle.LEFT || floatStyle == ParagraphRenderStyle.FloatStyle.RIGHT) {
        int leftParagraphIndent = paragraphRenderStyle.getParagraphMarginLeft().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingLeft().getValue(containerWidth);
        int rightParagraphIndent = paragraphRenderStyle.getParagraphMarginRight().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingRight().getValue(containerWidth);
        int paragraphWidth = Math.max(paragraphRenderStyle.getParagraphMinimumWidth().getValue(containerWidth), paragraphContents.getContentsMinWidth(paragraphRenderStyle) + leftParagraphIndent + rightParagraphIndent);
        int height = paragraphRenderStyle.getParagraphMarginTop().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth);
        int paragraphHeight = paragraphContents.getPreferredContentsHeight(paragraphRenderStyle, 0, new ContainerFlowContainerRenderSpace(paragraphWidth), leftParagraphIndent + rightParagraphIndent);
        height += paragraphHeight;
        height += paragraphRenderStyle.getParagraphPaddingBottom().getValue(containerWidth) + paragraphRenderStyle.getParagraphMarginBottom().getValue(containerWidth);
        Rectanglei position;
        if (floatStyle == ParagraphRenderStyle.FloatStyle.LEFT) {
            position = containerRenderSpace.addLeftFloat(yShift, paragraphWidth, height);
        } else {
            position = containerRenderSpace.addRightFloat(yShift, paragraphWidth, height);
        }
        Rectanglei paragraphBorderRegion = new Rectanglei(position.minX + paragraphRenderStyle.getParagraphMarginLeft().getValue(containerWidth), position.minY + paragraphRenderStyle.getParagraphMarginTop().getValue(containerWidth), position.maxX - paragraphRenderStyle.getParagraphMarginRight().getValue(containerWidth) - 1, position.maxY - paragraphRenderStyle.getParagraphMarginBottom().getValue(containerWidth) - 1);
        Color paragraphBackground = paragraphRenderStyle.getParagraphBackground();
        if (paragraphBackground != null) {
            canvas.drawFilledRectangle(paragraphBorderRegion, paragraphBackground);
        }
        Vector2i paragraphStart = new Vector2i(position.minX, position.minY + paragraphRenderStyle.getParagraphMarginTop().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth));
        paragraphContents.renderContents(canvas, paragraphStart, new ContainerFlowContainerRenderSpace(paragraphWidth), leftParagraphIndent, rightParagraphIndent, paragraphRenderStyle, paragraphRenderStyle.getHorizontalAlignment(), register);
        yShift = position.minY;
    } else {
        yShift += paragraphRenderStyle.getParagraphMarginTop().getValue(containerWidth);
        int leftParagraphIndent = paragraphRenderStyle.getParagraphMarginLeft().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingLeft().getValue(containerWidth);
        int rightParagraphIndent = paragraphRenderStyle.getParagraphMarginRight().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingRight().getValue(containerWidth);
        int paragraphHeight = paragraphContents.getPreferredContentsHeight(paragraphRenderStyle, yShift + paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth), containerRenderSpace, leftIndent + leftParagraphIndent + rightParagraphIndent + rightIndent);
        Color paragraphBackground = paragraphRenderStyle.getParagraphBackground();
        if (paragraphBackground != null) {
            int borderAdvance = 0;
            int borderHeight = paragraphHeight + paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingBottom().getValue(containerWidth);
            while (borderAdvance < borderHeight) {
                int backgroundStart = yShift + borderAdvance;
                int availableBackgroundWidth = containerRenderSpace.getWidthForVerticalPosition(backgroundStart);
                int backgroundAdvance = containerRenderSpace.getAdvanceForVerticalPosition(backgroundStart);
                int maxSpace = containerRenderSpace.getNextWidthChange(backgroundStart);
                Rectanglei backgroundRegion = RectUtility.createFromMinAndSize(xShift + paragraphRenderStyle.getParagraphMarginLeft().getValue(containerWidth) + backgroundAdvance, backgroundStart, availableBackgroundWidth - 1, Math.min(maxSpace, borderHeight - borderAdvance) - 1);
                canvas.drawFilledRectangle(backgroundRegion, paragraphBackground);
                borderAdvance += maxSpace - backgroundStart;
            }
        }
        yShift += paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth);
        paragraphContents.renderContents(canvas, new Vector2i(xShift, yShift), containerRenderSpace, leftIndent + leftParagraphIndent, rightIndent + rightParagraphIndent, paragraphRenderStyle, paragraphRenderStyle.getHorizontalAlignment(), register);
        yShift += paragraphHeight;
        yShift += paragraphRenderStyle.getParagraphPaddingBottom().getValue(containerWidth);
        yShift += paragraphRenderStyle.getParagraphMarginBottom().getValue(containerWidth);
    }
    return yShift - startY;
}
Also used : Color(org.terasology.nui.Color) Rectanglei(org.terasology.joml.geom.Rectanglei) Vector2i(org.joml.Vector2i) FallbackParagraphRenderStyle(org.terasology.engine.rendering.nui.widgets.browser.ui.style.FallbackParagraphRenderStyle) ParagraphRenderStyle(org.terasology.engine.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle)

Example 7 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class HTMLLikeParser method parseHTMLLike.

public static Collection<FlowRenderable> parseHTMLLike(String text) {
    if (text == null) {
        return null;
    }
    StringReader reader = new StringReader(text);
    int character;
    try {
        StringBuilder sb = new StringBuilder();
        Font font = null;
        Color color = null;
        String hyperlink = null;
        List<FlowRenderable> result = new LinkedList<>();
        while ((character = reader.read()) != -1) {
            char c = (char) character;
            if (c == '\n') {
                throw new IllegalArgumentException("Parsed text cannot contain line breaks.");
            } else if (c == '<') {
                char nextChar = (char) reader.read();
                if (nextChar == '/') {
                    char id = (char) reader.read();
                    if (id == 'f') {
                        if (sb.length() > 0) {
                            result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                            sb.setLength(0);
                        }
                        font = null;
                    } else if (id == 'c') {
                        if (sb.length() > 0) {
                            result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                            sb.setLength(0);
                        }
                        color = null;
                    } else if (id == 'h') {
                        if (sb.length() > 0) {
                            result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                            sb.setLength(0);
                        }
                        hyperlink = null;
                    } else {
                        throw new IllegalArgumentException("Unable to parse text - " + text);
                    }
                    reader.read();
                } else if (nextChar == 'f') {
                    if (sb.length() > 0) {
                        result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                        sb.setLength(0);
                    }
                    reader.read();
                    font = Assets.getFont(readUntilCharacter(reader, '>')).get();
                } else if (nextChar == 'c') {
                    if (sb.length() > 0) {
                        result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                        sb.setLength(0);
                    }
                    reader.read();
                    color = new Color(Integer.parseInt(readUntilCharacter(reader, '>'), 16));
                } else if (nextChar == 'h') {
                    if (sb.length() > 0) {
                        result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
                        sb.setLength(0);
                    }
                    reader.read();
                    hyperlink = readUntilCharacter(reader, '>');
                } else if (nextChar == 'l') {
                    readUntilCharacter(reader, '>');
                    sb.append('\n');
                }
            } else if (c == '&') {
                String escape = readUntilCharacter(reader, ';');
                if (escape.equals("gt")) {
                    sb.append('>');
                } else if (escape.equals("lt")) {
                    sb.append('<');
                } else if (escape.equals("amp")) {
                    sb.append('&');
                } else {
                    throw new IllegalArgumentException("Unknown escape sequence - " + escape);
                }
            } else {
                sb.append(c);
            }
        }
        if (sb.length() > 0) {
            result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
        }
        return result;
    } catch (IOException exp) {
    // Ignore - can't happen
    }
    return null;
}
Also used : Color(org.terasology.nui.Color) IOException(java.io.IOException) Font(org.terasology.nui.asset.font.Font) LinkedList(java.util.LinkedList) StringReader(java.io.StringReader) FlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable) TextFlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable) TextFlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable)

Example 8 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class HTMLUtils method createParagraphRenderStyleFromCommonAttributes.

public static ParagraphRenderStyle createParagraphRenderStyleFromCommonAttributes(Attributes attributes) {
    Color textColor = getTextColor(attributes);
    ParagraphRenderStyle.FloatStyle floatStyle = getFloatStyle(attributes);
    ParagraphRenderStyle.ClearStyle clearStyle = getClearStyle(attributes);
    HorizontalAlign horizontalAlign = getHorizontalAlign(attributes);
    Color backgroundColor = getBackgroundColor(attributes);
    ContainerInteger paragraphMarginTop = getParagraphMarginTop(attributes);
    ContainerInteger paragraphMarginBottom = getParagraphMarginBottom(attributes);
    ContainerInteger paragraphMarginLeft = getParagraphMarginLeft(attributes);
    ContainerInteger paragraphMarginRight = getParagraphMarginRight(attributes);
    ContainerInteger paragraphPaddingTop = getParagraphPaddingTop(attributes);
    ContainerInteger paragraphPaddingBottom = getParagraphPaddingBottom(attributes);
    ContainerInteger paragraphPaddingLeft = getParagraphPaddingLeft(attributes);
    ContainerInteger paragraphPaddingRight = getParagraphPaddingRight(attributes);
    ContainerInteger minimumWidth = getMinimumWidth(attributes);
    return new ParagraphRenderStyle() {

        @Override
        public Color getColor(boolean hyperlink) {
            return textColor;
        }

        @Override
        public FloatStyle getFloatStyle() {
            return floatStyle;
        }

        @Override
        public ClearStyle getClearStyle() {
            return clearStyle;
        }

        @Override
        public HorizontalAlign getHorizontalAlignment() {
            return horizontalAlign;
        }

        @Override
        public Color getParagraphBackground() {
            return backgroundColor;
        }

        @Override
        public ContainerInteger getParagraphMarginTop() {
            return paragraphMarginTop;
        }

        @Override
        public ContainerInteger getParagraphMarginBottom() {
            return paragraphMarginBottom;
        }

        @Override
        public ContainerInteger getParagraphMarginLeft() {
            return paragraphMarginLeft;
        }

        @Override
        public ContainerInteger getParagraphMarginRight() {
            return paragraphMarginRight;
        }

        @Override
        public ContainerInteger getParagraphPaddingTop() {
            return paragraphPaddingTop;
        }

        @Override
        public ContainerInteger getParagraphPaddingBottom() {
            return paragraphPaddingBottom;
        }

        @Override
        public ContainerInteger getParagraphPaddingLeft() {
            return paragraphPaddingLeft;
        }

        @Override
        public ContainerInteger getParagraphPaddingRight() {
            return paragraphPaddingRight;
        }

        @Override
        public ContainerInteger getParagraphMinimumWidth() {
            return minimumWidth;
        }
    };
}
Also used : PercentageContainerInteger(org.terasology.engine.rendering.nui.widgets.browser.ui.style.PercentageContainerInteger) ContainerInteger(org.terasology.engine.rendering.nui.widgets.browser.ui.style.ContainerInteger) FixedContainerInteger(org.terasology.engine.rendering.nui.widgets.browser.ui.style.FixedContainerInteger) Color(org.terasology.nui.Color) HorizontalAlign(org.terasology.nui.HorizontalAlign) ParagraphRenderStyle(org.terasology.engine.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle)

Example 9 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class NominalFacetLayer method render.

@Override
public void render(BufferedImage img, Region region) {
    ObjectFacet2D<E> facet = region.getFacet(facetClass);
    int width = img.getWidth();
    int height = img.getHeight();
    ColorModel colorModel = img.getColorModel();
    ColorBlender blender = ColorBlenders.forColorModel(ColorModels.RGBA, colorModel);
    DataBufferInt dataBuffer = (DataBufferInt) img.getRaster().getDataBuffer();
    for (int z = 0; z < height; z++) {
        for (int x = 0; x < width; x++) {
            Color src = getColor(facet, x, z);
            if (src != null) {
                int blend = blender.get(src.rgba());
                dataBuffer.setElem(z * width + x, blend);
            }
        }
    }
}
Also used : ColorBlender(org.terasology.engine.world.viewer.color.ColorBlender) ColorModel(java.awt.image.ColorModel) Color(org.terasology.nui.Color) DataBufferInt(java.awt.image.DataBufferInt)

Example 10 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class MeshComponent method copyFrom.

@Override
public void copyFrom(MeshComponent other) {
    this.mesh = other.mesh;
    this.material = other.material;
    this.translucent = other.translucent;
    this.selfLuminance = other.selfLuminance;
    this.hideFromOwner = other.hideFromOwner;
    this.color = new Color(other.color);
}
Also used : Color(org.terasology.nui.Color)

Aggregations

Color (org.terasology.nui.Color)39 Test (org.junit.jupiter.api.Test)9 Vector3f (org.joml.Vector3f)5 ByteBuffer (java.nio.ByteBuffer)3 Mesh (org.terasology.engine.rendering.assets.mesh.Mesh)3 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)3 Colorc (org.terasology.nui.Colorc)3 ColorModel (java.awt.image.ColorModel)2 DataBufferInt (java.awt.image.DataBufferInt)2 Vector2f (org.joml.Vector2f)2 Vector3fc (org.joml.Vector3fc)2 Vector4f (org.joml.Vector4f)2 Test (org.junit.Test)2 ColorConstraint (org.terasology.engine.config.flexible.constraints.ColorConstraint)2 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)2 StandardMeshData (org.terasology.engine.rendering.assets.mesh.StandardMeshData)2 VertexResource (org.terasology.engine.rendering.assets.mesh.resource.VertexResource)2 VertexResourceBuilder (org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder)2 ParagraphRenderStyle (org.terasology.engine.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle)2 FastRandom (org.terasology.engine.utilities.random.FastRandom)2