Skip to content

Commit

Permalink
Add single line support to skia labels
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 21, 2024
1 parent 8fe3b04 commit 40cdeec
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 205 deletions.
414 changes: 209 additions & 205 deletions Assets/Scenes/Tests/SkiaUITest.unity

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Assets/Scripts/OpenTS2/UI/Skia/SkiaInputField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ private void OnGUI()
private string SanitizeText(string text)
{
text = text.Replace('\t'.ToString(), "");
if (Label.SingleLine)
{
text = text.Replace('\n'.ToString(), "");
text = text.Replace('\r'.ToString(), "");
}
return text;
}

Expand Down
33 changes: 33 additions & 0 deletions Assets/Scripts/OpenTS2/UI/Skia/SkiaLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ namespace OpenTS2.UI.Skia
[RequireComponent(typeof(RawImage))]
public class SkiaLabel : MonoBehaviour
{
public bool SingleLine
{
get
{
return m_SingleLine;
}

set
{
if (m_SingleLine != value)
{
m_SingleLine = value;
Render();
}
}
}
public ParsedLabelText ParsedText
{
get
Expand Down Expand Up @@ -174,11 +190,16 @@ public float LineSpacing
[SerializeField]
protected int m_VerticalScroll = 0;
protected SkiaFont m_Font;
[SerializeField]
protected bool m_SingleLine = false;

[SerializeField]
private TextAsset m_FontAsset;
[SerializeField]
[HideInInspector]
private bool _fontFromAsset = false;


private SKPaint _skPaint = null;
private ParsedLabelText _parsedText = null;

Expand Down Expand Up @@ -436,6 +457,18 @@ private void ParseText(SKImageInfo imageInfo)
var wrappedHeight = (float)m_FontSize;

var lines = new List<TextLine>();

if (SingleLine)
{
parsedText.Height = wrappedHeight;
text = text.Replace('\n', ' ');
text = text.Replace('\r', ' ');
lines.Add(new TextLine(text, 0, text.Length));
parsedText.Lines = lines;
_parsedText = parsedText;
return;
}

var lastWordIndex = -1;
var lastLineIndex = 0;
for (var i = 0; i < text.Length; i++)
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/OpenTS2/UI/UITextEditElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public override UIComponent Instantiate(Transform parent)
inputField.Caret.anchorMax = new Vector2(0f, 1f);
inputField.Caret.pivot = new Vector2(0.5f, 1f);
inputField.OnTextEdited += component.FireTextEdited;
inputField.Label.SingleLine = SingleLine;
/*
if (SingleLine)
inputField.lineType = InputField.LineType.MultiLineSubmit;
Expand Down

0 comments on commit 40cdeec

Please sign in to comment.