From 4ccec4f3adc1eb08fd5c3fe08d4168803b9f8e6c Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Tue, 14 Jun 2016 02:18:48 +0200 Subject: [PATCH] added mouse wheel support also for SDL2 target --- src/events.c | 50 ++++++++++++++++++++++++++++++++++++++++--- src/events.h | 1 + src/libgame/gadgets.c | 2 +- src/libgame/sdl.h | 2 ++ src/libgame/system.c | 1 + src/libgame/system.h | 13 +++++------ 6 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/events.c b/src/events.c index d1f69f99..dcb957a5 100644 --- a/src/events.c +++ b/src/events.c @@ -27,6 +27,7 @@ #define DEBUG_EVENTS_BUTTON (DEBUG_EVENTS * 0) #define DEBUG_EVENTS_MOTION (DEBUG_EVENTS * 0) +#define DEBUG_EVENTS_WHEEL (DEBUG_EVENTS * 1) #define DEBUG_EVENTS_WINDOW (DEBUG_EVENTS * 0) #define DEBUG_EVENTS_FINGER (DEBUG_EVENTS * 0) #define DEBUG_EVENTS_TEXT (DEBUG_EVENTS * 1) @@ -174,6 +175,10 @@ void HandleEvents() break; #if defined(TARGET_SDL2) + case EVENT_WHEELMOTION: + HandleWheelEvent((WheelEvent *) &event); + break; + case SDL_WINDOWEVENT: HandleWindowEvent((WindowEvent *) &event); break; @@ -434,6 +439,45 @@ void HandleMotionEvent(MotionEvent *event) #if defined(TARGET_SDL2) +void HandleWheelEvent(WheelEvent *event) +{ + int button_nr; + +#if DEBUG_EVENTS_WHEEL +#if 1 + Error(ERR_DEBUG, "WHEEL EVENT: mouse == %d, x/y == %d/%d\n", + event->which, event->x, event->y); +#else + // (SDL_MOUSEWHEEL_NORMAL/SDL_MOUSEWHEEL_FLIPPED needs SDL 2.0.4 or newer) + Error(ERR_DEBUG, "WHEEL EVENT: mouse == %d, x/y == %d/%d, direction == %s\n", + event->which, event->x, event->y, + (event->direction == SDL_MOUSEWHEEL_NORMAL ? "SDL_MOUSEWHEEL_NORMAL" : + "SDL_MOUSEWHEEL_FLIPPED")); +#endif +#endif + + button_nr = (event->x < 0 ? MB_WHEEL_LEFT : + event->x > 0 ? MB_WHEEL_RIGHT : + event->y < 0 ? MB_WHEEL_DOWN : + event->y > 0 ? MB_WHEEL_UP : 0); + +#if defined(PLATFORM_WIN32) || defined(PLATFORM_MACOSX) + // accelerated mouse wheel available on Mac and Windows + wheel_steps = (event->x ? ABS(event->x) : ABS(event->y)); +#else + // no accelerated mouse wheel available on Unix/Linux + wheel_steps = DEFAULT_WHEEL_STEPS; +#endif + + motion_status = FALSE; + + button_status = button_nr; + HandleButton(0, 0, button_status, -button_nr); + + button_status = MB_RELEASED; + HandleButton(0, 0, button_status, -button_nr); +} + void HandleWindowEvent(WindowEvent *event) { #if DEBUG_EVENTS_WINDOW @@ -948,11 +992,11 @@ void HandleButton(int mx, int my, int button, int button_nr) static int old_mx = 0, old_my = 0; boolean button_hold = FALSE; - if (button < 0) + if (button_nr < 0) { mx = old_mx; my = old_my; - button = -button; + button_nr = -button_nr; button_hold = TRUE; } else @@ -1618,7 +1662,7 @@ void HandleNoEvent() // if (button_status && game_status != GAME_MODE_PLAYING) if (button_status && (game_status != GAME_MODE_PLAYING || tape.pausing)) { - HandleButton(0, 0, -button_status, button_status); + HandleButton(0, 0, button_status, -button_status); } else { diff --git a/src/events.h b/src/events.h index 664f3778..0768f11f 100644 --- a/src/events.h +++ b/src/events.h @@ -32,6 +32,7 @@ void HandleExposeEvent(ExposeEvent *); void HandleButtonEvent(ButtonEvent *); void HandleMotionEvent(MotionEvent *); #if defined(TARGET_SDL2) +void HandleWheelEvent(WheelEvent *); void HandleWindowEvent(WindowEvent *); void HandleFingerEvent(FingerEvent *); void HandleTextEvent(TextEvent *); diff --git a/src/libgame/gadgets.c b/src/libgame/gadgets.c index 6200c9dc..1a0da4b4 100644 --- a/src/libgame/gadgets.c +++ b/src/libgame/gadgets.c @@ -1779,7 +1779,7 @@ boolean HandleGadgets(int mx, int my, int button) { boolean scroll_single_step = ((GetKeyModState() & KMOD_Alt) != 0); - item_steps = (scroll_single_step ? 1 : DEFAULT_WHEEL_STEPS); + item_steps = (scroll_single_step ? 1 : wheel_steps); item_direction = (button == MB_WHEEL_UP || button == MB_WHEEL_LEFT ? -1 : +1); } diff --git a/src/libgame/sdl.h b/src/libgame/sdl.h index 4d70bfcd..60a8411f 100644 --- a/src/libgame/sdl.h +++ b/src/libgame/sdl.h @@ -80,6 +80,7 @@ typedef SDL_Event Event; typedef SDL_MouseButtonEvent ButtonEvent; typedef SDL_MouseMotionEvent MotionEvent; #if defined(TARGET_SDL2) +typedef SDL_MouseWheelEvent WheelEvent; typedef SDL_TouchFingerEvent FingerEvent; typedef SDL_TextInputEvent TextEvent; typedef SDL_Event PauseResumeEvent; @@ -127,6 +128,7 @@ struct MouseCursorInfo #define EVENT_BUTTONRELEASE SDL_MOUSEBUTTONUP #define EVENT_MOTIONNOTIFY SDL_MOUSEMOTION #if defined(TARGET_SDL2) +#define EVENT_WHEELMOTION SDL_MOUSEWHEEL #define EVENT_FINGERPRESS SDL_FINGERDOWN #define EVENT_FINGERRELEASE SDL_FINGERUP #define EVENT_FINGERMOTION SDL_FINGERMOTION diff --git a/src/libgame/system.c b/src/libgame/system.c index 600b4b32..7dc231a8 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -50,6 +50,7 @@ DrawBuffer *drawto = NULL; int button_status = MB_NOT_PRESSED; boolean motion_status = FALSE; +int wheel_steps = DEFAULT_WHEEL_STEPS; #if defined(TARGET_SDL2) boolean keyrepeat_status = TRUE; #endif diff --git a/src/libgame/system.h b/src/libgame/system.h index 0bd948bd..06bed391 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -179,12 +179,12 @@ #define MB_WHEEL_DOWN 5 #define MB_WHEEL_LEFT 6 #define MB_WHEEL_RIGHT 7 -#define IS_WHEEL_BUTTON_VERTICAL(b) ((b) >= MB_WHEEL_UP && \ - (b) <= MB_WHEEL_DOWN) -#define IS_WHEEL_BUTTON_HORIZONTAL(b) ((b) >= MB_WHEEL_LEFT && \ - (b) <= MB_WHEEL_RIGHT) -#define IS_WHEEL_BUTTON(b) ((b) >= MB_WHEEL_UP && \ - (b) <= MB_WHEEL_DOWN) +#define IS_WHEEL_BUTTON_VERTICAL(b) ((b) == MB_WHEEL_UP || \ + (b) == MB_WHEEL_DOWN) +#define IS_WHEEL_BUTTON_HORIZONTAL(b) ((b) == MB_WHEEL_LEFT || \ + (b) == MB_WHEEL_RIGHT) +#define IS_WHEEL_BUTTON(b) (IS_WHEEL_BUTTON_VERTICAL(b) || \ + IS_WHEEL_BUTTON_HORIZONTAL(b)) #define DEFAULT_WHEEL_STEPS 3 #define BUTTON_STEPSIZE(b) ((b) == MB_LEFTBUTTON ? 1 : \ @@ -1376,6 +1376,7 @@ extern DrawBuffer *drawto; extern int button_status; extern boolean motion_status; +extern int wheel_steps; #if defined(TARGET_SDL2) extern boolean keyrepeat_status; #endif -- 2.34.1