eventqueue.bmx 5.7 KB

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