Tanoda
pb_ToolbarButton.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
4using System.Collections;
5using GILES.Interface;
6
7namespace GILES
8{
12 public class pb_ToolbarButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
13 {
14 protected Selectable selectable;
15
16 public virtual string tooltip { get { return ""; }
17 set { tooltip = value; }
18 }
19
20 public bool interactable
21 {
22 get
23 {
24 return selectable.interactable;
25 }
26
27 set
28 {
29 selectable.interactable = value;
30 }
31 }
32
33 protected virtual void Start()
34 {
35 selectable = GetComponent<Selectable>();
36 }
37
38 protected GameObject tooltip_label = null;
39
40 public virtual void OnPointerClick(PointerEventData eventData)
41 {
43 }
44
45 public virtual void OnPointerEnter(PointerEventData eventData)
46 {
48 }
49
50 public virtual void OnPointerExit(PointerEventData eventData)
51 {
53 }
54
55 public void ShowTooltip()
56 {
57 string description = tooltip;
58
59 if( !string.IsNullOrEmpty(description) )
60 {
61 tooltip_label = pb_GUIUtility.CreateLabel(description);
62 float width = tooltip_label.GetComponent<Text>().preferredWidth;
63 tooltip_label.GetComponent<RectTransform>().sizeDelta = new Vector2(width, 30f);
64 tooltip_label.transform.SetParent( GameObject.FindObjectOfType<Canvas>().transform );
65
66 if(transform.position.x + width < Screen.width)
67 tooltip_label.transform.position = new Vector3(transform.position.x + width * .5f, transform.position.y - 30f, 0f);
68 else
69 tooltip_label.transform.position = new Vector3(transform.position.x - width * .5f, transform.position.y - 30f, 0f);
70
71 if (!tooltip_label.GetComponent<Outline>())
72 {
73 tooltip_label.AddComponent<Outline>();
74 }
75 }
76 }
77
78 public void UpdateTooltip()
79 {
80 if(tooltip_label != null)
81 {
82 if( string.IsNullOrEmpty(tooltip) )
83 {
84 GameObject.Destroy(tooltip_label);
85 }
86 else
87 {
88 tooltip_label.GetComponent<Text>().text = tooltip;
89 if (!tooltip_label.GetComponent<Outline>())
90 {
91 tooltip_label.AddComponent<Outline>();
92 }
93 }
94 }
95 }
96
97 public void HideTooltip()
98 {
99 if(tooltip_label != null)
100 GameObject.Destroy(tooltip_label);
101 }
102 }
103}
virtual void OnPointerExit(PointerEventData eventData)
virtual void OnPointerEnter(PointerEventData eventData)
virtual void OnPointerClick(PointerEventData eventData)