Skip to content

Commit

Permalink
Refactor swipe event args
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-shariff committed Jan 10, 2024
1 parent b8333de commit af531ca
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 45 deletions.
18 changes: 9 additions & 9 deletions Runtime/Interaction/HPUIEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ public class HPUISwipeEventArgs: HPUIGestureEventArgs
public HPUISwipeState State { get; private set; }
public float TimeDelta { get; private set; }
public float StartTime { get; private set; }
public Vector3 StartPosition { get; private set; }
public Vector3 PreviousPosition { get; private set; }
public Vector3 CurrentPosition { get; private set; }
public Vector3 Direction { get; private set; }
public Vector3 DeltaDirection { get => CurrentPosition - PreviousPosition; }
public Vector2 StartPosition { get; private set; }
public Vector2 CumilativeDirection { get; private set; }
public float CumilativeDistance { get; private set; }
public Vector2 DeltaDirection { get; private set; }

public override void SetParams(IHPUIInteractor interactor, IHPUIInteractable interactable)
{
throw new InvalidOperationException("Call overloaded method!");
}

public void SetParams(IHPUIInteractor interactor, IHPUIInteractable interactable, HPUISwipeState state, float timeDelta, float startTime, Vector3 startPosition, Vector3 previousPosition, Vector3 currentPosition, Vector3 direction)
public void SetParams(IHPUIInteractor interactor, IHPUIInteractable interactable, HPUISwipeState state, float timeDelta, float startTime,
Vector2 startPosition, Vector2 cumilativeDirection, float cumilativeDistance, Vector2 deltaDirection)
{
base.SetParams(interactor, interactable);
State = state;
TimeDelta = timeDelta;
StartTime = startTime;
StartPosition = startPosition;
PreviousPosition = previousPosition;
CurrentPosition = currentPosition;
Direction = direction;
CumilativeDirection = cumilativeDirection;
CumilativeDistance = cumilativeDistance;
DeltaDirection = deltaDirection;
}
}

Expand Down
19 changes: 12 additions & 7 deletions Runtime/Interaction/Logic/HPUIGestureLogicDistributed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void OnSelectExiting(IHPUIInteractable interactable)
{
swipeEventArgs.SetParams(interactor, interactable,
HPUISwipeState.Stopped, Time.time - state.startTime, state.startTime, state.startPosition,
state.previousPosition, state.previousPosition, state.previousPosition - state.startPosition);
state.cumilativeDirection, state.cumilativeDistance, state.delta);
interactable.OnSwipe(swipeEventArgs);
interactor.OnSwipe(swipeEventArgs);

Expand All @@ -99,19 +99,21 @@ public void Update()
Vector2 currentPosition = hpuiInteractable.ComputeInteractorPostion(interactor);
HPUIInteractionState state = states[hpuiInteractable];
float timeDelta = currentTime - state.startTime;
Vector2 direction = currentPosition - state.startPosition;
state.delta = currentPosition - state.previousPosition;
state.cumilativeDirection += state.delta;
state.cumilativeDistance += state.delta.magnitude;

switch (state.gestureState)
{
case HPUIGestureState.Tap:
if (timeDelta > tapTimeThreshold || direction.magnitude > tapDistanceThreshold)
if (timeDelta > tapTimeThreshold || state.cumilativeDirection.magnitude > tapDistanceThreshold)
{
state.gestureState = HPUIGestureState.Swipe;
using (hpuiSwipeEventArgsPool.Get(out HPUISwipeEventArgs swipeEventArgs))
{
swipeEventArgs.SetParams(interactor, hpuiInteractable,
HPUISwipeState.Started, timeDelta, state.startTime, state.startPosition, state.previousPosition,
currentPosition, direction);
HPUISwipeState.Started, timeDelta, state.startTime, state.startPosition,
state.cumilativeDirection, state.cumilativeDistance, state.delta);
hpuiInteractable.OnSwipe(swipeEventArgs);
interactor.OnSwipe(swipeEventArgs);
}
Expand All @@ -121,8 +123,8 @@ public void Update()
using (hpuiSwipeEventArgsPool.Get(out HPUISwipeEventArgs swipeEventArgs))
{
swipeEventArgs.SetParams(interactor, hpuiInteractable,
HPUISwipeState.Updated, timeDelta, state.startTime, state.startPosition, state.previousPosition,
currentPosition, direction);
HPUISwipeState.Updated, timeDelta, state.startTime, state.startPosition,
state.cumilativeDirection, state.cumilativeDistance, state.delta);
hpuiInteractable.OnSwipe(swipeEventArgs);
interactor.OnSwipe(swipeEventArgs);
}
Expand Down Expand Up @@ -154,6 +156,9 @@ class HPUIInteractionState
public float startTime;
public Vector2 startPosition;
public Vector2 previousPosition;
public Vector2 delta;
public Vector2 cumilativeDirection;
public float cumilativeDistance;

public HPUIInteractionState SetParams(HPUIGestureState gestureState, float startTime, Vector2 startPosition)
{
Expand Down
76 changes: 47 additions & 29 deletions Runtime/Interaction/Logic/HPUIGestureLogicUnified.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ namespace ubco.ovilab.HPUI.Interaction
/// </summary>
public class HPUIGestureLogicUnified: IHPUIGestureLogic
{
// private Dictionary<IHPUIInteractable, HPUIInteractionState> states = new Dictionary<IHPUIInteractable, HPUIInteractionState>();

private LinkedPool<HPUITapEventArgs> hpuiTapEventArgsPool = new LinkedPool<HPUITapEventArgs>(() => new HPUITapEventArgs());
private LinkedPool<HPUISwipeEventArgs> hpuiSwipeEventArgsPool = new LinkedPool<HPUISwipeEventArgs>(() => new HPUISwipeEventArgs());

private float tapTimeThreshold, tapDistanceThreshold;
private IHPUIInteractor interactor;

private float startTime, cumilativeDistance;
private Vector2 previousPosition, cumilativeDelta;
private float startTime, cumilativeDistance, timeDelta;
private Vector2 delta, previousPosition, cumilativeDirection;

private int lowestTargetZIndex = int.MaxValue;
private int activeInteractablesCount = 0;
Expand Down Expand Up @@ -68,7 +66,7 @@ public void OnSelectEntering(IHPUIInteractable interactable)
float currentTime = Time.time;
bool inValidWindow = (currentTime - startTime) < tapTimeThreshold;

HPUIInteractionState state = new HPUIInteractionState(Time.time, inValidWindow);
HPUIInteractionState state = new HPUIInteractionState(Time.time, interactable.ComputeInteractorPostion(interactor), inValidWindow);
activeInteractables.Add(interactable, state);

// If a new higher priority targets is encountered within tap time window, we hand over control to that.
Expand Down Expand Up @@ -144,13 +142,18 @@ public void OnSelectExiting(IHPUIInteractable interactable)
case HPUIGestureState.Swipe:
using (hpuiSwipeEventArgsPool.Get(out HPUISwipeEventArgs swipeEventArgs))
{
// TODO
swipeEventArgs.interactorObject = interactor;
swipeEventArgs.interactableObject = activePriorityInteractable;
// swipeEventArgs.SetParams(interactor, activePriorityInteractable,
// HPUISwipeState.Stopped, Time.time - state.startTime, state.startTime, state.startPosition,
// state.previousPosition, state.previousPosition, state.previousPosition - state.startPosition);

HPUIInteractionState state;
if (activePriorityInteractable != null)
{
state = activeInteractables[activePriorityInteractable];
}
else
{
state = HPUIInteractionState.empty;
}
swipeEventArgs.SetParams(interactor, activePriorityInteractable,
HPUISwipeState.Stopped, timeDelta, state.startTime, state.startPosition,
cumilativeDirection, cumilativeDistance, delta);
activePriorityInteractable?.OnSwipe(swipeEventArgs);
interactor.OnSwipe(swipeEventArgs);

Expand Down Expand Up @@ -179,7 +182,7 @@ protected void Reset()
lowestTargetZIndex = int.MaxValue;
currentTrackingInteractable = null;
cumilativeDistance = 0;
cumilativeDelta = Vector2.zero;
cumilativeDirection = Vector2.zero;
}

/// <inheritdoc />
Expand All @@ -200,10 +203,10 @@ public void Update()
}

Vector2 currentPosition = currentTrackingInteractable.ComputeInteractorPostion(interactor);
Vector2 delta = previousPosition - currentPosition;
float timeDelta = Time.time - startTime;
delta = previousPosition - currentPosition;
timeDelta = Time.time - startTime;
cumilativeDistance += delta.magnitude;
cumilativeDelta += delta;
cumilativeDirection += delta;

switch(interactorGestureState)
{
Expand All @@ -214,12 +217,18 @@ public void Update()
ComputeActivePriorityInteractable();
using (hpuiSwipeEventArgsPool.Get(out HPUISwipeEventArgs swipeEventArgs))
{
// TODO
swipeEventArgs.interactorObject = interactor;
swipeEventArgs.interactableObject = activePriorityInteractable;
// swipeEventArgs.SetParams(interactor, activePriorityInteractable,
// HPUISwipeState.Started, timeDelta, state.startTime, state.startPosition, state.previousPosition,
// currentPosition, direction);
HPUIInteractionState state;
if (activePriorityInteractable != null)
{
state = activeInteractables[activePriorityInteractable];
}
else
{
state = HPUIInteractionState.empty;
}
swipeEventArgs.SetParams(interactor, activePriorityInteractable,
HPUISwipeState.Started, timeDelta, state.startTime, state.startPosition,
cumilativeDirection, cumilativeDistance, delta);
activePriorityInteractable?.OnSwipe(swipeEventArgs);
interactor.OnSwipe(swipeEventArgs);
}
Expand All @@ -228,12 +237,18 @@ public void Update()
case HPUIGestureState.Swipe:
using (hpuiSwipeEventArgsPool.Get(out HPUISwipeEventArgs swipeEventArgs))
{
// TODO
swipeEventArgs.interactorObject = interactor;
swipeEventArgs.interactableObject = activePriorityInteractable;
// swipeEventArgs.SetParams(interactor, activePriorityInteractable,
// HPUISwipeState.Updated, timeDelta, state.startTime, state.startPosition, state.previousPosition,
// currentPosition, direction);
HPUIInteractionState state;
if (activePriorityInteractable != null)
{
state = activeInteractables[activePriorityInteractable];
}
else
{
state = HPUIInteractionState.empty;
}
swipeEventArgs.SetParams(interactor, activePriorityInteractable,
HPUISwipeState.Updated, timeDelta, state.startTime, state.startPosition,
cumilativeDirection, cumilativeDistance, delta);
activePriorityInteractable?.OnSwipe(swipeEventArgs);
interactor.OnSwipe(swipeEventArgs);
}
Expand All @@ -257,13 +272,16 @@ public void Dispose()

class HPUIInteractionState
{
public static HPUIInteractionState empty = new HPUIInteractionState(0, Vector2.zero, false);
public float startTime;
public Vector2 startPosition;
public bool active;
public bool validTarget;

public HPUIInteractionState(float startTime, bool validTarget)
public HPUIInteractionState(float startTime, Vector2 startPosition, bool validTarget)
{
this.startTime = startTime;
this.startPosition = startPosition;
this.active = true;
this.validTarget = validTarget;
}
Expand Down

0 comments on commit af531ca

Please sign in to comment.