Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.2 KiB

  1. // Object Tooltip|Prefabs|0060
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// Event Payload
  8. /// </summary>
  9. /// <param name="newText">The optional new text that is given to the tooltip.</param>
  10. public struct ObjectTooltipEventArgs
  11. {
  12. public string newText;
  13. }
  14. /// <summary>
  15. /// Event Payload
  16. /// </summary>
  17. /// <param name="sender">this object</param>
  18. /// <param name="e"><see cref="ObjectTooltipEventArgs"/></param>
  19. public delegate void ObjectTooltipEventHandler(object sender, ObjectTooltipEventArgs e);
  20. /// <summary>
  21. /// Adds a World Space Canvas that can be used to provide additional information about an object by providing a piece of text with a line drawn to a destination point.
  22. /// </summary>
  23. /// <remarks>
  24. /// **Prefab Usage:**
  25. /// * Place the `VRTK/Prefabs/ObjectTooltip/ObjectTooltip` prefab into the scene hierarchy, preferably as a child of the GameObject it is associated with.
  26. /// * Set the `Draw Line To` option to the Transform component of the GameObject the Tooltip will be assoicated with.
  27. /// </remarks>
  28. /// <example>
  29. /// `VRTK/Examples/029_Controller_Tooltips` displays two cubes that have an object tooltip added to them along with tooltips that have been added to the controllers.
  30. /// </example>
  31. public class VRTK_ObjectTooltip : MonoBehaviour
  32. {
  33. [Tooltip("The text that is displayed on the tooltip.")]
  34. public string displayText;
  35. [Tooltip("The size of the text that is displayed.")]
  36. public int fontSize = 14;
  37. [Tooltip("The size of the tooltip container where `x = width` and `y = height`.")]
  38. public Vector2 containerSize = new Vector2(0.1f, 0.03f);
  39. [Tooltip("An optional transform of where to start drawing the line from. If one is not provided the centre of the tooltip is used for the initial line position.")]
  40. public Transform drawLineFrom;
  41. [Tooltip("A transform of another object in the scene that a line will be drawn from the tooltip to, this helps denote what the tooltip is in relation to. If no transform is provided and the tooltip is a child of another object, then the parent object's transform will be used as this destination position.")]
  42. public Transform drawLineTo;
  43. [Tooltip("The width of the line drawn between the tooltip and the destination transform.")]
  44. public float lineWidth = 0.001f;
  45. [Tooltip("The colour to use for the text on the tooltip.")]
  46. public Color fontColor = Color.black;
  47. [Tooltip("The colour to use for the background container of the tooltip.")]
  48. public Color containerColor = Color.black;
  49. [Tooltip("The colour to use for the line drawn between the tooltip and the destination transform.")]
  50. public Color lineColor = Color.black;
  51. [Tooltip("If this is checked then the tooltip will be rotated so it always face the headset.")]
  52. public bool alwaysFaceHeadset = false;
  53. /// <summary>
  54. /// Emitted when the object tooltip is reset.
  55. /// </summary>
  56. public event ObjectTooltipEventHandler ObjectTooltipReset;
  57. /// <summary>
  58. /// Emitted when the object tooltip text is updated.
  59. /// </summary>
  60. public event ObjectTooltipEventHandler ObjectTooltipTextUpdated;
  61. protected LineRenderer line;
  62. protected Transform headset;
  63. public virtual void OnObjectTooltipReset(ObjectTooltipEventArgs e)
  64. {
  65. if (ObjectTooltipReset != null)
  66. {
  67. ObjectTooltipReset(this, e);
  68. }
  69. }
  70. public virtual void OnObjectTooltipTextUpdated(ObjectTooltipEventArgs e)
  71. {
  72. if (ObjectTooltipTextUpdated != null)
  73. {
  74. ObjectTooltipTextUpdated(this, e);
  75. }
  76. }
  77. /// <summary>
  78. /// The ResetTooltip method resets the tooltip back to its initial state.
  79. /// </summary>
  80. public virtual void ResetTooltip()
  81. {
  82. SetContainer();
  83. SetText("UITextFront");
  84. SetText("UITextReverse");
  85. SetLine();
  86. if (drawLineTo == null && transform.parent != null)
  87. {
  88. drawLineTo = transform.parent;
  89. }
  90. OnObjectTooltipReset(SetEventPayload());
  91. }
  92. /// <summary>
  93. /// The UpdateText method allows the tooltip text to be updated at runtime.
  94. /// </summary>
  95. /// <param name="newText">A string containing the text to update the tooltip to display.</param>
  96. public virtual void UpdateText(string newText)
  97. {
  98. displayText = newText;
  99. OnObjectTooltipTextUpdated(SetEventPayload(newText));
  100. ResetTooltip();
  101. }
  102. protected virtual void Awake()
  103. {
  104. VRTK_SDKManager.AttemptAddBehaviourToToggleOnLoadedSetupChange(this);
  105. }
  106. protected virtual void OnEnable()
  107. {
  108. ResetTooltip();
  109. headset = VRTK_DeviceFinder.HeadsetTransform();
  110. }
  111. protected virtual void OnDestroy()
  112. {
  113. VRTK_SDKManager.AttemptRemoveBehaviourToToggleOnLoadedSetupChange(this);
  114. }
  115. protected virtual void Update()
  116. {
  117. DrawLine();
  118. if (alwaysFaceHeadset)
  119. {
  120. transform.LookAt(headset);
  121. }
  122. }
  123. protected virtual ObjectTooltipEventArgs SetEventPayload(string newText = "")
  124. {
  125. ObjectTooltipEventArgs e;
  126. e.newText = newText;
  127. return e;
  128. }
  129. protected virtual void SetContainer()
  130. {
  131. transform.Find("TooltipCanvas").GetComponent<RectTransform>().sizeDelta = containerSize;
  132. Transform tmpContainer = transform.Find("TooltipCanvas/UIContainer");
  133. tmpContainer.GetComponent<RectTransform>().sizeDelta = containerSize;
  134. tmpContainer.GetComponent<Image>().color = containerColor;
  135. }
  136. protected virtual void SetText(string name)
  137. {
  138. Text tmpText = transform.Find("TooltipCanvas/" + name).GetComponent<Text>();
  139. tmpText.material = Resources.Load("UIText") as Material;
  140. tmpText.text = displayText.Replace("\\n", "\n");
  141. tmpText.color = fontColor;
  142. tmpText.fontSize = fontSize;
  143. }
  144. protected virtual void SetLine()
  145. {
  146. line = transform.Find("Line").GetComponent<LineRenderer>();
  147. line.material = Resources.Load("TooltipLine") as Material;
  148. line.material.color = lineColor;
  149. #if UNITY_5_5_OR_NEWER
  150. line.startColor = lineColor;
  151. line.endColor = lineColor;
  152. line.startWidth = lineWidth;
  153. line.endWidth = lineWidth;
  154. #else
  155. line.SetColors(lineColor, lineColor);
  156. line.SetWidth(lineWidth, lineWidth);
  157. #endif
  158. if (drawLineFrom == null)
  159. {
  160. drawLineFrom = transform;
  161. }
  162. }
  163. protected virtual void DrawLine()
  164. {
  165. if (drawLineTo != null)
  166. {
  167. line.SetPosition(0, drawLineFrom.position);
  168. line.SetPosition(1, drawLineTo.position);
  169. }
  170. }
  171. }
  172. }