-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCCTextField.m
290 lines (202 loc) · 6.81 KB
/
CCTextField.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// CCTextField.m
//
//
// Created by Ignacio Inglese on 12/27/11.
// Copyright 2011 Ignacio Inglese. All rights reserved.
//
#import "CCTextField.h"
#define kTagDebugLayer 100
@interface CCTextField ()
- (void)updateTicker;
- (void)initContent;
@end
@implementation CCTextField
@synthesize delegate = delegate_;
@synthesize label = label_;
@synthesize textField = textField_;
@synthesize maxLength = maxLength_;
@synthesize password = password_;
@synthesize debugMode = debugMode_;
@dynamic text;
+ (id)textFieldWithFieldSize:(CGSize)textFieldSize {
return [[[self alloc] initWithFieldSize:textFieldSize] autorelease];
}
- (id)initWithFieldSize:(CGSize)textFieldSize {
return [self initWithFieldSize:textFieldSize fontName:@"Helvetica" andFontSize:12];
}
+ (id)textFieldWithFieldSize:(CGSize)textFieldSize fontName:(NSString *)fontName andFontSize:(CGFloat)fontSize {
return [[[self alloc] initWithFieldSize:textFieldSize fontName:fontName andFontSize:fontSize] autorelease];
}
- (id)initWithFieldSize:(CGSize)textFieldSize fontName:(NSString *)fontName andFontSize:(CGFloat)fontSize {
if (self = [super init]) {
// Redundant, but helps keep things clear
sharedTextField = NO;
CGSize s = [[CCDirector sharedDirector] winSize];
self.label = [CCLabelTTF labelWithString:@"" dimensions:textFieldSize alignment:UITextAlignmentLeft fontName:fontName fontSize:fontSize];
[self.label setColor:ccBLACK];
self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(s.width * 2, s.height * 2, textFieldSize.width, textFieldSize.height)] autorelease];
[self.textField addObserver:self forKeyPath:@"delegate" options:NSKeyValueObservingOptionNew context:NULL];
[[[CCDirector sharedDirector] openGLView] addSubview:self.textField];
[self initContent];
}
return self;
}
+ (id)textFieldWithLabel:(CCLabelTTF *)label andTextField:(UITextField *)textfield {
return [[[self alloc] initWithLabel:label andTextField:textfield] autorelease];
}
- (id)initWithLabel:(CCLabelTTF *)label andTextField:(UITextField *)textfield {
if (self = [super init]) {
sharedTextField = YES;
self.textField = textfield;
self.label = label;
[self.textField addObserver:self forKeyPath:@"delegate" options:NSKeyValueObservingOptionNew context:NULL];
[self initContent];
}
return self;
}
- (void)initContent {
self.contentSize = self.label.contentSize;
CCLayerColor * lc = [CCLayerColor layerWithColor:ccc4(120, 0, 50, 255) width:self.contentSize.width height:self.contentSize.height];
[self addChild:lc z:0 tag:kTagDebugLayer];
[lc setVisible:NO];
[self.label setPosition:ccp(self.label.contentSize.width * 0.5, self.label.contentSize.height * 0.5)];
[self addChild:self.label];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.textField && [keyPath isEqualToString:@"delegate"]) {
if (showingTicker) {
[self updateTicker];
}
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.textField resignFirstResponder];
if ([delegate_ respondsToSelector:@selector(textFieldDidReturn:)]) {
[delegate_ textFieldDidReturn:self];
}
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (showingTicker) {
hidingTicker = NO;
[self updateTicker];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([delegate_ respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
if (![delegate_ textField:self shouldChangeCharactersInRange:range replacementString:string]) {
return NO;
}
}
NSString * str = [[realString copy] autorelease];
if (showingTicker) {
[self updateTicker];
if (str.length == 0 && string.length == 0) {
return NO;
}
}
str = [str stringByReplacingCharactersInRange:range withString:string];
if (self.maxLength > 0 && [str length] > self.maxLength && string.length > 0) {
return NO;
}
if (self.password) {
[self.label setString:[@"************************************************************************************" substringToIndex:str.length]];
}
else {
[self.label setString:str];
}
[realString release];
realString = [str retain];
hidingTicker = YES;
return YES;
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint p = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]]];
CGRect r = self.boundingBox;
r.origin = CGPointZero;
if (CGRectContainsPoint(r, p)) {
BOOL reset = NO;
if ([self.textField delegate] != self) {
reset = YES;
}
[self.textField setDelegate:self];
if (reset) {
[realString release];
realString = [self.label.string retain];
[self.textField setText:realString];
}
[self.textField becomeFirstResponder];
return YES;
}
return NO;
}
- (void)onEnter {
[super onEnter];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[[CCScheduler sharedScheduler] scheduleSelector:@selector(updateTicker) forTarget:self interval:0.5 paused:NO];
}
- (void)onExit {
[super onExit];
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[[CCScheduler sharedScheduler] unscheduleAllSelectorsForTarget:self];
}
- (void)setText:(NSString *)text {
[realString release];
realString = [text retain];
self.textField.text = text;
self.label.string = text;
showingTicker = NO;
}
- (NSString *)text {
if (showingTicker) {
return [realString substringToIndex:realString.length - 1];
}
else {
return realString;
}
}
- (void)setTextColor:(ccColor3B)color {
[self.label setColor:color];
}
- (void)setDebugMode:(BOOL)debugMode {
debugMode_ = debugMode;
[(CCLayerColor *)[self getChildByTag:kTagDebugLayer] setVisible:debugMode_];
}
- (void)dealloc {
[textField_ removeObserver:self forKeyPath:@"delegate"];
if (textField_.delegate == self) {
[textField_ setDelegate:nil];
}
[realString release];
[super dealloc];
}
- (void)updateTicker {
if (!showingTicker && (![self.textField isFirstResponder] || self.textField.delegate != self)) {
return;
}
if (hidingTicker) {
hidingTicker = NO;
return;
}
showingTicker = !showingTicker;
NSString * str = [self.label string];
if (showingTicker) {
[self.label setString:[str stringByAppendingString:@"|"]];
}
else {
[self.label setString:[str substringToIndex:str.length - 1]];
}
}
- (void)doesNotRecognizeSelector:(SEL)aSelector {
// If UITextField isn't shared, forward messages to it
if (!sharedTextField && [self.textField respondsToSelector:aSelector]) {
NSLog(@"passing %@", NSStringFromSelector(aSelector));
[self.textField performSelector:aSelector];
}
else {
[super doesNotRecognizeSelector:aSelector];
}
}
@end