Removed Window::GetCursorPosition/SetCursorPosition, added Mouse::GetPosition/SetPosition (two versions: one that handles desktop coordinates, one that handles window coordinates)

This commit is contained in:
Laurent Gomila 2011-07-08 07:56:14 +02:00 committed by Marco Antognini
parent e95323e2ea
commit 6fd80e2598
19 changed files with 287 additions and 199 deletions

View File

@ -90,8 +90,8 @@ int main()
glClear(GL_DEPTH_BUFFER_BIT);
// We get the position of the mouse cursor, so that we can move the box accordingly
float x = window.GetCursorPosition().x * 200.f / window.GetWidth() - 100.f;
float y = -window.GetCursorPosition().y * 200.f / window.GetHeight() + 100.f;
float x = sf::Mouse::GetPosition(window).x * 200.f / window.GetWidth() - 100.f;
float y = -sf::Mouse::GetPosition(window).y * 200.f / window.GetHeight() + 100.f;
// Apply some transformations
glMatrixMode(GL_MODELVIEW);

View File

@ -202,8 +202,8 @@ int main()
}
// Get the mouse position in the range [0, 1]
float mouseX = window.GetCursorPosition().x / static_cast<float>(window.GetWidth());
float mouseY = window.GetCursorPosition().y / static_cast<float>(window.GetHeight());
float mouseX = sf::Mouse::GetPosition(window).x / static_cast<float>(window.GetWidth());
float mouseY = sf::Mouse::GetPosition(window).y / static_cast<float>(window.GetHeight());
// Update the shaders
backgroundShader.Update(mouseX, mouseY);

View File

@ -34,6 +34,8 @@
namespace sf
{
class Window;
////////////////////////////////////////////////////////////
/// \brief Give access to the real-time state of the mouse
///
@ -68,18 +70,51 @@ public :
static bool IsButtonPressed(Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the current position of the mouse
/// cursor.
/// If the cursor is over a SFML window, the returned position
/// is relative to this window. Otherwise, the returned position
/// is in desktop coordinates.
/// This function returns the global position of the mouse
/// cursor on the desktop.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i GetPosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i GetPosition(const Window& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the global position of the mouse
/// cursor on the desktop.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void SetPosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void SetPosition(const Vector2i& position, const Window& relativeTo);
};
} // namespace sf
@ -107,10 +142,11 @@ public :
/// moved, pressed or released when your window is out of focus
/// and no event is triggered.
///
/// Note that the sf::Mouse::GetPosition function has a special
/// behaviour: it returns the cursor position relative to the
/// window which has the mouse focus (ie. the window on which
/// the cursor is).
/// The SetPosition and GetPosition functions can be used to change
/// or retrieve the current position of the mouse pointer. There are
/// two versions: one that operates in global coordinates (relative
/// to the desktop) and one that operates in window coordinates
/// (relative to a specific window).
///
/// Usage example:
/// \code
@ -118,12 +154,12 @@ public :
/// {
/// // left click...
/// }
/// else if (sf::Mouse::IsButtonPressed(sf::Mouse::Right))
/// {
/// // right click...
/// }
///
/// // get global mouse position
/// sf::Vector2i position = sf::Mouse::GetPosition();
///
/// // set mouse position relative to a window
/// sf::Mouse::SetPosition(sf::Vector2i(100, 200), window);
/// \endcode
///
/// \see sf::Joystick, sf::Keyboard

View File

@ -278,23 +278,6 @@ public :
////////////////////////////////////////////////////////////
void ShowMouseCursor(bool show);
////////////////////////////////////////////////////////////
/// \brief Change the position of the mouse cursor
///
/// \param x Left coordinate of the cursor, relative to the window
/// \param y Top coordinate of the cursor, relative to the window
///
////////////////////////////////////////////////////////////
void SetCursorPosition(unsigned int x, unsigned int y);
////////////////////////////////////////////////////////////
/// \brief Get the position of the mouse cursor
///
/// \return Current mouse cursor position, relative to the window
///
////////////////////////////////////////////////////////////
Vector2i GetCursorPosition() const;
////////////////////////////////////////////////////////////
/// \brief Change the position of the window on screen
///

View File

@ -213,16 +213,48 @@ Vector2i InputImpl::GetMousePosition()
{
// we don't care about these but they are required
::Window root, child;
int wx, wy;
int x, y;
unsigned int buttons;
int gx = 0;
int gy = 0;
XQueryPointer(global.display, global.window, &root, &child, &gx, &gy, &x, &y, &buttons);
return Vector2i(gx, gy);
}
////////////////////////////////////////////////////////////
Vector2i InputImpl::GetMousePosition(const Window& relativeTo)
{
// we don't care about these but they are required
::Window root, child;
int gx, gy;
unsigned int buttons;
int x = 0;
int y = 0;
XQueryPointer(global.display, global.window, &root, &child, &x, &y, &wx, &wy, &buttons);
XQueryPointer(global.display, relativeTo.GetSystemHandle(), &root, &child, &gx, &gy, &x, &y, &buttons);
return Vector2i(x, y);
}
////////////////////////////////////////////////////////////
void InputImpl::SetMousePosition(const Vector2i& position)
{
XWarpPointer(global.display, None, global.window, 0, 0, 0, 0, position.x, position.y);
XFlush(global.display);
}
////////////////////////////////////////////////////////////
void InputImpl::SetMousePosition(const Vector2i& position, const Window& relativeTo)
{
XWarpPointer(global.display, None, relativeTo.GetSystemHandle(), 0, 0, 0, 0, position.x, position.y);
XFlush(global.display);
}
} // namespace priv
} // namespace sf

View File

@ -65,14 +65,54 @@ public :
static bool IsMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the mouse position in desktop coordinates.
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i GetMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i GetMousePosition(const Window& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void SetMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void SetMousePosition(const Vector2i& position, const Window& relativeTo);
};
} // namespace priv

View File

@ -302,30 +302,6 @@ void WindowImplX11::ShowMouseCursor(bool show)
}
////////////////////////////////////////////////////////////
void WindowImplX11::SetCursorPosition(unsigned int x, unsigned int y)
{
XWarpPointer(myDisplay, None, myWindow, 0, 0, 0, 0, x, y);
XFlush(myDisplay);
}
////////////////////////////////////////////////////////////
Vector2i WindowImplX11::GetCursorPosition() const
{
// we don't care about these but they are required
::Window root, child;
int gx, gy;
unsigned int buttons;
int x = 0;
int y = 0;
XQueryPointer(myDisplay, myWindow, &root, &child, &gx, &gy, &x, &y, &buttons);
return Vector2i(x, y);
}
////////////////////////////////////////////////////////////
void WindowImplX11::SetPosition(int x, int y)
{

View File

@ -105,23 +105,6 @@ private :
////////////////////////////////////////////////////////////
virtual void ShowMouseCursor(bool show);
////////////////////////////////////////////////////////////
/// \brief Change the position of the mouse cursor
///
/// \param x Left coordinate of the cursor, relative to the window
/// \param y Top coordinate of the cursor, relative to the window
///
////////////////////////////////////////////////////////////
virtual void SetCursorPosition(unsigned int x, unsigned int y);
////////////////////////////////////////////////////////////
/// \brief Get the position of the mouse cursor
///
/// \return Current mouse cursor position, relative to the window
///
////////////////////////////////////////////////////////////
virtual Vector2i GetCursorPosition() const;
////////////////////////////////////////////////////////////
/// \brief Change the position of the window on screen
///

View File

@ -42,17 +42,28 @@ bool Mouse::IsButtonPressed(Button button)
////////////////////////////////////////////////////////////
Vector2i Mouse::GetPosition()
{
const Window* focusWindow = Window::GetMouseFocusWindow();
if (focusWindow)
{
// Position relative to the focus window
return focusWindow->GetCursorPosition();
}
else
{
// Desktop position
return priv::InputImpl::GetMousePosition();
}
return priv::InputImpl::GetMousePosition();
}
////////////////////////////////////////////////////////////
Vector2i Mouse::GetPosition(const Window& relativeTo)
{
return priv::InputImpl::GetMousePosition(relativeTo);
}
////////////////////////////////////////////////////////////
void Mouse::SetPosition(const Vector2i& position)
{
priv::InputImpl::SetMousePosition(position);
}
////////////////////////////////////////////////////////////
void Mouse::SetPosition(const Vector2i& position, const Window& relativeTo)
{
priv::InputImpl::SetMousePosition(position, relativeTo);
}
} // namespace sf

View File

@ -66,14 +66,54 @@ public :
static bool IsMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the mouse position in desktop coordinates.
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i GetMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i GetMousePosition(const Window& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void SetMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void SetMousePosition(const Vector2i& position, const Window& relativeTo);
};
} // namespace priv

View File

@ -60,6 +60,28 @@ Vector2i InputImpl::GetMousePosition()
return Vector2i(pos.x, pos.y);
}
////////////////////////////////////////////////////////////
Vector2i InputImpl::GetMousePosition(const Window& relativeTo)
{
// @to be implemented
return Vector2i();
}
////////////////////////////////////////////////////////////
void InputImpl::SetMousePosition(const Vector2i& position)
{
// @to be implemented
}
////////////////////////////////////////////////////////////
void InputImpl::SetMousePosition(const Vector2i& position, const Window& relativeTo)
{
// @to be implemented
}
} // namespace priv
} // namespace sf

View File

@ -256,23 +256,6 @@ private:
////////////////////////////////////////////////////////////
virtual void ShowMouseCursor(bool show);
////////////////////////////////////////////////////////////
/// \brief Change the position of the mouse cursor
///
/// \param x Left coordinate of the cursor, relative to the window
/// \param y Top coordinate of the cursor, relative to the window
///
////////////////////////////////////////////////////////////
virtual void SetCursorPosition(unsigned int x, unsigned int y);
////////////////////////////////////////////////////////////
/// \brief Get the position of the mouse cursor
///
/// \return Current mouse cursor position, relative to the window
///
////////////////////////////////////////////////////////////
virtual Vector2i GetCursorPosition() const;
////////////////////////////////////////////////////////////
/// \brief Change the position of the window on screen
///

View File

@ -338,21 +338,6 @@ void WindowImplCocoa::ShowMouseCursor(bool show)
}
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetCursorPosition(unsigned int x, unsigned int y)
{
[myDelegate setCursorPositionToX:x Y:y];
}
////////////////////////////////////////////////////////////
Vector2i WindowImplCocoa::GetCursorPosition() const
{
NSPoint pos = [myDelegate cursorPosition];
return Vector2i(pos.x, pos.y);
}
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetPosition(int x, int y)
{

View File

@ -27,6 +27,7 @@
////////////////////////////////////////////////////////////
#define _WIN32_WINDOWS 0x0501
#define _WIN32_WINNT 0x0501
#include <SFML/Window/Window.hpp>
#include <SFML/Window/Win32/InputImpl.hpp>
#include <windows.h>
@ -168,10 +169,35 @@ bool InputImpl::IsMouseButtonPressed(Mouse::Button button)
////////////////////////////////////////////////////////////
Vector2i InputImpl::GetMousePosition()
{
POINT position;
GetCursorPos(&position);
POINT point;
GetCursorPos(&point);
return Vector2i(point.x, point.y);
}
return Vector2i(position.x, position.y);
////////////////////////////////////////////////////////////
Vector2i InputImpl::GetMousePosition(const Window& relativeTo)
{
POINT point;
GetCursorPos(&point);
ScreenToClient(relativeTo.GetSystemHandle(), &point);
return Vector2i(point.x, point.y);
}
////////////////////////////////////////////////////////////
void InputImpl::SetMousePosition(const Vector2i& position)
{
SetCursorPos(position.x, position.y);
}
////////////////////////////////////////////////////////////
void InputImpl::SetMousePosition(const Vector2i& position, const Window& relativeTo)
{
POINT point = {position.x, position.y};
ClientToScreen(relativeTo.GetSystemHandle(), &point);
SetCursorPos(point.x, point.y);
}
} // namespace priv

View File

@ -65,14 +65,54 @@ public :
static bool IsMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the mouse position in desktop coordinates.
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i GetMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i GetMousePosition(const Window& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void SetMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void SetMousePosition(const Vector2i& position, const Window& relativeTo);
};
} // namespace priv

View File

@ -231,26 +231,6 @@ void WindowImplWin32::ShowMouseCursor(bool show)
}
////////////////////////////////////////////////////////////
void WindowImplWin32::SetCursorPosition(unsigned int x, unsigned int y)
{
POINT position = {x, y};
ClientToScreen(myHandle, &position);
SetCursorPos(position.x, position.y);
}
////////////////////////////////////////////////////////////
Vector2i WindowImplWin32::GetCursorPosition() const
{
POINT position;
GetCursorPos(&position);
ScreenToClient(myHandle, &position);
return Vector2i(position.x, position.y);
}
////////////////////////////////////////////////////////////
void WindowImplWin32::SetPosition(int x, int y)
{

View File

@ -94,23 +94,6 @@ private :
////////////////////////////////////////////////////////////
virtual void ShowMouseCursor(bool show);
////////////////////////////////////////////////////////////
/// \brief Change the position of the mouse cursor
///
/// \param x Left coordinate of the cursor, relative to the window
/// \param y Top coordinate of the cursor, relative to the window
///
////////////////////////////////////////////////////////////
virtual void SetCursorPosition(unsigned int x, unsigned int y);
////////////////////////////////////////////////////////////
/// \brief Get the position of the mouse cursor
///
/// \return Current mouse cursor position, relative to the window
///
////////////////////////////////////////////////////////////
virtual Vector2i GetCursorPosition() const;
////////////////////////////////////////////////////////////
/// \brief Change the position of the window on screen
///

View File

@ -242,21 +242,6 @@ void Window::ShowMouseCursor(bool show)
}
////////////////////////////////////////////////////////////
void Window::SetCursorPosition(unsigned int x, unsigned int y)
{
if (myWindow)
myWindow->SetCursorPosition(x, y);
}
////////////////////////////////////////////////////////////
Vector2i Window::GetCursorPosition() const
{
return myWindow ? myWindow->GetCursorPosition() : Vector2i(0, 0);
}
////////////////////////////////////////////////////////////
void Window::SetPosition(int x, int y)
{

View File

@ -141,23 +141,6 @@ public :
////////////////////////////////////////////////////////////
virtual void ShowMouseCursor(bool show) = 0;
////////////////////////////////////////////////////////////
/// \brief Change the position of the mouse cursor
///
/// \param x Left coordinate of the cursor, relative to the window
/// \param y Top coordinate of the cursor, relative to the window
///
////////////////////////////////////////////////////////////
virtual void SetCursorPosition(unsigned int x, unsigned int y) = 0;
////////////////////////////////////////////////////////////
/// \brief Get the position of the mouse cursor
///
/// \return Current mouse cursor position, relative to the window
///
////////////////////////////////////////////////////////////
virtual Vector2i GetCursorPosition() const = 0;
////////////////////////////////////////////////////////////
/// \brief Change the position of the window on screen
///