Esempio n. 1
0
s32 CGUIEditBox::getCursorPos(s32 x, s32 y)
{
    IGUIFont* font = OverrideFont;
    IGUISkin* skin = Environment->getSkin();
    if (!OverrideFont)
        font = skin->getFont();

    const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;

    core::stringw *txtLine=0;
    s32 startPos=0;
    x+=3;

    for (u32 i=0; i < lineCount; ++i)
    {
        setTextRect(i);
        if (i == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
            y = CurrentTextRect.UpperLeftCorner.Y;
        if (i == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y )
            y = CurrentTextRect.LowerRightCorner.Y;

        // is it inside this region?
        if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
        {
            // we've found the clicked line
            txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
            startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
            break;
        }
    }

    if (x < CurrentTextRect.UpperLeftCorner.X)
        x = CurrentTextRect.UpperLeftCorner.X;

    s32 idx = txtLine ? font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X) : -1;

    // click was on or left of the line
    if (idx != -1)
        return idx + startPos;

    // click was off the right edge of the line, go to end.
    return txtLine->size() + startPos;
}