mouse.monkey2 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. SDL_WarpMouseInWindow( Null,location.x,location.y )
  88. End
  89. #rem monkeydoc The mouse wheel delta x value since the last app update.
  90. #end
  91. Property WheelX:Int()
  92. Return Wheel.x
  93. End
  94. #rem monkeydoc The mouse wheel delta y value since the last app update.
  95. #end
  96. Property WheelY:Int()
  97. Return Wheel.y
  98. End
  99. #rem monkeydoc The mouse wheel delta value since the last app update.
  100. #end
  101. Property Wheel:Vec2i()
  102. Return _wheel
  103. End
  104. #rem monkeydoc Checks the current up/down state of a mouse button.
  105. Returns true if `button` is currently held down.
  106. @param button Button to checl.
  107. #end
  108. Method ButtonDown:Bool( button:MouseButton )
  109. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  110. Return _down[button]
  111. End
  112. #rem monkeydoc Checks if a mouse button was pressed.
  113. Returns true if `button` was pressed since the last app update.
  114. @param button Button to check.
  115. #end
  116. Method ButtonPressed:Bool( button:MouseButton )
  117. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  118. Local pressed:=_pressed[button]
  119. _pressed[button]=False
  120. Return pressed
  121. End
  122. #rem monkeydoc Checks if a mouse button was released.
  123. Returns true if `button` was released since the last app update.
  124. @param button Button to check.
  125. #end
  126. Method ButtonReleased:Bool( button:MouseButton )
  127. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  128. Local released:=_released[button]
  129. _released[button]=False
  130. Return released
  131. End
  132. #rem monkeydoc @hidden
  133. #end
  134. Method ButtonHit:Bool( button:MouseButton )
  135. Return ButtonPressed( button )
  136. End
  137. Internal
  138. Method Init()
  139. _cursors=New SDL_Cursor Ptr[ MouseCursor.Max ]
  140. For Local i:=0 Until _cursors.Length
  141. _cursors[i]=SDL_CreateSystemCursor( Cast<SDL_SystemCursor>( i ) )
  142. Next
  143. _cursor=MouseCursor.Arrow
  144. SDL_SetCursor( _cursors[_cursor] )
  145. End
  146. Method Update()
  147. Local mask:=SDL_GetMouseState( Varptr _location.x,Varptr _location.y )
  148. If App.ActiveWindow _location=App.ActiveWindow.TransformPointFromView( App.ActiveWindow.MouseScale * _location,Null )
  149. UpdateButton( MouseButton.Left,mask & 1 )
  150. UpdateButton( MouseButton.Middle,mask & 2 )
  151. UpdateButton( MouseButton.Right,mask & 4)
  152. _wheel=Null
  153. End
  154. Method SendEvent( event:SDL_Event Ptr )
  155. Select event->type
  156. Case SDL_MOUSEWHEEL
  157. Local mevent:=Cast<SDL_MouseWheelEvent Ptr>( event )
  158. Local window:=Window.WindowForID( mevent->windowID )
  159. If Not window Return
  160. _wheel+=New Vec2i( mevent->x,mevent->y )
  161. End
  162. End
  163. Method Reset()
  164. For Local i:=0 Until 4
  165. _pressed[i]=False
  166. _released[i]=false
  167. next
  168. End
  169. Private
  170. Field _init:Bool
  171. Field _location:Vec2i
  172. Field _wheel:Vec2i
  173. Field _down:=New Bool[4]
  174. Field _pressed:=New Bool[4]
  175. Field _released:=New Bool[4]
  176. Field _cursors:=New SDL_Cursor Ptr[ MouseCursor.Max ]
  177. Field _cursor:MouseCursor
  178. Method New()
  179. End
  180. Method UpdateButton( button:MouseButton,down:Bool )
  181. '_pressed[button]=False
  182. '_released[button]=False
  183. If down=_down[button] Return
  184. If down _pressed[button]=True Else _released[button]=True
  185. _down[button]=down
  186. End
  187. End