project_files/HedgewarsMobile/Classes/EditableCellView.m
author koda
Sat, 14 Aug 2010 02:10:27 +0200
changeset 3739 97cf933e5bd2
parent 3697 d5b30d6373fc
child 3782 dc3531e49e4c
permissions -rw-r--r--
disable AI teleport show on screen controls only if hedgehog is not a hog fix reloading of teams
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     1
//
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     2
//  WeaponCellView.m
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     3
//  Hedgewars
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     4
//
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     5
//  Created by Vittorio on 03/07/10.
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     7
//
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     8
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     9
#import "EditableCellView.h"
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    10
#import "CommodityFunctions.h"
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    11
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    12
@implementation EditableCellView
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
    13
@synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, oldValue;
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    14
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    15
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    16
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    17
        delegate = nil;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    18
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    19
        textField = [[UITextField alloc] initWithFrame:CGRectZero];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    20
        textField.backgroundColor = [UIColor clearColor];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    21
        textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    22
        textField.delegate = self;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    23
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    24
        textField.clearsOnBeginEditing = NO;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    25
        textField.returnKeyType = UIReturnKeyDone;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    26
        textField.adjustsFontSizeToFitWidth = YES;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    27
        [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    28
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    29
        [self.contentView addSubview:textField];
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    30
        //[textField release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    31
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
    32
        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
    33
        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
    34
        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
    35
        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
    36
        [self.contentView addSubview:titleLabel];
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    37
        //[titleLabel release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    38
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
    39
        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
    40
        maximumCharacters = 64;
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    41
        oldValue = nil;
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    42
    }
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    43
    return self;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    44
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    45
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    46
-(void) layoutSubviews {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    47
    [super layoutSubviews];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    48
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    49
    CGRect contentRect = self.contentView.bounds;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    50
    CGFloat boundsX = contentRect.origin.x;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    51
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    52
    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
    53
    int skew = 0;
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    54
    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
    55
        offset += self.imageView.frame.size.width;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    56
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
    57
    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
    58
        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
    59
    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
    60
        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
    61
        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
    62
        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
    63
    }
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
    64
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
    textField.frame = CGRectMake(boundsX+offset+10, skew+10, 250, [UIFont labelFontSize] + 4);
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    66
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    67
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    68
-(void) setSelected:(BOOL)selected animated:(BOOL)animated {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    69
    [super setSelected:selected animated:animated];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    70
    // Configure the view for the selected state
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    71
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    72
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    73
-(void) dealloc {
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    74
    self.delegate = nil;
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    75
    releaseAndNil(oldValue);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    76
    releaseAndNil(titleLabel);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    77
    releaseAndNil(textField);
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    78
    [super dealloc];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    79
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    80
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    81
#pragma mark -
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    82
#pragma mark textField delegate
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    83
// limit the size of the field to 64 characters like in original frontend
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    84
-(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
    85
    return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length);
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    86
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    87
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
    88
// allow editing only if delegate is set and conformant to protocol
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    89
-(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
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
    90
    return (delegate != nil) && [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)];
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    91
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    92
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    93
// the textfield is being modified, update the navigation controller
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    94
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    95
    // don't interact with table below
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    96
    ((UITableView*)[self superview]).scrollEnabled = NO;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    97
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    98
    self.oldValue = self.textField.text;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    99
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   100
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"")
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   101
                                                                     style:UIBarButtonItemStylePlain
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   102
                                                                    target:self
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   103
                                                                    action:@selector(cancel:)];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   104
    [(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   105
    [cancelButton release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   106
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   107
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"")
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   108
                                                                     style:UIBarButtonItemStyleDone
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   109
                                                                    target:self
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   110
                                                                    action:@selector(save:)];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   111
    [(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   112
    [saveButton release];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   113
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   114
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   115
/* with this a field might remain in editing status even if the view moved;
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   116
   use method below instead that allows some more interaction
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   117
// don't accept 0-length strings
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   118
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField {
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   119
    return ([aTextField.text length] > 0);
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   120
}
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   121
*/
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   122
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   123
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField {
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   124
    return ([aTextField.text length] >= self.minimumCharacters);
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   125
}
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   126
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   127
// the textfield has been modified, tell the delegate to do something
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   128
-(void) textFieldDidEndEditing:(UITextField *)aTextField{
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   129
    ((UITableView*)[self superview]).scrollEnabled = YES;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   130
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   131
    [(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   132
    [(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   133
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   134
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
   135
#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
   136
#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
   137
// the user wants to show the keyboard
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   138
-(void) replyKeyboard {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   139
    [self.textField becomeFirstResponder];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   140
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   141
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   142
// the user pressed cancel so hide keyboard
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   143
-(void) cancel:(id) sender {
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   144
    self.textField.text = self.oldValue;
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   145
    [self save:sender];
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   146
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   147
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   148
// send the value to the delegate
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   149
-(void) save:(id) sender {
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
   150
    if (delegate == nil || ![delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)])
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   151
        return;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   152
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   153
    // don't save if the textfield is invalid
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   154
    if (![self textFieldShouldReturn:textField])
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   155
        return;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   156
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   157
    [delegate saveTextFieldValue:self.textField.text withTag:self.tag];
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   158
    [self.textField resignFirstResponder];
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   159
    self.oldValue = nil;
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   160
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   161
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   162
@end