polledinput.bmx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. Strict
  2. Rem
  3. bbdoc: User input/Polled input
  4. End Rem
  5. Module BRL.PolledInput
  6. ModuleInfo "Version: 1.01"
  7. ModuleInfo "Author: Mark Sibly, Simon Armstrong"
  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 charQueue bug"
  13. Import BRL.System
  14. Private
  15. Global enabled
  16. Global autoPoll=True
  17. Global inputSource:Object
  18. Global suspended,terminate
  19. Global keyStates[256],keyHits[256]
  20. Global charGet,charPut,charQueue[256]
  21. Global mouseStates[4],mouseHits[4]
  22. Global mouseLocation[4],lastMouseLocation[4]
  23. Function Hook:Object( id,data:Object,context:Object )
  24. Local ev:TEvent=TEvent(data)
  25. If Not ev Return data
  26. If inputSource And inputSource<>ev.source Return data
  27. Select ev.id
  28. Case EVENT_KEYDOWN
  29. If Not keyStates[ev.data]
  30. keyStates[ev.data]=1
  31. keyHits[ev.data]:+1
  32. EndIf
  33. Case EVENT_KEYUP
  34. keyStates[ev.data]=0
  35. Case EVENT_KEYCHAR
  36. If charPut-charGet<256
  37. charQueue[charPut & 255]=ev.data
  38. charPut:+1
  39. EndIf
  40. Case EVENT_MOUSEDOWN
  41. If Not mouseStates[ev.data]
  42. mouseStates[ev.data]=1
  43. mouseHits[ev.data]:+1
  44. EndIf
  45. Case EVENT_MOUSEUP
  46. mouseStates[ev.data]=0
  47. Case EVENT_MOUSEMOVE
  48. mouseLocation[0]=ev.x
  49. mouseLocation[1]=ev.y
  50. Case EVENT_MOUSEWHEEL
  51. mouseLocation[2]:+ev.data
  52. Case EVENT_APPSUSPEND
  53. FlushKeys
  54. FlushMouse
  55. suspended=True
  56. Case EVENT_APPRESUME
  57. FlushKeys
  58. FlushMouse
  59. suspended=False
  60. Case EVENT_APPTERMINATE
  61. terminate=True
  62. End Select
  63. Return data
  64. End Function
  65. Public
  66. Rem
  67. Currently only called by Graphics/bglCreateContext.
  68. Private for now, as it really needs a source:object parameter.
  69. End Rem
  70. Function EnablePolledInput( source:Object=Null )
  71. If enabled Return
  72. inputSource=source
  73. FlushKeys
  74. FlushMouse
  75. AddHook EmitEventHook,Hook,Null,0
  76. enabled=True
  77. End Function
  78. Rem
  79. Currently only called by EndGraphics/bglDeleteContext
  80. End Rem
  81. Function DisablePolledInput()
  82. If Not enabled Return
  83. RemoveHook EmitEventHook,Hook
  84. FlushKeys
  85. FlushMouse
  86. inputSource=Null
  87. enabled=False
  88. End Function
  89. Rem
  90. bbdoc: Get app suspended state
  91. returns: True if application is currently suspended.
  92. End Rem
  93. Function AppSuspended()
  94. If autoPoll PollSystem
  95. Return suspended
  96. End Function
  97. Rem
  98. bbdoc: Return app terminate state
  99. returns: True if user has requested to terminate application
  100. End Rem
  101. Function AppTerminate()
  102. If autoPoll PollSystem
  103. Local n=terminate
  104. terminate=False
  105. Return n
  106. End Function
  107. Rem
  108. bbdoc: Check for key hit
  109. returns: Number of times @key has been hit.
  110. about:
  111. The returned value represents the number of the times @key has been hit since the last
  112. call to #KeyHit with the same @key.
  113. See the #{key codes} module for a list of valid key codes.
  114. End Rem
  115. Function KeyHit( key )
  116. If autoPoll PollSystem
  117. Local n=keyHits[key]
  118. keyHits[key]=0
  119. Return n
  120. End Function
  121. Rem
  122. bbdoc: Check for key state
  123. returns: #True if @key is currently down
  124. about:
  125. See the #{key codes} module for a list of valid keycodes.
  126. End Rem
  127. Function KeyDown( key )
  128. If autoPoll PollSystem
  129. Return keyStates[key]
  130. End Function
  131. Rem
  132. bbdoc: Get next character
  133. returns: The character code of the next character.
  134. about:
  135. As the user hits keys on the keyboard, BlitzMax records the character codes of these
  136. keystrokes into an internal 'character queue'.
  137. #GetChar removes the next character code from this queue and returns it the application.
  138. If the character queue is empty, 0 is returned.
  139. End Rem
  140. Function GetChar()
  141. If autoPoll PollSystem
  142. If charGet=charPut Return 0
  143. Local n=charQueue[charGet & 255]
  144. charGet:+1
  145. Return n
  146. End Function
  147. Rem
  148. bbdoc: Flush key states and character queue.
  149. about:
  150. #FlushKeys resets the state of all keys to 'off', and resets the character queue
  151. used by #GetChar.
  152. End Rem
  153. Function FlushKeys()
  154. PollSystem
  155. charGet=0
  156. charPut=0
  157. For Local i=0 Until 256
  158. keyStates[i]=0
  159. keyHits[i]=0
  160. Next
  161. End Function
  162. Rem
  163. bbdoc: Get mouse x location
  164. returns: Mouse x axis location
  165. about:
  166. The returned value is relative to the left of the screen.
  167. end rem
  168. Function MouseX()
  169. If autoPoll PollSystem
  170. Return mouseLocation[0]
  171. End Function
  172. Rem
  173. bbdoc: Get mouse y location
  174. returns: Mouse y axis location
  175. about:
  176. The returned value is relative to the top of the screen.
  177. end rem
  178. Function MouseY()
  179. If autoPoll PollSystem
  180. Return mouseLocation[1]
  181. End Function
  182. Rem
  183. bbdoc: Get mouse wheel
  184. returns: Mouse wheel value
  185. about:
  186. The mouse wheel value increments when the mouse wheel is rolled 'away' from the user, and
  187. decrements when the mouse wheel is rolled 'towards' the user.
  188. end rem
  189. Function MouseZ()
  190. If autoPoll PollSystem
  191. Return mouseLocation[2]
  192. End Function
  193. Rem
  194. bbdoc: Get mouse x speed
  195. returns: Mouse x speed
  196. End Rem
  197. Function MouseXSpeed()
  198. If autoPoll PollSystem
  199. Local d=mouseLocation[0]-lastMouseLocation[0]
  200. lastMouseLocation[0]=mouseLocation[0]
  201. Return d
  202. EndFunction
  203. Rem
  204. bbdoc: Get mouse y speed
  205. returns: Mouse y speed
  206. End Rem
  207. Function MouseYSpeed()
  208. If autoPoll PollSystem
  209. Local d=mouseLocation[1]-lastMouseLocation[1]
  210. lastMouseLocation[1]=mouseLocation[1]
  211. Return d
  212. EndFunction
  213. Rem
  214. bbdoc: Get mouse z speed
  215. returns: Mouse z speed
  216. End Rem
  217. Function MouseZSpeed()
  218. If autoPoll PollSystem
  219. Local d=mouseLocation[2]-lastMouseLocation[2]
  220. lastMouseLocation[2]=mouseLocation[2]
  221. Return d
  222. EndFunction
  223. Rem
  224. bbdoc: Flush mouse button states
  225. about:
  226. #FlushMouse resets the state of all mouse buttons to 'off'.
  227. End Rem
  228. Function FlushMouse()
  229. PollSystem
  230. For Local i=0 Until 4
  231. mouseStates[i]=0
  232. mouseHits[i]=0
  233. Next
  234. mouseLocation[2]=0
  235. End Function
  236. Rem
  237. bbdoc: Check for mouse button click
  238. returns: Number of times @button has been clicked.
  239. about:
  240. The returned value represents the number of the times @button has been clicked since the
  241. last call to #MouseHit with the same @button.
  242. @button should be 1 for the left mouse button, 2 for the right mouse button or 3 for the
  243. middle mouse button.
  244. End Rem
  245. Function MouseHit( button )
  246. If autoPoll PollSystem
  247. Local n=mouseHits[button]
  248. mouseHits[button]=0
  249. Return n
  250. End Function
  251. Rem
  252. bbdoc: Check for mouse button down state
  253. returns: #True if @button is currently down
  254. about:
  255. @button should be 1 for the left mouse button, 2 for the right mouse button or 3 for the
  256. middle mouse button.
  257. end rem
  258. Function MouseDown( button )
  259. If autoPoll PollSystem
  260. Return mouseStates[button]
  261. End Function
  262. Rem
  263. bbdoc: Wait for a key press
  264. returns: The keycode of the pressed key
  265. about:
  266. #WaitKey suspends program execution until a key has been hit. The keycode of this
  267. key is then returned to the application.
  268. See the #{key codes} module for a list of valid keycodes.
  269. End Rem
  270. Function WaitKey()
  271. FlushKeys
  272. Repeat
  273. WaitSystem
  274. For Local n=1 To 255
  275. If KeyHit(n) Return n
  276. Next
  277. Forever
  278. End Function
  279. Rem
  280. bbdoc: Wait for a key press
  281. returns: The character code of the pressed key
  282. about:
  283. #WaitChar suspends program execution until a character is available from #GetChar. This
  284. character is then returned to the application.
  285. End Rem
  286. Function WaitChar()
  287. FlushKeys
  288. Repeat
  289. WaitSystem
  290. Local n=GetChar()
  291. If n Return n
  292. Forever
  293. End Function
  294. Rem
  295. bbdoc: Wait for mouse button click
  296. returns: The clicked button
  297. about:
  298. #WaitMouse suspends program execution until a mouse button is clicked.
  299. #WaitMouse returns 1 if the left mouse button was clicked, 2 if the right mouse button was
  300. clicked or 3 if the middle mouse button was clicked.
  301. End Rem
  302. Function WaitMouse()
  303. FlushMouse
  304. Repeat
  305. WaitSystem
  306. For Local n=1 To 3
  307. If MouseHit(n) Return n
  308. Next
  309. Forever
  310. End Function