Skip to content

Commit 0447c2f

Browse files
committed
events: Add integer wheel fields for sdl2-compat
It's way simpler to just add them back to SDL3 than emulate them purely in sdl2-compat.
1 parent ae251a0 commit 0447c2f

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

include/SDL3/SDL_events.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ typedef struct SDL_MouseWheelEvent
492492
SDL_MouseWheelDirection direction; /**< Set to one of the SDL_MOUSEWHEEL_* defines. When FLIPPED the values in X and Y will be opposite. Multiply by -1 to change them back */
493493
float mouse_x; /**< X coordinate, relative to window */
494494
float mouse_y; /**< Y coordinate, relative to window */
495+
Sint32 integer_x; /**< The amount scrolled horizontally, accumulated to whole scroll "ticks" (added in 3.2.12) */
496+
Sint32 integer_y; /**< The amount scrolled vertically, accumulated to whole scroll "ticks" (added in 3.2.12) */
495497
} SDL_MouseWheelEvent;
496498

497499
/**

src/events/SDL_events.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,9 +625,10 @@ int SDL_GetEventDescription(const SDL_Event *event, char *buf, int buflen)
625625
#undef PRINT_MBUTTON_EVENT
626626

627627
SDL_EVENT_CASE(SDL_EVENT_MOUSE_WHEEL)
628-
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u x=%g y=%g direction=%s)",
628+
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u x=%g y=%g integer_x=%d integer_y=%d direction=%s)",
629629
(uint)event->wheel.timestamp, (uint)event->wheel.windowID,
630630
(uint)event->wheel.which, event->wheel.x, event->wheel.y,
631+
(int)event->wheel.integer_x, (int)event->wheel.integer_y,
631632
event->wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped");
632633
break;
633634

src/events/SDL_mouse.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,18 +1040,14 @@ void SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseI
10401040
SDL_SetMouseFocus(window);
10411041
}
10421042

1043-
// Accumulate fractional wheel motion if integer mode is enabled
1044-
if (mouse->integer_mode_flags & 2) {
1045-
mouse->integer_mode_residual_scroll_x = SDL_modff(mouse->integer_mode_residual_scroll_x + x, &x);
1046-
mouse->integer_mode_residual_scroll_y = SDL_modff(mouse->integer_mode_residual_scroll_y + y, &y);
1047-
}
1048-
10491043
if (x == 0.0f && y == 0.0f) {
10501044
return;
10511045
}
10521046

10531047
// Post the event, if desired
10541048
if (SDL_EventEnabled(SDL_EVENT_MOUSE_WHEEL)) {
1049+
float integer_x, integer_y;
1050+
10551051
if (!mouse->relative_mode || mouse->warp_emulation_active) {
10561052
// We're not in relative mode, so all mouse events are global mouse events
10571053
mouseID = SDL_GLOBAL_MOUSE_ID;
@@ -1062,11 +1058,26 @@ void SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseI
10621058
event.common.timestamp = timestamp;
10631059
event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
10641060
event.wheel.which = mouseID;
1065-
event.wheel.x = x;
1066-
event.wheel.y = y;
10671061
event.wheel.direction = direction;
10681062
event.wheel.mouse_x = mouse->x;
10691063
event.wheel.mouse_y = mouse->y;
1064+
1065+
mouse->residual_scroll_x = SDL_modff(mouse->residual_scroll_x + x, &integer_x);
1066+
event.wheel.integer_x = (Sint32)integer_x;
1067+
1068+
mouse->residual_scroll_y = SDL_modff(mouse->residual_scroll_y + y, &integer_y);
1069+
event.wheel.integer_y = (Sint32)integer_y;
1070+
1071+
// Return the accumulated values in x/y when integer wheel mode is enabled.
1072+
// This is necessary for compatibility with sdl2-compat 2.32.54.
1073+
if (mouse->integer_mode_flags & 2) {
1074+
event.wheel.x = integer_x;
1075+
event.wheel.y = integer_y;
1076+
} else {
1077+
event.wheel.x = x;
1078+
event.wheel.y = y;
1079+
}
1080+
10701081
SDL_PushEvent(&event);
10711082
}
10721083
}

src/events/SDL_mouse_c.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ typedef struct
9999
Uint8 integer_mode_flags; // 1 to enable mouse quantization, 2 to enable wheel quantization
100100
float integer_mode_residual_motion_x;
101101
float integer_mode_residual_motion_y;
102-
float integer_mode_residual_scroll_x;
103-
float integer_mode_residual_scroll_y;
104102

105103
// Data common to all mice
106104
SDL_Window *focus;
@@ -109,6 +107,8 @@ typedef struct
109107
float x_accu;
110108
float y_accu;
111109
float last_x, last_y; // the last reported x and y coordinates
110+
float residual_scroll_x;
111+
float residual_scroll_y;
112112
double click_motion_x;
113113
double click_motion_y;
114114
bool has_position;

0 commit comments

Comments
 (0)