Skip to content

Commit

Permalink
Quickly replace ui text to use skia labels
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 20, 2024
1 parent fe7fbaa commit e218602
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 124 deletions.
208 changes: 104 additions & 104 deletions Assets/Scenes/Tests/SkiaUITest.unity

Large diffs are not rendered by default.

44 changes: 40 additions & 4 deletions Assets/Scripts/OpenTS2/UI/Skia/SkiaLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SkiaSharp;
using UnityEngine.UI;
using Codice.Client.Common;
using System.Runtime.InteropServices.WindowsRuntime;

namespace OpenTS2.UI.Skia
{
Expand Down Expand Up @@ -171,6 +172,36 @@ protected RectTransform RectTransform
}
}

protected Canvas Canvas
{
get
{
return GetComponentInParent<Canvas>();
}
}

protected int PracticalWidth
{
get
{
float rectWidth = RectTransformUtility.PixelAdjustRect(RectTransform, Canvas).width;
var ceil = Mathf.CeilToInt(rectWidth);
ceil = Mathf.Max(ceil, 2);
return ceil;
}
}

protected int PracticalHeight
{
get
{
float rectHeight = RectTransformUtility.PixelAdjustRect(RectTransform, Canvas).height;
var ceil = Mathf.CeilToInt(rectHeight);
ceil = Mathf.Max(ceil, 2);
return ceil;
}
}

private Texture2D _texture;
private RawImage _rawImage;
private RectTransform _rectTransform;
Expand All @@ -196,6 +227,11 @@ private void OnRectTransformDimensionsChange()
private void ValidateTexture(SKImageInfo imageInfo)
{
var fmt = (imageInfo.ColorType == SKColorType.Rgba8888) ? TextureFormat.RGBA32 : TextureFormat.BGRA32;
var w = imageInfo.Width;
var h = imageInfo.Height;

w = Mathf.Max(w, 1);
h = Mathf.Max(h, 1);

if (_texture != null && !_texture.isReadable)
{
Expand All @@ -205,11 +241,11 @@ private void ValidateTexture(SKImageInfo imageInfo)

if (_texture == null)
{
_texture = new Texture2D(imageInfo.Width, imageInfo.Height, fmt, false, true);
_texture = new Texture2D(w, h, fmt, false, true);
}
else if (_texture.width != imageInfo.Width || _texture.height != imageInfo.Height || _texture.format != fmt)
else if (_texture.width != w || _texture.height != h || _texture.format != fmt)
{
_texture.Resize(imageInfo.Width, imageInfo.Height, fmt, false);
_texture.Resize(w, h, fmt, false);
}

_texture.wrapMode = TextureWrapMode.Clamp;
Expand All @@ -234,7 +270,7 @@ private void ValidateFont()
private void Render()
{
ValidateFont();
var skImageInfo = new SKImageInfo(Mathf.CeilToInt(RectTransform.sizeDelta.x), Mathf.CeilToInt(RectTransform.sizeDelta.y));
var skImageInfo = new SKImageInfo(PracticalWidth, PracticalHeight);
ValidateTexture(skImageInfo);

var surface = SKSurface.Create(skImageInfo);
Expand Down
9 changes: 5 additions & 4 deletions Assets/Scripts/OpenTS2/UI/UITextComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using OpenTS2.UI.Skia;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -13,13 +14,13 @@ public virtual string Text
{
get
{
return TextComponent.text;
return TextComponent.Text;
}
set
{
TextComponent.text = value;
TextComponent.Text = value;
}
}
public Text TextComponent => transform.GetComponentInChildren<Text>();
public SkiaLabel TextComponent => transform.GetComponentInChildren<SkiaLabel>();
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/OpenTS2/UI/UITextEditComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public override string Text
{
get
{
return "";
return Input.text;
}
set
{
return;
Input.text = value;
_originalText = value;
}
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/OpenTS2/UI/UITextEditElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public override void ParseProperties(UIProperties properties)
public override UIComponent Instantiate(Transform parent)
{
var component = base.Instantiate(parent) as UITextEditComponent;
/*
var inputField = component.gameObject.AddComponent<InputField>();
component.TextComponent.supportRichText = false;
inputField.textComponent = component.TextComponent;
Expand All @@ -28,7 +29,7 @@ public override UIComponent Instantiate(Transform parent)
else
inputField.lineType = InputField.LineType.MultiLineNewline;
inputField.onEndEdit.AddListener(delegate { component.CheckTextEdited(); });
component.gameObject.AddComponent<TextFieldBehaviour>();
component.gameObject.AddComponent<TextFieldBehaviour>();*/
return component;
}
}
Expand Down
46 changes: 35 additions & 11 deletions Assets/Scripts/OpenTS2/UI/UITextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using OpenTS2.UI.Skia;

namespace OpenTS2.UI
{
Expand All @@ -15,7 +16,8 @@ namespace OpenTS2.UI
public class UITextElement : UIElement
{
protected override Type UIComponentType => typeof(UITextComponent);
public TextAnchor Alignment = TextAnchor.MiddleCenter;
public HorizontalAlign HorizontalAlign = HorizontalAlign.Left;
public VerticalAlign VerticalAlign = VerticalAlign.Top;
public Color32 ForeColor = Color.black;

public override void ParseProperties(UIProperties properties)
Expand All @@ -36,31 +38,40 @@ public override void ParseProperties(UIProperties properties)
switch(align)
{
case "lefttop":
Alignment = TextAnchor.UpperLeft;
HorizontalAlign = HorizontalAlign.Left;
VerticalAlign = VerticalAlign.Top;
break;
case "centertop":
Alignment = TextAnchor.UpperCenter;
HorizontalAlign = HorizontalAlign.Center;
VerticalAlign = VerticalAlign.Top;
break;
case "righttop":
Alignment = TextAnchor.UpperRight;
HorizontalAlign = HorizontalAlign.Right;
VerticalAlign = VerticalAlign.Top;
break;
case "leftcenter":
Alignment = TextAnchor.MiddleLeft;
HorizontalAlign = HorizontalAlign.Left;
VerticalAlign = VerticalAlign.Middle;
break;
case "center":
Alignment = TextAnchor.MiddleCenter;
HorizontalAlign = HorizontalAlign.Center;
VerticalAlign = VerticalAlign.Middle;
break;
case "rightcenter":
Alignment = TextAnchor.MiddleRight;
HorizontalAlign = HorizontalAlign.Right;
VerticalAlign = VerticalAlign.Middle;
break;
case "leftbottom":
Alignment = TextAnchor.LowerLeft;
HorizontalAlign = HorizontalAlign.Left;
VerticalAlign = VerticalAlign.Bottom;
break;
case "centerbottom":
Alignment = TextAnchor.LowerCenter;
HorizontalAlign = HorizontalAlign.Center;
VerticalAlign = VerticalAlign.Bottom;
break;
case "rightbottom":
Alignment = TextAnchor.LowerRight;
HorizontalAlign = HorizontalAlign.Right;
VerticalAlign = VerticalAlign.Bottom;
break;
}
}
Expand All @@ -70,6 +81,19 @@ public override UIComponent Instantiate(Transform parent)
var uiComponent = base.Instantiate(parent);
var textGameObject = new GameObject("Text");
textGameObject.transform.SetParent(uiComponent.transform);
var text = textGameObject.AddComponent<SkiaLabel>();
text.FontColor = ForeColor;
text.Text = Caption;
text.FontSize = 14;
text.HorizontalAlign = HorizontalAlign;
text.VerticalAlign = VerticalAlign;
var rect = text.GetComponent<RectTransform>();
rect.anchorMax = new Vector2(1f, 1f);
rect.anchorMin = Vector2.zero;

rect.offsetMin = Vector2.zero;
rect.offsetMax = new Vector2(1f, 1f);
/*
var text = textGameObject.AddComponent<Text>();
text.color = ForeColor;
text.text = Caption;
Expand All @@ -81,7 +105,7 @@ public override UIComponent Instantiate(Transform parent)
rect.anchorMin = Vector2.zero;
rect.offsetMin = Vector2.zero;
rect.offsetMax = new Vector2(1f, 1f);
rect.offsetMax = new Vector2(1f, 1f);*/
return uiComponent;
}
}
Expand Down

0 comments on commit e218602

Please sign in to comment.