123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Assertions;
- namespace Rokid.UXR.Interaction
- {
- public class PointableElement : MonoBehaviour, IPointableElement
- {
- [HideInInspector, SerializeField]
- private bool _transferOnSecondSelection;
- [HideInInspector, SerializeField]
- private bool _addNewPointsToFront = false;
- [HideInInspector, SerializeField, Interface(typeof(IPointableElement)), Optional]
- private MonoBehaviour _forwardElement;
- public IPointableElement ForwardElement { get; private set; }
- #region Properties
- public bool TransferOnSecondSelection
- {
- get
- {
- return _transferOnSecondSelection;
- }
- set
- {
- _transferOnSecondSelection = value;
- }
- }
- public bool AddNewPointsToFront
- {
- get
- {
- return _addNewPointsToFront;
- }
- set
- {
- _addNewPointsToFront = value;
- }
- }
- #endregion
- public event Action<PointerEvent> WhenPointerEventRaised = delegate { };
- public List<Pose> Points => _points;
- public int PointsCount => _points.Count;
- public List<Pose> SelectingPoints => _selectingPoints;
- public int SelectingPointsCount => _selectingPoints.Count;
- protected List<Pose> _points;
- protected List<int> _pointIds;
- protected List<Pose> _selectingPoints;
- protected List<int> _selectingPointIds;
- protected virtual void Awake()
- {
- ForwardElement = _forwardElement as IPointableElement;
- _points = new List<Pose>();
- _pointIds = new List<int>();
- _selectingPoints = new List<Pose>();
- _selectingPointIds = new List<int>();
- }
- protected virtual void Start()
- {
- if (_forwardElement)
- {
- Assert.IsNotNull(ForwardElement);
- }
- }
- protected virtual void OnEnable()
- {
- if (ForwardElement != null)
- {
- ForwardElement.WhenPointerEventRaised += HandlePointerEventRaised;
- }
- }
- protected virtual void OnDisable()
- {
- while (_selectingPoints.Count > 0)
- {
- Cancel(new PointerEvent(_selectingPointIds[0], PointerEventType.Cancel, _selectingPoints[0]));
- }
- if (ForwardElement != null)
- {
- ForwardElement.WhenPointerEventRaised -= HandlePointerEventRaised;
- }
- }
- private void HandlePointerEventRaised(PointerEvent evt)
- {
- if (evt.Type == PointerEventType.Cancel)
- {
- ProcessPointerEvent(evt);
- }
- }
- public virtual void ProcessPointerEvent(PointerEvent evt)
- {
- switch (evt.Type)
- {
- case PointerEventType.Hover:
- Hover(evt);
- break;
- case PointerEventType.Unhover:
- Unhover(evt);
- break;
- case PointerEventType.Move:
- Move(evt);
- break;
- case PointerEventType.Select:
- Select(evt);
- break;
- case PointerEventType.Unselect:
- Unselect(evt);
- break;
- case PointerEventType.Cancel:
- Cancel(evt);
- break;
- }
- }
- private void Hover(PointerEvent evt)
- {
- if (_addNewPointsToFront)
- {
- _pointIds.Insert(0, evt.Identifier);
- _points.Insert(0, evt.Pose);
- }
- else
- {
- _pointIds.Add(evt.Identifier);
- _points.Add(evt.Pose);
- }
- PointableElementUpdated(evt);
- }
- private void Move(PointerEvent evt)
- {
- int index = _pointIds.IndexOf(evt.Identifier);
- if (index == -1)
- {
- return;
- }
- _points[index] = evt.Pose;
- index = _selectingPointIds.IndexOf(evt.Identifier);
- if (index != -1)
- {
- _selectingPoints[index] = evt.Pose;
- }
- PointableElementUpdated(evt);
- }
- private void Unhover(PointerEvent evt)
- {
- int index = _pointIds.IndexOf(evt.Identifier);
- if (index == -1)
- {
- return;
- }
- _pointIds.RemoveAt(index);
- _points.RemoveAt(index);
- PointableElementUpdated(evt);
- }
- private void Select(PointerEvent evt)
- {
- if (_selectingPoints.Count == 1 && _transferOnSecondSelection)
- {
- Cancel(new PointerEvent(_selectingPointIds[0], PointerEventType.Cancel, _selectingPoints[0]));
- }
- if (_addNewPointsToFront)
- {
- _selectingPointIds.Insert(0, evt.Identifier);
- _selectingPoints.Insert(0, evt.Pose);
- }
- else
- {
- _selectingPointIds.Add(evt.Identifier);
- _selectingPoints.Add(evt.Pose);
- }
- PointableElementUpdated(evt);
- }
- private void Unselect(PointerEvent evt)
- {
- int index = _selectingPointIds.IndexOf(evt.Identifier);
- if (index == -1)
- {
- return;
- }
- _selectingPointIds.RemoveAt(index);
- _selectingPoints.RemoveAt(index);
- PointableElementUpdated(evt);
- }
- private void Cancel(PointerEvent evt)
- {
- int index = _selectingPointIds.IndexOf(evt.Identifier);
- if (index != -1)
- {
- _selectingPointIds.RemoveAt(index);
- _selectingPoints.RemoveAt(index);
- }
- index = _pointIds.IndexOf(evt.Identifier);
- if (index != -1)
- {
- _pointIds.RemoveAt(index);
- _points.RemoveAt(index);
- }
- else
- {
- return;
- }
- PointableElementUpdated(evt);
- }
- protected virtual void PointableElementUpdated(PointerEvent evt)
- {
- if (ForwardElement != null)
- {
- ForwardElement.ProcessPointerEvent(evt);
- }
- RKLog.Debug($"====PointableElement====: PointableElementUpdated {ForwardElement == null} {gameObject.name} {evt.Pose.position}");
- WhenPointerEventRaised.Invoke(evt);
- }
- }
- }
|