1 module gfm.sdl2.mouse; 2 3 import derelict.sdl2.sdl; 4 import gfm.sdl2.sdl; 5 6 /// Holds SDL mouse state. 7 final class SDL2Mouse 8 { 9 public 10 { 11 /// Returns: true if a specific mouse button defined by mask is pressed 12 /// Example: 13 /// -------------------- 14 /// // Check if the left mouse button is pressed 15 /// if(_sdl2.mouse.isButtonPressed(SDL_BUTTON_LMASK)) 16 /// ... 17 /// -------------------- 18 bool isButtonPressed(int mask) pure const nothrow 19 { 20 return (_buttonState & mask) != 0; 21 } 22 23 /// Returns: X coordinate of mouse pointer. 24 int x() pure const nothrow 25 { 26 return _x; 27 } 28 29 /// Returns: Y coordinate of mouse pointer. 30 int y() pure const nothrow 31 { 32 return _y; 33 } 34 35 /// Returns: Coordinates of mouse pointer. 36 SDL_Point position() pure const nothrow 37 { 38 return SDL_Point(_x, _y); 39 } 40 41 /// Returns: How much was scrolled by X coordinate since the last call. 42 int wheelDeltaX() nothrow 43 { 44 int value = _wheelX; 45 _wheelX = 0; 46 return value; 47 } 48 49 /// Returns: How much was scrolled by Y coordinate since the last call. 50 int wheelDeltaY() nothrow 51 { 52 int value = _wheelY; 53 _wheelY = 0; 54 return value; 55 } 56 } 57 58 package 59 { 60 this(SDL2 sdl2) 61 { 62 _sdl2 = sdl2; 63 } 64 65 void updateMotion(const(SDL_MouseMotionEvent)* event) 66 { 67 // Get mouse buttons state but ignore mouse coordinates 68 // because we get them from event data 69 _buttonState = SDL_GetMouseState(null, null); 70 _x = event.x; 71 _y = event.y; 72 } 73 74 void updateButtons(const(SDL_MouseButtonEvent)* event) 75 { 76 // get mouse buttons state but ignore mouse coordinates 77 // because we get them from event data 78 _buttonState = SDL_GetMouseState(null, null); 79 _x = event.x; 80 _y = event.y; 81 } 82 83 void updateWheel(const(SDL_MouseWheelEvent)* event) 84 { 85 _buttonState = SDL_GetMouseState(&_x, &_y); 86 _wheelX += event.x; 87 _wheelY += event.y; 88 } 89 } 90 91 private 92 { 93 SDL2 _sdl2; 94 95 // Last button state 96 int _buttonState; 97 98 // Last mouse coordinates 99 int _x = 0, 100 _y = 0; 101 102 // mouse wheel scrolled amounts 103 int _wheelX = 0, 104 _wheelY =0; 105 106 } 107 108 }