Ejemplo n.º 1
0
        /// <summary>
        /// 通过本地坐标获得字符索引位置
        /// </summary>
        /// <param name="location">本地坐标</param>
        /// <returns></returns>
        TextField.CharPosition GetCharPosition(Vector2 location)
        {
            TextField.CharPosition result = new TextField.CharPosition();
            if (textField.charPositions.Count == 0)
            {
                result.lineIndex = 0;
                result.caretIndex = 0;
                return result;
            }

            location.x -= textField.x;
            location.y -= textField.y;

            List<TextField.LineInfo> lines = textField.lines;
            int len = lines.Count;
            TextField.LineInfo line;
            int i;
            for (i = 0; i < len; i++)
            {
                line = lines[i];
                if (line.y + line.height > location.y)
                    break;
            }
            if (i == len)
                i = len - 1;

            result.lineIndex = (short)i;

            len = textField.charPositions.Count;
            TextField.CharPosition v;
            int firstInLine = -1;
            for (i = 0; i < len; i++)
            {
                v = textField.charPositions[i];
                if (v.lineIndex == result.lineIndex)
                {
                    if (firstInLine == -1)
                        firstInLine = i;
                    if (v.offsetX > location.x)
                    {
                        if (i > firstInLine)
                        {
                            //最后一个字符有点难点
                            if (v.offsetX - location.x < 2)
                                result.caretIndex = i;
                            else
                                result.caretIndex = i - 1;
                        }
                        else
                            result.caretIndex = firstInLine;
                        return result;
                    }
                }
                else if (firstInLine != -1)
                    break;
            }
            result.caretIndex = i - 1;
            return result;
        }
Ejemplo n.º 2
0
 TextField.CharPosition GetCharPosition(int caretIndex)
 {
     if (caretIndex < textField.charPositions.Count)
         return textField.charPositions[caretIndex];
     else
     {
         TextField.CharPosition cp = new TextField.CharPosition();
         cp.caretIndex = caretIndex;
         cp.lineIndex = (short)Math.Max(0, textField.lines.Count - 1);
         return cp;
     }
 }