PointableElement.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Assertions;
  5. namespace Rokid.UXR.Interaction
  6. {
  7. public class PointableElement : MonoBehaviour, IPointableElement
  8. {
  9. [HideInInspector, SerializeField]
  10. private bool _transferOnSecondSelection;
  11. [HideInInspector, SerializeField]
  12. private bool _addNewPointsToFront = false;
  13. [HideInInspector, SerializeField, Interface(typeof(IPointableElement)), Optional]
  14. private MonoBehaviour _forwardElement;
  15. public IPointableElement ForwardElement { get; private set; }
  16. #region Properties
  17. public bool TransferOnSecondSelection
  18. {
  19. get
  20. {
  21. return _transferOnSecondSelection;
  22. }
  23. set
  24. {
  25. _transferOnSecondSelection = value;
  26. }
  27. }
  28. public bool AddNewPointsToFront
  29. {
  30. get
  31. {
  32. return _addNewPointsToFront;
  33. }
  34. set
  35. {
  36. _addNewPointsToFront = value;
  37. }
  38. }
  39. #endregion
  40. public event Action<PointerEvent> WhenPointerEventRaised = delegate { };
  41. public List<Pose> Points => _points;
  42. public int PointsCount => _points.Count;
  43. public List<Pose> SelectingPoints => _selectingPoints;
  44. public int SelectingPointsCount => _selectingPoints.Count;
  45. protected List<Pose> _points;
  46. protected List<int> _pointIds;
  47. protected List<Pose> _selectingPoints;
  48. protected List<int> _selectingPointIds;
  49. protected virtual void Awake()
  50. {
  51. ForwardElement = _forwardElement as IPointableElement;
  52. _points = new List<Pose>();
  53. _pointIds = new List<int>();
  54. _selectingPoints = new List<Pose>();
  55. _selectingPointIds = new List<int>();
  56. }
  57. protected virtual void Start()
  58. {
  59. if (_forwardElement)
  60. {
  61. Assert.IsNotNull(ForwardElement);
  62. }
  63. }
  64. protected virtual void OnEnable()
  65. {
  66. if (ForwardElement != null)
  67. {
  68. ForwardElement.WhenPointerEventRaised += HandlePointerEventRaised;
  69. }
  70. }
  71. protected virtual void OnDisable()
  72. {
  73. while (_selectingPoints.Count > 0)
  74. {
  75. Cancel(new PointerEvent(_selectingPointIds[0], PointerEventType.Cancel, _selectingPoints[0]));
  76. }
  77. if (ForwardElement != null)
  78. {
  79. ForwardElement.WhenPointerEventRaised -= HandlePointerEventRaised;
  80. }
  81. }
  82. private void HandlePointerEventRaised(PointerEvent evt)
  83. {
  84. if (evt.Type == PointerEventType.Cancel)
  85. {
  86. ProcessPointerEvent(evt);
  87. }
  88. }
  89. public virtual void ProcessPointerEvent(PointerEvent evt)
  90. {
  91. switch (evt.Type)
  92. {
  93. case PointerEventType.Hover:
  94. Hover(evt);
  95. break;
  96. case PointerEventType.Unhover:
  97. Unhover(evt);
  98. break;
  99. case PointerEventType.Move:
  100. Move(evt);
  101. break;
  102. case PointerEventType.Select:
  103. Select(evt);
  104. break;
  105. case PointerEventType.Unselect:
  106. Unselect(evt);
  107. break;
  108. case PointerEventType.Cancel:
  109. Cancel(evt);
  110. break;
  111. }
  112. }
  113. private void Hover(PointerEvent evt)
  114. {
  115. if (_addNewPointsToFront)
  116. {
  117. _pointIds.Insert(0, evt.Identifier);
  118. _points.Insert(0, evt.Pose);
  119. }
  120. else
  121. {
  122. _pointIds.Add(evt.Identifier);
  123. _points.Add(evt.Pose);
  124. }
  125. PointableElementUpdated(evt);
  126. }
  127. private void Move(PointerEvent evt)
  128. {
  129. int index = _pointIds.IndexOf(evt.Identifier);
  130. if (index == -1)
  131. {
  132. return;
  133. }
  134. _points[index] = evt.Pose;
  135. index = _selectingPointIds.IndexOf(evt.Identifier);
  136. if (index != -1)
  137. {
  138. _selectingPoints[index] = evt.Pose;
  139. }
  140. PointableElementUpdated(evt);
  141. }
  142. private void Unhover(PointerEvent evt)
  143. {
  144. int index = _pointIds.IndexOf(evt.Identifier);
  145. if (index == -1)
  146. {
  147. return;
  148. }
  149. _pointIds.RemoveAt(index);
  150. _points.RemoveAt(index);
  151. PointableElementUpdated(evt);
  152. }
  153. private void Select(PointerEvent evt)
  154. {
  155. if (_selectingPoints.Count == 1 && _transferOnSecondSelection)
  156. {
  157. Cancel(new PointerEvent(_selectingPointIds[0], PointerEventType.Cancel, _selectingPoints[0]));
  158. }
  159. if (_addNewPointsToFront)
  160. {
  161. _selectingPointIds.Insert(0, evt.Identifier);
  162. _selectingPoints.Insert(0, evt.Pose);
  163. }
  164. else
  165. {
  166. _selectingPointIds.Add(evt.Identifier);
  167. _selectingPoints.Add(evt.Pose);
  168. }
  169. PointableElementUpdated(evt);
  170. }
  171. private void Unselect(PointerEvent evt)
  172. {
  173. int index = _selectingPointIds.IndexOf(evt.Identifier);
  174. if (index == -1)
  175. {
  176. return;
  177. }
  178. _selectingPointIds.RemoveAt(index);
  179. _selectingPoints.RemoveAt(index);
  180. PointableElementUpdated(evt);
  181. }
  182. private void Cancel(PointerEvent evt)
  183. {
  184. int index = _selectingPointIds.IndexOf(evt.Identifier);
  185. if (index != -1)
  186. {
  187. _selectingPointIds.RemoveAt(index);
  188. _selectingPoints.RemoveAt(index);
  189. }
  190. index = _pointIds.IndexOf(evt.Identifier);
  191. if (index != -1)
  192. {
  193. _pointIds.RemoveAt(index);
  194. _points.RemoveAt(index);
  195. }
  196. else
  197. {
  198. return;
  199. }
  200. PointableElementUpdated(evt);
  201. }
  202. protected virtual void PointableElementUpdated(PointerEvent evt)
  203. {
  204. if (ForwardElement != null)
  205. {
  206. ForwardElement.ProcessPointerEvent(evt);
  207. }
  208. RKLog.Debug($"====PointableElement====: PointableElementUpdated {ForwardElement == null} {gameObject.name} {evt.Pose.position}");
  209. WhenPointerEventRaised.Invoke(evt);
  210. }
  211. }
  212. }