EventBridge.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #if FAIRYGUI_TOLUA
  2. using System;
  3. using LuaInterface;
  4. #endif
  5. namespace FairyGUI
  6. {
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. class EventBridge
  11. {
  12. public EventDispatcher owner;
  13. EventCallback0 _callback0;
  14. EventCallback1 _callback1;
  15. EventCallback1 _captureCallback;
  16. internal bool _dispatching;
  17. public EventBridge(EventDispatcher owner)
  18. {
  19. this.owner = owner;
  20. }
  21. public void AddCapture(EventCallback1 callback)
  22. {
  23. _captureCallback -= callback;
  24. _captureCallback += callback;
  25. }
  26. public void RemoveCapture(EventCallback1 callback)
  27. {
  28. _captureCallback -= callback;
  29. }
  30. public void Add(EventCallback1 callback)
  31. {
  32. _callback1 -= callback;
  33. _callback1 += callback;
  34. }
  35. public void Remove(EventCallback1 callback)
  36. {
  37. _callback1 -= callback;
  38. }
  39. public void Add(EventCallback0 callback)
  40. {
  41. _callback0 -= callback;
  42. _callback0 += callback;
  43. }
  44. public void Remove(EventCallback0 callback)
  45. {
  46. _callback0 -= callback;
  47. }
  48. #if FAIRYGUI_TOLUA
  49. public void Add(LuaFunction func, LuaTable self)
  50. {
  51. EventCallback1 callback;
  52. if(self != null)
  53. callback = (EventCallback1)DelegateTraits<EventCallback1>.Create(func, self);
  54. else
  55. callback = (EventCallback1)DelegateTraits<EventCallback1>.Create(func);
  56. _callback1 -= callback;
  57. _callback1 += callback;
  58. }
  59. public void Add(LuaFunction func, GComponent self)
  60. {
  61. if (self._peerTable == null)
  62. throw new Exception("self is not connected to lua.");
  63. Add(func, self._peerTable);
  64. }
  65. public void Remove(LuaFunction func, LuaTable self)
  66. {
  67. LuaState state = func.GetLuaState();
  68. LuaDelegate target;
  69. if (self != null)
  70. target = state.GetLuaDelegate(func, self);
  71. else
  72. target = state.GetLuaDelegate(func);
  73. Delegate[] ds = _callback1.GetInvocationList();
  74. for (int i = 0; i < ds.Length; i++)
  75. {
  76. LuaDelegate ld = ds[i].Target as LuaDelegate;
  77. if (ld != null && ld.Equals(target))
  78. {
  79. _callback1 = (EventCallback1)Delegate.Remove(_callback1, ds[i]);
  80. //DelayDispose will cause problem
  81. //state.DelayDispose(ld.func);
  82. //if (ld.self != null)
  83. // state.DelayDispose(ld.self);
  84. break;
  85. }
  86. }
  87. }
  88. public void Remove(LuaFunction func, GComponent self)
  89. {
  90. if (self._peerTable == null)
  91. throw new Exception("self is not connected to lua.");
  92. Remove(func, self._peerTable);
  93. }
  94. #endif
  95. public bool isEmpty
  96. {
  97. get { return _callback1 == null && _callback0 == null && _captureCallback == null; }
  98. }
  99. public void Clear()
  100. {
  101. #if FAIRYGUI_TOLUA
  102. //if (_callback1 != null)
  103. //{
  104. // Delegate[] ds = _callback1.GetInvocationList();
  105. // for (int i = 0; i < ds.Length; i++)
  106. // {
  107. // LuaDelegate ld = ds[i].Target as LuaDelegate;
  108. // if (ld != null)
  109. // {
  110. // LuaState state = ld.func.GetLuaState();
  111. // state.DelayDispose(ld.func);
  112. // if (ld.self != null)
  113. // state.DelayDispose(ld.self);
  114. // }
  115. // }
  116. //}
  117. #endif
  118. _callback1 = null;
  119. _callback0 = null;
  120. _captureCallback = null;
  121. }
  122. public void CallInternal(EventContext context)
  123. {
  124. _dispatching = true;
  125. context.sender = owner;
  126. try
  127. {
  128. if (_callback1 != null)
  129. _callback1(context);
  130. if (_callback0 != null)
  131. _callback0();
  132. }
  133. finally
  134. {
  135. _dispatching = false;
  136. }
  137. }
  138. public void CallCaptureInternal(EventContext context)
  139. {
  140. if (_captureCallback == null)
  141. return;
  142. _dispatching = true;
  143. context.sender = owner;
  144. try
  145. {
  146. _captureCallback(context);
  147. }
  148. finally
  149. {
  150. _dispatching = false;
  151. }
  152. }
  153. }
  154. }