author | Wuzzy <Wuzzy2@mail.ru> |
Wed, 26 Sep 2018 20:45:55 +0200 | |
changeset 13813 | 5c6052351991 |
parent 12877 | 00215a7ec5f5 |
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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
6832
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
3829 | 17 |
*/ |
18 |
||
3659 | 19 |
|
20 |
#import "EditableCellView.h" |
|
11256
3dc674af7662
- FindTable functionality for HoldTableViewCell and EditableCellView extracted to category
antonc27 <antonc27@mail.ru>
parents:
11220
diff
changeset
|
21 |
#import "UITableViewCell+FindTable.h" |
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 |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
26 |
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { |
3659 | 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]; |
3697 | 43 |
|
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
|
44 |
titleLabel = [[UILabel alloc] init]; |
11554
893722a2a1f9
- Some warnings fixed with text alignment on iOS front-end
antonc27 <antonc27@mail.ru>
parents:
11256
diff
changeset
|
45 |
titleLabel.textAlignment = NSTextAlignmentLeft; |
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
|
46 |
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
|
47 |
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
|
48 |
[self.contentView addSubview:titleLabel]; |
3697 | 49 |
|
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
|
50 |
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
|
51 |
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
|
52 |
respectEditing = NO; |
3660 | 53 |
oldValue = nil; |
3659 | 54 |
} |
55 |
return self; |
|
56 |
} |
|
57 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
58 |
- (void)layoutSubviews { |
3659 | 59 |
[super layoutSubviews]; |
60 |
||
61 |
CGRect contentRect = self.contentView.bounds; |
|
62 |
CGFloat boundsX = contentRect.origin.x; |
|
3697 | 63 |
|
3660 | 64 |
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
|
65 |
int skew = 0; |
3660 | 66 |
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
|
67 |
offset += self.imageView.frame.size.width; |
3697 | 68 |
|
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 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
} |
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 |
|
3782 | 77 |
textField.frame = CGRectMake(boundsX+offset+10, skew+10, 300, [UIFont labelFontSize] + 4); |
3659 | 78 |
} |
79 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
80 |
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { |
3659 | 81 |
[super setSelected:selected animated:animated]; |
82 |
// Configure the view for the selected state |
|
83 |
} |
|
84 |
||
85 |
#pragma mark - |
|
86 |
#pragma mark textField delegate |
|
87 |
// limit the size of the field to 64 characters like in original frontend |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
88 |
- (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
|
89 |
return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length); |
3659 | 90 |
} |
91 |
||
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
|
92 |
// allow editing only if delegate is set and conformant to protocol, and if editableOnlyWhileEditing |
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
93 |
- (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
|
94 |
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
|
95 |
[delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] && |
11256
3dc674af7662
- FindTable functionality for HoldTableViewCell and EditableCellView extracted to category
antonc27 <antonc27@mail.ru>
parents:
11220
diff
changeset
|
96 |
(respectEditing) ? [self findTable].editing : YES; |
3659 | 97 |
} |
98 |
||
99 |
// the textfield is being modified, update the navigation controller |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
100 |
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{ |
3660 | 101 |
// don't interact with table below |
11256
3dc674af7662
- FindTable functionality for HoldTableViewCell and EditableCellView extracted to category
antonc27 <antonc27@mail.ru>
parents:
11220
diff
changeset
|
102 |
[self findTable].scrollEnabled = NO; |
3697 | 103 |
|
3660 | 104 |
self.oldValue = self.textField.text; |
3697 | 105 |
|
3659 | 106 |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
107 |
style:UIBarButtonItemStylePlain |
|
108 |
target:self |
|
109 |
action:@selector(cancel:)]; |
|
110 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton; |
|
3697 | 111 |
|
3659 | 112 |
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"") |
113 |
style:UIBarButtonItemStyleDone |
|
114 |
target:self |
|
115 |
action:@selector(save:)]; |
|
116 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
|
117 |
} |
|
118 |
||
3660 | 119 |
/* with this a field might remain in editing status even if the view moved; |
120 |
use method below instead that allows some more interaction |
|
3659 | 121 |
// don't accept 0-length strings |
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
122 |
- (BOOL)textFieldShouldEndEditing:(UITextField *)aTextField { |
3697 | 123 |
return ([aTextField.text length] > 0); |
3659 | 124 |
} |
3660 | 125 |
*/ |
126 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
127 |
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField { |
3697 | 128 |
return ([aTextField.text length] >= self.minimumCharacters); |
3660 | 129 |
} |
3659 | 130 |
|
131 |
// the textfield has been modified, tell the delegate to do something |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
132 |
- (void)textFieldDidEndEditing:(UITextField *)aTextField { |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
133 |
// 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
|
134 |
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
|
135 |
[self save:aTextField]; |
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
136 |
|
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
137 |
// restores default behaviour on caller |
11256
3dc674af7662
- FindTable functionality for HoldTableViewCell and EditableCellView extracted to category
antonc27 <antonc27@mail.ru>
parents:
11220
diff
changeset
|
138 |
[self findTable].scrollEnabled = YES; |
11220
76fd61a88c1e
- Better way of creating/using doneButton in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
139 |
[(UITableViewController *)delegate navigationItem].leftBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem; |
76fd61a88c1e
- Better way of creating/using doneButton in Settings
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
140 |
[(UITableViewController *)delegate navigationItem].rightBarButtonItem = nil; |
3659 | 141 |
} |
142 |
||
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
|
143 |
#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
|
144 |
#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
|
145 |
// the user wants to show the keyboard |
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
146 |
- (void)replyKeyboard { |
3659 | 147 |
[self.textField becomeFirstResponder]; |
148 |
} |
|
149 |
||
150 |
// the user pressed cancel so hide keyboard |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
151 |
- (void)cancel:(id)sender { |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
152 |
// reverts any changes and performs a fake save for removing the keyboard |
3660 | 153 |
self.textField.text = self.oldValue; |
154 |
[self save:sender]; |
|
3659 | 155 |
} |
156 |
||
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
157 |
// send the value to the delegate (called before textFieldDidEndEditing) |
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
158 |
- (void)save:(id)sender { |
3878
348f4104461d
fix a small glitch in textfield cells and remove cheese from pro
koda
parents:
3829
diff
changeset
|
159 |
if (delegate == nil || [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] == NO) |
3659 | 160 |
return; |
3697 | 161 |
|
3660 | 162 |
// 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
|
163 |
if ([self textFieldShouldReturn:textField] == NO) |
3660 | 164 |
return; |
3697 | 165 |
|
3660 | 166 |
[delegate saveTextFieldValue:self.textField.text withTag:self.tag]; |
3659 | 167 |
[self.textField resignFirstResponder]; |
3660 | 168 |
self.oldValue = nil; |
3659 | 169 |
} |
170 |
||
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
|
171 |
// when field is editable only when the tableview is editable, resign responder when exiting editing mode |
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11554
diff
changeset
|
172 |
- (void)willTransitionToState:(UITableViewCellStateMask)state { |
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
|
173 |
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
|
174 |
[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
|
175 |
|
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
176 |
[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
|
177 |
} |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3878
diff
changeset
|
178 |
|
3659 | 179 |
@end |