/// <summary> /// Moves the caret in the direction indicated by the vector. The caret will automatically /// wrap to the last/next line if movement in the Y direction would result in the index /// going out of range. /// </summary> /// <param name="dir">Index direction vector</param> public void Move(Vector2I dir, bool navigate = false) { Vector2I newIndex, min = new Vector2I(0, -1); if (dir.Y < 0 && Index == min) { dir.Y = 0; } bool moveLeft = dir.Y < 0, moveRight = dir.Y > 0, prepending = Index.Y == -1, startPrepend = moveLeft && Index.Y == 0; if (startPrepend || (dir.Y == 0 && prepending)) { newIndex = Index + new Vector2I(dir.X, 0); newIndex.Y = -1; newIndex = ClampIndex(newIndex); caretOffset = GetOffsetFromIndex(new Vector2I(newIndex.X, 0)); } else { int newOffset = Math.Max(caretOffset + dir.Y, 0); // Stop prepending if ((prepending && moveRight) && (Index.X > 0 || text[0].Count > 1)) { newOffset -= 1; } newIndex = GetIndexFromOffset(newOffset) + new Vector2I(dir.X, 0); newIndex = ClampIndex(newIndex); caretOffset = GetOffsetFromIndex(newIndex); if (navigate && moveRight && newIndex.X > Index.X) { newIndex.Y = -1; } } Index = ClampIndex(newIndex); caretMoved = true; if (Index.Y >= 0) { text.MoveToChar(Index); } else { text.MoveToChar(Index + new Vector2I(0, 1)); } blink = true; blinkTimer.Restart(); }
protected override void HandleInput(Vector2 cursorPos) { ITextBoard textBoard = textBox.TextBoard; IMouseInput vertControl = verticalScroll.slide.MouseInput; verticalScroll.Max = Math.Max(0f, textBoard.TextSize.Y - textBoard.Size.Y); // Update the ScrollBar positions to represent the current offset unless they're being clicked. if (!vertControl.IsLeftClicked) { if (IsMousedOver || verticalScroll.IsMousedOver) { Vector2I lineRange = textBoard.VisibleLineRange; if (SharedBinds.MousewheelUp.IsPressed || SharedBinds.UpArrow.IsNewPressed || SharedBinds.UpArrow.IsPressedAndHeld) { textBoard.MoveToChar(new Vector2I(lineRange.X - 1, 0)); } else if (SharedBinds.MousewheelDown.IsPressed || SharedBinds.DownArrow.IsNewPressed || SharedBinds.DownArrow.IsPressedAndHeld) { textBoard.MoveToChar(new Vector2I(lineRange.Y + 1, 0)); } else if (SharedBinds.PageUp.IsNewPressed || SharedBinds.PageUp.IsPressedAndHeld) { // A hacky, somewhat inefficient solution, but it works well textBoard.MoveToChar(new Vector2I(0, 0)); textBoard.MoveToChar(new Vector2I(lineRange.X - 1, 0)); } else if (SharedBinds.PageDown.IsNewPressed || SharedBinds.PageDown.IsPressedAndHeld) { textBoard.MoveToChar(new Vector2I(textBoard.Count - 1, 0)); textBoard.MoveToChar(new Vector2I(lineRange.Y + 1, 0)); } } verticalScroll.Current = textBoard.TextOffset.Y; } textBoard.TextOffset = new Vector2(0, verticalScroll.Current); }