Use OS-provided click counts on macOS and iOS for mouse press and release events.

This commit is contained in:
Alex Szpakowski 2016-09-24 18:46:34 -03:00
parent bac5394127
commit 450fa8cdf9
4 changed files with 36 additions and 15 deletions

View File

@ -322,15 +322,13 @@ static SDL_MouseClickState *GetMouseClickState(SDL_Mouse *mouse, Uint8 button)
return &mouse->clickstate[button]; return &mouse->clickstate[button];
} }
int static int
SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button) SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks)
{ {
SDL_Mouse *mouse = SDL_GetMouse(); SDL_Mouse *mouse = SDL_GetMouse();
int posted; int posted;
Uint32 type; Uint32 type;
Uint32 buttonstate = mouse->buttonstate; Uint32 buttonstate = mouse->buttonstate;
SDL_MouseClickState *clickstate = GetMouseClickState(mouse, button);
Uint8 click_count;
/* Figure out which event to perform */ /* Figure out which event to perform */
switch (state) { switch (state) {
@ -358,7 +356,8 @@ SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8
} }
mouse->buttonstate = buttonstate; mouse->buttonstate = buttonstate;
if (clickstate) { if (clicks < 0) {
SDL_MouseClickState *clickstate = GetMouseClickState(mouse, button);
if (state == SDL_PRESSED) { if (state == SDL_PRESSED) {
Uint32 now = SDL_GetTicks(); Uint32 now = SDL_GetTicks();
@ -374,9 +373,7 @@ SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8
++clickstate->click_count; ++clickstate->click_count;
} }
} }
click_count = clickstate->click_count; clicks = clickstate->click_count;
} else {
click_count = 1;
} }
/* Post the event, if desired */ /* 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.which = mouseID;
event.button.state = state; event.button.state = state;
event.button.button = button; event.button.button = button;
event.button.clicks = click_count; event.button.clicks = (Uint8) SDL_min(clicks, 255);
event.button.x = mouse->x; event.button.x = mouse->x;
event.button.y = mouse->y; event.button.y = mouse->y;
posted = (SDL_PushEvent(&event) > 0); posted = (SDL_PushEvent(&event) > 0);
@ -402,6 +399,19 @@ SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8
return posted; 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 int
SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction) SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction)
{ {

View File

@ -119,6 +119,9 @@ extern int SDL_SendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int rel
/* Send a mouse button event */ /* Send a mouse button event */
extern int SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button); 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 */ /* Send a mouse wheel event */
extern int SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction); extern int SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction);

View File

@ -823,6 +823,7 @@ SetWindowStyle(SDL_Window * window, NSUInteger style)
- (void)mouseDown:(NSEvent *)theEvent - (void)mouseDown:(NSEvent *)theEvent
{ {
int button; int button;
int clicks;
/* Ignore events that aren't inside the client area (i.e. title bar.) */ /* Ignore events that aren't inside the client area (i.e. title bar.) */
if ([theEvent window]) { if ([theEvent window]) {
@ -858,7 +859,9 @@ SetWindowStyle(SDL_Window * window, NSUInteger style)
button = (int) [theEvent buttonNumber] + 1; button = (int) [theEvent buttonNumber] + 1;
break; 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 - (void)rightMouseDown:(NSEvent *)theEvent
@ -874,6 +877,7 @@ SetWindowStyle(SDL_Window * window, NSUInteger style)
- (void)mouseUp:(NSEvent *)theEvent - (void)mouseUp:(NSEvent *)theEvent
{ {
int button; int button;
int clicks;
if ([self processHitTest:theEvent]) { if ([self processHitTest:theEvent]) {
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0); SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
@ -899,7 +903,9 @@ SetWindowStyle(SDL_Window * window, NSUInteger style)
button = (int) [theEvent buttonNumber] + 1; button = (int) [theEvent buttonNumber] + 1;
break; 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 - (void)rightMouseUp:(NSEvent *)theEvent

View File

@ -143,12 +143,13 @@
if (!firstFingerDown) { if (!firstFingerDown) {
CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO]; CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
int clicks = (int) touch.tapCount;
/* send mouse moved event */ /* send mouse moved event */
SDL_SendMouseMotion(sdlwindow, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y); SDL_SendMouseMotion(sdlwindow, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
/* send mouse down event */ /* 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; firstFingerDown = touch;
} }
@ -166,7 +167,8 @@
if (touch == firstFingerDown) { if (touch == firstFingerDown) {
/* send mouse up */ /* 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; firstFingerDown = nil;
} }