SFML community forums

Help => Window => Topic started by: FierceForm on April 13, 2011, 05:49:30 am

Title: Window GetPosition or desktop mouse position?
Post by: FierceForm on April 13, 2011, 05:49:30 am
Hello,

For my current project, I am attempting to create a custom titlebar, along the lines of Starcraft 2, the League of Legends launcher and the World of Warcraft launcher. I've set my RenderWindow's style to None, and I can render the top bar with other SFML code. However, I want the user to be able to drag the window around the desktop using the titlebar. I can check if the user is pressing the title bar, and see when the mouse moves, but I can't find a way to figure out where to move the window. I need to move the window based on the movement of the mouse. My first thought was to find the position of the window and add the position of the mouse on the window, but I failed to find a GetPosition function on the window. My second option was to see if I could get the cursor's desktop position, and set the window to that minus the mouse's position on the window, but I also failed to find this. Does anyone know where I can get one of these two functions or another way to accomplish my goal?
Title: Window GetPosition or desktop mouse position?
Post by: Groogy on April 13, 2011, 08:55:12 am
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Window.htm#a70557bca21ed03310820eb503afc699e

Couldn't find a GetPosition though, probably on the way.

And to get the desktop coordinates for the cursor you'll have to rely on some OS-specific code: http://msdn.microsoft.com/en-us/library/ms648390%28v=vs.85%29.aspx

This should be enough to get the effect you want, though if the mouse moves faster than the application can update the "drag" might be "dropped" :P
Code: [Select]

sf::Vector2i mouseScreenPosition = /* Convert value from GetCursorPos to sf::Vector2i */
window.SetPosition( mouseScreenPosition.x, mouseScreenPosition.y );
Title: Window GetPosition or desktop mouse position?
Post by: FierceForm on April 13, 2011, 09:01:00 pm
That's a great way to do it for now. I've built a quick demo, and you can view this code below.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <Windows.h>
 
int main()
{
sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML window", sf::Style::None);

int sx = 0;
int sy = 0;
bool mouseDown = false;
    while (window.IsOpened())
    {
        sf::Event event;
        while (window.PollEvent(event))
        {
            if (event.Type == sf::Event::Closed)
{
                window.Close();
}

if (event.Type == sf::Event::MouseButtonPressed)
{
if (event.MouseButton.Button == sf::Mouse::Left)
{
sx = event.MouseButton.X;
sy = event.MouseButton.Y;

mouseDown = true;
}
}

if (event.Type == sf::Event::MouseButtonReleased)
{
if (event.MouseButton.Button == sf::Mouse::Left)
{
mouseDown = false;
}
}

if (event.Type == sf::Event::MouseMoved)
{
if (mouseDown)
{
POINT p;
GetCursorPos(&p);
window.SetPosition(p.x-sx,
p.y-sy);
}
}
        }

        window.Clear();

        window.Display();
    }
 
    return EXIT_SUCCESS;
}


However, this does require the use of a platform specific function. I do plan to port my project to Mac OS X and possibly Linux in the future, so this can only be a temporary solution. Is there any plan to add some similar function to GetCursorPos in SFML in the future (either in the event handling, Input class, or both)? If not, is there a way I can suggest it to Laurent, or perhaps contribute such code myself?
Title: Window GetPosition or desktop mouse position?
Post by: JAssange on April 13, 2011, 09:01:45 pm
Quote from: "Groogy"

This should be enough to get the effect you want, though if the mouse moves faster than the application can update the "drag" might be "dropped" :P
Code: [Select]

sf::Vector2i mouseScreenPosition = /* Convert value from GetCursorPos to sf::Vector2i */
window.SetPosition( mouseScreenPosition.x, mouseScreenPosition.y );


That will always move the window so that the top left corner is on the mouse. If you're willing to lose portability you could use the Windows API to properly let it use the built in titlebar system in a custom region.

@fierce
SFML already can get the mouseposition, the issue is not being able to get Window position.
Title: Window GetPosition or desktop mouse position?
Post by: Groogy on April 14, 2011, 12:33:59 am
IF you want to suggest the feature to Laurent then do so here: https://github.com/SFML/SFML/issues
Title: Window GetPosition or desktop mouse position?
Post by: Laurent on April 14, 2011, 08:04:10 am
Window::GetPosition is already in the list.