|
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(); |
|
} |
|
} |
|
} |