mouse.monkey2 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 The MouseDevice class.
  19. To access the mouse device, use the global [[Mouse]] constant.
  20. The mouse device should only used after a new [[app.AppInstance]] is created.
  21. #end
  22. Class MouseDevice Extends InputDevice
  23. #rem monkeydoc Pointer visiblity state.
  24. #end
  25. Property PointerVisible:Bool()
  26. Return SDL_ShowCursor( -1 )=SDL_ENABLE
  27. Setter( pointerVisible:Bool )
  28. SDL_ShowCursor( pointerVisible ? SDL_ENABLE Else SDL_DISABLE )
  29. End
  30. #rem monkeydoc X coordinate of the mouse location.
  31. #end
  32. Property X:Int()
  33. Return Location.x
  34. End
  35. #rem monkeydoc Y coordinate of the mouse location.
  36. #end
  37. Property Y:Int()
  38. Return Location.y
  39. End
  40. #rem monkeydoc The mouse location.
  41. #end
  42. Property Location:Vec2i()
  43. Return _location
  44. End
  45. #rem monkeydoc The mouse wheel delta x value since the last app update.
  46. #end
  47. Property WheelX:Int()
  48. Return Wheel.x
  49. End
  50. #rem monkeydoc The mouse wheel delta y value since the last app update.
  51. #end
  52. Property WheelY:Int()
  53. Return Wheel.y
  54. End
  55. #rem monkeydoc The mouse wheel delta value since the last app update.
  56. #end
  57. Property Wheel:Vec2i()
  58. Return _wheel
  59. End
  60. #rem monkeydoc Checks the current up/down state of a mouse button.
  61. Returns true if `button` is currently held down.
  62. @param button Button to checl.
  63. #end
  64. Method ButtonDown:Bool( button:MouseButton )
  65. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  66. Return _down[button]
  67. End
  68. #rem monkeydoc Checks if a mouse button was pressed.
  69. Returns true if `button` was pressed since the last app update.
  70. @param button Button to check.
  71. #end
  72. Method ButtonPressed:Bool( button:MouseButton )
  73. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  74. Return _pressed[button]
  75. End
  76. #rem monkeydoc Checks if a mouse button was released.
  77. Returns true if `button` was released since the last app update.
  78. @param button Button to check.
  79. #end
  80. Method ButtonReleased:Bool( button:MouseButton )
  81. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  82. Return _released[button]
  83. End
  84. #rem monkeydoc @hidden
  85. #end
  86. Method ButtonHit:Bool( button:MouseButton )
  87. Return ButtonPressed( button )
  88. End
  89. '***** Internal *****
  90. #rem monkeydoc @hidden
  91. #end
  92. Method Init()
  93. End
  94. #rem monkeydoc @hidden
  95. #end
  96. Method Update()
  97. Local mask:=SDL_GetMouseState( Varptr _location.x,Varptr _location.y )
  98. If App.ActiveWindow _location=App.ActiveWindow.TransformPointFromView( App.ActiveWindow.MouseScale * _location,Null )
  99. UpdateButton( MouseButton.Left,mask & 1 )
  100. UpdateButton( MouseButton.Middle,mask & 2 )
  101. UpdateButton( MouseButton.Right,mask & 4)
  102. _wheel=Null
  103. End
  104. #rem monkeydoc @hidden
  105. #end
  106. Method SendEvent( event:SDL_Event Ptr )
  107. Select event->type
  108. Case SDL_MOUSEWHEEL
  109. Local mevent:=Cast<SDL_MouseWheelEvent Ptr>( event )
  110. Local window:=Window.WindowForID( mevent->windowID )
  111. If Not window Return
  112. _wheel+=New Vec2i( mevent->x,mevent->y )
  113. End
  114. End
  115. Private
  116. Field _init:Bool
  117. Field _location:Vec2i
  118. Field _wheel:Vec2i
  119. Field _down:=New Bool[4]
  120. Field _pressed:=New Bool[4]
  121. Field _released:=New Bool[4]
  122. Method New()
  123. End
  124. Method UpdateButton( button:MouseButton,down:Bool )
  125. _pressed[button]=False
  126. _released[button]=False
  127. If down=_down[button] Return
  128. If down _pressed[button]=True Else _released[button]=True
  129. _down[button]=down
  130. End
  131. End