mouse.monkey2 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 @hidden
  24. #end
  25. Method Reset()
  26. For Local i:=0 Until 4
  27. _pressed[i]=True
  28. _released[i]=True
  29. Next
  30. End
  31. #rem monkeydoc Pointer visiblity state.
  32. #end
  33. Property PointerVisible:Bool()
  34. Return SDL_ShowCursor( -1 )=SDL_ENABLE
  35. Setter( pointerVisible:Bool )
  36. SDL_ShowCursor( pointerVisible ? SDL_ENABLE Else SDL_DISABLE )
  37. End
  38. #rem monkeydoc X coordinate of the mouse location.
  39. #end
  40. Property X:Int()
  41. Return Location.x
  42. End
  43. #rem monkeydoc Y coordinate of the mouse location.
  44. #end
  45. Property Y:Int()
  46. Return Location.y
  47. End
  48. #rem monkeydoc The mouse location.
  49. #end
  50. Property Location:Vec2i()
  51. Return _location
  52. End
  53. #rem monkeydoc Checks the current up/down state of a mouse button.
  54. Returns true if `button` is currently held down.
  55. @param button Button to checl.
  56. #end
  57. Method ButtonDown:Bool( button:MouseButton )
  58. DebugAssert( button>=0 And button<4,"Mouse buttton out of range" )
  59. Return _buttons[button]
  60. End
  61. #rem monkeydoc Checks if a mouse button was pressed.
  62. Returns true if `button` was pressed since the last call to ButtonPressed with the same button.
  63. @param button Button to check.
  64. #end
  65. Method ButtonPressed:Bool( button:MouseButton )
  66. DebugAssert( button>=0 And button<4,"Mouse buttton out of range" )
  67. If _buttons[button]
  68. If _pressed[button] Return False
  69. _pressed[button]=True
  70. Return True
  71. Endif
  72. _pressed[button]=False
  73. Return False
  74. End
  75. #rem monkeydoc Checks if a mouse button was released.
  76. Returns true if `button` was released since the last call to ButtonReleased with the same button.
  77. @param button Button to check.
  78. #end
  79. Method ButtonReleased:Bool( button:MouseButton )
  80. DebugAssert( button>=0 And button<4,"Mouse buttton out of range" )
  81. If Not _buttons[button]
  82. If _released[button] Return False
  83. _released[button]=True
  84. Return True
  85. Endif
  86. _released[button]=False
  87. Return False
  88. End
  89. #rem monkeydoc @hidden
  90. #end
  91. Method ButtonHit:Bool( button:MouseButton )
  92. Return ButtonPressed( button )
  93. End
  94. '***** Internal *****
  95. #rem monkeydoc @hidden
  96. #end
  97. Method Init()
  98. If _init Return
  99. App.Idle+=Poll
  100. _init=True
  101. Reset()
  102. End
  103. Method SendEvent( event:SDL_Event Ptr )
  104. 'NOP for now...
  105. End
  106. Private
  107. Field _init:Bool
  108. Field _location:Vec2i
  109. Field _buttons:=New Bool[4]
  110. Field _pressed:=New Bool[4]
  111. Field _released:=New Bool[4]
  112. Method New()
  113. End
  114. Method Poll()
  115. Local mask:=SDL_GetMouseState( Varptr _location.x,Varptr _location.y )
  116. If App.ActiveWindow _location=App.ActiveWindow.TransformPointFromView( _location,Null )
  117. _buttons[MouseButton.Left]=mask & 1
  118. _buttons[MouseButton.Middle]=mask & 2
  119. _buttons[MouseButton.Right]=mask & 4
  120. App.Idle+=Poll
  121. End
  122. End