author | unc0rr |
Tue, 28 Aug 2012 20:30:57 +0400 | |
changeset 7615 | b39beffcf05e |
parent 6832 | fae8fd118da9 |
child 10108 | c68cf030eded |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
*/ |
|
18 |
||
3659 | 19 |
|
20 |
#import "EditableCellView.h" |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5700
diff
changeset
|
21 |
|
3659 | 22 |
|
23 |
@implementation EditableCellView |
|
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
24 |
@synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, respectEditing, oldValue; |
3659 | 25 |
|
26 |
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { |
|
27 |
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { |
|
28 |
delegate = nil; |
|
3697 | 29 |
|
3659 | 30 |
textField = [[UITextField alloc] initWithFrame:CGRectZero]; |
31 |
textField.backgroundColor = [UIColor clearColor]; |
|
32 |
textField.delegate = self; |
|
33 |
textField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
34 |
textField.clearsOnBeginEditing = NO; |
|
35 |
textField.returnKeyType = UIReturnKeyDone; |
|
36 |
textField.adjustsFontSizeToFitWidth = YES; |
|
5700
f0960a88ab0e
savedgamesviewcontroller refactor, added icons to supportviewcontroller
koda
parents:
4976
diff
changeset
|
37 |
textField.minimumFontSize = 9; |
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
38 |
textField.userInteractionEnabled = YES; |
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4211
diff
changeset
|
39 |
textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
3659 | 40 |
[textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
3697 | 41 |
|
3659 | 42 |
[self.contentView addSubview:textField]; |
3667 | 43 |
//[textField release]; |
3697 | 44 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
45 |
titleLabel = [[UILabel alloc] init]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
46 |
titleLabel.textAlignment = UITextAlignmentLeft; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
47 |
titleLabel.backgroundColor = [UIColor clearColor]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
48 |
titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
49 |
[self.contentView addSubview:titleLabel]; |
3667 | 50 |
//[titleLabel release]; |
3697 | 51 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
52 |
minimumCharacters = 1; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
53 |
maximumCharacters = 64; |
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
54 |
respectEditing = NO; |
3660 | 55 |
oldValue = nil; |
3659 | 56 |
} |
57 |
return self; |
|
58 |
} |
|
59 |
||
60 |
-(void) layoutSubviews { |
|
61 |
[super layoutSubviews]; |
|
62 |
||
63 |
CGRect contentRect = self.contentView.bounds; |
|
64 |
CGFloat boundsX = contentRect.origin.x; |
|
3697 | 65 |
|
3660 | 66 |
int offset = 0; |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
67 |
int skew = 0; |
3660 | 68 |
if (self.imageView != nil) |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
69 |
offset += self.imageView.frame.size.width; |
3697 | 70 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
71 |
if ([self.titleLabel.text length] == 0) |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
72 |
titleLabel.frame = CGRectZero; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
73 |
else { |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
74 |
titleLabel.frame = CGRectMake(boundsX+offset+10, 10, 100, [UIFont labelFontSize] + 4); |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
75 |
offset += 100; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
76 |
skew +=2; |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
77 |
} |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
78 |
|
3782 | 79 |
textField.frame = CGRectMake(boundsX+offset+10, skew+10, 300, [UIFont labelFontSize] + 4); |
3659 | 80 |
} |
81 |
||
82 |
-(void) setSelected:(BOOL)selected animated:(BOOL)animated { |
|
83 |
[super setSelected:selected animated:animated]; |
|
84 |
// Configure the view for the selected state |
|
85 |
} |
|
86 |
||
87 |
-(void) dealloc { |
|
3667 | 88 |
self.delegate = nil; |
89 |
releaseAndNil(oldValue); |
|
90 |
releaseAndNil(titleLabel); |
|
91 |
releaseAndNil(textField); |
|
3659 | 92 |
[super dealloc]; |
93 |
} |
|
94 |
||
95 |
#pragma mark - |
|
96 |
#pragma mark textField delegate |
|
97 |
// limit the size of the field to 64 characters like in original frontend |
|
98 |
-(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
99 |
return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length); |
3659 | 100 |
} |
101 |
||
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
102 |
// allow editing only if delegate is set and conformant to protocol, and if editableOnlyWhileEditing |
3659 | 103 |
-(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField { |
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
104 |
return (delegate != nil) && |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
105 |
[delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] && |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
106 |
(respectEditing) ? ((UITableView*)[self superview]).editing : YES; |
3659 | 107 |
} |
108 |
||
109 |
// the textfield is being modified, update the navigation controller |
|
110 |
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
|
3660 | 111 |
// don't interact with table below |
3659 | 112 |
((UITableView*)[self superview]).scrollEnabled = NO; |
3697 | 113 |
|
3660 | 114 |
self.oldValue = self.textField.text; |
3697 | 115 |
|
3659 | 116 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
117 |
style:UIBarButtonItemStylePlain |
|
118 |
target:self |
|
119 |
action:@selector(cancel:)]; |
|
120 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton; |
|
121 |
[cancelButton release]; |
|
3697 | 122 |
|
3659 | 123 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"") |
124 |
style:UIBarButtonItemStyleDone |
|
125 |
target:self |
|
126 |
action:@selector(save:)]; |
|
127 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
|
128 |
[saveButton release]; |
|
129 |
} |
|
130 |
||
3660 | 131 |
/* with this a field might remain in editing status even if the view moved; |
132 |
use method below instead that allows some more interaction |
|
3659 | 133 |
// don't accept 0-length strings |
134 |
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
3697 | 135 |
return ([aTextField.text length] > 0); |
3659 | 136 |
} |
3660 | 137 |
*/ |
138 |
||
139 |
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField { |
|
3697 | 140 |
return ([aTextField.text length] >= self.minimumCharacters); |
3660 | 141 |
} |
3659 | 142 |
|
143 |
// the textfield has been modified, tell the delegate to do something |
|
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
144 |
-(void) textFieldDidEndEditing:(UITextField *)aTextField { |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
145 |
// this forces a save when user selects a new field |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
146 |
if ([self.textField.text isEqualToString:self.oldValue] == NO) |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
147 |
[self save:aTextField]; |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
148 |
|
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
149 |
// restores default behaviour on caller |
3659 | 150 |
((UITableView*)[self superview]).scrollEnabled = YES; |
151 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem; |
|
152 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil; |
|
153 |
} |
|
154 |
||
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
155 |
#pragma mark - |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
156 |
#pragma mark instance methods |
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3661
diff
changeset
|
157 |
// the user wants to show the keyboard |
3659 | 158 |
-(void) replyKeyboard { |
159 |
[self.textField becomeFirstResponder]; |
|
160 |
} |
|
161 |
||
162 |
// the user pressed cancel so hide keyboard |
|
163 |
-(void) cancel:(id) sender { |
|
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
164 |
// reverts any changes and performs a fake save for removing the keyboard |
3660 | 165 |
self.textField.text = self.oldValue; |
166 |
[self save:sender]; |
|
3659 | 167 |
} |
168 |
||
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
169 |
// send the value to the delegate (called before textFieldDidEndEditing) |
3659 | 170 |
-(void) save:(id) sender { |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
171 |
if (delegate == nil || [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] == NO) |
3659 | 172 |
return; |
3697 | 173 |
|
3660 | 174 |
// don't save if the textfield is invalid |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
175 |
if ([self textFieldShouldReturn:textField] == NO) |
3660 | 176 |
return; |
3697 | 177 |
|
3660 | 178 |
[delegate saveTextFieldValue:self.textField.text withTag:self.tag]; |
3659 | 179 |
[self.textField resignFirstResponder]; |
3660 | 180 |
self.oldValue = nil; |
3659 | 181 |
} |
182 |
||
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
183 |
// when field is editable only when the tableview is editable, resign responder when exiting editing mode |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
184 |
-(void) willTransitionToState:(UITableViewCellStateMask)state { |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
185 |
if (respectEditing && state == UITableViewCellStateDefaultMask) |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
186 |
[self save:nil]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
187 |
|
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
188 |
[super willTransitionToState:state]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
189 |
} |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
190 |
|
3659 | 191 |
@end |