Exemple #1
0
        public override void Update(Universe universe, Vector2 origin, AvatarController avatar, List <ScanCode> input, bool ctrl, bool shift, IReadOnlyList <InterfaceLogicalButton> inputPressed,
                                    Vector2 mouseLocation, bool click, bool clickHold)
        {
            base.Update(universe, origin, avatar, input, ctrl, shift, inputPressed, mouseLocation, click, clickHold);
            var inside = Helpers.VectorContains(origin, origin + GetSize(), mouseLocation);

            if (click)
            {
                _selected = inside;
            }

            if (!_selected)
            {
                _textBlock.RemoveCaret();
                return;
            }

            if ((DateTime.Now - _lastTick).Milliseconds > 500)
            {
                if (!_textBlock.HasCaret())
                {
                    _textBlock.SetCaret();
                }
                _textBlock.ToggleCaret();
                _lastTick = DateTime.Now;
            }

            if (!input.Any())
            {
                return;
            }

            if (ctrl)
            {
                if (input.Contains(ScanCode.V))
                {
                    try {
                        ForceSetValue(GetValue() + Helpers.GetClipboardText(), true);
                    } catch {
                        // ignore
                    }
                    return;
                }
            }

            if (input.Any())
            {
                var direction = input.GetDirectionKey();
                var key       = input.GetPressedKey();

                if (key == null && direction == null)
                {
                    if (input.Contains(ScanCode.Space))
                    {
                        key = ' ';
                    }
                    else if (input.Contains(ScanCode.Delete) || input.Contains(ScanCode.Backspace))
                    {
                    }
                    else
                    {
                        return;
                    }
                }

                if (direction != null)
                {
                    if (direction == ScanCode.Left)
                    {
                        if (_cursorIndex > 0)
                        {
                            _cursorIndex--;
                            _textBlock.SetCaretIndex((uint)_cursorIndex);
                        }

                        return;
                    }

                    if (direction == ScanCode.Right)
                    {
                        if (_cursorIndex < GetValue().Length)
                        {
                            _cursorIndex++;
                            _textBlock.SetCaretIndex((uint)_cursorIndex);
                        }

                        return;
                    }
                }

                var current = GetValue();

                if (input.Contains(ScanCode.Backspace))
                {
                    if (current.Length == 0)
                    {
                        return;
                    }
                    if (_cursorIndex != 0 && _cursorIndex != current.Length)
                    {
                        SetValue(current.Substring(0, _cursorIndex - 1) + current.Substring(_cursorIndex), true);
                        _cursorIndex--;
                        if (_cursorIndex < 0)
                        {
                            _cursorIndex = 0;
                        }
                        if (_cursorIndex >= current.Length)
                        {
                            _cursorIndex = GetValue().Length;
                        }
                        _textBlock.SetCaretIndex((uint)_cursorIndex);
                    }
                    else if (_cursorIndex >= current.Length)
                    {
                        SetValue(current.Substring(0, current.Length - 1), true);
                        _cursorIndex--;
                        if (_cursorIndex < 0)
                        {
                            _cursorIndex = 0;
                        }
                        if (_cursorIndex >= current.Length)
                        {
                            _cursorIndex = GetValue().Length;
                        }
                        _textBlock.SetCaretIndex((uint)_cursorIndex);
                    }

                    return;
                }

                if (input.Contains(ScanCode.Delete))
                {
                    if (_cursorIndex != current.Length && current.Length != 0)
                    {
                        SetValue(current.Substring(0, _cursorIndex) + (_cursorIndex == current.Length ? "" : current.Substring(_cursorIndex + 1)), true);
                    }

                    return;
                }

                if (current.Length >= _limit)
                {
                    return;
                }

                if (key != null)
                {
                    var canInput = true;

                    if (InputCheck != null)
                    {
                        canInput = InputCheck.Invoke(key.Value);
                    }

                    if (canInput)
                    {
                        if (!shift)
                        {
                            key = key.ToString().ToLower()[0];
                        }
                        SetValue(current.Substring(0, _cursorIndex <= 0 ? 0 : _cursorIndex) + key + (_cursorIndex >= current.Length ? "" : current.Substring(_cursorIndex)), true);
                        _cursorIndex++;
                        if (_cursorIndex >= GetValue().Length)
                        {
                            _cursorIndex = GetValue().Length;
                        }
                        _textBlock.SetCaretIndex((uint)_cursorIndex);
                    }
                }
            }
        }