This commit is contained in:
Sinai
2021-05-12 21:29:59 +10:00
parent ada239c828
commit b61020fe67
2 changed files with 27 additions and 26 deletions

View File

@ -68,7 +68,6 @@ The following helper methods are available:
public static InputFieldRef Input => Panel.Input; public static InputFieldRef Input => Panel.Input;
public static int LastCaretPosition { get; private set; } public static int LastCaretPosition { get; private set; }
public static int PreviousCaretPosition { get; private set; }
internal static float defaultInputFieldAlpha; internal static float defaultInputFieldAlpha;
// Todo save as config? // Todo save as config?
@ -143,9 +142,7 @@ The following helper methods are available:
public static void Update() public static void Update()
{ {
int lastCaretPos = LastCaretPosition; UpdateCaret(out bool caretMoved);
UpdateCaret();
bool caretMoved = lastCaretPos != LastCaretPosition;
if (!settingAutoCompletion && EnableSuggestions && caretMoved) if (!settingAutoCompletion && EnableSuggestions && caretMoved)
{ {
@ -162,17 +159,23 @@ The following helper methods are available:
private const int CSCONSOLE_LINEHEIGHT = 18; private const int CSCONSOLE_LINEHEIGHT = 18;
private static void UpdateCaret() private static void UpdateCaret(out bool caretMoved)
{ {
int prevCaret = LastCaretPosition;
caretMoved = false;
if (Input.Component.isFocused) if (Input.Component.isFocused)
{ {
PreviousCaretPosition = LastCaretPosition;
LastCaretPosition = Input.Component.caretPosition; LastCaretPosition = Input.Component.caretPosition;
caretMoved = LastCaretPosition != prevCaret;
} }
if (Input.Text.Length == 0) if (Input.Text.Length == 0)
return; return;
// If caret moved, ensure caret is visible in the viewport
if (caretMoved)
{
var charInfo = Input.TextGenerator.characters[LastCaretPosition]; var charInfo = Input.TextGenerator.characters[LastCaretPosition];
var charTop = charInfo.cursorPos.y; var charTop = charInfo.cursorPos.y;
var charBot = charTop - CSCONSOLE_LINEHEIGHT; var charBot = charTop - CSCONSOLE_LINEHEIGHT;
@ -192,6 +195,7 @@ The following helper methods are available:
rect.anchoredPosition = new Vector2(rect.anchoredPosition.x, rect.anchoredPosition.y - diff); rect.anchoredPosition = new Vector2(rect.anchoredPosition.x, rect.anchoredPosition.y - diff);
} }
} }
}
#region Evaluating #region Evaluating

View File

@ -84,9 +84,6 @@ namespace UnityExplorer.UI.CSConsole
/// <returns>A string which contains the amount of leading lines specified, as well as the rich-text highlighted section.</returns> /// <returns>A string which contains the amount of leading lines specified, as well as the rich-text highlighted section.</returns>
public string BuildHighlightedString(string input, int startIdx, int endIdx, int leadingLines) public string BuildHighlightedString(string input, int startIdx, int endIdx, int leadingLines)
{ {
if (string.IsNullOrEmpty(input) || endIdx <= startIdx) if (string.IsNullOrEmpty(input) || endIdx <= startIdx)
return input; return input;