SDL DOES support mouse wheel

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
tsev
Space Kraken
Posts: 167
Joined: Thu Jun 26, 2003 2:17 pm
Location: Pittsburgh, PA

SDL DOES support mouse wheel

#1 Post by tsev »

I found in the CVS repository for SDL that it has support for mouse buttons 4 and 5, which correspond to mouse wheel up and mouse wheel down.

Revision 1.56 / (download) / (as text) - annotate - [select for diffs] , Mon Aug 19 18:09:44 2002 UTC (10 months, 2 weeks ago) by slouken
Changes since 1.55: +1 -0 lines
Diff to previous 1.55
Added SDL_BUTTON_WHEELUP (4) and SDL_BUTTON_WHEELDOWN (5)

FreeOrion Programmer

Tyreth
FreeOrion Lead Emeritus
Posts: 885
Joined: Thu Jun 26, 2003 6:23 am
Location: Australia

#2 Post by Tyreth »

Indeed, I should have mentioned that this was possible, as I have used it in previous exporations into SDL myself :)

tzlaine
Programming Lead Emeritus
Posts: 1092
Joined: Thu Jun 26, 2003 1:33 pm

#3 Post by tzlaine »

Let me guess. The only place this functionality is "documented" is in the SDL source code, right? If I can figure out how it works, I'll add mouse wheel support to GG, and our app can use that.

Edit: Specifically, how do you get the amount of scrolling up/down from the pair of flags SDL_BUTTON_WHEELUP and SDL_BUTTON_WHEELDOWN? Is there something else in the button state that indicates scrolled distance?

For the windows platform, here's how mouse wheel messages are generated.
From SDL's src/video/wincommon/SDL_sysevents.c:

Code: Select all

		case WM_MOUSEWHEEL: 
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
				int move = (short)HIWORD(wParam);
				if ( move ) {
					Uint8 button;
					if ( move > 0 )
						button = SDL_BUTTON_WHEELUP;
					else
						button = SDL_BUTTON_WHEELDOWN;
					posted = SDL_PrivateMouseButton(
						SDL_PRESSED, button, 0, 0);
					posted |= SDL_PrivateMouseButton(
						SDL_RELEASED, button, 0, 0);
				}
			}
			return(0);
Notice that move, which is an integer value, is converted effectively into a bool that means up or down. The distance is excluded. For the DirectX DirectInput code, it does the exact same thing; it takes the amount of movement of the wheel and converts it do up or down only.

Tyreth, do you remember how well this works under Linux? Is it pretty smooth?

tsev
Space Kraken
Posts: 167
Joined: Thu Jun 26, 2003 2:17 pm
Location: Pittsburgh, PA

#4 Post by tsev »

The mouse driver defines the amount of movement for a wheel up or down in terms of lines of text, the default being 3.

If the value is converted to just "on" or "off" then we might actually have to set up a polling loop in a separate thread or something to make the mouse scroll smoothly.

What about pressing the mouse wheel as a mouse button? Anything in the source about that? I assume its just another constant...button 6 or something.

edit: Or we can rebuild SDL so that it stores the delta for the mouse wheel :D
FreeOrion Programmer

tzlaine
Programming Lead Emeritus
Posts: 1092
Joined: Thu Jun 26, 2003 1:33 pm

#5 Post by tzlaine »

tsev wrote:The mouse driver defines the amount of movement for a wheel up or down in terms of lines of text, the default being 3.
This is true; what I'm referring to though is the fact that you may move up two ticks of the wheel between each message that SDL generates if you spin it fast, but may get all the ticks at slower speeds. So that the faster you spin the wheel, the less responsive it is. I son't know if this will be an issue, but since the code is going from +/-n to +/-1 I think it might be.
If the value is converted to just "on" or "off" then we might actually have to set up a polling loop in a separate thread or something to make the mouse scroll smoothly.

What about pressing the mouse wheel as a mouse button? Anything in the source about that? I assume its just another constant...button 6 or something.
The mouse wheel is the same as the middle button (button 2).
edit: Or we can rebuild SDL so that it stores the delta for the mouse wheel :D
If we need to, we may have to do this, but I really, really hope not.

Tyreth
FreeOrion Lead Emeritus
Posts: 885
Joined: Thu Jun 26, 2003 6:23 am
Location: Australia

#6 Post by Tyreth »

I had some code like this in another project of mine:

Code: Select all

  SDL_Event event;
  while ( SDL_PollEvent(&event) )
        {
                if (event.type == SDL_MOUSEBUTTONDOWN)
                {
                        if (event.button.button == 4)
                        {
                                zoom += 0.4f;
                                if (zoom >= -0.4f)
                                        zoom = -0.4f;
                        }
                                                                                                             
                        if (event.button.button == 5)
                                zoom -= 0.4f;
                }
        }
Please excuse how messy it is.

Post Reply