protected override void DrawCaret(ICaret caret, Color color, ITextComponentWrapper text, Vector2 offset)
 {
     using (var helper = new VertexHelper())
     {
         caret.Draw(CalculateCaretDrawRect(text, offset, caret.GetIndex() - _drawStart), color, helper);
     }
 }
 public SingleLineInputField(ICaret caret,
                             IInputEventProcessor inputEventProcessor,
                             IInputFieldController controller,
                             ITextComponentWrapper editableText
                             ) : base(caret, inputEventProcessor, controller, editableText)
 {
 }
 public MultiLineInputFieldImpl(
     ICaret caret,
     IInputEventProcessor inputEventProcessor,
     IInputFieldController controller,
     ITextComponentWrapper text)
     : base(caret, inputEventProcessor, controller, text)
 {
 }
        protected override void DrawSelection(ICaret caret, Color color, ITextComponentWrapper text, Vector2 offset)
        {
            int index          = LocalIndex();
            int selectionIndex = LocalSelectionIndex(text);

            using (var helper = new VertexHelper())
            {
                Caret.Draw(CalculateCaretDrawRect(text, offset, index, selectionIndex), color, helper);
            }
        }
        private Rect HightedLineRect(int startIndex, int endIndex, float height, ITextComponentWrapper text)
        {
            Vector2 start = text.CursorPositionAt(startIndex);
            Vector2 end   = text.CursorPositionAt(endIndex);
            // FIXME: add last char width according to caret moving direction.
            Rect rect = new Rect(start.x, start.y - height,
                                 Mathf.Abs(end.x + text.CharWidth(endIndex + _drawStart) - start.x), height);

            return(rect);
        }
Ejemplo n.º 6
0
 public AbstractInputField(ICaret caret,
                           IInputEventProcessor inputEventProcessor,
                           IInputFieldController controller,
                           ITextComponentWrapper text)
 {
     _editableText        = text;
     _caret               = caret;
     _inputEventProcessor = inputEventProcessor;
     _controller          = controller;
 }
Ejemplo n.º 7
0
 public virtual void DrawCaretOrSelection(ITextComponentWrapper text)
 {
     if (Caret.GetIndex() != Caret.GetSelectionIndex())
     {
         DrawSelection(Caret, SelectionColor, text, text.CaretOffset());
     }
     else
     {
         DrawCaret(Caret, DefaultColor, text, text.CaretOffset());
     }
 }
        protected override void DrawSelection(ICaret caret, Color color, ITextComponentWrapper text, Vector2 offset)
        {
            var lines = CalculateSelectionRects(caret, text, offset);

            using (var helper = new VertexHelper())
            {
                foreach (var rect in lines)
                {
                    caret.Draw(rect, color, helper);
                }
            }
        }
        private Rect CalculateCaretDrawRect(
            ITextComponentWrapper text,
            Vector2 offset,
            int relativeIndex)
        {
            Vector2 localCursorPos = text.CursorPositionAt(relativeIndex);
            int     line           = text.GetLineByCharIndex(relativeIndex);
            float   height         = text.LineHeight(line);

            return(new Rect(localCursorPos.x + offset.x,
                            localCursorPos.y - height + offset.y, 1,
                            height));
        }
        protected override string ProcessText(ITextComponentWrapper editableText, ICaret caret)
        {
            // truncate text to display within the bounds of text rect.
            Vector2 textRectExtents = editableText.DisplayRect.size;

            float width = 0;

            if (caret.GetIndex() > _drawEnd || (caret.GetIndex() == TextValue.Length && _drawStart > 0))
            {
                _drawEnd   = caret.GetIndex();
                _drawStart = _drawEnd - 1;
                while (width < textRectExtents.x && _drawStart >= 0)
                {
                    width += editableText.CharWidth(_drawStart--);
                }

                if (width >= textRectExtents.x)
                {
                    _drawStart++;
                }

                _drawStart++;
            }
            else
            {
                if (caret.GetIndex() < _drawStart)
                {
                    _drawStart = caret.GetIndex();
                }

                _drawEnd = _drawStart;
                while (width < textRectExtents.x && _drawEnd < TextValue.Length)
                {
                    width += editableText.CharWidth(_drawEnd++);
                }

                if (width >= textRectExtents.x)
                {
                    _drawEnd--;
                }
            }

            _drawStart = Mathf.Clamp(_drawStart, 0, TextValue.Length);
            _drawEnd   = Mathf.Clamp(_drawEnd, 0, TextValue.Length);
            return(TextValue.Substring(_drawStart, _drawEnd - _drawStart));
        }
Ejemplo n.º 11
0
        private void initialize()
        {
            _editableText = new EditableText(_textComponent, new TextGenerator());

            var caret = CreateCaret();

            if (_lineType.Equals(LineType.SingleLine))
            {
                _impl = new VrInputFieldImpl(caret,
                                             new SingleLineInputProcessor(new System.Text.StringBuilder(), new CaretNavigator(caret, _editableText, this)),
                                             this,
                                             _editableText);
            }
            else
            {
                _impl = new MultiLineInputFieldImpl(caret,
                                                    new BaseTextProcessor(new System.Text.StringBuilder(), new CaretNavigator(caret, _editableText, this)),
                                                    this,
                                                    _editableText);
            }
        }
Ejemplo n.º 12
0
        protected override void Start()
        {
            base.Start();
            _editableText = new EditableText(_textComponent, new TextGenerator());

            var caret = CreateCaret();

            if (_lineType.Equals(LineType.SingleLine))
            {
                Impl = new SingleLineInputField(caret,
                                                new UnityTextProcessor(new System.Text.StringBuilder(), new CaretNavigator(caret, _editableText, this)),
                                                this,
                                                _editableText);
            }
            else
            {
                Impl = new MultiLineInputField(caret,
                                               new UnityTextProcessor(new System.Text.StringBuilder(), new CaretNavigator(caret, _editableText, this)),
                                               this,
                                               _editableText);
            }
        }
        private Rect CalculateCaretDrawRect(
            ITextComponentWrapper text,
            Vector2 offset,
            int index,
            int selectionIndex)
        {
            Vector2 localCursorPos    = text.CursorPositionAt(index);
            Vector2 localSelectionPos = text.CursorPositionAt(selectionIndex);
            float   height            = text.LineHeight(0);

            if (index > selectionIndex)
            {
                Vector2 temp = localSelectionPos;
                localSelectionPos = localCursorPos;
                localCursorPos    = temp;
            }

            return(new Rect(localCursorPos.x + offset.x,
                            localCursorPos.y - height + offset.y,
                            index != selectionIndex ? Mathf.Abs(localSelectionPos.x - localCursorPos.x) : 1,
                            height));
        }
        private List <Rect> CalculateSelectionRects(ICaret caret, ITextComponentWrapper text, Vector2 offset)
        {
            List <Rect> highlightRects = new List <Rect>();

            int start = Mathf.Min(caret.GetIndex(), caret.GetSelectionIndex()) - _drawStart;

            start = Mathf.Clamp(start, 0, text.DisplayedTextLength);
            int end = Mathf.Max(caret.GetIndex(), caret.GetSelectionIndex()) - _drawStart;

            end = Mathf.Clamp(end, 0, text.DisplayedTextLength);

            int startLine = text.GetLineByCharIndex(start);
            int endLine   = text.GetLineByCharIndex(end);

            if (startLine == endLine)
            {
                highlightRects.Add(HightedLineRect(start, end, text.LineHeight(startLine), text));
            }
            else
            {
                int currentLineEndIndex = text.LineEndIndex(startLine);
                highlightRects.Add(HightedLineRect(start, currentLineEndIndex, text.LineHeight(startLine), text));

                while (startLine < endLine - 1)
                {
                    startLine++;
                    highlightRects.Add(HightedLineRect(
                                           text.LineStartIndex(startLine),
                                           text.LineEndIndex(startLine),
                                           text.LineHeight(startLine),
                                           text));
                }

                highlightRects.Add(HightedLineRect(
                                       text.LineStartIndex(endLine), end, text.LineHeight(endLine), text));
            }
            return(highlightRects);
        }
        protected override string ProcessText(ITextComponentWrapper text, ICaret caret)
        {
            Vector2 extents = text.DisplayRect.size;

            int caretLine = text.GetLineByCharIndex(caret.GetIndex());

            if (caret.GetIndex() > _drawEnd)
            {
                _drawEnd = text.LineEndIndex(caretLine);
                float bottomY = text.LineTop(caretLine) - text.LineHeight(caretLine);
                // TODO: Remove interline spacing on last line.
                int startLine = caretLine;
                while (startLine > 0)
                {
                    float topY = text.LineTop(startLine - 1);
                    if (topY - bottomY > extents.y)
                    {
                        break;
                    }
                    startLine--;
                }
                _drawStart = text.LineStartIndex(startLine);
            }
            else
            {
                if (caret.GetIndex() < _drawStart)
                {
                    _drawStart = text.LineStartIndex(caretLine);
                }

                int startLine = text.GetLineByCharIndex(_drawStart);
                int endLine   = startLine;

                float topY    = text.LineTop(startLine);
                float bottomY = text.LineTop(endLine) - text.LineHeight(endLine);

                // TODO: Remove interline spacing on last line.

                while (endLine < text.LineCount - 1)
                {
                    bottomY = text.LineTop(endLine + 1) - text.LineHeight(endLine + 1);
                    // TODO: Remove interline spacing on last line.
                    if (topY - bottomY > extents.y)
                    {
                        break;
                    }
                    ++endLine;
                }

                _drawEnd = text.LineEndIndex(endLine);

                while (startLine > 0)
                {
                    topY = text.LineTop(startLine - 1);
                    if (topY - bottomY > extents.y)
                    {
                        break;
                    }

                    startLine--;
                }

                _drawStart = text.LineStartIndex(startLine);
            }
            return(TextValue.Substring(_drawStart, _drawEnd - _drawStart));
        }
 private int LocalSelectionIndex(ITextComponentWrapper text)
 {
     // selection index could be out of bounds.
     return(Mathf.Clamp(Caret.GetSelectionIndex() - _drawStart, 0, text.DisplayedTextLength));
 }
Ejemplo n.º 17
0
 protected abstract void DrawCaret(ICaret caret, Color color, ITextComponentWrapper text, Vector2 offset);
Ejemplo n.º 18
0
 public CaretNavigator(ICaret caret, ITextComponentWrapper text, IVrInputField inputField)
 {
     _caret       = caret;
     _textWrapper = text;
     _inputField  = inputField;
 }
Ejemplo n.º 19
0
 protected abstract string ProcessText(ITextComponentWrapper text, ICaret caret);