Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving sf::View with mouse  (Read 8253 times)

0 Members and 1 Guest are viewing this topic.

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Moving sf::View with mouse
« on: August 07, 2015, 11:47:30 am »
I have an sf::View which i want to move with my mouse. For example in Paint.net when you hold middle mouse button and then move your mouse, the image moves with the mouse. I want to achieve the same effect in SFML.
Also i want to zoom at the mouse position with the mouse wheel like in Paint.net. I have tried various solutions but none worked correclty.

Here is the code which i tried for moving the view:
mPos = sf::Mouse::getPosition(window);
camera.move(mPos.x, mPos.y);
window.setView(camera);

Thanks in advance.

kitteh-warrior

  • Guest
Re: Moving sf::View with mouse
« Reply #1 on: August 07, 2015, 01:30:26 pm »
mPos = sf::Mouse::getPosition(window);
camera.move(mPos.x, mPos.y);
window.setView(camera);

Shouldn't you be moving the view based on the change of the cursor's position, and not the just the cursor's position? ;)

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Moving sf::View with mouse
« Reply #2 on: August 07, 2015, 04:04:36 pm »
Actually i'm not sure how i should move it or what calculations i need to make to move it  :(
But i will try to move it by the change and see how that goes.

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving sf::View with mouse
« Reply #3 on: August 07, 2015, 04:17:57 pm »
As kitteh-warrior said, you move by the change in mouse position. You can, instead, set by the current mouse position.
To set a view's position, use camera.setCenter(). Be aware that you'll be telling it where in the scene should be at the centre of the window. So, using camera.setCenter(0, 0); would place 0, 0 at the centre of your window.

Also i want to zoom at the mouse position with the mouse wheel like in Paint.net. I have tried various solutions but none worked correclty
Did you try this;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Moving sf::View with mouse
« Reply #4 on: August 07, 2015, 04:28:15 pm »
Did you try this;)

That looks just like what i need! Thanks  ;D

I also managed to make the view movable like i wanted but there is a little problem with it.
When i zoom out it moves very slowly and when i zoom in it goes way too fast when moving it.
Here is the code:
mPos = sf::Mouse::getPosition(window);
// ...
float x = (float)mPos_old.x-mPos.x;
float y = (float)mPos_old.y-mPos.y;
camera.move(x, y);
window.setView(camera);
// ...
mPos_old = mPos;
 

I tried multiplying the x and y values with the size of the view like:
float x = ((float)mPos_old.x-mPos.x)*camera.getSize().x/32;
float y = ((float)mPos_old.y-mPos.y)*camera.getSize().y/32;
 
but that dint work :-\
« Last Edit: August 07, 2015, 04:31:05 pm by Oilfan »

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving sf::View with mouse
« Reply #5 on: August 07, 2015, 04:50:31 pm »
Did you try this;)

That looks just like what i need! Thanks  ;D
You're welcome. It was made for making people's lives easier  ;)

I also managed to make the view movable like i wanted but there is a little problem with it.
When i zoom out it moves very slowly and when i zoom in it goes way too fast when moving it.
This is because the size of the world co-ordinates are smaller when you zoom out and larger when you zoom in. You should consider converting the pixel co-ordinates of the mouse position to world co-ordinates.
If you're using views at all, you should really read that entire tutorial and make sure you fully understand it. It can be really confusing at first!
You find that this Wiki Tutorial explains the concept a little more clearly.

I tried multiplying the x and y values with the size of the view like:
Although the solution was given in my previous paragraph, I wanted to just say that the multiplication would needed to have been multiplied by the ratio of the view size to the window size. I don't know where you got the 32 from...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Moving sf::View with mouse
« Reply #6 on: August 07, 2015, 05:44:07 pm »
I added the zooming code you linked and it works great. However, when i zoom in or out and then move the image with middle mouse button, the zoom resets to default value. I read through the code and noticed this:
sf::Vector2f pos((float)mPos_old.x-mPos.x, (float)mPos_old.y-mPos.y);
camera.move(pos.x, pos.y);
window.setView(camera); // If i remove this, i cant move the image but zoom works.
// If i dont remove it, zooming works but resets whe image is moved.
 

I also managed to make the view movable like i wanted but there is a little problem with it.
When i zoom out it moves very slowly and when i zoom in it goes way too fast when moving it.
This is because the size of the world co-ordinates are smaller when you zoom out and larger when you zoom in. You should consider converting the pixel co-ordinates of the mouse position to world co-ordinates.
If you're using views at all, you should really read that entire tutorial and make sure you fully understand it. It can be really confusing at first!
You find that this Wiki Tutorial explains the concept a little more clearly.

I tried multiplying the x and y values with the size of the view like:
Although the solution was given in my previous paragraph, I wanted to just say that the multiplication would needed to have been multiplied by the ratio of the view size to the window size. I don't know where you got the 32 from...
I tried to convert the pixel coordinates like you suggested:
sf::Vector2f pos = window.mapPixelToCoords(sf::Vector2i(mPos_old.x-mPos.x,mPos_old.y-mPos.y));
camera.move(pos.x, pos.y);
window.setView(camera);
 
but when i try to move the image it just moves somewhere really fast.
I read the view tutorial couple of times but i still cant get the hang of it :(
I will read it again but if you could provide some kind of example of how i should convert them i would appreciate that very much.

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving sf::View with mouse
« Reply #7 on: August 07, 2015, 06:57:25 pm »
I added the zooming code you linked and it works great. However, when i zoom in or out and then move the image with middle mouse button, the zoom resets to default value. I read through the code and noticed this:
sf::Vector2f pos((float)mPos_old.x-mPos.x, (float)mPos_old.y-mPos.y);
camera.move(pos.x, pos.y);
window.setView(camera); // If i remove this, i cant move the image but zoom works.
// If i dont remove it, zooming works but resets whe image is moved.
 
You should be aware that that function modifies the view that is used by the window and then sets the window to use that view.
When you set the view to your own view (camera), it's ignoring any alterations made by the zoom function.
You could either modify the function to pass your view as a parameter and it could alter that one or, after using the function, use something like camera = window.getView();
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Moving sf::View with mouse
« Reply #8 on: August 07, 2015, 09:18:49 pm »
I added the zooming code you linked and it works great. However, when i zoom in or out and then move the image with middle mouse button, the zoom resets to default value. I read through the code and noticed this:
sf::Vector2f pos((float)mPos_old.x-mPos.x, (float)mPos_old.y-mPos.y);
camera.move(pos.x, pos.y);
window.setView(camera); // If i remove this, i cant move the image but zoom works.
// If i dont remove it, zooming works but resets whe image is moved.
 
You should be aware that that function modifies the view that is used by the window and then sets the window to use that view.
When you set the view to your own view (camera), it's ignoring any alterations made by the zoom function.
You could either modify the function to pass your view as a parameter and it could alter that one or, after using the function, use something like camera = window.getView();
I got everything working now. Thank you for the help!  8)

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving sf::View with mouse
« Reply #9 on: August 08, 2015, 12:12:40 am »
You're welcome.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*