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