PointerInteractor.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using UnityEngine;
  2. namespace Rokid.UXR.Interaction
  3. {
  4. public abstract class PointerInteractor<TInteractor, TInteractable> : Interactor<TInteractor, TInteractable>
  5. where TInteractor : Interactor<TInteractor, TInteractable>
  6. where TInteractable : PointerInteractable<TInteractor, TInteractable>
  7. {
  8. public int realId => idIndex + Identifier;
  9. [SerializeField]
  10. public int idIndex = 0;
  11. /// <summary>
  12. /// 生成点的事件
  13. /// </summary>
  14. /// <param name="pointerEventType"></param>
  15. /// <param name="interactable"></param>
  16. protected void GeneratePointerEvent(PointerEventType pointerEventType, TInteractable interactable, int idIndex)
  17. {
  18. Pose pose = ComputePointerPose();
  19. if (interactable == null)
  20. {
  21. return;
  22. }
  23. if (interactable.PointableElement != null)
  24. {
  25. if (pointerEventType == PointerEventType.Hover)
  26. {
  27. interactable.PointableElement.WhenPointerEventRaised +=
  28. HandlePointerEventRaised;
  29. }
  30. else if (pointerEventType == PointerEventType.Unhover)
  31. {
  32. interactable.PointableElement.WhenPointerEventRaised -=
  33. HandlePointerEventRaised;
  34. }
  35. }
  36. interactable.PublishPointerEvent(
  37. new PointerEvent(Identifier + idIndex, pointerEventType, pose, Data));
  38. }
  39. /// <summary>
  40. /// 处理引发的事件指针
  41. /// </summary>
  42. /// <param name="evt"></param>
  43. protected virtual void HandlePointerEventRaised(PointerEvent evt)
  44. {
  45. if (evt.Identifier == Identifier &&
  46. evt.Type == PointerEventType.Cancel &&
  47. Interactable != null)
  48. {
  49. TInteractable interactable = Interactable;
  50. interactable.RemoveInteractorByIdentifier(Identifier);
  51. interactable.PointableElement.WhenPointerEventRaised -=
  52. HandlePointerEventRaised;
  53. }
  54. }
  55. protected override void InteractableSet(TInteractable interactable)
  56. {
  57. idIndex++;
  58. base.InteractableSet(interactable);
  59. GeneratePointerEvent(PointerEventType.Hover, interactable, idIndex);
  60. }
  61. protected override void InteractableUnset(TInteractable interactable)
  62. {
  63. GeneratePointerEvent(PointerEventType.Unhover, interactable, idIndex);
  64. base.InteractableUnset(interactable);
  65. }
  66. // 增加接口,长按button进入锁定状态后,需要cancel,以免退出锁定状态后触发click
  67. public void CancelWhenEnterLock()
  68. {
  69. if (_selectedInteractable == null)
  70. {
  71. return;
  72. }
  73. GeneratePointerEvent(PointerEventType.Cancel, _selectedInteractable, idIndex);
  74. }
  75. // cancel后重新获取hover
  76. public void ReHoverWhenLock()
  77. {
  78. if (_selectedInteractable == null)
  79. {
  80. return;
  81. }
  82. GeneratePointerEvent(PointerEventType.Hover, _selectedInteractable, idIndex);
  83. }
  84. protected override void InteractableSelected(TInteractable interactable)
  85. {
  86. base.InteractableSelected(interactable);
  87. GeneratePointerEvent(PointerEventType.Select, interactable, idIndex);
  88. }
  89. protected override void InteractableUnselected(TInteractable interactable)
  90. {
  91. GeneratePointerEvent(PointerEventType.Unselect, interactable, idIndex);
  92. base.InteractableUnselected(interactable);
  93. }
  94. protected override void DoPostprocess()
  95. {
  96. base.DoPostprocess();
  97. if (_interactable != null)
  98. {
  99. GeneratePointerEvent(PointerEventType.Move, _interactable, idIndex);
  100. }
  101. }
  102. protected abstract Pose ComputePointerPose();
  103. }
  104. }