Interactor.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Assertions;
  5. namespace Rokid.UXR.Interaction
  6. {
  7. /// <summary>
  8. /// Interactor provides a base template for any kind of interaction.
  9. /// Interactions can be wholly defined by three things: the concrete Interactor,
  10. /// the concrete Interactable, and the logic governing their coordination.
  11. ///
  12. /// Subclasses are responsible for implementing that coordination logic via template
  13. /// methods that operate on the concrete interactor and interactable classes.
  14. /// Interactor 为任何类型的交互提供了一个基本模板。
  15. /// 交互可以完全由三件事定义:具体的 Interactor、具体的 Interactable 以及控制它们协调的逻辑。
  16. /// 子类负责通过在具体交互器和可交互类上运行的模板方法来实现协调逻辑。
  17. /// </summary>
  18. public abstract class Interactor<TInteractor, TInteractable> : MonoBehaviour, IInteractor
  19. where TInteractor : Interactor<TInteractor, TInteractable>
  20. where TInteractable : Interactable<TInteractor, TInteractable>
  21. {
  22. [HideInInspector, SerializeField, Interface(typeof(IActiveState)), Optional]
  23. private MonoBehaviour _activeState;
  24. private IActiveState ActiveState = null;
  25. [SerializeField, Interface(typeof(IGameObjectFilter)), Optional]
  26. private List<MonoBehaviour> _interactableFilters = new List<MonoBehaviour>();
  27. private List<IGameObjectFilter> InteractableFilters = null;
  28. private Func<TInteractable> _computeCandidateOverride;
  29. private bool _clearComputeCandidateOverrideOnSelect = false;
  30. private Func<bool> _computeShouldSelectOverride;
  31. private bool _clearComputeShouldSelectOverrideOnSelect = false;
  32. private Func<bool> _computeShouldUnselectOverride;
  33. private bool _clearComputeShouldUnselectOverrideOnUnselect;
  34. protected virtual void DoPreprocess() { }
  35. protected virtual void DoNormalUpdate() { }
  36. protected virtual void DoHoverUpdate() { }
  37. protected virtual void DoSelectUpdate() { }
  38. public virtual bool ShouldHover
  39. {
  40. get
  41. {
  42. if (State != InteractorState.Normal)
  43. {
  44. return false;
  45. }
  46. return HasCandidate || ComputeShouldSelect();
  47. }
  48. }
  49. public virtual bool ShouldUnhover
  50. {
  51. get
  52. {
  53. if (State != InteractorState.Hover)
  54. {
  55. return false;
  56. }
  57. return _interactable != _candidate || _candidate == null;
  58. }
  59. }
  60. public bool ShouldSelect
  61. {
  62. set { }
  63. get
  64. {
  65. if (State != InteractorState.Hover)
  66. {
  67. return false;
  68. }
  69. if (_computeShouldSelectOverride != null)
  70. {
  71. return _computeShouldSelectOverride.Invoke();
  72. }
  73. return _candidate == _interactable && ComputeShouldSelect();
  74. }
  75. }
  76. public bool ShouldUnselect
  77. {
  78. get
  79. {
  80. if (State != InteractorState.Select)
  81. {
  82. return false;
  83. }
  84. if (_computeShouldUnselectOverride != null)
  85. {
  86. return _computeShouldUnselectOverride.Invoke();
  87. }
  88. return ComputeShouldUnselect();
  89. }
  90. }
  91. protected virtual bool ComputeShouldSelect()
  92. {
  93. return QueuedSelect;
  94. }
  95. protected virtual bool ComputeShouldUnselect()
  96. {
  97. return QueuedUnselect;
  98. }
  99. [SerializeField]
  100. private InteractorState _state = InteractorState.Normal;
  101. public event Action<InteractorStateChangeArgs> WhenStateChanged = delegate { };
  102. public event Action WhenPreprocessed = delegate { };
  103. public event Action WhenProcessed = delegate { };
  104. public event Action WhenPostprocessed = delegate { };
  105. private ISelector _selector = null;
  106. [SerializeField]
  107. private int _maxIterationsPerFrame = 3;
  108. public int MaxIterationsPerFrame
  109. {
  110. get
  111. {
  112. return _maxIterationsPerFrame;
  113. }
  114. set
  115. {
  116. _maxIterationsPerFrame = value;
  117. }
  118. }
  119. protected ISelector Selector
  120. {
  121. get
  122. {
  123. return _selector;
  124. }
  125. set
  126. {
  127. if (value != _selector)
  128. {
  129. if (_selector != null)
  130. {
  131. _selector.WhenSelected -= HandleSelected;
  132. _selector.WhenUnselected -= HandleUnselected;
  133. }
  134. }
  135. _selector = value;
  136. if (_selector != null)
  137. {
  138. _selector.WhenSelected += HandleSelected;
  139. _selector.WhenUnselected += HandleUnselected;
  140. }
  141. }
  142. }
  143. private Queue<bool> _selectorQueue = new Queue<bool>();
  144. private bool QueuedSelect => _selectorQueue.Count > 0 && _selectorQueue.Peek();
  145. private bool QueuedUnselect => _selectorQueue.Count > 0 && !_selectorQueue.Peek();
  146. /// <summary>
  147. /// 这里处理交互器状态的变化
  148. /// </summary>
  149. /// <value></value>
  150. public InteractorState State
  151. {
  152. get
  153. {
  154. return _state;
  155. }
  156. protected set
  157. {
  158. if (_state == value)
  159. {
  160. return;
  161. }
  162. InteractorState previousState = _state;
  163. _state = value;
  164. WhenStateChanged(new InteractorStateChangeArgs(previousState, _state));
  165. }
  166. }
  167. protected TInteractable _candidate;
  168. protected TInteractable _interactable;
  169. protected TInteractable _selectedInteractable;
  170. public virtual object CandidateProperties
  171. {
  172. get
  173. {
  174. return null;
  175. }
  176. }
  177. public TInteractable Candidate => _candidate;
  178. public TInteractable Interactable => _interactable;
  179. public TInteractable SelectedInteractable => _selectedInteractable;
  180. public bool HasCandidate => _candidate != null;
  181. public bool HasInteractable => _interactable != null;
  182. public bool HasSelectedInteractable => _selectedInteractable != null;
  183. private MultiAction<TInteractable> _whenInteractableSet = new MultiAction<TInteractable>();
  184. private MultiAction<TInteractable> _whenInteractableUnset = new MultiAction<TInteractable>();
  185. private MultiAction<TInteractable> _whenInteractableSelected = new MultiAction<TInteractable>();
  186. private MultiAction<TInteractable> _whenInteractableUnselected = new MultiAction<TInteractable>();
  187. public MAction<TInteractable> WhenInteractableSet => _whenInteractableSet;
  188. public MAction<TInteractable> WhenInteractableUnset => _whenInteractableUnset;
  189. public MAction<TInteractable> WhenInteractableSelected => _whenInteractableSelected;
  190. public MAction<TInteractable> WhenInteractableUnselected => _whenInteractableUnselected;
  191. protected virtual void InteractableSet(TInteractable interactable)
  192. {
  193. _whenInteractableSet.Invoke(interactable);
  194. }
  195. protected virtual void InteractableUnset(TInteractable interactable)
  196. {
  197. _whenInteractableUnset.Invoke(interactable);
  198. }
  199. protected virtual void InteractableSelected(TInteractable interactable)
  200. {
  201. _whenInteractableSelected.Invoke(interactable);
  202. }
  203. protected virtual void InteractableUnselected(TInteractable interactable)
  204. {
  205. _whenInteractableUnselected.Invoke(interactable);
  206. }
  207. protected virtual void DoPostprocess() { }
  208. private UniqueIdentifier _identifier;
  209. public int Identifier => _identifier.ID;
  210. [HideInInspector, SerializeField, Optional]
  211. private UnityEngine.Object _data = null;
  212. public object Data { get; protected set; } = null;
  213. protected virtual void Awake()
  214. {
  215. _identifier = UniqueIdentifier.Generate();
  216. ActiveState = _activeState as IActiveState;
  217. InteractableFilters =
  218. _interactableFilters.ConvertAll(mono => mono as IGameObjectFilter);
  219. }
  220. protected virtual void Start()
  221. {
  222. foreach (IGameObjectFilter filter in InteractableFilters)
  223. {
  224. Assert.IsNotNull(filter);
  225. }
  226. if (Data == null)
  227. {
  228. _data = this;
  229. Data = _data;
  230. }
  231. }
  232. protected virtual void OnEnable()
  233. {
  234. _selectorQueue.Clear();
  235. if (_selector != null)
  236. {
  237. _selector.WhenSelected += HandleSelected;
  238. _selector.WhenUnselected += HandleUnselected;
  239. }
  240. }
  241. protected virtual void OnDisable()
  242. {
  243. if (_selector != null)
  244. {
  245. _selector.WhenSelected -= HandleSelected;
  246. _selector.WhenUnselected -= HandleUnselected;
  247. }
  248. Disable();
  249. }
  250. protected virtual void OnDestroy()
  251. {
  252. UniqueIdentifier.Release(_identifier);
  253. }
  254. public virtual void SetComputeCandidateOverride(Func<TInteractable> computeCandidate,
  255. bool shouldClearOverrideOnSelect = true)
  256. {
  257. _computeCandidateOverride = computeCandidate;
  258. _clearComputeCandidateOverrideOnSelect = shouldClearOverrideOnSelect;
  259. }
  260. public virtual void ClearComputeCandidateOverride()
  261. {
  262. _computeCandidateOverride = null;
  263. _clearComputeCandidateOverrideOnSelect = false;
  264. }
  265. public virtual void SetComputeShouldSelectOverride(Func<bool> computeShouldSelect,
  266. bool clearOverrideOnSelect = true)
  267. {
  268. _computeShouldSelectOverride = computeShouldSelect;
  269. _clearComputeShouldSelectOverrideOnSelect = clearOverrideOnSelect;
  270. }
  271. public virtual void ClearComputeShouldSelectOverride()
  272. {
  273. _computeShouldSelectOverride = null;
  274. _clearComputeShouldSelectOverrideOnSelect = false;
  275. }
  276. public virtual void SetComputeShouldUnselectOverride(Func<bool> computeShouldUnselect,
  277. bool clearOverrideOnUnselect = true)
  278. {
  279. _computeShouldUnselectOverride = computeShouldUnselect;
  280. _clearComputeShouldUnselectOverrideOnUnselect = clearOverrideOnUnselect;
  281. }
  282. public virtual void ClearComputeShouldUnselectOverride()
  283. {
  284. _computeShouldUnselectOverride = null;
  285. _clearComputeShouldUnselectOverrideOnUnselect = false;
  286. }
  287. public void Preprocess()
  288. {
  289. DoPreprocess();
  290. if (!UpdateActiveState())
  291. {
  292. Disable();
  293. }
  294. WhenPreprocessed();
  295. }
  296. public void Process()
  297. {
  298. switch (State)
  299. {
  300. case InteractorState.Normal:
  301. DoNormalUpdate();
  302. break;
  303. case InteractorState.Hover:
  304. DoHoverUpdate();
  305. break;
  306. case InteractorState.Select:
  307. DoSelectUpdate();
  308. break;
  309. }
  310. WhenProcessed();
  311. }
  312. public void Postprocess()
  313. {
  314. _selectorQueue.Clear();
  315. DoPostprocess();
  316. WhenPostprocessed();
  317. }
  318. /// <summary>
  319. /// 处理候选者,获取相应的候选者
  320. /// </summary>
  321. public virtual void ProcessCandidate()
  322. {
  323. _candidate = null;
  324. if (!UpdateActiveState())
  325. {
  326. return;
  327. }
  328. if (_computeCandidateOverride != null)
  329. {
  330. _candidate = _computeCandidateOverride.Invoke();
  331. }
  332. else
  333. {
  334. _candidate = ComputeCandidate();
  335. }
  336. }
  337. public void InteractableChangesUpdate()
  338. {
  339. if (_selectedInteractable != null &&
  340. !_selectedInteractable.HasSelectingInteractor(this as TInteractor))
  341. {
  342. UnselectInteractable();
  343. }
  344. if (_interactable != null &&
  345. !_interactable.HasInteractor(this as TInteractor))
  346. {
  347. UnsetInteractable();
  348. }
  349. }
  350. public void Hover()
  351. {
  352. if (State != InteractorState.Normal)
  353. {
  354. return;
  355. }
  356. SetInteractable(_candidate);
  357. State = InteractorState.Hover;
  358. }
  359. public void Unhover()
  360. {
  361. if (State != InteractorState.Hover)
  362. {
  363. return;
  364. }
  365. UnsetInteractable();
  366. State = InteractorState.Normal;
  367. }
  368. public virtual void Select()
  369. {
  370. if (State != InteractorState.Hover)
  371. {
  372. return;
  373. }
  374. if (_clearComputeCandidateOverrideOnSelect)
  375. {
  376. ClearComputeCandidateOverride();
  377. }
  378. if (_clearComputeShouldSelectOverrideOnSelect)
  379. {
  380. ClearComputeShouldSelectOverride();
  381. }
  382. while (QueuedSelect)
  383. {
  384. _selectorQueue.Dequeue();
  385. }
  386. if (Interactable != null)
  387. {
  388. SelectInteractable(Interactable);
  389. }
  390. State = InteractorState.Select;
  391. }
  392. public virtual void Unselect()
  393. {
  394. if (State != InteractorState.Select)
  395. {
  396. return;
  397. }
  398. if (_clearComputeShouldUnselectOverrideOnUnselect)
  399. {
  400. ClearComputeShouldUnselectOverride();
  401. }
  402. while (QueuedUnselect)
  403. {
  404. _selectorQueue.Dequeue();
  405. }
  406. UnselectInteractable();
  407. State = InteractorState.Hover;
  408. }
  409. // Returns the best interactable for selection or null
  410. protected abstract TInteractable ComputeCandidate();
  411. public virtual bool CanSelect(TInteractable interactable)
  412. {
  413. if (InteractableFilters == null)
  414. {
  415. return true;
  416. }
  417. foreach (IGameObjectFilter interactableFilter in InteractableFilters)
  418. {
  419. if (!interactableFilter.Filter(interactable.gameObject))
  420. {
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. private void SetInteractable(TInteractable interactable)
  427. {
  428. if (_interactable == interactable)
  429. {
  430. return;
  431. }
  432. UnsetInteractable();
  433. _interactable = interactable;
  434. interactable.AddInteractor(this as TInteractor);
  435. InteractableSet(interactable);
  436. }
  437. private void UnsetInteractable()
  438. {
  439. TInteractable interactable = _interactable;
  440. if (interactable == null)
  441. {
  442. return;
  443. }
  444. _interactable = null;
  445. interactable.RemoveInteractor(this as TInteractor);
  446. InteractableUnset(interactable);
  447. }
  448. private void SelectInteractable(TInteractable interactable)
  449. {
  450. Unselect();
  451. _selectedInteractable = interactable;
  452. interactable.AddSelectingInteractor(this as TInteractor);
  453. InteractableSelected(interactable);
  454. }
  455. private void UnselectInteractable()
  456. {
  457. TInteractable interactable = _selectedInteractable;
  458. if (interactable == null)
  459. {
  460. return;
  461. }
  462. _selectedInteractable = null;
  463. interactable.RemoveSelectingInteractor(this as TInteractor);
  464. InteractableUnselected(interactable);
  465. }
  466. public void Enable()
  467. {
  468. if (!UpdateActiveState())
  469. {
  470. return;
  471. }
  472. if (State == InteractorState.Disabled)
  473. {
  474. State = InteractorState.Normal;
  475. HandleEnabled();
  476. }
  477. }
  478. public void Disable()
  479. {
  480. if (State == InteractorState.Disabled)
  481. {
  482. return;
  483. }
  484. HandleDisabled();
  485. if (State == InteractorState.Select)
  486. {
  487. UnselectInteractable();
  488. State = InteractorState.Hover;
  489. }
  490. if (State == InteractorState.Hover)
  491. {
  492. UnsetInteractable();
  493. State = InteractorState.Normal;
  494. }
  495. if (State == InteractorState.Normal)
  496. {
  497. State = InteractorState.Disabled;
  498. }
  499. }
  500. protected virtual void HandleEnabled() { }
  501. protected virtual void HandleDisabled() { }
  502. protected virtual void HandleSelected()
  503. {
  504. _selectorQueue.Enqueue(true);
  505. }
  506. protected virtual void HandleUnselected()
  507. {
  508. _selectorQueue.Enqueue(false);
  509. }
  510. private bool UpdateActiveState()
  511. {
  512. if (ActiveState == null || ActiveState.Active)
  513. {
  514. return true;
  515. }
  516. return false;
  517. }
  518. public bool IsRootDriver { get; set; } = true;
  519. protected virtual void Update()
  520. {
  521. if (!IsRootDriver)
  522. {
  523. return;
  524. }
  525. Drive();
  526. }
  527. public virtual void Drive()
  528. {
  529. Preprocess();
  530. if (!UpdateActiveState())
  531. {
  532. Disable();
  533. Postprocess();
  534. return;
  535. }
  536. Enable();
  537. InteractorState previousState = State;
  538. for (int i = 0; i < MaxIterationsPerFrame; i++)
  539. {
  540. if (State == InteractorState.Normal ||
  541. (State == InteractorState.Hover && previousState != InteractorState.Normal))
  542. {
  543. ProcessCandidate();
  544. }
  545. previousState = State;
  546. Process();
  547. if (State == InteractorState.Disabled)
  548. {
  549. break;
  550. }
  551. if (State == InteractorState.Normal)
  552. {
  553. RKLog.Debug($"Interactor Process Hover: {ShouldHover}");
  554. if (ShouldHover)
  555. {
  556. Hover();
  557. continue;
  558. }
  559. break;
  560. }
  561. if (State == InteractorState.Hover)
  562. {
  563. if (ShouldSelect)
  564. {
  565. Select();
  566. continue;
  567. }
  568. if (ShouldUnhover)
  569. {
  570. Unhover();
  571. continue;
  572. }
  573. break;
  574. }
  575. if (State == InteractorState.Select)
  576. {
  577. if (ShouldUnselect)
  578. {
  579. Unselect();
  580. continue;
  581. }
  582. break;
  583. }
  584. }
  585. Postprocess();
  586. RKLog.Debug($"====Interaction==== Interactor State: {gameObject.name} {_state}");
  587. }
  588. }
  589. }