GetCharacterIndexAtPosition
Introduction
The GetCharacterIndexAtPosition method returns the character index under the cursor.
The returned index is the index into a string obtained by combining all strings in WrappedText, so additional logic is needed to find the character if the text is not all displayed in one line.
Every backend counts a CRLF line break as one character, so WrappedText never contains the \r. If your own string uses CRLF line breaks, the index drifts by one character per line break when mapped back into it.
This method can also be used on text which includes BBCode, returning an index after BBCode has been removed.
The position is in the same units the text is drawn in, so the method accounts for FontScale.
To find which BBCode tag is under that index, pass it to GetTagAtIndex. See Combining With GetTagAtIndex.
Code Example: Printing Character at Position
The following code shows how to use the Gum cursor to hover over a TextRuntime and display the character under the cursor.
// Class scope
TextRuntime textRuntime;
// Initialize
textRuntime = new TextRuntime();
textRuntime.AddToRoot();
textRuntime.Text =
"I am a text runtime which should line wrap because my " +
"width is set to an absolute 200 units";
textRuntime.Width = 200;
textRuntime.WidthUnits = Gum.DataTypes.DimensionUnitType.Absolute;
textRuntime.Anchor(Anchor.Center);
textRuntime.HorizontalAlignment = HorizontalAlignment.Left;
textRuntime.VerticalAlignment = VerticalAlignment.Top;
// Update
GumUI.Update(gameTime);
var cursor = GumUI.Cursor;
var x = cursor.XRespectingGumZoomAndBounds();
var y = cursor.YRespectingGumZoomAndBounds();
if(textRuntime.HasCursorOver(GumUI.Cursor))
{
var innerText = (RenderingLibrary.Graphics.Text)textRuntime.RenderableComponent;
int characterIndex = innerText.GetCharacterIndexAtPosition(x, y);
if(characterIndex != -1)
{
int indexLeft = characterIndex;
// We need to loop through the wrapped text to see which line we're on:
foreach(var line in textRuntime.WrappedText)
{
if(line.Length > indexLeft)
{
Debug.WriteLine($"At character {characterIndex}");
Debug.WriteLine($"Character is {line[indexLeft]}");
break;
}
else
{
indexLeft -= line.Length;
}
}
}
}
Combining With GetTagAtIndex
TextRuntime.GetTagAtIndex(tagName, index) returns the BBCode tag with the given name that contains a character index, or null if there is none. It uses the same index GetCharacterIndexAtPosition returns, so the two combine to find the tag under the cursor. This is how a Link tag becomes clickable:
GetCharacterIndexAtPosition returns the nearest caret position, not the character under the point. A point past the end of the text returns the text length, and a point on the right half of a character returns the index after it. For a tag, that means the right half of its last letter gives an index equal to EndIndex, and GetTagAtIndex returns null there because EndIndex is exclusive. Check that the cursor is over the text first, as the sample does, and expect a click on the right half of a link's last letter to miss.
Last updated
Was this helpful?

