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,34 +159,41 @@ 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;
var charInfo = Input.TextGenerator.characters[LastCaretPosition]; // If caret moved, ensure caret is visible in the viewport
var charTop = charInfo.cursorPos.y; if (caretMoved)
var charBot = charTop - CSCONSOLE_LINEHEIGHT;
var viewportMin = Input.Rect.rect.height - Input.Rect.anchoredPosition.y - (Input.Rect.rect.height * 0.5f);
var viewportMax = viewportMin - Panel.InputScroll.ViewportRect.rect.height;
float diff = 0f;
if (charTop > viewportMin)
diff = charTop - viewportMin;
else if (charBot < viewportMax)
diff = charBot - viewportMax;
if (Math.Abs(diff) > 1)
{ {
var rect = Input.Rect; var charInfo = Input.TextGenerator.characters[LastCaretPosition];
rect.anchoredPosition = new Vector2(rect.anchoredPosition.x, rect.anchoredPosition.y - diff); var charTop = charInfo.cursorPos.y;
var charBot = charTop - CSCONSOLE_LINEHEIGHT;
var viewportMin = Input.Rect.rect.height - Input.Rect.anchoredPosition.y - (Input.Rect.rect.height * 0.5f);
var viewportMax = viewportMin - Panel.InputScroll.ViewportRect.rect.height;
float diff = 0f;
if (charTop > viewportMin)
diff = charTop - viewportMin;
else if (charBot < viewportMax)
diff = charBot - viewportMax;
if (Math.Abs(diff) > 1)
{
var rect = Input.Rect;
rect.anchoredPosition = new Vector2(rect.anchoredPosition.x, rect.anchoredPosition.y - diff);
}
} }
} }

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;