src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 10:50:53 -0800
changeset 5371 fc3d3d580777
parent 5262 b530ef003506
child 5376 183ec2d4485c
permissions -rw-r--r--
Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode. The only reason we need a custom view is to handle right mouse down. Implemented mouse grabbing, although it's kind of clunky right now. I'll be adding a relative mode that will be smoother soon.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
5262
b530ef003506 Happy 2011! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5060
diff changeset
     3
    Copyright (C) 1997-2011 Sam Lantinga
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
    Lesser General Public License for more details.
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free Software
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
    Sam Lantinga
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
    slouken@libsdl.org
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
*/
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
#include "SDL_config.h"
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    24
#include "SDL_events.h"
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include "SDL_cocoavideo.h"
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "../../events/SDL_mouse_c.h"
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
void
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
Cocoa_InitMouse(_THIS)
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
{
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
}
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    34
static int
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    35
ConvertMouseButtonToSDL(int button)
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    36
{
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    37
    switch (button)
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    38
    {
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    39
        case 0:
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    40
            return(SDL_BUTTON_LEFT);   /* 1 */
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    41
        case 1:
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    42
            return(SDL_BUTTON_RIGHT);  /* 3 */
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    43
        case 2:
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    44
            return(SDL_BUTTON_MIDDLE); /* 2 */
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    45
    }
5060
2170edcdfbd0 Fixed mouse button index for additional mouse buttons
Sam Lantinga <slouken@libsdl.org>
parents: 5058
diff changeset
    46
    return button+1;
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    47
}
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    48
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    49
void
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    50
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    51
{
5371
fc3d3d580777 Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    52
    /* We're correctly using views even in fullscreen mode now */
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    53
}
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    54
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
void
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    56
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    57
{
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    58
    float x = [event deltaX];
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    59
    float y = [event deltaY];
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    60
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    61
    if (x > 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    62
        x += 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    63
    } else if (x < 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    64
        x -= 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    65
    }
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    66
    if (y > 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    67
        y += 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    68
    } else if (y < 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    69
        y -= 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    70
    }
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    71
    SDL_SendMouseWheel(window, (int)x, (int)y);
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    72
}
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
    73
5058
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
    74
void
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
    75
Cocoa_QuitMouse(_THIS)
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
    76
{
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
    77
}
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
    78
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
/* vi: set ts=4 sw=4 expandtab: */