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

Author Topic: Mouse Wheel Scroll Zoom (C# / SFML.Net) [Solved]  (Read 10178 times)

0 Members and 1 Guest are viewing this topic.

Nbxy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Mouse Wheel Scroll Zoom (C# / SFML.Net) [Solved]
« on: April 26, 2014, 06:13:16 am »
So I'm trying to program a zoom feature with using the mouse scroll. I want to be able to zoom in and out. This is what you see in every day games when you want to change the weapon or when you scroll up and down web pages - so you know what I mean.
Quote
if (Mouse.IsButtonPressed(Mouse.Button.Right))
{
   gameZoom += 0.01f;
}
else if (Mouse.IsButtonPressed(Mouse.Button.Left))
{
   gameZoom -= 0.01f;
}
This is the code that I'm using right now to control the zooming. And it works perfectly just using right and left buttons of the mouse. However it's not as practical as using the mouse wheel to do it. I have looked through the tutorials and tried googling a dozen times for an example with C# and SFML.Net - no such luck...

Please, don't bother giving a another link to the tutorial page. If you have the courtesy to answer, please attach a short C# code which can lead me to my goal. Thank you.
« Last Edit: April 26, 2014, 07:13:11 pm by Nbxy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse Wheel Scroll Zoom (C# / SFML.Net)
« Reply #1 on: April 26, 2014, 09:34:45 am »
You can't access the wheel through the real-time Mouse class, because the wheel has no "absolute" state, only up/down events. So you must use events, namely the MouseWheelMoved one. Details are in the API documentation ;)
Laurent Gomila - SFML developer

Nbxy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Mouse Wheel Scroll Zoom (C# / SFML.Net)
« Reply #2 on: April 26, 2014, 04:42:16 pm »
Yes. I've read through several topics which all state the exact same. No absolute state, got it.
Quote
static void OnMouseScroll(object sender, EventArgs e)
{
    RenderWindow window = (RenderWindow)sender;
    MouseWheelEventArgs mouseEvent = (MouseWheelEventArgs)e;
    if (mouseEvent.Delta.Equals(1))
    {
        gameZoom += 0.01f;
    }
    if (mouseEvent.Delta.Equals(-1))
    {
        gameZoom -= 0.01f;
    }
}
Okay this is me trying to check for events in the mouse wheel. It doesn't work. How can I make it work? The tutorials and documentation are a no-go because there's not a single line of C# code anywhere and example for this kind of feature is nowhere to be found.

Somewhere I got the idea that the mouse wheel has either the value of 1 = up or value of -1 = down. Am I doing it correctly? The code doesn't give out any errors but it doesn't act in anyway either.
« Last Edit: April 26, 2014, 04:45:56 pm by Nbxy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse Wheel Scroll Zoom (C# / SFML.Net)
« Reply #3 on: April 26, 2014, 04:57:03 pm »
mouseEvent.Delta might any number, don't just compare it to 1. Instead, make your zoom factor depend on it.

// for example
gameZoom += 0.01f * mouseEvent.Delta;

And I assume you've properly registered your OnMouseScroll function, right? Do you have other events working?
Laurent Gomila - SFML developer

Nbxy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Mouse Wheel Scroll Zoom (C# / SFML.Net)
« Reply #4 on: April 26, 2014, 07:12:36 pm »
Thank you Laurent for your input. I got my mouse wheel zoom feature working beautifully. As there are zero examples on the subject in the past topics I'm going to go ahead and make this one a learning experience for everyone who are seeking for this kind zoom feature in the future.
Quote
public static class Game
{
       static float gameZoom = 1;

       static void OnMouseScroll(object sender, EventArgs e)
       {
                RenderWindow window = (RenderWindow)sender;
                MouseWheelEventArgs mouseEvent = (MouseWheelEventArgs)e;
                gameZoom -= 0.05f * mouseEvent.Delta;
        }

       static void Main()
       {
                RenderWindow window = new RenderWindow(new VideoMode(1024, 768), "Mouse Wheel Zoom", Styles.Close);
                window.MouseWheelMoved += new EventHandler<MouseWheelEventArgs>(OnMouseScroll);
                View gameView = new View();

                while (window.IsOpen())
                {
                        window.DispatchEvents();
                        gameView.Zoom(gameZoom);
                        window.SetView(gameView);
                        window.Display();
                        window.Clear();
                }
        }
}
This is a minimized code from my project. I have bolded the parts which are the key elements here. I hope this can provide help for people trying to figure out the magics of mouse wheel handling. I'm going to mark this topic as solved so it will stand out when searching the forums for this.
« Last Edit: April 26, 2014, 07:18:48 pm by Nbxy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse Wheel Scroll Zoom (C# / SFML.Net) [Solved]
« Reply #5 on: April 26, 2014, 07:43:26 pm »
Why don't you call gameView.Zoom(gameZoom); only when gameZoom changes, in OnMouseScroll?
Laurent Gomila - SFML developer