1 /* 2 * DSFML - The Simple and Fast Multimedia Library for D 3 * 4 * Copyright (c) 2013 - 2017 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 * 6 * This software is provided 'as-is', without any express or implied warranty. 7 * In no event will the authors be held liable for any damages arising from the 8 * use of this software. 9 * 10 * Permission is granted to anyone to use this software for any purpose, 11 * including commercial applications, and to alter it and redistribute it 12 * freely, subject to the following restrictions: 13 * 14 * 1. The origin of this software must not be misrepresented; you must not claim 15 * that you wrote the original software. If you use this software in a product, 16 * an acknowledgment in the product documentation would be appreciated but is 17 * not required. 18 * 19 * 2. Altered source versions must be plainly marked as such, and must not be 20 * misrepresented as being the original software. 21 * 22 * 3. This notice may not be removed or altered from any source distribution 23 */ 24 25 /** 26 * $(U Mouse) provides an interface to the state of the mouse. It only contains 27 * static functions (a single mouse is assumed), so it's not meant to be 28 * instanciated. 29 * 30 * This class allows users to query the mouse state at any time and directly, without having to deal with 31 * a window and its events. Compared to the MouseMoved, MouseButtonPressed and MouseButtonReleased events, 32 * Mouse can retrieve the state of the cursor and the buttons at any time (you don't need to store and update 33 * a boolean on your side in order to know if a button is pressed or released), and you always get the real 34 * state of the mouse, even if it is moved, pressed or released when your window is out of focus and no event is triggered. 35 * 36 * The setPosition and getPosition functions can be used to change or retrieve the current position of the 37 * mouse pointer. There are two versions: one that operates in global coordinates (relative to the desktop) 38 * and one that operates in window coordinates (relative to a specific window). 39 * 40 * Example: 41 * --- 42 * if (Mouse.isButtonPressed(Mouse.Button.Left)) 43 * { 44 * // left click... 45 * } 46 * 47 * // get global mouse position 48 * auto position = Mouse.getPosition(); 49 * 50 * // set mouse position relative to a window 51 * Mouse.setPosition(Vector2i(100, 200), window); 52 * --- 53 * 54 * See_Also: 55 * $(JOYSTICK_LINK), $(KEYBOARD_LINK), $(TOUCH_LINK) 56 */ 57 module dsfml.window.mouse; 58 59 import dsfml.system.vector2; 60 import dsfml.window.window; 61 62 /** 63 * Give access to the real-time state of the mouse. 64 */ 65 final abstract class Mouse 66 { 67 /// Mouse buttons. 68 enum Button 69 { 70 /// The left mouse button 71 Left, 72 /// The right mouse button 73 Right, 74 /// The middle (wheel) mouse button 75 Middle, 76 /// The first extra mouse button 77 XButton1, 78 /// The second extra mouse button 79 XButton2, 80 81 ///Keep last -- the total number of mouse buttons 82 Count 83 84 } 85 86 /// Mouse wheels. 87 enum Wheel 88 { 89 /// Vertically oriented mouse wheel 90 VerticalWheel, 91 /// Horizontally oriented mouse wheel 92 HorizontalWheel 93 } 94 95 /** 96 * Set the current position of the mouse in desktop coordinates. 97 * 98 * This function sets the global position of the mouse cursor on the 99 * desktop. 100 * 101 * Params: 102 * position = New position of the mouse 103 */ 104 static void setPosition(Vector2i position) 105 { 106 sfMouse_setPosition(position.x, position.y,null); 107 } 108 109 /** 110 * Set the current position of the mouse in window coordinates. 111 * 112 * This function sets the current position of the mouse cursor, relative to the given window. 113 * 114 * Params: 115 * position = New position of the mouse 116 * relativeTo = Reference window 117 */ 118 static void setPosition(Vector2i position, const(Window) relativeTo) 119 { 120 relativeTo.mouse_SetPosition(position); 121 } 122 123 /** 124 * Get the current position of the mouse in desktop coordinates. 125 * 126 * This function returns the global position of the mouse cursor on the 127 * desktop. 128 * 129 * Returns: Current position of the mouse. 130 */ 131 static Vector2i getPosition() 132 { 133 Vector2i temp; 134 sfMouse_getPosition(null,&temp.x, &temp.y); 135 136 return temp; 137 } 138 139 /** 140 * Get the current position of the mouse in window coordinates. 141 * 142 * This function returns the current position of the mouse cursor, relative 143 * to the given window. 144 * 145 * Params: 146 * relativeTo = Reference window 147 * 148 * Returns: Current position of the mouse. 149 */ 150 static Vector2i getPosition(const(Window) relativeTo) 151 { 152 return relativeTo.mouse_getPosition(); 153 } 154 155 /** 156 * Check if a mouse button is pressed. 157 * 158 * Params: 159 * button = Button to check 160 * 161 * Returns: true if the button is pressed, false otherwise. 162 */ 163 static bool isButtonPressed(Button button) 164 { 165 return (sfMouse_isButtonPressed(button) ); 166 } 167 } 168 169 unittest 170 { 171 version(DSFML_Unittest_Window) 172 { 173 import std.stdio; 174 175 writeln("Unit test for Mouse class"); 176 177 writeln("Current mouse position: ", Mouse.getPosition().toString()); 178 179 Mouse.setPosition(Vector2i(100,400)); 180 181 writeln("New mouse position: ", Mouse.getPosition().toString()); 182 183 } 184 } 185 186 private extern(C) 187 { 188 189 //Check if a mouse button is pressed 190 bool sfMouse_isButtonPressed(int button); 191 192 //Get the current position of the mouse 193 void sfMouse_getPosition(const(sfWindow)* relativeTo, int* x, int* y); 194 195 //Set the current position of the mouse 196 void sfMouse_setPosition(int x, int y, const(sfWindow)* relativeTo); 197 }