Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
134 lines (116 loc) · 3.53 KB

SDL_cocoamouse.m

File metadata and controls

134 lines (116 loc) · 3.53 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Jul 8, 2010
Jul 8, 2010
3
Copyright (C) 1997-2010 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
Dec 3, 2009
Dec 3, 2009
24
#include "SDL_events.h"
25
26
27
28
29
30
31
32
33
#include "SDL_cocoavideo.h"
#include "../../events/SDL_mouse_c.h"
void
Cocoa_InitMouse(_THIS)
{
}
Dec 3, 2009
Dec 3, 2009
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
static int
ConvertMouseButtonToSDL(int button)
{
switch (button)
{
case 0:
return(SDL_BUTTON_LEFT); /* 1 */
case 1:
return(SDL_BUTTON_RIGHT); /* 3 */
case 2:
return(SDL_BUTTON_MIDDLE); /* 2 */
}
return button;
}
void
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
{
int i;
May 10, 2010
May 10, 2010
53
NSPoint point = { 0, 0 };
Dec 3, 2009
Dec 3, 2009
54
SDL_Window *window;
Jul 6, 2010
Jul 6, 2010
55
SDL_Window *focus = SDL_GetMouseFocus();
Dec 3, 2009
Dec 3, 2009
56
57
58
59
60
61
62
63
/* See if there are any fullscreen windows that might handle this event */
window = NULL;
for (i = 0; i < _this->num_displays; ++i) {
SDL_VideoDisplay *display = &_this->displays[i];
SDL_Window *candidate = display->fullscreen_window;
if (candidate) {
Dec 6, 2009
Dec 6, 2009
64
SDL_Rect bounds;
Dec 3, 2009
Dec 3, 2009
65
Dec 6, 2009
Dec 6, 2009
66
Cocoa_GetDisplayBounds(_this, display, &bounds);
Dec 3, 2009
Dec 3, 2009
67
point = [NSEvent mouseLocation];
Dec 6, 2009
Dec 6, 2009
68
69
point.x = point.x - bounds.x;
point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - bounds.y;
Jul 6, 2010
Jul 6, 2010
70
if ((point.x >= 0 && point.x < candidate->w) &&
May 10, 2010
May 10, 2010
71
(point.y >= 0 && point.y < candidate->h)) {
Dec 3, 2009
Dec 3, 2009
72
73
74
/* This is it! */
window = candidate;
break;
Jul 6, 2010
Jul 6, 2010
75
76
} else if (candidate == focus) {
SDL_SetMouseFocus(NULL);
Dec 3, 2009
Dec 3, 2009
77
78
79
80
}
}
}
Jul 6, 2010
Jul 6, 2010
81
if (!window) {
May 10, 2010
May 10, 2010
82
return;
Dec 3, 2009
Dec 3, 2009
83
84
85
86
87
88
}
switch ([event type]) {
case NSLeftMouseDown:
case NSOtherMouseDown:
case NSRightMouseDown:
Jul 6, 2010
Jul 6, 2010
89
SDL_SendMouseButton(window, SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
Dec 3, 2009
Dec 3, 2009
90
91
92
93
break;
case NSLeftMouseUp:
case NSOtherMouseUp:
case NSRightMouseUp:
Jul 6, 2010
Jul 6, 2010
94
SDL_SendMouseButton(window, SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
Dec 3, 2009
Dec 3, 2009
95
break;
Jan 20, 2011
Jan 20, 2011
96
97
98
case NSScrollWheel:
Cocoa_HandleMouseWheel(window, event);
break;
Dec 3, 2009
Dec 3, 2009
99
100
101
102
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged: /* usually middle mouse dragged */
case NSMouseMoved:
Jul 6, 2010
Jul 6, 2010
103
SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
Dec 3, 2009
Dec 3, 2009
104
105
106
107
108
109
break;
default: /* just to avoid compiler warnings */
break;
}
}
110
111
112
113
114
void
Cocoa_QuitMouse(_THIS)
{
}
Jan 20, 2011
Jan 20, 2011
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
void
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
{
float x = [event deltaX];
float y = [event deltaY];
if (x > 0) {
x += 0.9f;
} else if (x < 0) {
x -= 0.9f;
}
if (y > 0) {
y += 0.9f;
} else if (y < 0) {
y -= 0.9f;
}
SDL_SendMouseWheel(window, (int)x, (int)y);
}
134
/* vi: set ts=4 sw=4 expandtab: */