project_files/HedgewarsMobile/Classes/otherSrc/SquareButtonView.m
changeset 3535 9e78c1f3d8d8
parent 3514 59dbd31e9953
equal deleted inserted replaced
3534:51521238724f 3535:9e78c1f3d8d8
    31         [colors release];
    31         [colors release];
    32 
    32 
    33         // set the color to the first available one
    33         // set the color to the first available one
    34         [self nextColor];
    34         [self nextColor];
    35         
    35         
    36         // this makes the button round and nice
    36         // this makes the button round and nice with a border
    37         [self.layer setCornerRadius:7.0f];
    37         [self.layer setCornerRadius:7.0f];
    38         [self.layer setMasksToBounds:YES];        
    38         [self.layer setMasksToBounds:YES];        
       
    39         [self.layer setBorderWidth:2];
    39         
    40         
    40         // this changes the color at button press
    41         // this changes the color at button press
    41         [self addTarget:self action:@selector(nextColor) forControlEvents:UIControlEventTouchUpInside];
    42         [self addTarget:self action:@selector(nextColor) forControlEvents:UIControlEventTouchUpInside];
    42 
       
    43         self.backgroundColor = [UIColor blackColor];
       
    44     }
    43     }
    45     return self;
    44     return self;
    46 }
    45 }
    47 
    46 
    48 -(void) nextColor {
    47 -(void) nextColor {
    49     colorIndex++;
    48     colorIndex++;
       
    49 
    50     if (colorIndex >= [colorArray count])
    50     if (colorIndex >= [colorArray count])
    51         colorIndex = 0;
    51         colorIndex = 0;
    52 
    52 
    53     NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue];
    53     NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue];
    54     [self selectColor:color];
    54     self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f 
       
    55                                            green:((color & 0x0000FF00) >> 8)/255.0f 
       
    56                                             blue: (color & 0x000000FF)/255.0f 
       
    57                                            alpha:1.0f];
    55     
    58     
    56     [ownerDictionary setObject:[NSNumber numberWithInt:color] forKey:@"color"];
    59     [ownerDictionary setObject:[NSNumber numberWithInt:color] forKey:@"color"];
    57 }
    60 }
    58 
    61 
    59 -(void) selectColor:(NSUInteger) color {
    62 -(void) selectColor:(NSUInteger) color {
    60     if (color != selectedColor) {
    63     if (color != selectedColor) {
    61         selectedColor = color;
    64         selectedColor = color;
       
    65         colorIndex = [colorArray indexOfObject:[NSNumber numberWithUnsignedInt:color]];
    62         
    66         
    63         UIGraphicsBeginImageContext(self.frame.size);	
    67         self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f 
    64         CGContextRef context = UIGraphicsGetCurrentContext();
    68                                                green:((color & 0x0000FF00) >> 8)/255.0f 
    65         CGContextSetRGBFillColor(context, ((color & 0x00FF0000) >> 16)/255.0f,
    69                                                 blue: (color & 0x000000FF)/255.0f 
    66                                           ((color & 0x0000FF00) >> 8)/255.0f,
    70                                                alpha:1.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     }
    71     }
    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 }
    72 }
    85 
    73 
    86 -(void) dealloc {
    74 -(void) dealloc {
    87     [ownerDictionary release];
    75     [ownerDictionary release];
    88     [colorArray release];
    76     [colorArray release];