InputDispatcher.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. public class InputDispatcher<Event> : IInputDispatcher where Event : IInputEventBase
  6. {
  7. protected Dictionary<string, InputEventHandle<Event>> m_Listeners = new Dictionary<string, InputEventHandle<Event>>();
  8. /// <summary>
  9. /// 所有此类输入事件调用时都会调用
  10. /// </summary>
  11. public InputEventHandle<Event> OnEventDispatch;
  12. /// <summary>
  13. /// 基础输入类型和泛型输入类型在这里进行一次映射
  14. /// </summary>
  15. Dictionary<InputEventHandle<IInputEventBase>, InputEventHandle<Event>> m_ListenerHash = new Dictionary<InputEventHandle<IInputEventBase>, InputEventHandle<Event>>();
  16. public override void AddListener(string eventKey, InputEventHandle<IInputEventBase> callBack)
  17. {
  18. InputEventHandle<Event> temp = (inputEvent) =>
  19. {
  20. callBack((IInputEventBase)inputEvent);
  21. };
  22. m_ListenerHash.Add(callBack, temp);
  23. AddListener(eventKey, temp);
  24. }
  25. public override void RemoveListener(string eventKey, InputEventHandle<IInputEventBase> callBack)
  26. {
  27. if (!m_ListenerHash.ContainsKey(callBack))
  28. {
  29. throw new Exception("RemoveListener Exception: dont find Listener Hash ! eventKey: ->" + eventKey +"<-");
  30. }
  31. InputEventHandle<Event> temp = m_ListenerHash[callBack];
  32. m_ListenerHash.Remove(callBack);
  33. RemoveListener(eventKey, temp);
  34. }
  35. public override void Dispatch( IInputEventBase inputEvent)
  36. {
  37. Dispatch((Event)inputEvent);
  38. }
  39. public void AddListener(string eventKey, InputEventHandle<Event> callBack)
  40. {
  41. if (!m_Listeners.ContainsKey(eventKey))
  42. {
  43. m_Listeners.Add(eventKey, callBack);
  44. }
  45. else
  46. {
  47. m_Listeners[eventKey] += callBack;
  48. }
  49. }
  50. public void RemoveListener(string eventKey, InputEventHandle<Event> callBack)
  51. {
  52. if (m_Listeners.ContainsKey(eventKey))
  53. {
  54. m_Listeners[eventKey] -= callBack;
  55. }
  56. //else
  57. //{
  58. // Debug.LogError("不存在的UI事件 " + eventKey);
  59. //}
  60. }
  61. InputEventHandle<Event> m_handle;
  62. string m_eventKey;
  63. public void Dispatch(Event inputEvent)
  64. {
  65. m_eventKey = inputEvent.EventKey;
  66. if (m_Listeners.TryGetValue(m_eventKey,out m_handle))
  67. {
  68. DispatchSingleEvent(inputEvent, m_handle);
  69. }
  70. //此类事件派发时调用
  71. DispatchSingleEvent(inputEvent, OnEventDispatch);
  72. //所有事件派发时都调用
  73. AllEventDispatch(m_eventKey, inputEvent);
  74. }
  75. void DispatchSingleEvent(Event inputEvent, InputEventHandle<Event> callBack)
  76. {
  77. if (callBack != null)
  78. {
  79. try
  80. {
  81. callBack(inputEvent);
  82. }
  83. catch (Exception e)
  84. {
  85. Debug.LogError("DispatchSingleEvent Name: " + typeof(Event).ToString() + " key: " + inputEvent.EventKey + " Exception: " + e.ToString());
  86. }
  87. }
  88. }
  89. }