// The purpose of this class is to check what object was tapped on the screen, UI element or brick.
// This is to reduce the conditional logic required for the listeners to check whether the event is relevant to them.
public class InputCategorizer : MonoBehaviour {
[SerializeField] private GameObject xrOrigin;
private ARRaycastManager _arRaycastManager;
private static List<ARRaycastHit> _arHits;
private void Start() {
_arHits = new List<ARRaycastHit>();
_arRaycastManager = xrOrigin.GetComponent<ARRaycastManager>();
InputManager.instance.Tapped += HandleTapped;
}
private void HandleTapped(Vector2 touchPosition, int touchID) {
if (IsUIElementTapped(touchID)) {
EventManager.instance.OnUIElementTapped(touchPosition);
}
else {
if (IsBrickTapped(touchPosition, out RaycastHit physicsHit)) {
EventManager.instance.OnBrickTapped(touchPosition, physicsHit);
}
else if (IsARPlaneTapped(touchPosition, _arHits)) {
EventManager.instance.OnARPlaneTapped(touchPosition, _arHits[0]);
}
}
}
private bool IsBrickTapped(Vector2 touchPosition, out RaycastHit hit) {
Ray ray = Camera.main.ScreenPointToRay(touchPosition);
Physics.Raycast(ray, out hit);
return hit.collider.gameObject.TryGetComponent<Brick>(out _);
}
private bool IsUIElementTapped(int touchId) {
return EventSystem.current.IsPointerOverGameObject(touchId);
}
private bool IsARPlaneTapped(Vector2 touchPosition, List<ARRaycastHit> arHits) {
_arRaycastManager.Raycast(touchPosition, arHits, TrackableType.Planes);
return arHits.Count > 0;
}
}
@analog quarry
Picking up from yesterday, what would you call this class?