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