/
ExpandingInputField.cs
86 lines (68 loc) · 2.78 KB
/
ExpandingInputField.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Nodeplay.UI
{
/// <summary>
/// behavior to do horrible hack to input field, will force input field
/// to expand by resizing the background image to the unbounded rect of
/// it's full text
/// </summary>
///
public class ExpandingInputField : UIBehaviour
{
public GameObject inputparent;
InputField inf;
private Regex colorTags = new Regex("<[^>]*>");
private Regex keyWords = new Regex("and |assert |break |class |continue |def |del |elif |else |except |exec |finally |for |from |global |if |import |in |is |lambda |not |or |pass |print |raise |return |try |while |yield |None |True |False ");
private Regex operators = new Regex("<=|>=|!=");
public Regex definedTriggers { get; set; }
protected override void Start()
{
inf = this.gameObject.GetComponent<InputField>();
inf.onValueChange.AddListener(new UnityEngine.Events.UnityAction<string>(ResizeInput));
inf.onEndEdit.AddListener(new UnityEngine.Events.UnityAction<string>(highlight));
inf.onValueChange.AddListener(new UnityEngine.Events.UnityAction<string>(removeTags));
}
public void highlight(string text)
{
inf.text = colorTags.Replace(inf.text, @"");
inf.text = keyWords.Replace(inf.text, @"<color=blue>$&</color>");
inf.text = operators.Replace(inf.text, @"<color=red>$&</color>");
if (definedTriggers != null)
{
inf.text = definedTriggers.Replace(inf.text, @"<color=green>$&</color>");
}
inf.MoveTextEnd(false);
}
private void removeTags(string text)
{
inf.text = colorTags.Replace(inf.text, @"");
}
void ResizeInput(string text)
{
Debug.Log("some kind of resizing horror");
var fullText = inf.text;
Vector2 extents = inf.textComponent.rectTransform.rect.size;
var settings = inf.textComponent.GetGenerationSettings(extents);
settings.generateOutOfBounds = false;
var prefheight = new TextGenerator().GetPreferredHeight(fullText, settings) + 10;
if (prefheight > inf.textComponent.rectTransform.rect.height - 10)
{
Debug.Log("i grew because the inputfield was only this big" + inf.GetComponent<RectTransform>().rect.height + "and I needed" + prefheight + "space");
inputparent.GetComponent<LayoutElement>().preferredHeight = prefheight;
}
else
{
if (prefheight < inf.textComponent.rectTransform.rect.height + 10)
{
Debug.Log("i shrank because the inputfield was this big" + inf.GetComponent<RectTransform>().rect.height + "and I needed" + prefheight + "space");
inputparent.GetComponent<LayoutElement>().preferredHeight = prefheight;
}
}
}
}
}