Skip to content

Commit

Permalink
Use OS-provided click counts on macOS and iOS for mouse press and rel…
Browse files Browse the repository at this point in the history
…ease events.
  • Loading branch information
slime73 committed Sep 24, 2016
1 parent bac5394 commit 450fa8c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
30 changes: 20 additions & 10 deletions src/events/SDL_mouse.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand All @@ -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 */
Expand All @@ -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);
Expand All @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions src/events/SDL_mouse_c.h
Expand Up @@ -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);

Expand Down
12 changes: 9 additions & 3 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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 */
Expand Down
6 changes: 4 additions & 2 deletions src/video/uikit/SDL_uikitview.m
Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down

0 comments on commit 450fa8c

Please sign in to comment.