Commit 73db548d authored by Sam Lantinga's avatar Sam Lantinga

Fixed the code so we receive Cocoa touch events

parent dafed2a5
...@@ -26,17 +26,11 @@ ...@@ -26,17 +26,11 @@
#include "../../events/SDL_keyboard_c.h" #include "../../events/SDL_keyboard_c.h"
#include "../../events/scancodes_darwin.h" #include "../../events/scancodes_darwin.h"
//Touch Code
#include "../../events/SDL_touch_c.h"
#include "SDL_cocoatouch.h"
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
//#define DEBUG_IME NSLog //#define DEBUG_IME NSLog
#define DEBUG_IME #define DEBUG_IME
#define DEBUG_TOUCH NSLog
#ifndef NX_DEVICERCTLKEYMASK #ifndef NX_DEVICERCTLKEYMASK
#define NX_DEVICELCTLKEYMASK 0x00000001 #define NX_DEVICELCTLKEYMASK 0x00000001
#endif #endif
...@@ -71,7 +65,6 @@ ...@@ -71,7 +65,6 @@
} }
- (void) doCommandBySelector:(SEL)myselector; - (void) doCommandBySelector:(SEL)myselector;
- (void) setInputRect:(SDL_Rect *) rect; - (void) setInputRect:(SDL_Rect *) rect;
- (void) handleTouches:(cocoaTouchType)type WithEvent:(NSEvent*) event;
@end @end
@implementation SDLTranslatorResponder @implementation SDLTranslatorResponder
...@@ -198,83 +191,6 @@ ...@@ -198,83 +191,6 @@
return [NSArray array]; return [NSArray array];
} }
// Touch Code Begins -----------
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setAcceptsTouchEvents:YES];
[self setWantsRestingTouches:YES];
DEBUG_TOUCH(@"Initializing Cocoa Touch System....");
}
return self;
}
//Not an API function
- (void)handleTouches:(cocoaTouchType)type WithEvent:(NSEvent *)event {
NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:self];
NSEnumerator *enumerator = [touches objectEnumerator];
NSTouch *touch = (NSTouch*)[enumerator nextObject];
while (touch) {
long touchId = (long)[touch device];
if(!SDL_GetTouchIndex(touchId)) {
if(Cocoa_AddTouch(touch) < 0) continue;
}
float x = [touch normalizedPosition].x;
float y = [touch normalizedPosition].y;
long fingerId = (long)[touch identity];
switch (type) {
case COCOA_TOUCH_DOWN:
SDL_SendFingerDown(touchId,fingerId,
SDL_TRUE,x,y,1);
break;
case COCOA_TOUCH_UP:
case COCOA_TOUCH_CANCELLED:
SDL_SendFingerDown(touchId,fingerId,
SDL_FALSE,x,y,1);
case COCOA_TOUCH_MOVE:
SDL_SendTouchMotion(touchId,fingerId,
SDL_FALSE,x,y,1);
}
touch = (NSTouch*)[enumerator nextObject];
}
}
- (void)touchesBeganWithEvent:(NSEvent *)event {
DEBUG_TOUCH(@"Finger Down");
[self handleTouches: COCOA_TOUCH_DOWN WithEvent: event];
//Documentation said to call super, but examples do not
//[super touchesBeganWithEvent:event]
}
- (void)touchesMovedWithEvent:(NSEvent *)event {
DEBUG_TOUCH(@"Finger Moved");
[self handleTouches: COCOA_TOUCH_MOVE WithEvent: event];
//[super touchesMovedWithEvent:event]
}
- (void)touchesEndedWithEvent:(NSEvent *)event {
DEBUG_TOUCH(@"Finger Up");
[self handleTouches: COCOA_TOUCH_UP WithEvent: event];
//[super touchesEndedWithEvent:event]
}
- (void)touchesCancelledWithEvent:(NSEvent *)event {
DEBUG_TOUCH(@"Finger Cancelled");
[self handleTouches: COCOA_TOUCH_CANCELLED WithEvent: event];
//[super touchesCancelledWithEvent:event]
}
//Touch Code Ends --------------
@end @end
/* This is the original behavior, before support was added for /* This is the original behavior, before support was added for
...@@ -713,7 +629,6 @@ Cocoa_StartTextInput(_THIS) ...@@ -713,7 +629,6 @@ Cocoa_StartTextInput(_THIS)
if (!data->fieldEdit) { if (!data->fieldEdit) {
data->fieldEdit = data->fieldEdit =
[[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)]; [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
DEBUG_TOUCH(@"Accepts Touch events? %i",[data->fieldEdit acceptsTouchEvents]);
} }
if (![[data->fieldEdit superview] isEqual: parentView]) if (![[data->fieldEdit superview] isEqual: parentView])
...@@ -776,6 +691,7 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event) ...@@ -776,6 +691,7 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
if (![event isARepeat]) { if (![event isARepeat]) {
/* See if we need to rebuild the keyboard layout */ /* See if we need to rebuild the keyboard layout */
UpdateKeymap(data); UpdateKeymap(data);
}
SDL_SendKeyboardKey(SDL_PRESSED, code); SDL_SendKeyboardKey(SDL_PRESSED, code);
#if 1 #if 1
...@@ -783,7 +699,6 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event) ...@@ -783,7 +699,6 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
fprintf(stderr, "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n", scancode); fprintf(stderr, "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n", scancode);
} }
#endif #endif
}
if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) { if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
/* FIXME CW 2007-08-16: only send those events to the field editor for which we actually want text events, not e.g. esc or function keys. Arrow keys in particular seem to produce crashes sometimes. */ /* FIXME CW 2007-08-16: only send those events to the field editor for which we actually want text events, not e.g. esc or function keys. Arrow keys in particular seem to produce crashes sometimes. */
[data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]]; [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
...@@ -808,29 +723,6 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event) ...@@ -808,29 +723,6 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
} }
} }
Cocoa_AddTouch(NSTouch* finger) {
SDL_Touch touch;
touch.id = (long)[finger device];
//NSSize size = [finger deviceSize];
//touch.driverdata = SDL_malloc(sizeof(EventTouchData));
//EventTouchData* data = (EventTouchData*)(touch.driverdata);
touch.x_min = 0;
touch.x_max = 1;
touch.xres = touch.x_max - touch.x_min;
touch.y_min = 0;
touch.y_max = 1;
touch.yres = touch.y_max - touch.y_min;
touch.pressure_min = 0;
touch.pressure_max = 1;
touch.pressureres = touch.pressure_max - touch.pressure_min;
return SDL_AddTouch(&touch, "");
}
void void
Cocoa_QuitKeyboard(_THIS) Cocoa_QuitKeyboard(_THIS)
{ {
......
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2010 Sam Lantinga
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"
#ifndef _SDL_cocoatouch_h
#define _SDL_cocoatouch_h
extern void Cocoa_InitTouch(_THIS);
extern int Cocoa_AddTouch(NSTouch* finger);
extern void Cocoa_QuitTouch(_THIS);
typedef enum {COCOA_TOUCH_DOWN,COCOA_TOUCH_UP,COCOA_TOUCH_MOVE,COCOA_TOUCH_CANCELLED} cocoaTouchType;
#endif /* _SDL_touch_h */
/* vi: set ts=4 sw=4 expandtab: */
...@@ -64,6 +64,20 @@ typedef struct SDL_WindowData SDL_WindowData; ...@@ -64,6 +64,20 @@ typedef struct SDL_WindowData SDL_WindowData;
-(void) rightMouseDragged:(NSEvent *) theEvent; -(void) rightMouseDragged:(NSEvent *) theEvent;
-(void) otherMouseDragged:(NSEvent *) theEvent; -(void) otherMouseDragged:(NSEvent *) theEvent;
-(void) scrollWheel:(NSEvent *) theEvent; -(void) scrollWheel:(NSEvent *) theEvent;
-(void) touchesBeganWithEvent:(NSEvent *) theEvent;
-(void) touchesMovedWithEvent:(NSEvent *) theEvent;
-(void) touchesEndedWithEvent:(NSEvent *) theEvent;
-(void) touchesCancelledWithEvent:(NSEvent *) theEvent;
/* Touch event handling */
typedef enum {
COCOA_TOUCH_DOWN,
COCOA_TOUCH_UP,
COCOA_TOUCH_MOVE,
COCOA_TOUCH_CANCELLED
} cocoaTouchType;
-(void) handleTouches:(cocoaTouchType)type withEvent:(NSEvent*) event;
@end @end
/* *INDENT-ON* */ /* *INDENT-ON* */
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
#include "../../events/SDL_keyboard_c.h" #include "../../events/SDL_keyboard_c.h"
#include "../../events/SDL_mouse_c.h" #include "../../events/SDL_mouse_c.h"
#include "../../events/SDL_touch_c.h"
#include "../../events/SDL_windowevents_c.h" #include "../../events/SDL_windowevents_c.h"
#include "SDL_cocoavideo.h" #include "SDL_cocoavideo.h"
...@@ -60,6 +61,7 @@ static __inline__ void ConvertNSRect(NSRect *r) ...@@ -60,6 +61,7 @@ static __inline__ void ConvertNSRect(NSRect *r)
[center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp]; [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
[_data->nswindow setAcceptsMouseMovedEvents:YES]; [_data->nswindow setAcceptsMouseMovedEvents:YES];
[[_data->nswindow contentView] setAcceptsTouchEvents:YES];
} }
- (void)close - (void)close
...@@ -268,6 +270,72 @@ static __inline__ void ConvertNSRect(NSRect *r) ...@@ -268,6 +270,72 @@ static __inline__ void ConvertNSRect(NSRect *r)
SDL_SendMouseWheel(_data->window, (int)x, (int)y); SDL_SendMouseWheel(_data->window, (int)x, (int)y);
} }
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
{
[self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
}
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
{
[self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
}
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
{
[self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
}
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
{
[self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
}
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
{
NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
NSEnumerator *enumerator = [touches objectEnumerator];
NSTouch *touch = (NSTouch*)[enumerator nextObject];
while (touch) {
long touchId = (long)[touch device];
if (!SDL_GetTouch(touchId)) {
SDL_Touch touch;
touch.id = touchId;
touch.x_min = 0;
touch.x_max = 1;
touch.xres = touch.x_max - touch.x_min;
touch.y_min = 0;
touch.y_max = 1;
touch.yres = touch.y_max - touch.y_min;
touch.pressure_min = 0;
touch.pressure_max = 1;
touch.pressureres = touch.pressure_max - touch.pressure_min;
if (SDL_AddTouch(&touch, "") < 0) {
return;
}
}
float x = [touch normalizedPosition].x;
float y = [touch normalizedPosition].y;
long fingerId = (long)[touch identity];
switch (type) {
case COCOA_TOUCH_DOWN:
SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
break;
case COCOA_TOUCH_UP:
case COCOA_TOUCH_CANCELLED:
SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
break;
case COCOA_TOUCH_MOVE:
SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
break;
}
touch = (NSTouch*)[enumerator nextObject];
}
}
@end @end
@interface SDLWindow : NSWindow @interface SDLWindow : NSWindow
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment