UnityEventDispatcher.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2012-2017 DragonBones team and other contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. using System.Collections.Generic;
  24. using UnityEngine;
  25. namespace DragonBones
  26. {
  27. /**
  28. * @inheritDoc
  29. */
  30. public class UnityEventDispatcher<T> : MonoBehaviour
  31. {
  32. private readonly Dictionary<string, ListenerDelegate<T>> _listeners = new Dictionary<string, ListenerDelegate<T>>();
  33. /**
  34. * @private
  35. */
  36. public UnityEventDispatcher()
  37. {
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. public void DispatchEvent(string type, T eventObject)
  43. {
  44. if (!_listeners.ContainsKey(type))
  45. {
  46. return;
  47. }
  48. else
  49. {
  50. _listeners[type](type, eventObject);
  51. }
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. public bool HasEventListener(string type)
  57. {
  58. return _listeners.ContainsKey(type);
  59. }
  60. /**
  61. * @inheritDoc
  62. */
  63. public void AddEventListener(string type, ListenerDelegate<T> listener)
  64. {
  65. if (_listeners.ContainsKey(type))
  66. {
  67. var delegates = _listeners[type].GetInvocationList();
  68. for (int i = 0, l = delegates.Length; i < l; ++i)
  69. {
  70. if (listener == delegates[i] as ListenerDelegate<T>)
  71. {
  72. return;
  73. }
  74. }
  75. _listeners[type] += listener;
  76. }
  77. else
  78. {
  79. _listeners.Add(type, listener);
  80. }
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. public void RemoveEventListener(string type, ListenerDelegate<T> listener)
  86. {
  87. if (!_listeners.ContainsKey(type))
  88. {
  89. return;
  90. }
  91. var delegates = _listeners[type].GetInvocationList();
  92. for (int i = 0, l = delegates.Length; i < l; ++i)
  93. {
  94. if (listener == delegates[i] as ListenerDelegate<T>)
  95. {
  96. _listeners[type] -= listener;
  97. break;
  98. }
  99. }
  100. if (_listeners[type] == null)
  101. {
  102. _listeners.Remove(type);
  103. }
  104. }
  105. }
  106. [DisallowMultipleComponent]
  107. public class DragonBoneEventDispatcher : UnityEventDispatcher<EventObject>, IEventDispatcher<EventObject>
  108. {
  109. public void AddDBEventListener(string type, ListenerDelegate<EventObject> listener)
  110. {
  111. AddEventListener(type, listener);
  112. }
  113. public void DispatchDBEvent(string type, EventObject eventObject)
  114. {
  115. DispatchEvent(type, eventObject);
  116. }
  117. public bool HasDBEventListener(string type)
  118. {
  119. return HasEventListener(type);
  120. }
  121. public void RemoveDBEventListener(string type, ListenerDelegate<EventObject> listener)
  122. {
  123. RemoveEventListener(type, listener);
  124. }
  125. }
  126. }