-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUniMobText.cs
61 lines (48 loc) · 2.01 KB
/
UniMobText.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using UnityEngine;
namespace UniMob.UI.Widgets
{
public class UniMobText : StatefulWidget
{
public static WidgetViewReference DefaultView =
WidgetViewReference.Resource("UniMob.Text");
public UniMobText()
{
}
public UniMobText(WidgetSize size)
{
Size = size;
}
public WidgetSize? Size { get; set; }
public string Value { get; set; }
public Color Color { get; set; } = Color.black;
public CrossAxisAlignment CrossAxisAlignment { get; set; } = CrossAxisAlignment.Start;
public MainAxisAlignment MainAxisAlignment { get; set; } = MainAxisAlignment.Start;
public float MaxCrossAxisExtent { get; set; } = int.MaxValue;
public int FontSize { get; set; } = 12;
public WidgetViewReference View { get; set; } = DefaultView;
public override State CreateState() => new UniMobTextState();
}
internal class UniMobTextState : ViewState<UniMobText>, IUniMobTextState
{
[Atom] public override WidgetViewReference View => Widget.View;
public string Value => Widget.Value ?? string.Empty;
public int FontSize => Widget.FontSize;
public Color Color => Widget.Color;
public MainAxisAlignment MainAxisAlignment => Widget.MainAxisAlignment;
public CrossAxisAlignment CrossAxisAlignment => Widget.CrossAxisAlignment;
public override WidgetSize CalculateSize()
{
if (Widget.Size.HasValue)
{
return Widget.Size.Value;
}
var prefab = UniMobViewContext.Loader.LoadViewPrefab(View);
var tmpText = prefab.gameObject.GetComponent<UniMobTextMeshProBehaviour>();
tmpText.fontSize = FontSize;
var maxExtent = Widget.MaxCrossAxisExtent;
var size = tmpText.GetPreferredValues(Value, maxExtent, int.MaxValue);
size.x = Mathf.Min(maxExtent, size.x);
return WidgetSize.Fixed(size.x, size.y);
}
}
}