Search in sources :

Example 6 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GTextArea method moveCaretRight.

/**
	 * Move caret right by one character. If necessary move to the start of the next line 
	 * @return true if caret was moved else false
	 */
protected boolean moveCaretRight(TextLayoutHitInfo currPos) {
    TextLayoutInfo ntli;
    TextHitInfo nthi = currPos.tli.layout.getNextRightHit(currPos.thi);
    if (nthi == null) {
        // Move the caret to the start of the next line the previous line 
        if (currPos.tli.lineNo >= stext.getNbrLines() - 1)
            // Can't goto next line because this is the last line
            return false;
        else {
            // Move to start of next line
            ntli = stext.getTLIforLineNo(currPos.tli.lineNo + 1);
            nthi = ntli.layout.getNextLeftHit(1);
            currPos.tli = ntli;
            currPos.thi = nthi;
        }
    } else {
        // Move the caret to the right of current position
        currPos.thi = nthi;
    }
    return true;
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo)

Example 7 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GTextArea method updateBuffer.

/**
	 * Add text to the end of the current text. This is useful for a logging' type activity.
	 * 
	 * @param extraText the text to append
	 */
//	public void appendText(String extraText){
//		appendText(extraText, false);
//	}
/**
	 * If the buffer is invalid then redraw it.
	 */
protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        // Get the latest lines of text
        LinkedList<TextLayoutInfo> lines = stext.getLines(g2d);
        if (lines.isEmpty() && defaultText != null)
            lines = defaultText.getLines(g2d);
        bufferInvalid = false;
        TextLayoutHitInfo startSelTLHI = null, endSelTLHI = null;
        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);
        // Translate in preparation for display selection and text
        if (hasSelection()) {
            if (endTLHI.compareTo(startTLHI) == -1) {
                startSelTLHI = endTLHI;
                endSelTLHI = startTLHI;
            } else {
                startSelTLHI = startTLHI;
                endSelTLHI = endTLHI;
            }
        }
        // Display selection and text
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            // Draw selection if any
            if (hasSelection() && lineInfo.compareTo(startSelTLHI.tli) >= 0 && lineInfo.compareTo(endSelTLHI.tli) <= 0) {
                int ss = 0;
                ss = (lineInfo.compareTo(startSelTLHI.tli) == 0) ? startSelTLHI.thi.getInsertionIndex() : 0;
                int ee = endSelTLHI.thi.getInsertionIndex();
                ee = (lineInfo.compareTo(endSelTLHI.tli) == 0) ? endSelTLHI.thi.getInsertionIndex() : lineInfo.nbrChars - 1;
                g2d.setColor(jpalette[14]);
                Shape selShape = layout.getLogicalHighlightShape(ss, ee);
                g2d.fill(selShape);
            }
            // display 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 : TextLayoutHitInfo(g4p_controls.StyledString.TextLayoutHitInfo) Shape(java.awt.Shape) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 8 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GTextArea method moveCaretDown.

protected boolean moveCaretDown(TextLayoutHitInfo currPos) {
    if (currPos.tli.lineNo == stext.getNbrLines() - 1)
        return false;
    TextLayoutInfo ntli = stext.getTLIforLineNo(currPos.tli.lineNo + 1);
    TextHitInfo nthi = ntli.layout.hitTestChar(caretX, 0);
    currPos.tli = ntli;
    currPos.thi = nthi;
    return true;
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo)

Example 9 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GTextField 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);
        if (lines.isEmpty() && defaultText != null)
            lines = defaultText.getLines(g2d);
        bufferInvalid = false;
        TextLayoutHitInfo startSelTLHI = null, endSelTLHI = null;
        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);
        if (hasSelection()) {
            if (endTLHI.compareTo(startTLHI) == -1) {
                startSelTLHI = endTLHI;
                endSelTLHI = startTLHI;
            } else {
                startSelTLHI = startTLHI;
                endSelTLHI = endTLHI;
            }
        }
        // Display selection and text
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            // Draw selection if any
            if (hasSelection() && lineInfo.compareTo(startSelTLHI.tli) >= 0 && lineInfo.compareTo(endSelTLHI.tli) <= 0) {
                int ss = startSelTLHI.thi.getInsertionIndex();
                int ee = endSelTLHI.thi.getInsertionIndex();
                g2d.setColor(jpalette[14]);
                Shape selShape = layout.getLogicalHighlightShape(ss, ee);
                g2d.fill(selShape);
            }
            // 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 : TextLayoutHitInfo(g4p_controls.StyledString.TextLayoutHitInfo) Shape(java.awt.Shape) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 10 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GToggleControl 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
        if (opaque == true)
            buffer.background(palette[6]);
        else
            buffer.background(buffer.color(255, 0));
        // Calculate text and icon placement
        calcAlignment();
        // If there is an icon draw it
        if (iconW != 0)
            if (selected)
                buffer.image(bicon[1], siX, siY);
            else
                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.getAdvance();
                    tw = (tw > wrapWidth) ? tw - wrapWidth : tw;
                    sx = (wrapWidth - tw) / 2;
                    break;
                case RIGHT:
                    tw = layout.getAdvance();
                    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(g4p_controls.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Aggregations

TextLayoutInfo (g4p_controls.StyledString.TextLayoutInfo)11 Graphics2D (java.awt.Graphics2D)5 TextHitInfo (java.awt.font.TextHitInfo)5 TextLayout (java.awt.font.TextLayout)5 TextLayoutHitInfo (g4p_controls.StyledString.TextLayoutHitInfo)3 Shape (java.awt.Shape)2