Skip to content

Commit

Permalink
fix(UIPointer): ensure event input pointers list is managed correctly
Browse files Browse the repository at this point in the history
Previously, whenever a UIPointer was initialised it would get added to
the Event Input pointers list, but it could end up being added multiple
times to the same list as no checking was done to see if it already
existed within the list and it was also not removed from the list when
the UIPointer was disabled.

This fix ensures the list is correctly managed and also caches the
event system and event system input to prevent them from being re-found
each time a pointer is enabled.
  • Loading branch information
thestonefox committed Jan 6, 2017
1 parent 1b782a3 commit 23b4dbd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Assets/VRTK/Scripts/Internal/VRTK_EventSystemVRInput.cs
Expand Up @@ -7,11 +7,11 @@

public class VRTK_EventSystemVRInput : PointerInputModule
{
public List<VRTK_UIPointer> pointers;
public List<VRTK_UIPointer> pointers = new List<VRTK_UIPointer>();

public void Initialise()
{
pointers = new List<VRTK_UIPointer>();
pointers.Clear();
}

public override void Process()
Expand Down
41 changes: 35 additions & 6 deletions Assets/VRTK/Scripts/UI/VRTK_UIPointer.cs
Expand Up @@ -120,6 +120,9 @@ public enum ClickMethods
private bool lastPointerPressState = false;
private bool lastPointerClickState = false;

private EventSystem cachedEventSystem;
private VRTK_EventSystemVRInput cachedEventSystemInput;

private const string ACTIVATOR_FRONT_TRIGGER_GAMEOBJECT = "UIPointer_Activator_Front_Trigger";

public virtual void OnUIPointerElementEnter(UIPointerEventArgs e)
Expand Down Expand Up @@ -317,20 +320,46 @@ private void OnEnable()
controllerRenderModel = VRTK_SDK_Bridge.GetControllerRenderModel(controller.gameObject);
}

private void OnDisable()
{
if (cachedEventSystemInput && cachedEventSystemInput.pointers.Contains(this))
{
cachedEventSystemInput.pointers.Remove(this);
}
}

private void ConfigureEventSystem()
{
var eventSystem = FindObjectOfType<EventSystem>();
var eventSystemInput = SetEventSystem(eventSystem);
if (!cachedEventSystem)
{
cachedEventSystem = FindObjectOfType<EventSystem>();
}

if (!cachedEventSystemInput)
{
cachedEventSystemInput = SetEventSystem(cachedEventSystem);
}

if (cachedEventSystem && cachedEventSystemInput)
{
if (pointerEventData == null)
{
pointerEventData = new PointerEventData(cachedEventSystem);
}

StartCoroutine(WaitForPointerId());

pointerEventData = new PointerEventData(eventSystem);
StartCoroutine(WaitForPointerId());
eventSystemInput.pointers.Add(this);
if (!cachedEventSystemInput.pointers.Contains(this))
{
cachedEventSystemInput.pointers.Add(this);
}
}
}

private IEnumerator WaitForPointerId()
{
var index = (int)VRTK_SDK_Bridge.GetControllerIndex(controller.gameObject);
while(index < 0 || index == int.MaxValue)
while (index < 0 || index == int.MaxValue)
{
index = (int)VRTK_SDK_Bridge.GetControllerIndex(controller.gameObject);
yield return null;
Expand Down

0 comments on commit 23b4dbd

Please sign in to comment.