public void ReceiveSpecialInput(Keys key) { switch (key) { case Keys.Left: if (isSelecting) { InitSelectionStart(); } CaretIndex = Math.Max(CaretIndex - 1, 0); caretTimer = 0; HandleSelection(); break; case Keys.Right: if (isSelecting) { InitSelectionStart(); } CaretIndex = Math.Min(CaretIndex + 1, Text.Length); caretTimer = 0; HandleSelection(); break; case Keys.Up: if (isSelecting) { InitSelectionStart(); } float lineHeight = Font.MeasureString("T").Y *TextBlock.TextScale; int newIndex = textBlock.GetCaretIndexFromLocalPos(new Vector2(caretPos.X, caretPos.Y - lineHeight)); CaretIndex = newIndex; caretTimer = 0; HandleSelection(); break; case Keys.Down: if (isSelecting) { InitSelectionStart(); } lineHeight = Font.MeasureString("T").Y *TextBlock.TextScale; newIndex = textBlock.GetCaretIndexFromLocalPos(new Vector2(caretPos.X, caretPos.Y + lineHeight)); CaretIndex = newIndex; caretTimer = 0; HandleSelection(); break; case Keys.Delete when !Readonly: if (selectedCharacters > 0) { RemoveSelectedText(); } else if (Text.Length > 0 && CaretIndex < Text.Length) { SetText(Text.Remove(CaretIndex, 1)); OnTextChanged?.Invoke(this, Text); caretPosDirty = true; } break; case Keys.Tab: // Select the next text box. var editor = RectTransform.GetParents().Select(p => p.GUIComponent as SerializableEntityEditor).FirstOrDefault(e => e != null); if (editor == null) { break; } var allTextBoxes = GetAndSortTextBoxes(editor).ToList(); if (allTextBoxes.Any()) { int currentIndex = allTextBoxes.IndexOf(this); int nextIndex = Math.Min(allTextBoxes.Count - 1, currentIndex + 1); var next = allTextBoxes[nextIndex]; if (next != this) { next.Select(); next.Flash(Color.White * 0.5f, 0.5f); } else { // Select the first text box in the next editor that has text boxes. var listBox = RectTransform.GetParents().Select(p => p.GUIComponent as GUIListBox).FirstOrDefault(lb => lb != null); if (listBox == null) { break; } // TODO: The get's out of focus if the selection is out of view. // Not sure how's that possible, but it seems to work when the auto scroll is disabled and you handle the scrolling manually. listBox.SelectNext(); while (SelectNextTextBox(listBox) == null) { var previous = listBox.SelectedComponent; listBox.SelectNext(); if (listBox.SelectedComponent == previous) { break; } } } } IEnumerable <GUITextBox> GetAndSortTextBoxes(GUIComponent parent) => parent.GetAllChildren <GUITextBox>().OrderBy(t => t.Rect.Y).ThenBy(t => t.Rect.X); GUITextBox SelectNextTextBox(GUIListBox listBox) { var textBoxes = GetAndSortTextBoxes(listBox.SelectedComponent); if (textBoxes.Any()) { var next = textBoxes.First(); next.Select(); next.Flash(Color.White * 0.5f, 0.5f); return(next); } return(null); } break; } OnKeyHit?.Invoke(this, key); void HandleSelection() { if (isSelecting) { InitSelectionStart(); CalculateSelection(); } else { ClearSelection(); } } }
// for text highlighting public void OnType() { OnKeyHit?.Invoke(inputField.text); }