Clicking on a text node char

Is it possible to know exactly what character has been clicked inside a text node? Can we know, for instance, the offset of the character that was under the mouse and it’s bounding box inside the text node?

Well, it’s not easy, but you can get the point of intersection in 2-d coordinates, and from that you can determine which row and column was clicked. If nothing else, you can iterate through all of the rows and columsn and call textNode.get_xpos(r, c) and textNode.get_ypos(r, c) until you find the closest match.

But if you use a little smarts, you can probably come up with a better system than that.


I can’t find a get_xpos or get_ypos in the TextNode class reference. Maybe this method has changed or renamed recently?

You’re right, my mistake. These are methods of TextAssembler, which the TextNode uses internally to lay out its text. I don’t know why TextAssembler doesn’t appear in the generated docs, but here’s a quick example of its use.

tn = TextNode('tn')
ta = TextAssembler(tn)
for ri in range(ta.getNumRows()):
  for ci in range(ta.getNumCols(ri)):
    print "ri = %s, ci = %s, char = %s, pos = %s, %s" % (ri, ci, chr(ta.getCharacter(ri, ci)), ta.getXpos(ri, ci), ta.getYpos(ri, ci))