cocoaTouch/SDLOverrides/SDL_uikitview.m
author koda
Mon, 08 Feb 2010 21:49:52 +0000
changeset 2772 1965eba10be6
parent 2754 ad4f81fbfb76
child 2805 36a8cebb91e8
permissions -rw-r--r--
some code cleanup for the ifrontend
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     1
/*
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     2
 SDL - Simple DirectMedia Layer
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     3
 Copyright (C) 1997-2009 Sam Lantinga
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     4
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     5
 This library is free software; you can redistribute it and/or
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     6
 modify it under the terms of the GNU Lesser General Public
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     7
 License as published by the Free Software Foundation; either
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     8
 version 2.1 of the License, or (at your option) any later version.
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     9
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    10
 This library is distributed in the hope that it will be useful,
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    13
 Lesser General Public License for more details.
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    14
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    15
 You should have received a copy of the GNU Lesser General Public
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    16
 License along with this library; if not, write to the Free Software
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    18
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    19
 Sam Lantinga, mods for Hedgewars by Vittorio Giovara
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    20
 slouken@libsdl.org, vittorio.giovara@gmail.com
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    21
 */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    22
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
    23
#import "PascalImports.h"
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
    24
#import "CGPointUtils.h"
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    25
#import "SDL_uikitview.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    26
#import "SDL_uikitappdelegate.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    27
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    28
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    29
#import "SDL_keyboard_c.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    30
#import "keyinfotable.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    31
#import "SDL_uikitwindow.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    32
#endif
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    33
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    34
@implementation SDL_uikitview
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    35
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    36
// they have to be global variables to allow showControls() to use them
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    37
UIButton *attackButton, *menuButton;
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    38
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
    39
-(void) dealloc {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    40
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    41
	SDL_DelKeyboard(0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    42
	[textField release];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    43
#endif
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    44
	[menuButton release];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    45
	[attackButton release];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    46
	[super dealloc];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    47
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    48
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    49
- (id)initWithFrame:(CGRect)frame {
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    50
	// the addTarget parameter for the buttons below is set like that because
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    51
	// this object is inherited by SDL_openglesview.m which is the one allocated by SDL.
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    52
	// We select this class with [self superclass] and call the selectors with "+" because
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    53
	// they are superclass methods 
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
    54
	self = [super initWithFrame:frame];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    55
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    56
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    57
	[self initializeKeyboard];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    58
#endif	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    59
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    60
	int i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    61
	for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    62
        mice[i].id = i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    63
		mice[i].driverdata = NULL;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    64
		SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    65
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    66
2724
601158aaa201 redo LANDSCAPE MODE in a saner way with lots of fps
koda
parents: 2714
diff changeset
    67
	self.multipleTouchEnabled = YES;
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
    68
	self.exclusiveTouch = YES;
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
    69
/*
2724
601158aaa201 redo LANDSCAPE MODE in a saner way with lots of fps
koda
parents: 2714
diff changeset
    70
	// custom code
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    71
	// the coordinate system is still like in Portrait even though everything is rotated
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    72
	attackButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 480, 260, 50)];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    73
	[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
2705
2b5625c4ec16 fix a nasty 196 bytes memory leak in engine, plus other stuff for iphone frontend
koda
parents: 2701
diff changeset
    74
	[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateHighlighted];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    75
	[attackButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchDown];
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    76
	[attackButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    77
	//[self addSubview:attackButton];
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    78
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    79
	menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 480, 30, 50)];
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    80
	[menuButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    81
	[menuButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchUpInside];
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    82
	[menuButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    83
	//[self addSubview:menuButton];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    84
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    85
	UIButton *walkLeftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    86
	[walkLeftButton addTarget:[self superclass] action:@selector(walkingLeft) forControlEvents:UIControlEventTouchDown];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    87
	[walkLeftButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    88
	[self insertSubview:walkLeftButton atIndex:0];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    89
	[walkLeftButton release];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    90
	
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    91
	UIButton *walkRightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 380, 320, 100)];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    92
	[walkRightButton addTarget:[self superclass] action:@selector(walkingRight) forControlEvents:UIControlEventTouchDown];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    93
	[walkRightButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    94
	[self insertSubview:walkRightButton atIndex:0];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    95
	[walkRightButton release];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    96
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    97
	UIButton *moveDownButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 70, 280)];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    98
	[moveDownButton addTarget:[self superclass] action:@selector(movingDown) forControlEvents:UIControlEventTouchDown];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
    99
	[moveDownButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   100
	[self insertSubview:moveDownButton atIndex:0];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   101
	[moveDownButton release];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   102
	
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   103
	UIButton *moveUpButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 100, 70, 280)];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   104
	[moveUpButton addTarget:[self superclass] action:@selector(movingUp) forControlEvents:UIControlEventTouchDown];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   105
	[moveUpButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   106
	[self insertSubview:moveUpButton atIndex:0];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   107
	[moveUpButton release];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   108
	
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   109
	//dummy button used to test chat
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   110
	UIButton *chatButton = [[UIButton alloc] initWithFrame:CGRectMake(70, 100, 250, 280)];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   111
	[chatButton addTarget:[self superclass] action:@selector(chatBegin) forControlEvents:UIControlEventTouchDown];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   112
	[self insertSubview:chatButton atIndex:0];
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   113
	[chatButton release];
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   114
	*/
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   115
	return self;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   116
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   117
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   118
#pragma mark -
2690
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
   119
#pragma mark Exported functions for FreePascal
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
   120
const char* IPH_getDocumentsPath() {
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   121
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   122
	NSString *documentsDirectory = [paths objectAtIndex: 0];
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   123
	return [documentsDirectory UTF8String];
2690
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
   124
}
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
   125
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
   126
void IPH_showControls (void) {
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   127
	NSLog(@"Showing controls");
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   128
	/*
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   129
	[UIView beginAnimations:nil context:NULL];
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   130
	[UIView setAnimationDuration:0.5];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   131
	attackButton.frame = CGRectMake(30, 430, 260, 50);
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   132
	menuButton.frame = CGRectMake(0, 430, 30, 50);
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   133
	[UIView commitAnimations];
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   134
	 */
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   135
}
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   136
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   137
#pragma mark -
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   138
#pragma mark Superclass methods to call Pascal code
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   139
+(void) attackButtonPressed {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   140
	HW_shoot();
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   141
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   142
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   143
+(void) buttonsReleased {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   144
	HW_allKeysUp();
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   145
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   146
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   147
+(void) walkingLeft {
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   148
	HW_walkLeft();
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   149
}
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   150
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   151
+(void) walkingRight {
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   152
	HW_walkRight();
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   153
}
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   154
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   155
+(void) movingUp {
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   156
	HW_aimUp();
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   157
}
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   158
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   159
+(void) movingDown {
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   160
	HW_aimDown();
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   161
}
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   162
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   163
+(void) chatBegin {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   164
	//TODO: implement a UITextScroll and put received chat lines in there
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   165
}
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   166
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   167
#pragma mark -
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   168
#pragma mark Custom SDL_UIView input handling
2754
ad4f81fbfb76 touchinput works, invisible buttons added and initial support for chat
koda
parents: 2740
diff changeset
   169
#define kMinimumPinchDelta	50
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   170
#define kMinimumGestureLength	10
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   171
#define kMaximumVariance	3
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   172
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   173
// we override default touch input to implement our own gestures
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   174
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   175
	NSArray *twoTouches;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   176
	UITouch *touch = [touches anyObject];
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   177
	
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   178
	switch ([touches count]) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   179
		case 1:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   180
			gestureStartPoint = [touch locationInView:self];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   181
			initialDistanceForPinching = 0;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   182
			switch ([touch tapCount]) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   183
				case 1:
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   184
					
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   185
					NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y );
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   186
					SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   187
							      (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   188
					HW_click();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   189
					break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   190
				case 2:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   191
					HW_ammoMenu();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   192
					break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   193
				default:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   194
					break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   195
			}
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   196
			break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   197
		case 2:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   198
			if (2 == [touch tapCount]) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   199
				HW_zoomReset();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   200
			}
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   201
			
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   202
			// pinching
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   203
			twoTouches = [touches allObjects];
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   204
			UITouch *first = [twoTouches objectAtIndex:0];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   205
			UITouch *second = [twoTouches objectAtIndex:1];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   206
			initialDistanceForPinching = distanceBetweenPoints([first locationInView:self], [second locationInView:self]);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   207
			break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   208
		default:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   209
			break;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   210
	}
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   211
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   212
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   213
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   214
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   215
	initialDistanceForPinching = 0;
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   216
	gestureStartPoint.x = 0;
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   217
	gestureStartPoint.y = 0;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   218
	HW_allKeysUp();
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   219
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   220
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   221
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   222
	// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances.
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   223
	// Usually (it seems) all active touches are canceled.
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   224
	[self touchesEnded:touches withEvent:event];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   225
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   226
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   227
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   228
	NSArray *twoTouches;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   229
	CGPoint currentPosition;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   230
	UITouch *touch = [touches anyObject];
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   231
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   232
	switch ([touches count]) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   233
		case 1:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   234
			currentPosition = [touch locationInView:self];
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   235
			// panning
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   236
			SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   237
							(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   238
			// remember that we have x and y inverted
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   239
			/* temporarily disabling hog movements for camera panning testing
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   240
			CGFloat vertDiff = gestureStartPoint.x - currentPosition.x;
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   241
			CGFloat horizDiff = gestureStartPoint.y - currentPosition.y;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   242
			CGFloat deltaX = fabsf(vertDiff);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   243
			CGFloat deltaY = fabsf(horizDiff);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   244
			
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   245
			if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   246
				NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   247
				if (horizDiff > 0) HW_walkLeft();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   248
				else HW_walkRight();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   249
			} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   250
				NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   251
				if (vertDiff < 0) HW_aimUp();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   252
				else HW_aimDown();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   253
			}
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   254
			*/
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   255
			break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   256
		case 2:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   257
			twoTouches = [touches allObjects];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   258
			UITouch *first = [twoTouches objectAtIndex:0];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   259
			UITouch *second = [twoTouches objectAtIndex:1];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   260
			CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self], [second locationInView:self]);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   261
			
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   262
			if (0 == initialDistanceForPinching) 
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   263
				initialDistanceForPinching = currentDistanceOfPinching;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   264
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2734
diff changeset
   265
			if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta)
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   266
				HW_zoomOut();
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2734
diff changeset
   267
			else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta)
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   268
				HW_zoomIn();
2738
bfccb2ec4334 new graphics from Tiy and frontend is loaded in landscape mode
koda
parents: 2734
diff changeset
   269
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   270
			currentDistanceOfPinching = initialDistanceForPinching;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   271
			break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   272
		default:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   273
			break;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   274
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   275
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   276
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   277
/*
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   278
#pragma mark -
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   279
#pragma mark Keyboard related functionality
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   280
#if SDL_IPHONE_KEYBOARD
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   281
// Is the iPhone virtual keyboard visible onscreen? 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   282
- (BOOL)keyboardVisible {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   283
	return keyboardVisible;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   284
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   285
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   286
// Set ourselves up as a UITextFieldDelegate
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   287
- (void)initializeKeyboard {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   288
		
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   289
	textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   290
	textField.delegate = self;
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   291
	// placeholder so there is something to delete!
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   292
	textField.text = @" ";	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   293
	
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   294
	// set UITextInputTrait properties, mostly to defaults
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   295
	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   296
	textField.autocorrectionType = UITextAutocorrectionTypeNo;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   297
	textField.enablesReturnKeyAutomatically = NO;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   298
	textField.keyboardAppearance = UIKeyboardAppearanceDefault;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   299
	textField.keyboardType = UIKeyboardTypeDefault;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   300
	textField.returnKeyType = UIReturnKeyDefault;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   301
	textField.secureTextEntry = NO;	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   302
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   303
	textField.hidden = YES;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   304
	keyboardVisible = NO;
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   305
	// add the UITextField (hidden) to our view
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   306
	[self addSubview: textField];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   307
	
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   308
	// create our SDL_Keyboard
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   309
	SDL_Keyboard keyboard;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   310
	SDL_zero(keyboard);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   311
	SDL_AddKeyboard(&keyboard, 0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   312
	SDLKey keymap[SDL_NUM_SCANCODES];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   313
	SDL_GetDefaultKeymap(keymap);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   314
	SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   315
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   316
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   317
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   318
// reveal onscreen virtual keyboard
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   319
- (void)showKeyboard {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   320
	keyboardVisible = YES;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   321
	[textField becomeFirstResponder];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   322
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   323
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   324
// hide onscreen virtual keyboard
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   325
- (void)hideKeyboard {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   326
	keyboardVisible = NO;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   327
	[textField resignFirstResponder];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   328
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   329
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   330
// UITextFieldDelegate method.  Invoked when user types something.
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   331
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   332
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   333
	if ([string length] == 0) {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   334
		// it wants to replace text with nothing, ie a delete
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   335
		SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   336
		SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   337
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   338
	else {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   339
		// go through all the characters in the string we've been sent and convert them to key presses
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   340
		int i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   341
		for (i=0; i<[string length]; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   342
			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   343
			unichar c = [string characterAtIndex: i];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   344
			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   345
			Uint16 mod = 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   346
			SDL_scancode code;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   347
			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   348
			if (c < 127) {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   349
				// figure out the SDL_scancode and SDL_keymod for this unichar
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   350
				code = unicharToUIKeyInfoTable[c].code;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   351
				mod  = unicharToUIKeyInfoTable[c].mod;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   352
			}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   353
			else {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   354
				// we only deal with ASCII right now
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   355
				code = SDL_SCANCODE_UNKNOWN;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   356
				mod = 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   357
			}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   358
			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   359
			if (mod & KMOD_SHIFT) {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   360
				// If character uses shift, press shift down
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   361
				SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   362
			}
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   363
			// send a keydown and keyup even for the character
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   364
			SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   365
			SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   366
			if (mod & KMOD_SHIFT) {
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   367
				// If character uses shift, press shift back up
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   368
				SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   369
			}			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   370
		}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   371
	}
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   372
	return NO; // don't allow the edit! (keep placeholder text there)
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   373
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   374
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   375
// Terminates the editing session
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   376
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   377
	[self hideKeyboard];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   378
	return YES;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   379
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   380
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   381
#endif
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   382
*/
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   383
@end
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   384
/*
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   385
// iPhone keyboard addition functions
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   386
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   387
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   388
int SDL_iPhoneKeyboardShow(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   389
	SDL_WindowData *data;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   390
	SDL_uikitview *view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   391
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   392
	if (NULL == window) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   393
		SDL_SetError("Window does not exist");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   394
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   395
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   396
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   397
	data = (SDL_WindowData *)window->driverdata;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   398
	view = data->view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   399
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   400
	if (nil == view) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   401
		SDL_SetError("Window has no view");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   402
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   403
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   404
	else {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   405
		[view showKeyboard];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   406
		return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   407
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   408
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   409
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   410
int SDL_iPhoneKeyboardHide(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   411
	SDL_WindowData *data;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   412
	SDL_uikitview *view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   413
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   414
	if (NULL == window) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   415
		SDL_SetError("Window does not exist");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   416
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   417
	}	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   418
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   419
	data = (SDL_WindowData *)window->driverdata;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   420
	view = data->view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   421
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   422
	if (NULL == view) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   423
		SDL_SetError("Window has no view");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   424
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   425
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   426
	else {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   427
		[view hideKeyboard];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   428
		return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   429
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   430
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   431
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   432
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   433
	SDL_WindowData *data;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   434
	SDL_uikitview *view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   435
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   436
	if (NULL == window) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   437
		SDL_SetError("Window does not exist");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   438
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   439
	}	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   440
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   441
	data = (SDL_WindowData *)window->driverdata;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   442
	view = data->view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   443
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   444
	if (NULL == view) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   445
		SDL_SetError("Window has no view");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   446
		return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   447
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   448
	else {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   449
		return view.keyboardVisible;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   450
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   451
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   452
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   453
int SDL_iPhoneKeyboardToggle(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   454
	SDL_WindowData *data;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   455
	SDL_uikitview *view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   456
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   457
	if (NULL == window) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   458
		SDL_SetError("Window does not exist");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   459
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   460
	}	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   461
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   462
	data = (SDL_WindowData *)window->driverdata;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   463
	view = data->view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   464
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   465
	if (NULL == view) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   466
		SDL_SetError("Window has no view");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   467
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   468
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   469
	else {
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   470
		if (SDL_iPhoneKeyboardIsShown(window)) {
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   471
			SDL_iPhoneKeyboardHide(window);
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   472
		}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   473
		else {
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   474
			SDL_iPhoneKeyboardShow(window);
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   475
		}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   476
		return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   477
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   478
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   479
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   480
#else
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   481
2772
1965eba10be6 some code cleanup for the ifrontend
koda
parents: 2754
diff changeset
   482
// stubs, used if compiled without keyboard support
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   483
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   484
int SDL_iPhoneKeyboardShow(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   485
	SDL_SetError("Not compiled with keyboard support");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   486
	return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   487
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   488
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   489
int SDL_iPhoneKeyboardHide(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   490
	SDL_SetError("Not compiled with keyboard support");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   491
	return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   492
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   493
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   494
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   495
	return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   496
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   497
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   498
int SDL_iPhoneKeyboardToggle(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   499
	SDL_SetError("Not compiled with keyboard support");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   500
	return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   501
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   502
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   503
#endif /* SDL_IPHONE_KEYBOARD */