Tanoda
NodeComment.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using NaughtyAttributes;
4using UnityEngine;
5using UnityEngine.UI;
6
7public class NodeComment : MonoBehaviour
8{
10 public Text displayText;
11 public InputField inputField;
12
13 [ReadOnly] [SerializeField] private bool toggled = false;
14 private float maxWidth = -1f;
15
16 private void Start()
17 {
18 if (!string.IsNullOrEmpty(displayText.text))
19 {
20 (displayText.rectTransform.parent as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, GetMaxWidth());
21 RecalculateSize();
22 }
23 }
24
25 private float GetMaxWidth()
26 {
27 if (maxWidth != -1)
28 {
29 return maxWidth;
30 }
31
32 maxWidth = (transform.parent as RectTransform).rect.width;
33 return maxWidth;
34 }
35
36 public void ToggleEditor()
37 {
38 if (toggled)
39 {
40 editButton.gameObject.SetActive(true);
41 displayText.gameObject.SetActive(true);
42 inputField.gameObject.SetActive(false);
43 }
44 else
45 {
46 editButton.gameObject.SetActive(false);
47 displayText.gameObject.SetActive(false);
48 inputField.gameObject.SetActive(true);
49 (displayText.rectTransform.parent as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, GetMaxWidth());
50 }
51
52 toggled = !toggled;
53 }
54
55 public void InputFieldEditEnded(string value)
56 {
58 (displayText.rectTransform.parent as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, GetTextSize(displayText).y + 10 + 30);
59 GetComponentInParent<ActionObject>().comment = value;
60 }
61
62 public void InputFieldEdited(string value)
63 {
64 displayText.text = value;
65 RecalculateSize();
66 }
67
68 [Button]
69 internal void RecalculateSize()
70 {
71 (displayText.rectTransform.parent as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, GetTextSize(displayText).y + 10 + 30);
72 }
73
74 internal Vector2 GetTextSize(Text t)
75 {
76 TextGenerator textGen = new TextGenerator();
77 TextGenerationSettings generationSettings = t.GetGenerationSettings(t.rectTransform.rect.size);
78 float width = textGen.GetPreferredWidth(t.text, generationSettings);
79 float height = textGen.GetPreferredHeight(t.text, generationSettings);
80 return new Vector2(width, height);
81 }
82}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
Button editButton
Definition: NodeComment.cs:9
void InputFieldEditEnded(string value)
Definition: NodeComment.cs:55
void InputFieldEdited(string value)
Definition: NodeComment.cs:62
void ToggleEditor()
Definition: NodeComment.cs:36
InputField inputField
Definition: NodeComment.cs:11
Text displayText
Definition: NodeComment.cs:10