Skip to content

Commit

Permalink
fix caret position bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
hecomi committed Aug 14, 2016
1 parent 1f3ef0c commit 3ee8c23
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions Assets/uREPL/Scripts/Gui/Window/CommandInputField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ public override void OnUpdateSelected(BaseEventData eventData)

public void MoveCaretPosition(int x)
{
caretPosition = Mathf.Clamp(caretPosition + x, 0, text.Length);
SetCaretPosition(Mathf.Clamp(caretPosition + x, 0, text.Length));
}

public void SetCaretPosition(int pos)
{
caretPosition = Mathf.Clamp(pos, 0, text.Length);
UpdateLabel();
}

public void MoveCaretPositionToLineHead()
Expand All @@ -78,7 +84,7 @@ public void MoveCaretPositionToLineHead()
if (caretPosition == 0) return;
var lastEnd = text.LastIndexOf('\n', caretPosition - 1);
if (lastEnd != -1) {
caretPosition = lastEnd + 1;
SetCaretPosition(lastEnd + 1);
} else {
MoveTextStart(false);
}
Expand All @@ -92,7 +98,7 @@ public void MoveCaretPositionToLineEnd()
if (multiLine) {
var nextEnd = text.IndexOf('\n', caretPosition);
if (nextEnd != -1) {
caretPosition = nextEnd;
SetCaretPosition(nextEnd);
} else {
MoveTextEnd(false);
}
Expand All @@ -107,7 +113,7 @@ public void BackspaceOneCharacterFromCaretPosition()
var isCaretPositionLast = caretPosition == text.Length;
text = text.Remove(caretPosition - 1, 1);
if (!isCaretPositionLast) {
--caretPosition;
MoveCaretPosition(-1);
}
}
}
Expand Down Expand Up @@ -161,17 +167,17 @@ public void InsertToCaretPosition(string str)
public Vector3 GetPositionBeforeCaret(int offsetLen)
{
if (isFocused) {
var generator = textComponent.cachedTextGenerator;
var len = caretPosition - m_DrawStart;
if (len < generator.characterCount) {
var info = generator.characters[len];
var characters = textComponent.cachedTextGenerator.characters;
var len = m_CaretPosition - m_DrawStart;
if (len > 0 && len < characters.Count) {
var info = characters[len];
var ppu = textComponent.pixelsPerUnit;
var x = info.cursorPos.x / ppu;
var y = info.cursorPos.y / ppu;
var z = 0f;
var prefixWidth = 0f;
for (int i = 0; i < offsetLen && i < len; ++i) {
prefixWidth += generator.characters[len - 1 - i].charWidth;
prefixWidth += characters[len - 1 - i].charWidth;
}
prefixWidth /= ppu;
var inputTform = GetComponent<RectTransform>();
Expand Down Expand Up @@ -201,14 +207,14 @@ public void MoveUp()
var preLineEnd = text.LastIndexOf('\n', caretPosition - 1);
if (preLineEnd == -1) return;
if (preLineEnd == 0) {
caretPosition = 0;
SetCaretPosition(0);
return;
}

var currentLineHead = preLineEnd + 1;
var charCountFromLineHead = caretPosition - currentLineHead;
var preLineHead = text.LastIndexOf('\n', currentLineHead - 2) + 1;
caretPosition = Mathf.Min(preLineHead + charCountFromLineHead, preLineEnd);
SetCaretPosition(Mathf.Min(preLineHead + charCountFromLineHead, preLineEnd));
}

public void MoveDown()
Expand All @@ -225,7 +231,7 @@ public void MoveDown()
var nextLineHead = currentLineEnd + 1;
var nextLineEnd = text.IndexOf('\n', nextLineHead);
if (nextLineEnd == -1) nextLineEnd = text.Length;
caretPosition = Mathf.Min(nextLineHead + charCountFromLineHead, nextLineEnd);
SetCaretPosition(Mathf.Min(nextLineHead + charCountFromLineHead, nextLineEnd));
}
}

Expand Down

0 comments on commit 3ee8c23

Please sign in to comment.