mouse.monkey2 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Checks the current up/down state of a mouse button.
  46. Returns true if `button` is currently held down.
  47. @param button Button to checl.
  48. #end
  49. Method ButtonDown:Bool( button:MouseButton )
  50. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  51. Return _down[button]
  52. End
  53. #rem monkeydoc Checks if a mouse button was pressed.
  54. Returns true if `button` was pressed since the last app update.
  55. @param button Button to check.
  56. #end
  57. Method ButtonPressed:Bool( button:MouseButton )
  58. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  59. Return _pressed[button]
  60. End
  61. #rem monkeydoc Checks if a mouse button was released.
  62. Returns true if `button` was released since the last app update.
  63. @param button Button to check.
  64. #end
  65. Method ButtonReleased:Bool( button:MouseButton )
  66. DebugAssert( button>=0 And button<4,"Mouse button out of range" )
  67. Return _released[button]
  68. End
  69. #rem monkeydoc @hidden
  70. #end
  71. Method ButtonHit:Bool( button:MouseButton )
  72. Return ButtonPressed( button )
  73. End
  74. '***** Internal *****
  75. #rem monkeydoc @hidden
  76. #end
  77. Method Init()
  78. End
  79. #rem monkeydoc @hidden
  80. #end
  81. Method Update()
  82. Local mask:=SDL_GetMouseState( Varptr _location.x,Varptr _location.y )
  83. If App.ActiveWindow _location=App.ActiveWindow.TransformPointFromView( App.ActiveWindow.MouseScale * _location,Null )
  84. UpdateButton( MouseButton.Left,mask & 1 )
  85. UpdateButton( MouseButton.Middle,mask & 2 )
  86. UpdateButton( MouseButton.Right,mask & 4)
  87. End
  88. #rem monkeydoc @hidden
  89. #end
  90. Method UpdateButton( button:MouseButton,down:Bool )
  91. _pressed[button]=False
  92. _released[button]=False
  93. If down=_down[button] Return
  94. If down _pressed[button]=True Else _released[button]=True
  95. _down[button]=down
  96. End
  97. Private
  98. Field _init:Bool
  99. Field _location:Vec2i
  100. Field _down:=New Bool[4]
  101. Field _pressed:=New Bool[4]
  102. Field _released:=New Bool[4]
  103. Method New()
  104. End
  105. End