Aetheria/Assets/Scripts/UI/ClickCatcher.cs

68 lines
1.9 KiB
C#
Raw Permalink Normal View History

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System;
2020-03-28 18:36:12 +00:00
using System.Collections;
using System.Collections.Generic;
using UniRx;
2020-03-28 18:36:12 +00:00
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickCatcher : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, /*IPointerUpHandler, */IPointerDownHandler, IPointerClickHandler
2020-03-28 18:36:12 +00:00
{
2020-03-29 04:19:24 +01:00
//public bool EnableMouse => PointerIsInside && !_catching;
2020-03-28 18:36:12 +00:00
public bool PointerIsInside { get; private set; }
public float DragDistance = 25f;
2020-03-28 18:36:12 +00:00
public Subject<PointerEventData> OnEnter = new Subject<PointerEventData>();
public Subject<PointerEventData> OnExit = new Subject<PointerEventData>();
public Subject<PointerEventData> OnClick = new Subject<PointerEventData>();
public Subject<PointerEventData> OnDown = new Subject<PointerEventData>();
2020-03-28 18:36:12 +00:00
// private float downTime;
2020-03-28 18:36:12 +00:00
public static ClickCatcher Background
{
get
{
if (_background == null)
_background = GameObject.FindGameObjectWithTag("UIBackground").GetComponent<ClickCatcher>();
return _background;
}
}
private static ClickCatcher _background;
public void OnPointerEnter(PointerEventData eventData)
{
//Debug.Log("Pointer Entered");
OnEnter.OnNext(eventData);
2020-03-28 18:36:12 +00:00
PointerIsInside = true;
}
public void OnPointerExit(PointerEventData eventData)
{
//Debug.Log("Pointer Exited");
OnExit.OnNext(eventData);
2020-03-28 18:36:12 +00:00
PointerIsInside = false;
}
// public void OnPointerUp(PointerEventData eventData)
// {
// if(Time.time - downTime < ClickTime)
// OnClick?.Invoke(eventData);
// }
//
public void OnPointerDown(PointerEventData eventData)
{
OnDown.OnNext(eventData);
}
2020-03-28 18:36:12 +00:00
public void OnPointerClick(PointerEventData eventData)
{
if((eventData.pressPosition - eventData.position).sqrMagnitude < DragDistance)
OnClick.OnNext(eventData);
2020-03-28 18:36:12 +00:00
}
}