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

Author Topic: Mouse Scroll Direction  (Read 5482 times)

0 Members and 2 Guests are viewing this topic.

DBD/Entropy

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Mouse Scroll Direction
« on: February 07, 2024, 02:01:01 am »
I am currently working on my camera control and am now tripping over the zooming. Ideally i want to be able to zoom in with a upward mouse scroll and down with a downward mouse scroll. After going through the docs i find this https://www.sfml-dev.org/documentation/2.6.1/structsf_1_1Event_1_1MouseWheelScrollEvent.php. And based on that information i have created this absolute "masterpiece":

public static void OnMouseScroll(object? sender, EventArgs e)
{
    if (sender == null) return;
    var window = (RenderWindow)sender;
    var mouseEvent = (MouseWheelScrollEventArgs)e;
    switch (mouseEvent.Delta)
    {
         case > 0:
             Program.ZoomView(.01f);
             break;
         case < 0:
             Program.ZoomView(-.01f);
             break;
    }
}
Note: Program.ZoomView() does what it says: it zooms my view.

The zooming itself works just fine, with one exception:
It doesnt matter where i scroll, the way it scroll is kind of random (meaning i haven't found a pattern)

Any ideas what might cause this or how to fix it are appreciated.
Second Note: I am using SFML 2.5.1 as that is the newest C# binding that NuGet provides

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Mouse Scroll Direction
« Reply #1 on: February 07, 2024, 10:29:10 am »
The zooming itself works just fine, with one exception:
It doesnt matter where i scroll, the way it scroll is kind of random (meaning i haven't found a pattern)
You mean the direction is random?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

DBD/Entropy

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Mouse Scroll Direction
« Reply #2 on: February 07, 2024, 11:33:21 am »
What i can say, is that the scroll direction does not directly translate into the zoom direction.
Basically if you scroll up and down and left and right a bit, you can get it to change the zoom direction, but any scrolling between these changes are going into the same zoom direction.
To the outsider its more or less random.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Mouse Scroll Direction
« Reply #3 on: February 07, 2024, 05:21:28 pm »
I have an issue with my mouse that sometimes scrolls in the wrong direction. This is just a bad mouse though; it's certainly not SFML. It's probably not this though.

You mentioned scrolling up, down, left and right.
Does this mean you have two wheels or maybe a multi-directional ball/wheel?
Note that, per wheel, it can only go in one direction: positive or negative.
It also doesn't look like you're checking which wheel is being scrolled.

In addition, are you certain that the only time this function is called is when the event type is correct?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DBD/Entropy

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Mouse Scroll Direction
« Reply #4 on: February 07, 2024, 07:23:38 pm »
Does this mean you have two wheels or maybe a multi-directional ball/wheel?

No, many mouses (that i know) support pressing your mouse wheel to the side to scroll sideways

In addition, are you certain that the only time this function is called is when the event type is correct?
The codebase is a very small private project, with 6 C# files (and technically one c++ function) with maybe 600 lines combined at max. Assuming you mean Project.ZoomView(), yes that is only called from that event, since it is a binding for the EventHandler Class (Which contains the event handling functions). This is what it looks like (_view and _window are my view and window, and _zoom is a float that keeps track of the zooming i have done to make resetting the zoom possible)
    public static void ZoomView(float delta)
    {
        _zoom += delta;
        _view.Zoom(_zoom);
        _window.SetView(_view);
    }

Note that, per wheel, it can only go in one direction: positive or negative.
It also doesn't look like you're checking which wheel is being scrolled.

1. I have only one mouse wheel, so I don't need to check which wheel is being scrolled (for now).
2. I am aware that my delta is a one-dimensional value, and you can probably tell from the info i have given until now, that this should work perfectly fine.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Mouse Scroll Direction
« Reply #5 on: February 07, 2024, 10:39:12 pm »
Does this mean you have two wheels or maybe a multi-directional ball/wheel?

No, many mouses (that i know) support pressing your mouse wheel to the side to scroll sideways
1. I have only one mouse wheel, so I don't need to check which wheel is being scrolled (for now).
This is exactly what I meant by multi-directional wheel.
Up and down scrolling is a wheel scroll and left and right will likely be a separate wheel scroll. This would mean that it would be giving the information to the driver (and then to your OS and then to you via SFML) as two separate wheels.
This brings me to believe it would be a good idea to the check which wheel.

In addition, are you certain that the only time this function is called is when the event type is correct?
Assuming you mean Project.ZoomView(), yes that is only called from that event, since it is a binding for the EventHandler Class (Which contains the event handling functions).
No, actually. Project.ZoomView was not to what I was referring.
Rather, the code that called OnMouseScroll.
I'm guessing (because this is C#) that this is called by the action of a mouse scroll so it should be okay but I would wonder if other SFML event types are getting passed into OnMouseScroll as it doesn't actively check its type to make sure and using the event if it's the incorrect type can "give bad values" - to state it casually.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DBD/Entropy

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Mouse Scroll Direction
« Reply #6 on: February 07, 2024, 10:52:32 pm »
Ill look into the multiple wheels thing.

The way events in C#-SFML work is the following:
The Window has a property public event "Eventname" for example MouseWheelScrolled, and you then add your event function onto this property like this:
_window.MouseWheelScrolled += EventHandler.OnMouseScroll:
assuming the guys at SFML did their job this should prevent bad inputs.

*A few minutes after typing*
I have put this in front of the switch statement, but this doesnt resolve the issue (as i personally expected) but at least people cant just scroll with their horizontal axis;
if (mouseEvent.Wheel == Mouse.Wheel.HorizontalWheel) return;

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Mouse Scroll Direction
« Reply #7 on: February 09, 2024, 09:31:58 am »
I don't quite understand what the issue.

This example programs works fine for me.
If I scroll up, it shows "Vertical Up 1" for example and if I scroll down it shows "Vertical Down 2" for example.

Does this not work for you?

using SFML.Graphics;
using SFML.Window;

var window = new RenderWindow(new VideoMode(400, 300), "SFML Test");
window.SetFramerateLimit(30);

var font = new Font("C:/Windows/Fonts/Arial.ttf");
var text = new Text("Test", font);

window.Closed += (sender, _) => ((RenderWindow)sender).Close();
window.MouseWheelScrolled += (sender, eventArgs) =>
{
    var wheel = eventArgs.Wheel switch
    {
        Mouse.Wheel.VerticalWheel => "Vertical",
        _ => "Horizontal"
    };

    var direction = eventArgs.Delta switch
    {
        > 0 => "Up",
        < 0 => "Down",
        _ => "??"
    };
   
    text.DisplayedString = $"{wheel} {direction} {eventArgs.Delta}";
};

while (window.IsOpen)
{
    window.DispatchEvents();
    window.Clear();
    window.Draw(text);
    window.Display();
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/