Skip to content

Commit f3d1ba3

Browse files
committed
fix(Tooltips): initialise tips when controller is enabled
The Controller Tooltips would attempt to initialise the tips every frame in an Update loop until they were correctly initialised. This would cause an issue if the controller elements were not available as the Initialise method would run every Update. The fix now only initialises the tips when the controller enabled event is thrown which is a more appropriate time to set the tooltips up. Another fix was added that prevented the tooltips from being visible if the `Hide when not in view` option was unticked.
1 parent 172a9bd commit f3d1ba3

File tree

1 file changed

+45
-71
lines changed

1 file changed

+45
-71
lines changed

Assets/VRTK/Prefabs/Resources/Scripts/VRTK_ControllerTooltips.cs

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ public enum TooltipButtons
7777
[Tooltip("If this is checked then the tooltips will be hidden when the headset is not looking at the controller.")]
7878
public bool hideWhenNotInView = true;
7979

80-
protected bool triggerInitialised = false;
81-
protected bool gripInitialised = false;
82-
protected bool touchpadInitialised = false;
83-
protected bool buttonOneInitialised = false;
84-
protected bool buttonTwoInitialised = false;
85-
protected bool startMenuInitialised = false;
8680
protected TooltipButtons[] availableButtons;
8781
protected VRTK_ObjectTooltip[] buttonTooltips;
8882
protected bool[] tooltipStates;
@@ -92,12 +86,7 @@ public enum TooltipButtons
9286
/// </summary>
9387
public virtual void ResetTooltip()
9488
{
95-
triggerInitialised = false;
96-
gripInitialised = false;
97-
touchpadInitialised = false;
98-
buttonOneInitialised = false;
99-
buttonTwoInitialised = false;
100-
startMenuInitialised = false;
89+
InitialiseTips();
10190
}
10291

10392
/// <summary>
@@ -158,10 +147,41 @@ public virtual void ToggleTips(bool state, TooltipButtons element = TooltipButto
158147
}
159148

160149
protected virtual void Awake()
150+
{
151+
VRTK_SDKManager.instance.AddBehaviourToToggleOnLoadedSetupChange(this);
152+
}
153+
154+
protected virtual void OnEnable()
161155
{
162156
controllerEvents = (controllerEvents != null ? controllerEvents : GetComponentInParent<VRTK_ControllerEvents>());
157+
InitButtonsArray();
158+
InitListeners();
163159
ResetTooltip();
160+
}
161+
162+
protected virtual void OnDisable()
163+
{
164+
if (controllerEvents != null)
165+
{
166+
controllerEvents.ControllerEnabled -= DoControllerEnabled;
167+
controllerEvents.ControllerVisible -= DoControllerVisible;
168+
controllerEvents.ControllerHidden -= DoControllerInvisible;
169+
}
164170

171+
if (headsetControllerAware != null)
172+
{
173+
headsetControllerAware.ControllerGlanceEnter -= DoGlanceEnterController;
174+
headsetControllerAware.ControllerGlanceExit -= DoGlanceExitController;
175+
}
176+
}
177+
178+
protected virtual void OnDestroy()
179+
{
180+
VRTK_SDKManager.instance.RemoveBehaviourToToggleOnLoadedSetupChange(this);
181+
}
182+
183+
protected virtual void InitButtonsArray()
184+
{
165185
availableButtons = new TooltipButtons[]
166186
{
167187
TooltipButtons.None,
@@ -180,22 +200,18 @@ protected virtual void Awake()
180200
{
181201
buttonTooltips[i] = transform.FindChild(availableButtons[i].ToString()).GetComponent<VRTK_ObjectTooltip>();
182202
}
183-
184-
headsetControllerAware = (headsetControllerAware != null ? headsetControllerAware : FindObjectOfType<VRTK_HeadsetControllerAware>());
185-
186-
VRTK_SDKManager.instance.AddBehaviourToToggleOnLoadedSetupChange(this);
187203
}
188204

189-
protected virtual void OnEnable()
205+
protected virtual void InitListeners()
190206
{
191-
InitialiseTips();
192-
193207
if (controllerEvents != null)
194208
{
209+
controllerEvents.ControllerEnabled += DoControllerEnabled;
195210
controllerEvents.ControllerVisible += DoControllerVisible;
196211
controllerEvents.ControllerHidden += DoControllerInvisible;
197212
}
198213

214+
headsetControllerAware = (headsetControllerAware != null ? headsetControllerAware : FindObjectOfType<VRTK_HeadsetControllerAware>());
199215
if (headsetControllerAware != null)
200216
{
201217
headsetControllerAware.ControllerGlanceEnter += DoGlanceEnterController;
@@ -204,34 +220,14 @@ protected virtual void OnEnable()
204220
}
205221
}
206222

207-
protected virtual void OnDisable()
223+
protected virtual void DoControllerEnabled(object sender, ControllerInteractionEventArgs e)
208224
{
209225
if (controllerEvents != null)
210-
{
211-
controllerEvents.ControllerVisible -= DoControllerVisible;
212-
controllerEvents.ControllerHidden -= DoControllerInvisible;
213-
}
214-
215-
if (headsetControllerAware != null)
216-
{
217-
headsetControllerAware.ControllerGlanceEnter -= DoGlanceEnterController;
218-
headsetControllerAware.ControllerGlanceExit -= DoGlanceExitController;
219-
}
220-
}
221-
222-
protected virtual void OnDestroy()
223-
{
224-
VRTK_SDKManager.instance.RemoveBehaviourToToggleOnLoadedSetupChange(this);
225-
}
226-
227-
protected virtual void Update()
228-
{
229-
if (!TipsInitialised() && controllerEvents != null)
230226
{
231227
GameObject actualController = VRTK_DeviceFinder.GetActualController(controllerEvents.gameObject);
232-
if (actualController && actualController.activeInHierarchy)
228+
if (actualController != null && actualController.activeInHierarchy)
233229
{
234-
InitialiseTips();
230+
ResetTooltip();
235231
}
236232
}
237233
}
@@ -280,8 +276,10 @@ protected virtual void DoGlanceExitController(object sender, HeadsetControllerAw
280276

281277
protected virtual void InitialiseTips()
282278
{
283-
foreach (VRTK_ObjectTooltip tooltip in GetComponentsInChildren<VRTK_ObjectTooltip>(true))
279+
VRTK_ObjectTooltip[] tooltips = GetComponentsInChildren<VRTK_ObjectTooltip>(true);
280+
for (int i = 0; i < tooltips.Length; i++)
284281
{
282+
VRTK_ObjectTooltip tooltip = tooltips[i];
285283
string tipText = "";
286284
Transform tipTransform = null;
287285

@@ -290,50 +288,26 @@ protected virtual void InitialiseTips()
290288
case "trigger":
291289
tipText = triggerText;
292290
tipTransform = GetTransform(trigger, SDK_BaseController.ControllerElements.Trigger);
293-
if (tipTransform != null)
294-
{
295-
triggerInitialised = true;
296-
}
297291
break;
298292
case "grip":
299293
tipText = gripText;
300294
tipTransform = GetTransform(grip, SDK_BaseController.ControllerElements.GripLeft);
301-
if (tipTransform != null)
302-
{
303-
gripInitialised = true;
304-
}
305295
break;
306296
case "touchpad":
307297
tipText = touchpadText;
308298
tipTransform = GetTransform(touchpad, SDK_BaseController.ControllerElements.Touchpad);
309-
if (tipTransform != null)
310-
{
311-
touchpadInitialised = true;
312-
}
313299
break;
314300
case "buttonone":
315301
tipText = buttonOneText;
316302
tipTransform = GetTransform(buttonOne, SDK_BaseController.ControllerElements.ButtonOne);
317-
if (tipTransform != null)
318-
{
319-
buttonOneInitialised = true;
320-
}
321303
break;
322304
case "buttontwo":
323305
tipText = buttonTwoText;
324306
tipTransform = GetTransform(buttonTwo, SDK_BaseController.ControllerElements.ButtonTwo);
325-
if (tipTransform != null)
326-
{
327-
buttonTwoInitialised = true;
328-
}
329307
break;
330308
case "startmenu":
331309
tipText = startMenuText;
332310
tipTransform = GetTransform(startMenu, SDK_BaseController.ControllerElements.StartMenu);
333-
if (tipTransform != null)
334-
{
335-
startMenuInitialised = true;
336-
}
337311
break;
338312
}
339313

@@ -351,11 +325,11 @@ protected virtual void InitialiseTips()
351325
tooltip.gameObject.SetActive(false);
352326
}
353327
}
354-
}
355328

356-
protected virtual bool TipsInitialised()
357-
{
358-
return (triggerInitialised && gripInitialised && touchpadInitialised && (buttonOneInitialised || buttonTwoInitialised || startMenuInitialised));
329+
if (!hideWhenNotInView)
330+
{
331+
ToggleTips(true);
332+
}
359333
}
360334

361335
protected virtual Transform GetTransform(Transform setTransform, SDK_BaseController.ControllerElements findElement)

0 commit comments

Comments
 (0)