polledinput.bmx 8.6 KB

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