eventqueue.bmx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. Strict
  2. Rem
  3. bbdoc: Events/Event queue
  4. End Rem
  5. Module BRL.EventQueue
  6. ModuleInfo "Version: 1.01"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.01 Release"
  12. ModuleInfo "History: Fixed CurrentEvent being retained in queue array"
  13. ModuleInfo "History: 1.00 Release"
  14. ModuleInfo "History: Created module"
  15. Import BRL.Event
  16. Import BRL.System
  17. Private
  18. Const QUEUESIZE=256
  19. Const QUEUEMASK=QUEUESIZE-1
  20. Global queue:TEvent[QUEUESIZE],queue_put,queue_get
  21. Function Hook:Object( id,data:Object,context:Object )
  22. Local ev:TEvent=TEvent( data )
  23. If Not ev Return
  24. Select ev.id
  25. Case EVENT_WINDOWMOVE,EVENT_WINDOWSIZE,EVENT_TIMERTICK,EVENT_GADGETACTION
  26. PostEvent ev,True
  27. Default
  28. PostEvent ev
  29. End Select
  30. Return data
  31. End Function
  32. AddHook EmitEventHook,Hook,Null,-10000
  33. Global NullEvent:TEvent=New TEvent
  34. Public
  35. Rem
  36. bbdoc: Current Event
  37. about: The #CurrentEvent global variable contains the event most recently returned by
  38. #PollEvent or #WaitEvent.
  39. End Rem
  40. Global CurrentEvent:TEvent=NullEvent
  41. Rem
  42. bbdoc: Examine the next event in the event queue
  43. about:
  44. #PeekEvent examines the next event in the event queue, without removing it from the
  45. event queue or modifying the #CurrentEvent global variable.
  46. If there are no events in the event queue, #PeekEvent returns #Null.
  47. End Rem
  48. Function PeekEvent:TEvent()
  49. If queue_get=queue_put
  50. PollSystem
  51. If queue_get=queue_put Return Null
  52. EndIf
  53. Return queue[queue_get & QUEUEMASK]
  54. End Function
  55. Rem
  56. bbdoc: Get the next event from the event queue
  57. returns: The id of the next event in the event queue, or 0 if the event queue is empty
  58. about:
  59. #PollEvent removes an event from the event queue and updates the #CurrentEvent
  60. global variable.
  61. If there are no events in the event queue, #PollEvent returns 0.
  62. End Rem
  63. Function PollEvent()
  64. If queue_get=queue_put
  65. PollSystem
  66. If queue_get=queue_put
  67. CurrentEvent=NullEvent
  68. Return 0
  69. EndIf
  70. EndIf
  71. CurrentEvent=queue[queue_get & QUEUEMASK]
  72. queue[queue_get & QUEUEMASK]=Null
  73. queue_get:+1
  74. Return CurrentEvent.id
  75. End Function
  76. Rem
  77. bbdoc: Get the next event from the event queue, waiting if necessary
  78. returns: The id of the next event in the event queue
  79. about:
  80. #WaitEvent removes an event from the event queue and updates the #CurrentEvent
  81. global variable.
  82. If there are no events in the event queue, #WaitEvent halts program execution until
  83. an event is available.
  84. End Rem
  85. Function WaitEvent()
  86. While queue_get=queue_put
  87. WaitSystem
  88. Wend
  89. CurrentEvent=queue[queue_get & QUEUEMASK]
  90. queue[queue_get & QUEUEMASK]=Null
  91. queue_get:+1
  92. Return CurrentEvent.id
  93. End Function
  94. Rem
  95. bbdoc: Post an event to the event queue
  96. about:#PostEvent adds an event to the end of the event queue.
  97. The @update flag can be used to update an existing event. If @update is True
  98. and an event with the same @id and @source is found in the event
  99. queue, the existing event will be updated instead of @event
  100. being added to the event queue. This can be useful to prevent high frequency
  101. events such as timer events from flooding the event queue.
  102. End Rem
  103. Function PostEvent( event:TEvent,update=False )
  104. If update
  105. Local i=queue_get
  106. While i<>queue_put
  107. Local t:TEvent=queue[i & QUEUEMASK ]
  108. If t.id=event.id And t.source=event.source
  109. t.data=event.data
  110. t.mods=event.mods
  111. t.x=event.x
  112. t.y=event.y
  113. t.extra=event.extra
  114. Return
  115. EndIf
  116. i:+1
  117. Wend
  118. EndIf
  119. If queue_put-queue_get=QUEUESIZE Return
  120. queue[queue_put & QUEUEMASK]=event
  121. queue_put:+1
  122. End Function
  123. Rem
  124. bbdoc: Get current event id
  125. returns: The @id field of the #CurrentEvent global variable
  126. EndRem
  127. Function EventID()
  128. Return CurrentEvent.id
  129. End Function
  130. Rem
  131. bbdoc: Get current event data
  132. returns: The @data field of the #CurrentEvent global variable
  133. EndRem
  134. Function EventData()
  135. Return CurrentEvent.data
  136. End Function
  137. Rem
  138. bbdoc: Get current event modifiers
  139. returns: The @mods field of the #CurrentEvent global variable
  140. EndRem
  141. Function EventMods()
  142. Return CurrentEvent.mods
  143. End Function
  144. Rem
  145. bbdoc: Get current event x value
  146. returns: The @x field of the #CurrentEvent global variable
  147. EndRem
  148. Function EventX()
  149. Return CurrentEvent.x
  150. End Function
  151. Rem
  152. bbdoc: Get current event y value
  153. returns: The @y field of the #CurrentEvent global variable
  154. EndRem
  155. Function EventY()
  156. Return CurrentEvent.y
  157. End Function
  158. Rem
  159. bbdoc: Get current event extra value
  160. returns: The @extra field of the #CurrentEvent global variable
  161. EndRem
  162. Function EventExtra:Object()
  163. Return CurrentEvent.extra
  164. End Function
  165. Rem
  166. bbdoc: Get current event extra value converted to a string
  167. returns: The @extra field of the #CurrentEvent global variable converted to a string
  168. EndRem
  169. Function EventText$()
  170. Return String( CurrentEvent.extra )
  171. End Function
  172. Rem
  173. bbdoc: Get current event source object
  174. returns: The @source field of the #CurrentEvent global variable
  175. EndRem
  176. Function EventSource:Object()
  177. Return CurrentEvent.source
  178. End Function
  179. Rem
  180. bbdoc: Get current event source object handle
  181. returns: The @source field of the #CurrentEvent global variable converted to an integer handle
  182. EndRem
  183. Function EventSourceHandle()
  184. Return HandleFromObject( CurrentEvent.source )
  185. End Function