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

Author Topic: Zoom a sf::View to Mouse-Point Center?  (Read 3826 times)

0 Members and 1 Guest are viewing this topic.

M74

  • Newbie
  • *
  • Posts: 17
    • View Profile
Zoom a sf::View to Mouse-Point Center?
« on: January 26, 2015, 11:40:35 pm »
Hi,

i`m currently working on a synthesizer program where i can place my virtual devices on a virtual desktop, which i can Zoom and Pan:



This works fine now, thanks to the sf::View :D

But... if i Zoom-In with the Mousewheel i also would like to pan the whole virtual desktop to the Device where the Mouse points to.

At first i thought this would be simple, if i would set the View-Center to the current Mouse-Coordinates, but unfortunately this does not seem to work so simple (if i try it this way the whole desktop-panning flickers in all directions).

Please take a look at my attached screenshot and imagine that the user points the mouse over the Device "Grainshifter" and rotates the Mousewheel. In this case the virtual desktop not only should Zoom In - it also should scroll to the device where the mouse points to. I often see this behavior in similar programs but i have no idea how i could implement this (currently the Zoom works only with the Center-Point set to the center of the whole screen).

I also found a similar question in this forum, but it was not answered there.

Does someone have a idea how to implement this?

Please excuse my bad english :)

Regards,
M74

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Zoom a sf::View to Mouse-Point Center?
« Reply #1 on: January 27, 2015, 07:36:16 am »
Quote
At first i thought this would be simple, if i would set the View-Center to the current Mouse-Coordinates, but unfortunately this does not seem to work so simple (if i try it this way the whole desktop-panning flickers in all directions).
I can't imagine why this wouldn't work. Maybe some code or a video of the problem could help?
Laurent Gomila - SFML developer

Silderan

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Zoom a sf::View to Mouse-Point Center?
« Reply #2 on: January 27, 2015, 02:04:15 pm »
I agree with Laurent.

I have some code to do that but is not correctly done due to pixel maths and don't zoom out well. But, no jumps. As I don't need this feature right now and code is just to test Scale feature, I will repair code later.

I don't have the code right now but I can give it to you this night (Spain night time) if you like.

Silderán.

Hapax

  • Hero Member
  • *****
  • Posts: 3339
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Zoom a sf::View to Mouse-Point Center?
« Reply #3 on: January 27, 2015, 07:03:26 pm »
Setting the centre of the view to the mouse position would move the things under the mouse cursor to the centre of the window. You would need to offset it with the difference.
« Last Edit: January 27, 2015, 10:20:58 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Hapax

  • Hero Member
  • *****
  • Posts: 3339
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Zoom a sf::View to Mouse-Point Center?
« Reply #4 on: January 27, 2015, 11:14:09 pm »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Blaspie

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Zoom a sf::View to Mouse-Point Center?
« Reply #5 on: January 28, 2015, 07:57:30 pm »
This works for me:
                        view.zoom(1 - event.mouseWheel.delta*0.1);
                        view.move(((sf::Vector2f(window.mapPixelToCoords((sf::Mouse::getPosition())) - sf::Vector2f(view.getCenter()))).x)*event.mouseWheel.delta*0.1,
                                          ((sf::Vector2f(window.mapPixelToCoords((sf::Mouse::getPosition())) - sf::Vector2f(view.getCenter()))).y)*event.mouseWheel.delta*0.1);
 

Hapax

  • Hero Member
  • *****
  • Posts: 3339
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Zoom a sf::View to Mouse-Point Center?
« Reply #6 on: January 29, 2015, 01:07:18 am »
Wouldn't that rely on the amount of delta being the same for everyone?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

M74

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Zoom a sf::View to Mouse-Point Center?
« Reply #7 on: January 29, 2015, 08:45:21 am »
Thanks for all answers. And thank you Hapax for the Wiki Tutorial  :)