Debian Codegraph @

platforms/apple/CocoaWrapper/ios/GEM_SDL_Keyboard.m

raw
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2011 The GemRB Project
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 *
19 */
20
21 #import <QuartzCore/QuartzCore.h>
22 #import <SDL.h>
23 #import <UIKit/UIKit.h>
24
25 extern int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode);
26 #include <SDL/SDL_keyboard.h>
27
28 #pragma mark UIKit button subclasses
29 @interface GEM_UIKit_RoundedButton : UIButton
30 {}
31 @end
32
33 @implementation GEM_UIKit_RoundedButton
34
35 - (id)initWithFrame:(CGRect)frame
36 {
37 self = [super initWithFrame:frame];
38 if (self) {
39 //custom drawing
40 self.backgroundColor = [UIColor colorWithRed:0.196 green:0.3098 blue:0.52 alpha:1.0];
41 self.titleLabel.textColor = [UIColor darkTextColor];
42 self.titleLabel.font = [UIFont systemFontOfSize:24];
43 self.layer.cornerRadius = 10;
44 self.layer.borderWidth = 2;
45 self.layer.borderColor = [UIColor grayColor].CGColor;
46 self.clipsToBounds = YES;
47 }
48 return self;
49 }
50
51 @end
52
53 @interface SDL_UIKit_ModifierKeyButton : GEM_UIKit_RoundedButton {
54 @private
55 SDL_Keymod _modifierKey;
56 }
57 @property(nonatomic, assign, getter = modifierKey, setter = setModifierKey:) SDL_Keymod _modifierKey;
58
59 - (void)toggleModifier;
60 @end
61
62 @implementation SDL_UIKit_ModifierKeyButton
63 @synthesize _modifierKey;
64
65 - (id)initWithFrame:(CGRect)frame
66 {
67 self = [super initWithFrame:frame];
68 if (self) {
69 _modifierKey = KMOD_NONE;
70 [self setSelected:NO];
71 [self addTarget:self action:@selector(toggleModifier) forControlEvents:UIControlEventTouchDown];
72 }
73 return self;
74 }
75
76 - (BOOL)selected
77 {
78 return (BOOL)(SDL_GetModState() & _modifierKey);
79 }
80
81 - (void)setSelected:(BOOL)selected
82 {
83 [super setSelected:selected];
84 if (selected){
85 SDL_SetModState(SDL_GetModState() | _modifierKey);
86 self.backgroundColor = [UIColor colorWithRed:0.196 green:0.3098 blue:0.52 alpha:1.0];
87 }else{
88 SDL_SetModState(SDL_GetModState() & ~_modifierKey);
89 self.backgroundColor = [UIColor colorWithRed:0.196 green:0.3098 blue:0.52 alpha:0.25];
90 }
91 }
92
93 - (void)toggleModifier
94 {
95 [self setSelected:!self.selected];
96 }
97
98 @end
99
100 @interface SDL_UIKit_KeyButton : GEM_UIKit_RoundedButton {
101 @private
102 SDL_Scancode _keyCode;
103 }
104 @property(nonatomic, assign, getter = keyCode, setter = setKeyCode:) SDL_Scancode _keyCode;
105
106 - (void)sendKey;
107 @end
108
109 @implementation SDL_UIKit_KeyButton
110 @synthesize _keyCode;
111
112 - (id)initWithFrame:(CGRect)frame
113 {
114 self = [super initWithFrame:frame];
115 if (self) {
116 _keyCode = SDL_SCANCODE_UNKNOWN;
117 [self addTarget:self action:@selector(sendKey) forControlEvents:UIControlEventTouchDown];
118 }
119 return self;
120 }
121
122 - (void)sendKey
123 {
124 //send both press and release
125 SDL_SendKeyboardKey(SDL_PRESSED, _keyCode);
126 SDL_SendKeyboardKey(SDL_RELEASED, _keyCode);
127 }
128
129 @end
130
131 #pragma mark end button subclasses
132 // need a custom accessory view for the keybaord (for ctrl key modifier)
133 @interface SDL_UIKIT_KBoardAccessoryView : UIView
134 {}
135 - (void)willMoveToWindow:(UIWindow *)newWindow;
136 @end
137
138 @implementation SDL_UIKIT_KBoardAccessoryView
139
140 - (id) initWithFrame:(CGRect)frame
141 {
142 self = [super initWithFrame:frame];
143 if (self){
144 self.backgroundColor = [UIColor colorWithRed:138.0/255.0 green:139.0/255.0 blue:147.0/255.0 alpha:1.0];
145 }
146 return self;
147 }
148
149 - (void)willMoveToWindow:(UIWindow *)newWindow
150 {
151 // newWindow is the window with the soft keybord
152 [super willMoveToWindow:newWindow];
153 newWindow.alpha = 0.60;
154 }
155
156 @end
157
158 @interface UITextField (KeyboardAccesory)
159 -(UIView *)inputAccessoryView;// override UIKit and force our own
160 @end
161
162 @implementation UITextField (KeyboardAccesory)
163 - (UIView *)inputAccessoryView
164 {
165 static SDL_UIKIT_KBoardAccessoryView* accessoryView = nil;
166 if (!accessoryView){
167 CGRect accessFrame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 77.0);
168 accessoryView = [[SDL_UIKIT_KBoardAccessoryView alloc] initWithFrame:accessFrame];
169
170 CGFloat xSpacing = 20.0;
171 CGFloat width = 120.0;
172 CGFloat xPos = xSpacing;
173 //ctrl key
174 SDL_UIKit_ModifierKeyButton *ctrlButton = [[SDL_UIKit_ModifierKeyButton alloc] initWithFrame:CGRectMake(xPos, 20.0, width, 40.0)];
175 ctrlButton.modifierKey = KMOD_CTRL;
176 [ctrlButton setTitle: @"Ctrl" forState:UIControlStateNormal];
177 [accessoryView addSubview:ctrlButton];
178 [ctrlButton release];
179 //alt key
180 xPos += xSpacing + width;
181 SDL_UIKit_ModifierKeyButton *altButton = [[SDL_UIKit_ModifierKeyButton alloc] initWithFrame:CGRectMake(xPos, 20.0, width, 40.0)];
182 altButton.modifierKey = KMOD_ALT;
183 [altButton setTitle: @"Alt" forState:UIControlStateNormal];
184 [accessoryView addSubview:altButton];
185 [altButton release];
186 //pgUp key
187 xPos += xSpacing + width;
188 SDL_UIKit_KeyButton *pgKey = [[SDL_UIKit_KeyButton alloc] initWithFrame:CGRectMake(xPos, 20.0, width, 40.0)];
189 pgKey.keyCode = SDL_SCANCODE_PAGEUP;
190 [pgKey setTitle: @"PgUp" forState:UIControlStateNormal];
191 [accessoryView addSubview:pgKey];
192 [pgKey release];
193 //pgDown key
194 xPos += xSpacing + width;
195 pgKey = [[SDL_UIKit_KeyButton alloc] initWithFrame:CGRectMake(xPos, 20.0, width, 40.0)];
196 pgKey.keyCode = SDL_SCANCODE_PAGEDOWN;
197 [pgKey setTitle: @"PgDown" forState:UIControlStateNormal];
198 [accessoryView addSubview:pgKey];
199 [pgKey release];
200 }
201 return accessoryView;
202 }
203 @end