Skip to content

Commit

Permalink
fix(Pointers): ensure pointer id exists when checking ui pointer length
Browse files Browse the repository at this point in the history
There was an issue where the pointerID didn't exist when checking the
UI Pointer length which would cause a crash.

This has been fixed by adding a check when retrieving the pointer max
length from the dictionary and making the dictionary protected whilst
providing a static public getter method.
  • Loading branch information
thestonefox committed Nov 15, 2017
1 parent e6ffb82 commit 3de1e48
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Assets/VRTK/Documentation/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -7967,6 +7967,17 @@ Adding the `VRTK_UIPointer_UnityEvents` component to `VRTK_UIPointer` object all

### Class Methods

#### GetPointerLength/1

> `public static float GetPointerLength(int pointerId)`

* Parameters
* `int pointerId` - The pointer ID for the UI Pointer to recieve the length for.
* Returns
* `float` - The maximum length the UI Pointer will cast to.

The GetPointerLength method retrieves the maximum UI Pointer length for the given pointer ID.

#### SetEventSystem/1

> `public virtual VRTK_VRInputModule SetEventSystem(EventSystem eventSystem)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected virtual float GetHitDistance(Ray ray, float hitDistance)
//[Pure]
protected virtual void Raycast(Canvas canvas, Camera eventCamera, PointerEventData eventData, Ray ray, ref List<RaycastResult> results)
{
float hitDistance = GetHitDistance(ray, VRTK_UIPointer.pointerLengths[eventData.pointerId]);
float hitDistance = GetHitDistance(ray, VRTK_UIPointer.GetPointerLength(eventData.pointerId));
IList<Graphic> canvasGraphics = GraphicRegistry.GetGraphicsForCanvas(canvas);
for (int i = 0; i < canvasGraphics.Count; ++i)
{
Expand Down
18 changes: 16 additions & 2 deletions Assets/VRTK/Source/Scripts/UI/VRTK_UIPointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public struct UIPointerEventArgs
[AddComponentMenu("VRTK/Scripts/UI/VRTK_UIPointer")]
public class VRTK_UIPointer : MonoBehaviour
{
public static Dictionary<int, float> pointerLengths = new Dictionary<int, float>();

/// <summary>
/// Methods of activation.
/// </summary>
Expand Down Expand Up @@ -182,6 +180,7 @@ public enum ClickMethods
/// </summary>
public event UIPointerEventHandler UIPointerElementDragEnd;

protected static Dictionary<int, float> pointerLengths = new Dictionary<int, float>();
protected bool pointerClicked = false;
protected bool beamEnabledState = false;
protected bool lastPointerPressState = false;
Expand All @@ -193,6 +192,21 @@ public enum ClickMethods
protected EventSystem cachedEventSystem;
protected VRTK_VRInputModule cachedVRInputModule;

/// <summary>
/// The GetPointerLength method retrieves the maximum UI Pointer length for the given pointer ID.
/// </summary>
/// <param name="pointerId">The pointer ID for the UI Pointer to recieve the length for.</param>
/// <returns>The maximum length the UI Pointer will cast to.</returns>
public static float GetPointerLength(int pointerId)
{
float maxLength;
if (!pointerLengths.TryGetValue(pointerId, out maxLength))
{
maxLength = float.MaxValue;
}
return maxLength;
}

public virtual void OnUIPointerElementEnter(UIPointerEventArgs e)
{
if (e.currentTarget != currentTarget)
Expand Down

0 comments on commit 3de1e48

Please sign in to comment.