mouse.monkey2 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. Namespace mojo.input
  2. #rem monkeydoc Global instance of the MouseDevice class.
  3. #end
  4. Const Mouse:=New MouseDevice
  5. #rem monkeydoc Mouse buttons.
  6. | MouseButton | Description
  7. |:--------------|------------
  8. | Left | Left mouse button.
  9. | Middle | Middle mouse button.
  10. | Right | Right mouse button.
  11. #end
  12. Enum MouseButton
  13. None=0
  14. Left=1
  15. Middle=2
  16. Right=3
  17. End
  18. #rem monkeydoc Mouse system cursors.
  19. | MouseCursor | Description
  20. |:--------------|:------------
  21. | Arrow | Arrow.
  22. | IBeam | I-Beam.
  23. | Wait | Wait.
  24. | CrossHair | Crosshair.
  25. | WaitArrow | Small wait cursor (or Wait if not available).
  26. | SizeNWSE | Double arrow pointing north-west and south-east.
  27. | SizeNESW | Double arrow pointing north-east and south-west.
  28. | SizeWE | Double arrow pointing west and east.
  29. | SizeNS | Double arrow pointing north and south.
  30. | SizeALL | Four pointed arrow pointing north, south, east, and west.
  31. | No | Slashed circle or crossbones.
  32. | Hand | Hand.
  33. #end
  34. Enum MouseCursor
  35. Arrow=0
  36. IBeam=1
  37. Wait=2
  38. CrossHair=3
  39. WaitArrow=4
  40. SizeNWSE=5
  41. SizeNESW=6
  42. SizeWE=7
  43. SizeNS=8
  44. SizeALL=9
  45. No=10
  46. Hand=11
  47. '
  48. Max=12
  49. End
  50. #rem monkeydoc The MouseDevice class.
  51. To access the mouse device, use the global [[Mouse]] constant.
  52. The mouse device should only used after a new [[AppInstance]] is created.
  53. #end
  54. Class MouseDevice
  55. #rem monkeydoc The mouse cursor, see [[MouseCursor]] enumeration for details.
  56. #end
  57. Property Cursor:MouseCursor()
  58. Return _cursor
  59. Setter( cursor:MouseCursor )
  60. _cursor = cursor
  61. SDL_SetCursor( _cursors[_cursor] )
  62. End
  63. #rem monkeydoc Pointer visiblity state.
  64. #end
  65. Property PointerVisible:Bool()
  66. Return SDL_ShowCursor( -1 )=SDL_ENABLE
  67. Setter( pointerVisible:Bool )
  68. SDL_ShowCursor( pointerVisible ? SDL_ENABLE Else SDL_DISABLE )
  69. End
  70. #rem monkeydoc X coordinate of the mouse location.
  71. #end
  72. Property X:Int()
  73. Return Location.x
  74. End
  75. #rem monkeydoc Y coordinate of the mouse location.
  76. #end
  77. Property Y:Int()
  78. Return Location.y
  79. End
  80. #rem monkeydoc The mouse location.
  81. This property may be written to warp the mouse to a new location.
  82. #end
  83. Property Location:Vec2i()
  84. Return _location
  85. Setter( location:Vec2i )
  86. _location=location
  87. If App.ActiveWindow location=App.ActiveWindow.TransformPointFromView( location / App.ActiveWindow.MouseScale,Null )
  88. SDL_WarpMouseInWindow( Null,location.x,location.y )
  89. End
  90. #rem monkeydoc The mouse wheel delta x value since the last app update.
  91. #end
  92. Property WheelX:Int()
  93. Return Wheel.x
  94. End
  95. #rem monkeydoc The mouse wheel delta y value since the last app update.
  96. #end
  97. Property WheelY:Int()
  98. Return Wheel.y
  99. End
  100. #rem monkeydoc The mouse wheel delta value since the last app update.
  101. #end
  102. Property Wheel:Vec2i()
  103. Return _wheel
  104. End
  105. #rem monkeydoc Checks the current up/down state of a mouse button.
  106. Returns true if `button` is currently held down.
  107. @param button Button to checl.
  108. #end
  109. Method ButtonDown:Bool( button:MouseButton )
  110. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  111. Return _down[button]
  112. End
  113. #rem monkeydoc Checks if a mouse button was pressed.
  114. Returns true if `button` was pressed since the last app update.
  115. @param button Button to check.
  116. #end
  117. Method ButtonPressed:Bool( button:MouseButton )
  118. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  119. Local pressed:=_pressed[button]
  120. _pressed[button]=False
  121. Return pressed
  122. End
  123. #rem monkeydoc Checks if a mouse button was released.
  124. Returns true if `button` was released since the last app update.
  125. @param button Button to check.
  126. #end
  127. Method ButtonReleased:Bool( button:MouseButton )
  128. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  129. Local released:=_released[button]
  130. _released[button]=False
  131. Return released
  132. End
  133. #rem monkeydoc @hidden
  134. #end
  135. Method ButtonHit:Bool( button:MouseButton )
  136. Return ButtonPressed( button )
  137. End
  138. Internal
  139. Method Init()
  140. _cursors=New SDL_Cursor Ptr[ MouseCursor.Max ]
  141. For Local i:=0 Until _cursors.Length
  142. _cursors[i]=SDL_CreateSystemCursor( Cast<SDL_SystemCursor>( i ) )
  143. Next
  144. _cursor=MouseCursor.Arrow
  145. SDL_SetCursor( _cursors[_cursor] )
  146. End
  147. Method Update()
  148. Local mask:=SDL_GetMouseState( Varptr _location.x,Varptr _location.y )
  149. If App.ActiveWindow _location=App.ActiveWindow.TransformPointFromView( App.ActiveWindow.MouseScale * _location,Null )
  150. UpdateButton( MouseButton.Left,mask & 1 )
  151. UpdateButton( MouseButton.Middle,mask & 2 )
  152. UpdateButton( MouseButton.Right,mask & 4)
  153. _wheel=Null
  154. End
  155. Method SendEvent( event:SDL_Event Ptr )
  156. Select event->type
  157. Case SDL_MOUSEWHEEL
  158. Local mevent:=Cast<SDL_MouseWheelEvent Ptr>( event )
  159. Local window:=Window.WindowForID( mevent->windowID )
  160. If Not window Return
  161. _wheel+=New Vec2i( mevent->x,mevent->y )
  162. End
  163. End
  164. Method Reset()
  165. For Local i:=0 Until 4
  166. _pressed[i]=False
  167. _released[i]=false
  168. next
  169. End
  170. Private
  171. Field _init:Bool
  172. Field _location:Vec2i
  173. Field _wheel:Vec2i
  174. Field _down:=New Bool[4]
  175. Field _pressed:=New Bool[4]
  176. Field _released:=New Bool[4]
  177. Field _cursors:=New SDL_Cursor Ptr[ MouseCursor.Max ]
  178. Field _cursor:MouseCursor
  179. Method New()
  180. End
  181. Method UpdateButton( button:MouseButton,down:Bool )
  182. '_pressed[button]=False
  183. '_released[button]=False
  184. If down=_down[button] Return
  185. If down _pressed[button]=True Else _released[button]=True
  186. _down[button]=down
  187. End
  188. End