From 450fa8cdf9b58d87c5b1ee0a3a250106aa7adf2d Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 24 Sep 2016 18:46:34 -0300 Subject: [PATCH] Use OS-provided click counts on macOS and iOS for mouse press and release events. --- src/events/SDL_mouse.c | 30 ++++++++++++++++++++---------- src/events/SDL_mouse_c.h | 3 +++ src/video/cocoa/SDL_cocoawindow.m | 12 +++++++++--- src/video/uikit/SDL_uikitview.m | 6 ++++-- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c index 7793de870bee9..d54519c121be5 100644 --- a/src/events/SDL_mouse.c +++ b/src/events/SDL_mouse.c @@ -322,15 +322,13 @@ static SDL_MouseClickState *GetMouseClickState(SDL_Mouse *mouse, Uint8 button) return &mouse->clickstate[button]; } -int -SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button) +static int +SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks) { SDL_Mouse *mouse = SDL_GetMouse(); int posted; Uint32 type; Uint32 buttonstate = mouse->buttonstate; - SDL_MouseClickState *clickstate = GetMouseClickState(mouse, button); - Uint8 click_count; /* Figure out which event to perform */ switch (state) { @@ -358,7 +356,8 @@ SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 } mouse->buttonstate = buttonstate; - if (clickstate) { + if (clicks < 0) { + SDL_MouseClickState *clickstate = GetMouseClickState(mouse, button); if (state == SDL_PRESSED) { Uint32 now = SDL_GetTicks(); @@ -374,9 +373,7 @@ SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 ++clickstate->click_count; } } - click_count = clickstate->click_count; - } else { - click_count = 1; + clicks = clickstate->click_count; } /* Post the event, if desired */ @@ -388,7 +385,7 @@ SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 event.button.which = mouseID; event.button.state = state; event.button.button = button; - event.button.clicks = click_count; + event.button.clicks = (Uint8) SDL_min(clicks, 255); event.button.x = mouse->x; event.button.y = mouse->y; posted = (SDL_PushEvent(&event) > 0); @@ -398,10 +395,23 @@ SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 if (window && state == SDL_RELEASED) { SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate); } - + return posted; } +int +SDL_SendMouseButtonClicks(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks) +{ + clicks = SDL_max(clicks, 0); + return SDL_PrivateSendMouseButton(window, mouseID, state, button, clicks); +} + +int +SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button) +{ + return SDL_PrivateSendMouseButton(window, mouseID, state, button, -1); +} + int SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction) { diff --git a/src/events/SDL_mouse_c.h b/src/events/SDL_mouse_c.h index 03aca0a5c78fe..06dc88701324f 100644 --- a/src/events/SDL_mouse_c.h +++ b/src/events/SDL_mouse_c.h @@ -119,6 +119,9 @@ extern int SDL_SendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int rel /* Send a mouse button event */ extern int SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button); +/* Send a mouse button event with a click count */ +extern int SDL_SendMouseButtonClicks(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks); + /* Send a mouse wheel event */ extern int SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction); diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m index a1f93a5a3f3b9..3c86236291f5b 100644 --- a/src/video/cocoa/SDL_cocoawindow.m +++ b/src/video/cocoa/SDL_cocoawindow.m @@ -823,6 +823,7 @@ - (BOOL)processHitTest:(NSEvent *)theEvent - (void)mouseDown:(NSEvent *)theEvent { int button; + int clicks; /* Ignore events that aren't inside the client area (i.e. title bar.) */ if ([theEvent window]) { @@ -858,7 +859,9 @@ - (void)mouseDown:(NSEvent *)theEvent button = (int) [theEvent buttonNumber] + 1; break; } - SDL_SendMouseButton(_data->window, 0, SDL_PRESSED, button); + + clicks = (int) [theEvent clickCount]; + SDL_SendMouseButtonClicks(_data->window, 0, SDL_PRESSED, button, clicks); } - (void)rightMouseDown:(NSEvent *)theEvent @@ -874,6 +877,7 @@ - (void)otherMouseDown:(NSEvent *)theEvent - (void)mouseUp:(NSEvent *)theEvent { int button; + int clicks; if ([self processHitTest:theEvent]) { SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0); @@ -899,7 +903,9 @@ - (void)mouseUp:(NSEvent *)theEvent button = (int) [theEvent buttonNumber] + 1; break; } - SDL_SendMouseButton(_data->window, 0, SDL_RELEASED, button); + + clicks = (int) [theEvent clickCount]; + SDL_SendMouseButtonClicks(_data->window, 0, SDL_RELEASED, button, clicks); } - (void)rightMouseUp:(NSEvent *)theEvent @@ -1264,7 +1270,7 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent } } - [nswindow setContentView: contentView]; + [nswindow setContentView:contentView]; [contentView release]; /* Allow files and folders to be dragged onto the window by users */ diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m index d15b7efcdb459..c7d9f51d9ce1c 100644 --- a/src/video/uikit/SDL_uikitview.m +++ b/src/video/uikit/SDL_uikitview.m @@ -143,12 +143,13 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event if (!firstFingerDown) { CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO]; + int clicks = (int) touch.tapCount; /* send mouse moved event */ SDL_SendMouseMotion(sdlwindow, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y); /* send mouse down event */ - SDL_SendMouseButton(sdlwindow, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT); + SDL_SendMouseButtonClicks(sdlwindow, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT, clicks); firstFingerDown = touch; } @@ -166,7 +167,8 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event if (touch == firstFingerDown) { /* send mouse up */ - SDL_SendMouseButton(sdlwindow, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT); + int clicks = (int) touch.tapCount; + SDL_SendMouseButtonClicks(sdlwindow, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT, clicks); firstFingerDown = nil; }