IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
AnnotationLabel.cs
1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.UI;
4 using UnityEngine.EventSystems;
5 using System;
6 
7 public class AnnotationLabel : MonoBehaviour {
8 
9  public Image textBackground;
10  public Text myText;
11  public InputField myInputField;
12  public BoxCollider myBoxCollider;
13  //Padding to top + bot edges of background
14  private float padding = 0.0f;
15 
16  //Used to set Label when load from file
17  public void setLabelText(string newLabel) {
18  myText.text = newLabel;
19  myInputField.text = newLabel;
20  resizeLabel ();
21  }
22 
23  public String getLabelText() {
24  return myText.text;
25  }
26 
27  //Called when you click on Text Label
28  public void annotationLabelClicked() {
29  myInputField.gameObject.SetActive (true);
30  myInputField.ActivateInputField ();
31  myInputField.Select ();
32  myInputField.MoveTextEnd (true);
33  textBackground.color = new Color (textBackground.color.r, textBackground.color.b, textBackground.color.b, 0.0f);
34  myText.color = new Color (myText.color.r, myText.color.b, myText.color.b, 0.0f);
35  }
36 
37  // called when vlue in input Field changed
38  public void ValueChanged () {
39  myText.text = myInputField.text;
40  resizeLabel ();
41  }
42 
43  //Called when User finishs editing Label
44  public void EditingFinished () {
45  this.GetComponentInParent<Annotation> ().saveLabelChanges ();
46  myInputField.gameObject.SetActive (false);
47  textBackground.color = new Color (textBackground.color.r, textBackground.color.b, textBackground.color.b, 1.0f);
48  myText.color = new Color (myText.color.r, myText.color.b, myText.color.b, 1.0f);
49  }
50 
51  private void resizeLabel() {
52  if(padding == 0.0f) {
53  padding = textBackground.gameObject.GetComponent<VerticalLayoutGroup> ().padding.top
54  + textBackground.gameObject.GetComponent<VerticalLayoutGroup> ().padding.bottom;
55  }
56 
57  //calc height
58  Canvas.ForceUpdateCanvases();
59  float newHeight = myText.preferredHeight + padding;
60  Vector2 resize = new Vector2 (this.gameObject.GetComponent<RectTransform> ().rect.width, newHeight);
61  //resize objects
62  this.gameObject.GetComponent<RectTransform>().sizeDelta = resize;
63  myBoxCollider.size = new Vector3(myBoxCollider.size.x, newHeight, myBoxCollider.size.z);
64  myInputField.gameObject.GetComponent<RectTransform> ().sizeDelta = resize;
65  }
66 }