Skip to content

Commit

Permalink
fix(Tooltips): force initialise tooltips when controller ready
Browse files Browse the repository at this point in the history
This fix keeps trying to initialise the tooltips in `OnEnable` as the
controller elements may not be ready by the time the controller is
enabled.
  • Loading branch information
thestonefox committed May 11, 2017
1 parent 36da3a3 commit b177373
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Assets/VRTK/Prefabs/Resources/Scripts/VRTK_ControllerTooltips.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public enum TooltipButtons
protected VRTK_ObjectTooltip[] buttonTooltips;
protected bool[] tooltipStates;

protected float retryInitCounter = 0.1f;
protected int retryInitMaxTries = 5;
protected int retryInitCurrentTries = 0;

/// <summary>
/// The Reset method reinitalises the tooltips on all of the controller elements.
/// </summary>
Expand Down Expand Up @@ -149,6 +153,7 @@ public virtual void ToggleTips(bool state, TooltipButtons element = TooltipButto
protected virtual void Awake()
{
VRTK_SDKManager.instance.AddBehaviourToToggleOnLoadedSetupChange(this);
InitButtonsArray();
}

protected virtual void OnEnable()
Expand Down Expand Up @@ -200,6 +205,8 @@ protected virtual void InitButtonsArray()
{
buttonTooltips[i] = transform.FindChild(availableButtons[i].ToString()).GetComponent<VRTK_ObjectTooltip>();
}

retryInitCurrentTries = retryInitMaxTries;
}

protected virtual void InitListeners()
Expand Down Expand Up @@ -276,6 +283,7 @@ protected virtual void DoGlanceExitController(object sender, HeadsetControllerAw

protected virtual void InitialiseTips()
{
bool initComplete = false;
VRTK_ObjectTooltip[] tooltips = GetComponentsInChildren<VRTK_ObjectTooltip>(true);
for (int i = 0; i < tooltips.Length; i++)
{
Expand Down Expand Up @@ -311,6 +319,8 @@ protected virtual void InitialiseTips()
break;
}

initComplete = (tipTransform != null);

tooltip.displayText = tipText;
tooltip.drawLineTo = tipTransform;

Expand All @@ -320,12 +330,19 @@ protected virtual void InitialiseTips()

tooltip.ResetTooltip();

if (tipText.Trim().Length == 0)
if (tipTransform == null || tipText.Trim().Length == 0)
{
tooltip.gameObject.SetActive(false);
}
}

if (!initComplete && retryInitCurrentTries > 0)
{
retryInitCurrentTries--;
Invoke("ResetTooltip", retryInitCounter);
return;
}

if (!hideWhenNotInView)
{
ToggleTips(true);
Expand All @@ -347,7 +364,7 @@ protected virtual Transform GetTransform(Transform setTransform, SDK_BaseControl
{
SDK_BaseController.ControllerHand controllerHand = VRTK_DeviceFinder.GetControllerHand(controllerEvents.gameObject);
string elementPath = VRTK_SDK_Bridge.GetControllerElementPath(findElement, controllerHand, true);
returnTransform = modelController.transform.FindChild(elementPath);
returnTransform = (elementPath != null ? modelController.transform.FindChild(elementPath) : null);
}
}

Expand Down

0 comments on commit b177373

Please sign in to comment.