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