cocoaTouch/otherSrc/SquareButtonView.m
changeset 3514 59dbd31e9953
parent 3513 f589230fa21b
child 3515 3e8635f43972
equal deleted inserted replaced
3513:f589230fa21b 3514:59dbd31e9953
     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 <QuartzCore/QuartzCore.h>
       
    11 #import "CommodityFunctions.h"
       
    12 #import "UIImageExtra.h"
       
    13 
       
    14 @implementation SquareButtonView
       
    15 @synthesize colorArray, selectedColor, ownerDictionary;
       
    16 
       
    17 -(id) initWithFrame:(CGRect)frame {
       
    18     if ((self = [super initWithFrame:frame])) {
       
    19         colorIndex = -1;
       
    20         selectedColor = 0;
       
    21 
       
    22         // list of allowed colors
       
    23         NSArray *colors = [[NSArray alloc] initWithObjects: [NSNumber numberWithUnsignedInt:4421353],    // bluette
       
    24                                                             [NSNumber numberWithUnsignedInt:4100897],    // greeeen
       
    25                                                             [NSNumber numberWithUnsignedInt:10632635],   // violett
       
    26                                                             [NSNumber numberWithUnsignedInt:16749353],   // oranngy
       
    27                                                             [NSNumber numberWithUnsignedInt:14483456],   // reddish
       
    28                                                             [NSNumber numberWithUnsignedInt:7566195],    // graaaay
       
    29                                                             nil];
       
    30         self.colorArray = colors;
       
    31         [colors release];
       
    32 
       
    33         // set the color to the first available one
       
    34         [self nextColor];
       
    35         
       
    36         // this makes the button round and nice
       
    37         [self.layer setCornerRadius:7.0f];
       
    38         [self.layer setMasksToBounds:YES];        
       
    39         
       
    40         // this changes the color at button press
       
    41         [self addTarget:self action:@selector(nextColor) forControlEvents:UIControlEventTouchUpInside];
       
    42 
       
    43         self.backgroundColor = [UIColor blackColor];
       
    44     }
       
    45     return self;
       
    46 }
       
    47 
       
    48 -(void) nextColor {
       
    49     colorIndex++;
       
    50     if (colorIndex >= [colorArray count])
       
    51         colorIndex = 0;
       
    52 
       
    53     NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue];
       
    54     [self selectColor:color];
       
    55     
       
    56     [ownerDictionary setObject:[NSNumber numberWithInt:color] forKey:@"color"];
       
    57 }
       
    58 
       
    59 -(void) selectColor:(NSUInteger) color {
       
    60     if (color != selectedColor) {
       
    61         selectedColor = color;
       
    62         
       
    63         UIGraphicsBeginImageContext(self.frame.size);	
       
    64         CGContextRef context = UIGraphicsGetCurrentContext();
       
    65         CGContextSetRGBFillColor(context, ((color & 0x00FF0000) >> 16)/255.0f,
       
    66                                           ((color & 0x0000FF00) >> 8)/255.0f,
       
    67                                            (color & 0x000000FF)/255.0f,
       
    68                                             1.0f);
       
    69         CGContextFillRect(context, CGRectMake(1.1, 1.1, self.frame.size.width-2.2, self.frame.size.height-2.2));
       
    70         
       
    71         UIImageView *resultingImage = [[UIImageView alloc] initWithImage: UIGraphicsGetImageFromCurrentImageContext()];
       
    72         UIGraphicsEndImageContext();
       
    73         
       
    74         [self setImage:resultingImage.image forState:UIControlStateNormal];
       
    75         [resultingImage release];
       
    76         
       
    77     }
       
    78     /*  
       
    79     self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f 
       
    80                                            green:((color & 0x0000FF00) >> 8)/255.0f 
       
    81                                             blue: (color & 0x000000FF)/255.0f 
       
    82                                            alpha:1.0f];
       
    83     */
       
    84 }
       
    85 
       
    86 -(void) dealloc {
       
    87     [ownerDictionary release];
       
    88     [colorArray release];
       
    89     [super dealloc];
       
    90 }
       
    91 
       
    92 
       
    93 @end