use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GTextArea method updateBuffer.
/**
* 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() && promptText != null)
lines = promptText.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();
}
}
use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GTextArea method moveCaretTo.
/**
* Move the insertion point (caret) to the specified line and character. If the position is invalid
* then the caret is not moved. The text will be scrolled so that the caret position is visible.
*
* @param lineNo the line number (starts at 0)
* @param charNo the character position on the line (starts at 0)
*/
public void moveCaretTo(int lineNo, int charNo) {
try {
TextLayoutHitInfo tlhi = stext.getTLHIforCharPosition(lineNo, charNo);
if (tlhi != null) {
startTLHI.copyFrom(tlhi);
endTLHI.copyFrom(tlhi);
calculateCaretPos(tlhi);
keepCursorInView = true;
showCaret = true;
}
} catch (Exception e) {
}
}
use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GTextField method mouseEvent.
public void mouseEvent(MouseEvent event) {
if (!visible || !enabled || !available)
return;
calcTransformedOrigin(winApp.getCursorX(), winApp.getCursorY());
// Remove translation
ox -= tx;
// Remove translation
oy -= ty;
currSpot = whichHotSpot(ox, oy);
if (currSpot == 1 || focusIsWith == this)
cursorIsOver = this;
else if (cursorIsOver == this)
cursorIsOver = null;
switch(event.getAction()) {
case MouseEvent.PRESS:
if (currSpot == 1) {
if (focusIsWith != this && z >= focusObjectZ()) {
keepCursorInView = true;
takeFocus();
}
dragging = false;
if (stext == null || stext.length() == 0) {
stext = new StyledString(" ", wrapWidth);
stext.getLines(buffer.g2);
}
endTLHI = stext.calculateFromXY(buffer.g2, ox + ptx, oy + pty);
startTLHI = new TextLayoutHitInfo(endTLHI);
calculateCaretPos(endTLHI);
bufferInvalid = true;
} else {
// Not over this control so if we have focus loose it
if (focusIsWith == this)
loseFocus(null);
}
break;
case MouseEvent.RELEASE:
dragging = false;
bufferInvalid = true;
break;
case MouseEvent.DRAG:
if (focusIsWith == this) {
keepCursorInView = true;
dragging = true;
endTLHI = stext.calculateFromXY(buffer.g2, ox + ptx, oy + pty);
calculateCaretPos(endTLHI);
fireEvent(this, GEvent.SELECTION_CHANGED);
bufferInvalid = true;
}
break;
}
}
use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GEditableTextControl method loadText.
/**
* Load the styled string to be used by this control. <br>
* It will also restore any text selection saved with the text.
*
* @param fname the name of the file to use
* @return true if loaded successfully else false
*/
public boolean loadText(String fname) {
StyledString ss = StyledString.load(winApp, fname);
if (ss == null)
return false;
setStyledText(ss);
// Now restore any text selection
if (stext.startIdx >= 0) {
// we have a selection
// Selection starts at ...
startTLHI = new TextLayoutHitInfo();
startTLHI.tli = stext.getTLIforCharNo(stext.startIdx);
int pInLayout = stext.startIdx - startTLHI.tli.startCharIndex;
if (pInLayout == 0)
startTLHI.thi = startTLHI.tli.layout.getNextLeftHit(1);
else
startTLHI.thi = startTLHI.tli.layout.getNextRightHit(pInLayout - 1);
// Selection ends at ...
endTLHI = new TextLayoutHitInfo();
endTLHI.tli = stext.getTLIforCharNo(stext.endIdx);
pInLayout = stext.endIdx - endTLHI.tli.startCharIndex;
if (pInLayout == 0)
endTLHI.thi = endTLHI.tli.layout.getNextLeftHit(1);
else
endTLHI.thi = endTLHI.tli.layout.getNextRightHit(pInLayout - 1);
calculateCaretPos(endTLHI);
}
bufferInvalid = true;
return true;
}
use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GEditableTextControl method setFocus.
/**
* Determines whether this component is to have focus or not. <br>
*/
public void setFocus(boolean focus) {
if (!focus) {
loseFocus(null);
return;
}
// Make sure we have some text
if (focusIsWith != this) {
dragging = false;
if (stext == null || stext.length() == 0)
stext = new StyledString(" ", wrapWidth);
LinkedList<TextLayoutInfo> lines = stext.getLines(buffer.g2);
startTLHI = new TextLayoutHitInfo(lines.getFirst(), null);
startTLHI.thi = startTLHI.tli.layout.getNextLeftHit(1);
endTLHI = new TextLayoutHitInfo(lines.getLast(), null);
int lastChar = endTLHI.tli.layout.getCharacterCount();
endTLHI.thi = startTLHI.tli.layout.getNextRightHit(lastChar - 1);
calculateCaretPos(endTLHI);
bufferInvalid = true;
}
keepCursorInView = true;
takeFocus();
}
Aggregations