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

Commit

Permalink
Added Iphone support (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtla committed Jul 13, 2010
1 parent 42b3698 commit e289891
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 29 deletions.
10 changes: 7 additions & 3 deletions src/video/uikit/SDL_uikitview.h
Expand Up @@ -22,8 +22,6 @@

#import <UIKit/UIKit.h>
#include "SDL_stdinc.h"
#include "SDL_mouse.h"
#include "SDL_mouse_c.h"
#include "SDL_events.h"

#if SDL_IPHONE_MULTIPLE_MICE
Expand All @@ -38,8 +36,14 @@
#else
@interface SDL_uikitview : UIView {
#endif


#if FIXME_MULTITOUCH
SDL_Mouse mice[MAX_SIMULTANEOUS_TOUCHES];
#endif

#if FIXED_MULTITOUCH
int touchId;
#endif

#if SDL_IPHONE_KEYBOARD
UITextField *textField;
Expand Down
131 changes: 105 additions & 26 deletions src/video/uikit/SDL_uikitview.m
Expand Up @@ -22,8 +22,10 @@

#import "SDL_uikitview.h"

#include "../../events/SDL_keyboard_c.h"
#include "../../events/SDL_mouse_c.h"

#if SDL_IPHONE_KEYBOARD
#import "SDL_keyboard_c.h"
#import "keyinfotable.h"
#import "SDL_uikitappdelegate.h"
#import "SDL_uikitwindow.h"
Expand All @@ -33,7 +35,6 @@ @implementation SDL_uikitview

- (void)dealloc {
#if SDL_IPHONE_KEYBOARD
SDL_DelKeyboard(0);
[textField release];
#endif
[super dealloc];
Expand All @@ -47,13 +48,35 @@ - (id)initWithFrame:(CGRect)frame {
[self initializeKeyboard];
#endif

#if FIXME_MULTITOUCH
int i;
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
mice[i].id = i;
mice[i].driverdata = NULL;
SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1);
}
self.multipleTouchEnabled = YES;
#endif
#if FIXED_MULTITOUCH
SDL_Touch touch;
touch.id = 0; //TODO: Should be -1?

//touch.driverdata = SDL_malloc(sizeof(EventTouchData));
//EventTouchData* data = (EventTouchData*)(touch.driverdata);

touch.x_min = 0;
touch.x_max = frame.size.width;
touch.xres = touch.x_max - touch.x_min;
touch.y_min = 0;
touch.y_max = frame.size.height;
touch.yres = touch.y_max - touch.y_min;
touch.pressure_min = 0;
touch.pressure_max = 1;
touch.pressureres = touch.pressure_max - touch.pressure_min;


touchId = SDL_AddTouch(&touch, "IPHONE SCREEN");
#endif

return self;

Expand All @@ -62,8 +85,9 @@ - (id)initWithFrame:(CGRect)frame {
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSEnumerator *enumerator = [touches objectEnumerator];
UITouch *touch =(UITouch*)[enumerator nextObject];
UITouch *touch = (UITouch*)[enumerator nextObject];

#if FIXME_MULTITOUCH
/* associate touches with mice, so long as we have slots */
int i;
int found = 0;
Expand Down Expand Up @@ -98,21 +122,43 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* re-calibrate relative mouse motion */
SDL_GetRelativeMouseState(i, NULL, NULL);

/* grab next touch */
touch = (UITouch*)[enumerator nextObject];

/* switch back to our old mouse */
SDL_SelectMouse(oldMouse);

}
/* grab next touch */
touch = (UITouch*)[enumerator nextObject];
}
#else
if (touch) {
CGPoint locationInView = [touch locationInView: self];

/* send moved event */
SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);

/* send mouse down event */
SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
}
#endif

#if FIXED_MULTITOUCH
while(touch) {
CGPoint locationInView = [touch locationInView: self];
SDL_SendFingerDown(touchId,(int)touch,
SDL_TRUE,locationInView.x,locationInView.y,
1);

touch = (UITouch*)[enumerator nextObject];
}
#endif
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

NSEnumerator *enumerator = [touches objectEnumerator];
UITouch *touch=nil;
UITouch *touch = (UITouch*)[enumerator nextObject];

while(touch = (UITouch *)[enumerator nextObject]) {
#if FIXME_MULTITOUCH
while(touch) {
/* search for the mouse slot associated with this touch */
int i, found = NO;
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
Expand All @@ -126,7 +172,26 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
found = YES;
}
}

/* grab next touch */
touch = (UITouch*)[enumerator nextObject];
}
#else
if (touch) {
/* send mouse up */
SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
}
#endif
#if FIXED_MULTITOUCH
while(touch) {
CGPoint locationInView = [touch locationInView: self];
SDL_SendFingerDown(touchId,(int)touch,
SDL_FALSE,locationInView.x,locationInView.y,
1);

touch = (UITouch*)[enumerator nextObject];
}
#endif
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
Expand All @@ -141,9 +206,10 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

NSEnumerator *enumerator = [touches objectEnumerator];
UITouch *touch=nil;
UITouch *touch = (UITouch*)[enumerator nextObject];

while(touch = (UITouch *)[enumerator nextObject]) {
#if FIXME_MULTITOUCH
while(touch) {
/* try to find the mouse associated with this touch */
int i, found = NO;
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
Expand All @@ -156,7 +222,29 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
found = YES;
}
}

/* grab next touch */
touch = (UITouch*)[enumerator nextObject];
}
#else
if (touch) {
CGPoint locationInView = [touch locationInView: self];

/* send moved event */
SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
}
#endif

#if FIXED_MULTITOUCH
while(touch) {
CGPoint locationInView = [touch locationInView: self];
SDL_SendTouchMotion(touchId,(int)touch,
SDL_FALSE,locationInView.x,locationInView.y,
1);

touch = (UITouch*)[enumerator nextObject];
}
#endif
}

/*
Expand Down Expand Up @@ -190,15 +278,6 @@ - (void)initializeKeyboard {
keyboardVisible = NO;
/* add the UITextField (hidden) to our view */
[self addSubview: textField];

/* create our SDL_Keyboard */
SDL_Keyboard keyboard;
SDL_zero(keyboard);
SDL_AddKeyboard(&keyboard, 0);
SDLKey keymap[SDL_NUM_SCANCODES];
SDL_GetDefaultKeymap(keymap);
SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);

}

/* reveal onscreen virtual keyboard */
Expand All @@ -218,8 +297,8 @@ - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRan

if ([string length] == 0) {
/* it wants to replace text with nothing, ie a delete */
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE);
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
}
else {
/* go through all the characters in the string we've been sent
Expand All @@ -245,14 +324,14 @@ - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRan

if (mod & KMOD_SHIFT) {
/* If character uses shift, press shift down */
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
}
/* send a keydown and keyup even for the character */
SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
SDL_SendKeyboardKey(SDL_PRESSED, code);
SDL_SendKeyboardKey(SDL_RELEASED, code);
if (mod & KMOD_SHIFT) {
/* If character uses shift, press shift back up */
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
}
}
}
Expand Down

0 comments on commit e289891

Please sign in to comment.