Search in sources :

Example 1 with TextLayoutInfo

use of automenta.vivisect.gui.StyledString.TextLayoutInfo in project opennars by opennars.

the class GEditableTextControl method changeText.

// Only executed if text has changed
protected boolean changeText() {
    TextLayoutInfo tli;
    TextHitInfo thi = null, thiRight = null;
    pos += adjust;
    // Force layouts to be updated
    stext.getLines(buffer.g2);
    // Try to get text layout info for the current position
    tli = stext.getTLIforCharNo(pos);
    if (tli == null) {
        // If unable to get a layout for pos then reset everything
        endTLHI = null;
        startTLHI = null;
        ptx = pty = 0;
        caretX = caretY = 0;
        return false;
    }
    // We have a text layout so we can do something
    // First find the position in line
    int posInLine = pos - tli.startCharIndex;
    // Get some hit info so we can see what is happening
    try {
        thiRight = tli.layout.getNextRightHit(posInLine);
    } catch (Exception excp) {
        thiRight = null;
    }
    if (posInLine <= 0) {
        // At start of line
        thi = tli.layout.getNextLeftHit(thiRight);
    } else if (posInLine >= tli.nbrChars) {
        // End of line
        thi = tli.layout.getNextRightHit(tli.nbrChars - 1);
    } else {
        // Character in line;
        thi = tli.layout.getNextLeftHit(thiRight);
    }
    endTLHI.setInfo(tli, thi);
    // Cursor at end of paragraph graphic
    calculateCaretPos(endTLHI);
    // // Is do we have to move cursor to start of next line
    // if(newline) {
    // if(pos >= stext.length()){
    // stext.insertCharacters(pos, " ");
    // stext.getLines(buffer.g2);
    // }
    // moveCaretRight(endTLHI);
    // calculateCaretPos(endTLHI);
    // }
    // // Finish off by ensuring no selection, invalidate buffer etc.
    // startTLHI.copyFrom(endTLHI);
    // }
    bufferInvalid = true;
    return true;
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayoutInfo(automenta.vivisect.gui.StyledString.TextLayoutInfo)

Example 2 with TextLayoutInfo

use of automenta.vivisect.gui.StyledString.TextLayoutInfo in project opennars by opennars.

the class GLabel method updateBuffer.

protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        // Get the latest lines of text
        LinkedList<TextLayoutInfo> lines = stext.getLines(g2d);
        bufferInvalid = false;
        buffer.beginDraw();
        // Back ground colour
        buffer.background(opaque ? palette[6] : palette[2] & 0xFFFFFF);
        // Calculate text and icon placement
        calcAlignment();
        // If there is an icon draw it
        if (iconW != 0)
            buffer.image(bicon[0], siX, siY);
        float wrapWidth = stext.getWrapWidth();
        float sx = 0, tw = 0;
        buffer.translate(stX, stY);
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            switch(textAlignH) {
                case CENTER:
                    tw = layout.getVisibleAdvance();
                    tw = (tw > wrapWidth) ? tw - wrapWidth : tw;
                    sx = (wrapWidth - tw) / 2;
                    break;
                case RIGHT:
                    tw = layout.getVisibleAdvance();
                    tw = (tw > wrapWidth) ? tw - wrapWidth : tw;
                    sx = wrapWidth - tw;
                    break;
                case LEFT:
                case JUSTIFY:
                default:
                    sx = 0;
            }
            // display text
            g2d.setColor(jpalette[2]);
            lineInfo.layout.draw(g2d, sx, 0);
            buffer.translate(0, layout.getDescent() + layout.getLeading());
        }
        buffer.endDraw();
    }
}
Also used : TextLayoutInfo(automenta.vivisect.gui.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 3 with TextLayoutInfo

use of automenta.vivisect.gui.StyledString.TextLayoutInfo in project opennars by opennars.

the class GPassword method updateBuffer.

/**
 * If the buffer is invalid then redraw it.
 * @TODO need to use palette for colours
 */
protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        // Get the latest lines of text
        LinkedList<TextLayoutInfo> lines = stext.getLines(g2d);
        bufferInvalid = false;
        buffer.beginDraw();
        // Whole control surface if opaque
        if (opaque)
            buffer.background(palette[6]);
        else
            buffer.background(buffer.color(255, 0));
        // Now move to top left corner of text display area
        buffer.translate(tx, ty);
        // Typing area surface
        buffer.noStroke();
        buffer.fill(palette[7]);
        buffer.rect(-1, -1, tw + 2, th + 2);
        g2d.setClip(gpTextDisplayArea);
        buffer.translate(-ptx, -pty);
        // Display selection and text
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            // Draw text
            g2d.setColor(jpalette[2]);
            lineInfo.layout.draw(g2d, 0, 0);
            buffer.translate(0, layout.getDescent() + layout.getLeading());
        }
        g2d.setClip(null);
        buffer.endDraw();
    }
}
Also used : TextLayoutInfo(automenta.vivisect.gui.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 4 with TextLayoutInfo

use of automenta.vivisect.gui.StyledString.TextLayoutInfo in project opennars by opennars.

the class GTextArea method insertText.

/**
 * Insert text at the display position specified. <br>
 *
 * The area line number starts at 0 and includes any lines scrolled off the top. So if
 * three lines have been scrolled off the top the first visible line is number 3. <br>
 *
 * No events will be generated and the caret will be moved to the end of any inserted text. <br>
 *
 * @param text the text to insert
 * @param lineNo the area line number
 * @param charNo the character position to insert text in display line
 * @param startWithEOL if true,inserted text will start on newline
 * @param endWithEOL if true, text after inserted text will start on new line
 * @return true if some characters were inserted
 */
public boolean insertText(String text, int lineNo, int charNo, boolean startWithEOL, boolean endWithEOL) {
    if (text != null && text.length() > 0) {
        int pos = stext.getPos(lineNo, charNo);
        int change = stext.insertCharacters(text, lineNo, charNo, startWithEOL, endWithEOL);
        // displayCaretPos("Caret starts at ");
        if (change != 0) {
            LinkedList<TextLayoutInfo> lines = stext.getLines(buffer.g2);
            updateScrollbars(lines.getLast().layout.getVisibleAdvance());
            // Move caret to end of insert if possible
            pos += change;
            TextLayoutHitInfo tlhi = stext.getTLHIforCharPosition(pos);
            if (tlhi != null) {
                endTLHI.copyFrom(tlhi);
                moveCaretLeft(endTLHI);
                startTLHI.copyFrom(endTLHI);
                // displayCaretPos("Caret ends at ");
                calculateCaretPos(tlhi);
                keepCursorInView = true;
                showCaret = true;
            }
            bufferInvalid = true;
            return true;
        }
    }
    return false;
}
Also used : TextLayoutHitInfo(automenta.vivisect.gui.StyledString.TextLayoutHitInfo) TextLayoutInfo(automenta.vivisect.gui.StyledString.TextLayoutInfo)

Example 5 with TextLayoutInfo

use of automenta.vivisect.gui.StyledString.TextLayoutInfo in project opennars by opennars.

the class GTextArea method moveCaretLeft.

/**
 * Move caret left by one character. If necessary move to the end of the line above
 * @return true if caret was moved else false
 */
protected boolean moveCaretLeft(TextLayoutHitInfo currPos) {
    TextLayoutInfo ntli;
    TextHitInfo nthi = currPos.tli.layout.getNextLeftHit(currPos.thi);
    if (nthi == null) {
        // Move the caret to the end of the previous line
        if (currPos.tli.lineNo == 0)
            // Can't goto previous line because this is the first line
            return false;
        else {
            // Move to end of previous line
            ntli = stext.getTLIforLineNo(currPos.tli.lineNo - 1);
            nthi = ntli.layout.getNextRightHit(ntli.nbrChars - 1);
            currPos.tli = ntli;
            currPos.thi = nthi;
        }
    } else {
        // Move the caret to the left of current position
        currPos.thi = nthi;
    }
    return true;
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayoutInfo(automenta.vivisect.gui.StyledString.TextLayoutInfo)

Aggregations

TextLayoutInfo (automenta.vivisect.gui.StyledString.TextLayoutInfo)17 Graphics2D (java.awt.Graphics2D)8 TextHitInfo (java.awt.font.TextHitInfo)6 TextLayout (java.awt.font.TextLayout)6 TextLayoutHitInfo (automenta.vivisect.gui.StyledString.TextLayoutHitInfo)5 Shape (java.awt.Shape)2