Skip to content

Instantly share code, notes, and snippets.

@divide-by-zero
Last active October 17, 2015 15:45
Show Gist options
  • Save divide-by-zero/fa96866a7ef805c2e77f to your computer and use it in GitHub Desktop.
Save divide-by-zero/fa96866a7ef805c2e77f to your computer and use it in GitHub Desktop.
using UniRx;
using UniRx.Triggers;
using UnityEngine.EventSystems;
public class ObservableButtonDownTrigger : ObservableTriggerBase, IPointerDownHandler, IPointerUpHandler{
public bool IsPointerDown { private set; get; }
private Subject<bool> onButtonDown;
private int repeatStartFrame = 50;
private int repeatSpanFrame = 5;
private bool isOldPointerDown;
private int holdCount = 0;
private int repeatTargetFrame;
public IObservable<bool> OnButtonDown(int repeatStartFrame = 50,int repeatSpanFrame = 10)
{
this.repeatStartFrame = repeatStartFrame;
this.repeatSpanFrame = repeatSpanFrame;
return onButtonDown ?? (onButtonDown = new Subject<bool>());
}
private void Update() {
if (onButtonDown != null)
{
if (IsPointerDown)
{
if (isOldPointerDown == false)
{
onButtonDown.OnNext(true); //PUSH
holdCount = 0;
repeatTargetFrame = repeatStartFrame;
}
else
{
holdCount++;
if (holdCount >= repeatTargetFrame)
{
onButtonDown.OnNext(true); //HOLD
holdCount = 0;
repeatTargetFrame = repeatSpanFrame;
}
else
{
onButtonDown.OnNext(false);
}
}
}
else
{
onButtonDown.OnNext(false);
}
}
isOldPointerDown = IsPointerDown;
}
public void OnPointerDown(PointerEventData eventData)
{
IsPointerDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
IsPointerDown = false;
}
protected override void RaiseOnCompletedOnDestroy(){
if (onButtonDown != null){
onButtonDown.OnCompleted();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment