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