polledinput.bmx 8.2 KB

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