cocoaTouch/otherSrc/SquareButtonView.m
author koda
Wed, 21 Apr 2010 01:57:23 +0000
changeset 3361 cfc6cd502f85
child 3364 e5403e2bf02c
permissions -rw-r--r--
buttons for number of hogs in game config buttons for color of team in game config update and code cleanup fixed crashes at uitextfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3361
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     1
//
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     2
//  HogButtonView.m
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     3
//  HedgewarsMobile
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     4
//
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     5
//  Created by Vittorio on 20/04/10.
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     7
//
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     8
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
     9
#import "SquareButtonView.h"
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    10
#import "CommodityFunctions.h"
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    11
#import "UIImageExtra.h"
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    12
#import "QuartzCore/QuartzCore.h"
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    13
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    14
@implementation SquareButtonView
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    15
@synthesize colorArray;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    16
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    17
-(id) initWithFrame:(CGRect)frame {
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    18
    if ((self = [super initWithFrame:frame])) {
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    19
        colorIndex = -1;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    20
        
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    21
        // list of allowed colors
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    22
        NSArray *colors = [[NSArray alloc] initWithObjects:[NSNumber numberWithUnsignedInt:4421353], [NSNumber numberWithInt:4100897], nil];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    23
        self.colorArray = colors;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    24
        [colors release];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    25
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    26
        // set the color to the first available one
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    27
        [self nextColor];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    28
        
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    29
        // this makes the button round and nice
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    30
        [self.layer setCornerRadius:7.0f];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    31
        [self.layer setMasksToBounds:YES];        
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    32
        
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    33
        // this changes the color at button press
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    34
        [self addTarget:self action:@selector(nextColor) forControlEvents:UIControlEventTouchUpInside];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    35
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    36
        self.backgroundColor = [UIColor blackColor];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    37
    }
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    38
    return self;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    39
}
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    40
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    41
-(void) nextColor {
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    42
    colorIndex++;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    43
    if (colorIndex >= [colorArray count])
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    44
        colorIndex = 0;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    45
    
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    46
    NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    47
    selectedColor = color;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    48
    
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    49
    UIGraphicsBeginImageContext(self.frame.size);	
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    50
    CGContextRef context = UIGraphicsGetCurrentContext();
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    51
    CGContextSetRGBFillColor(context, ((color & 0x00FF0000) >> 16)/255.0f, ((color & 0x0000FF00) >> 8)/255.0f, (color & 0x000000FF)/255.0f, 1.0f);
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    52
    CGContextFillRect(context, CGRectMake(1.1, 1.1, self.frame.size.width-2.2, self.frame.size.height-2.2));
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    53
    
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    54
    UIImageView *resultingImage = [[UIImageView alloc] initWithImage: UIGraphicsGetImageFromCurrentImageContext()];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    55
    UIGraphicsEndImageContext();
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    56
    
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    57
    [self setImage:resultingImage.image forState:UIControlStateNormal];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    58
    [resultingImage release];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    59
    /*  
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    60
    self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f 
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    61
                                           green:((color & 0x0000FF00) >> 8)/255.0f 
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    62
                                            blue: (color & 0x000000FF)/255.0f 
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    63
                                           alpha:1.0f];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    64
    */
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    65
    NSLog(@"index:%d, color:%d, %@",colorIndex, color, self.backgroundColor);
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    66
}
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    67
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    68
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    69
-(void) dealloc {
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    70
    [colorArray release];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    71
    [super dealloc];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    72
}
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    73
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    74
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    75
@end