Search in sources :

Example 6 with Color

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

the class MeshBuilder method addColor.

public MeshBuilder addColor(Color c1, Color... colors) {
    meshData.getColors().add(c1.rf());
    meshData.getColors().add(c1.gf());
    meshData.getColors().add(c1.bf());
    meshData.getColors().add(c1.af());
    for (Color c : colors) {
        meshData.getColors().add(c.rf());
        meshData.getColors().add(c.gf());
        meshData.getColors().add(c.bf());
        meshData.getColors().add(c.af());
    }
    return this;
}
Also used : Color(org.terasology.rendering.nui.Color)

Example 7 with Color

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

the class TooltipLineRenderer method draw.

@Override
public void draw(TooltipLine value, Canvas canvas) {
    Font font = getFont(value);
    Color color = getColor(value);
    canvas.drawTextRaw(value.getText(), font, color, canvas.getRegion());
}
Also used : Color(org.terasology.rendering.nui.Color) Font(org.terasology.rendering.assets.font.Font)

Example 8 with Color

use of org.terasology.rendering.nui.Color 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 9 with Color

use of org.terasology.rendering.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.rendering.nui.Color) IOException(java.io.IOException) Font(org.terasology.rendering.assets.font.Font) LinkedList(java.util.LinkedList) StringReader(java.io.StringReader) TextFlowRenderable(org.terasology.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable) FlowRenderable(org.terasology.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable) TextFlowRenderable(org.terasology.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable)

Example 10 with Color

use of org.terasology.rendering.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.rendering.nui.widgets.browser.ui.style.PercentageContainerInteger) ContainerInteger(org.terasology.rendering.nui.widgets.browser.ui.style.ContainerInteger) FixedContainerInteger(org.terasology.rendering.nui.widgets.browser.ui.style.FixedContainerInteger) Color(org.terasology.rendering.nui.Color) HorizontalAlign(org.terasology.rendering.nui.HorizontalAlign) ParagraphRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle)

Aggregations

Color (org.terasology.rendering.nui.Color)30 Test (org.junit.Test)9 ColorModel (java.awt.image.ColorModel)3 DataBufferInt (java.awt.image.DataBufferInt)3 ResourceUrn (org.terasology.assets.ResourceUrn)3 Rect2i (org.terasology.math.geom.Rect2i)3 Vector2i (org.terasology.math.geom.Vector2i)3 Font (org.terasology.rendering.assets.font.Font)3 Map (java.util.Map)2 EntityRef (org.terasology.entitySystem.entity.EntityRef)2 PersistedData (org.terasology.persistence.typeHandling.PersistedData)2 ColorBlender (org.terasology.world.viewer.color.ColorBlender)2 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 Graphics2D (java.awt.Graphics2D)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ByteBuffer (java.nio.ByteBuffer)1