Search in sources :

Example 1 with FlowRenderable

use of org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable 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 2 with FlowRenderable

use of org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable in project Terasology by MovingBlocks.

the class FlowParagraphRenderable method renderContents.

@Override
public void renderContents(Canvas canvas, Vector2i startPos, ContainerRenderSpace containerRenderSpace, int leftIndent, int rightIndent, ParagraphRenderStyle defaultStyle, HorizontalAlign horizontalAlign, HyperlinkRegister hyperlinkRegister) {
    int y = startPos.y;
    for (LaidFlowLine<FlowRenderable> line : updateCacheIfNeeded(defaultStyle, startPos.y, containerRenderSpace)) {
        int x = 0;
        int insetXAdvance = containerRenderSpace.getAdvanceForVerticalPosition(y);
        int availableWidth = containerRenderSpace.getWidthForVerticalPosition(y);
        if (horizontalAlign == HorizontalAlign.LEFT || horizontalAlign == HorizontalAlign.CENTER) {
            availableWidth -= leftIndent;
        }
        if (horizontalAlign == HorizontalAlign.RIGHT || horizontalAlign == HorizontalAlign.CENTER) {
            availableWidth -= rightIndent;
        }
        int lineHeight = line.getHeight();
        int lineWidth = line.getWidth();
        int alignOffset = horizontalAlign.getOffset(lineWidth, availableWidth);
        for (FlowRenderable flowRenderable : line.getFlowRenderables()) {
            int elementWidth = flowRenderable.getWidth(defaultStyle);
            Rectanglei renderableRegion = RectUtility.createFromMinAndSize(insetXAdvance + leftIndent + alignOffset + startPos.x + x, y, elementWidth, lineHeight);
            String hyperlink = flowRenderable.getAction();
            if (hyperlink != null) {
                hyperlinkRegister.registerHyperlink(renderableRegion, hyperlink);
            }
            flowRenderable.render(canvas, renderableRegion, defaultStyle);
            x += elementWidth;
        }
        y += lineHeight;
    }
}
Also used : FlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable) Rectanglei(org.terasology.joml.geom.Rectanglei)

Aggregations

FlowRenderable (org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 LinkedList (java.util.LinkedList)1 TextFlowRenderable (org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.TextFlowRenderable)1 Rectanglei (org.terasology.joml.geom.Rectanglei)1 Color (org.terasology.nui.Color)1 Font (org.terasology.nui.asset.font.Font)1