Skip to content

Commit

Permalink
Input field caret
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 21, 2024
1 parent e37d997 commit 47fa459
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 221 deletions.
409 changes: 207 additions & 202 deletions Assets/Scenes/Tests/SkiaUITest.unity

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions Assets/Scripts/OpenTS2/Engine/Tests/MXFFontTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public class MXFFontTest : MonoBehaviour
{
public string BaseGameFontToUse = "";
public SkiaLabel Label;
public RectTransform Selector;
public SkiaLabel SelectableLabel;
public int SelectedCharacter = 0;
private int _previousCharacter = 0;

private void Start()
{
Expand All @@ -28,18 +24,5 @@ private void Start()
var font = new SkiaFont(mxf.DecodedData);
Label.Font = font;
}

private void Update()
{
var rect = SelectableLabel.GetCharacterRect(SelectedCharacter);
Selector.anchoredPosition = new Vector2(rect.Rect.x, rect.Rect.y);
Selector.sizeDelta = new Vector2(rect.Rect.width, rect.Rect.height);
Selector.gameObject.SetActive(!rect.OutOfBounds);
if (SelectedCharacter != _previousCharacter)
{
_previousCharacter = SelectedCharacter;
Debug.Log($"Selecting {SelectableLabel.ParsedText.OriginalText[SelectedCharacter]}");
}
}
}
}
70 changes: 70 additions & 0 deletions Assets/Scripts/OpenTS2/UI/Skia/SkiaInputField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,82 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UI;

namespace OpenTS2.UI.Skia
{
public class SkiaInputField : Selectable
{
private bool Selected => true;
[SerializeField]
private RectTransform _caret;
[SerializeField]
private SkiaLabel _label;

[SerializeField]
private int _selectedCharacter = 0;

private void Update()
{
if (!Application.isPlaying) return;
SelectedUpdate();
}

private void SelectedUpdate()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
MoveCaretLeft();
if (Input.GetKeyDown(KeyCode.RightArrow))
MoveCaretRight();

if (Input.GetKeyDown(KeyCode.Backspace))
{
if (_selectedCharacter >= 0)
{
var strBuilder = new StringBuilder(_label.Text);
strBuilder.Remove(_selectedCharacter, 1);
_label.Text = strBuilder.ToString();
MoveCaretLeft();
}
}

ValidateSelection();
UpdateCaretPosition();
}

private void MoveCaretLeft()
{
_selectedCharacter--;
if (_selectedCharacter < -1)
{
_selectedCharacter = -1;
}
}

private void MoveCaretRight()
{
_selectedCharacter++;
if (_selectedCharacter >= _label.Text.Length)
_selectedCharacter = _label.Text.Length - 1;
}

private void UpdateCaretPosition()
{
_caret.sizeDelta = new Vector2(1, _label.FontSize);
if (_selectedCharacter < 0)
{
_caret.anchoredPosition = new Vector2(0f, 0f);
return;
}
var rect = _label.GetCharacterRect(_selectedCharacter);
_caret.anchoredPosition = new Vector2(rect.Rect.x + rect.Rect.width, rect.Rect.y);
}

private void ValidateSelection()
{
_selectedCharacter = Mathf.Clamp(_selectedCharacter, -1, _label.Text.Length - 1);
}
}
}
14 changes: 12 additions & 2 deletions Assets/Scripts/OpenTS2/UI/Skia/SkiaLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public int FontSize
{
get
{
return FontSize;
return m_FontSize;
}

set
Expand Down Expand Up @@ -419,6 +419,11 @@ private void Render()
_texture.LoadRawTextureData(pixMap.GetPixels(), pixMap.RowBytes * pixMap.Height);
_texture.Apply(false, true);
}
public int GetTextCharIndexForLineChar(int lineIndex, int c)
{
var line = ParsedText.Lines[lineIndex];
return c + line.BeginIndex;
}

private void ParseText(SKImageInfo imageInfo)
{
Expand Down Expand Up @@ -506,7 +511,12 @@ private void ParseText(SKImageInfo imageInfo)
_parsedText = parsedText;
}

private float GetLineY(int line)
public float GetLineLength(int line)
{
return _skPaint.MeasureText(_parsedText.Lines[line].Text);
}

public float GetLineY(int line)
{
var height = PracticalHeight;
var heightMiddle = height / 2f;
Expand Down

0 comments on commit 47fa459

Please sign in to comment.