cocoaTouch/otherSrc/SquareButtonView.m
author smxx
Thu, 29 Apr 2010 17:21:10 +0000
changeset 3376 faee68a28b82
parent 3364 e5403e2bf02c
child 3479 972ae3ec178a
permissions -rw-r--r--
Engine: * Did some OpenGL cleanup, avoiding additional calls to glColor, etc.
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
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    15
@synthesize colorArray, selectedColor, ownerDictionary;
3361
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;
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    20
        selectedColor = 0;
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    21
3361
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    22
        // list of allowed colors
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    23
        NSArray *colors = [[NSArray alloc] initWithObjects: [NSNumber numberWithUnsignedInt:4421353],    // bluette
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    24
                                                            [NSNumber numberWithUnsignedInt:4100897],    // greeeen
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    25
                                                            [NSNumber numberWithUnsignedInt:10632635],   // violett
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    26
                                                            [NSNumber numberWithUnsignedInt:16749353],   // oranngy
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    27
                                                            [NSNumber numberWithUnsignedInt:14483456],   // reddish
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    28
                                                            [NSNumber numberWithUnsignedInt:7566195],    // graaaay
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    29
                                                            nil];
3361
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    30
        self.colorArray = colors;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    31
        [colors release];
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
        // set the color to the first available one
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    34
        [self nextColor];
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
        // this makes the button round and nice
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    37
        [self.layer setCornerRadius:7.0f];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    38
        [self.layer setMasksToBounds:YES];        
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
        // this changes the color at button press
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    41
        [self addTarget:self action:@selector(nextColor) forControlEvents:UIControlEventTouchUpInside];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    42
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    43
        self.backgroundColor = [UIColor blackColor];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    44
    }
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    45
    return self;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    46
}
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    47
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    48
-(void) nextColor {
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    49
    colorIndex++;
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    50
    if (colorIndex >= [colorArray count])
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    51
        colorIndex = 0;
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    52
3361
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    53
    NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue];
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    54
    [self selectColor:color];
3361
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    55
    
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    56
    [ownerDictionary setObject:[NSNumber numberWithInt:color] forKey:@"color"];
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    57
}
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    58
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    59
-(void) selectColor:(NSUInteger) color {
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    60
    if (color != selectedColor) {
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    61
        selectedColor = color;
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    62
        
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    63
        UIGraphicsBeginImageContext(self.frame.size);	
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    64
        CGContextRef context = UIGraphicsGetCurrentContext();
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    65
        CGContextSetRGBFillColor(context, ((color & 0x00FF0000) >> 16)/255.0f,
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    66
                                          ((color & 0x0000FF00) >> 8)/255.0f,
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    67
                                           (color & 0x000000FF)/255.0f,
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    68
                                            1.0f);
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    69
        CGContextFillRect(context, CGRectMake(1.1, 1.1, self.frame.size.width-2.2, self.frame.size.height-2.2));
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    70
        
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    71
        UIImageView *resultingImage = [[UIImageView alloc] initWithImage: UIGraphicsGetImageFromCurrentImageContext()];
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    72
        UIGraphicsEndImageContext();
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    73
        
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    74
        [self setImage:resultingImage.image forState:UIControlStateNormal];
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    75
        [resultingImage release];
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    76
        
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    77
    }
3361
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    78
    /*  
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    79
    self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f 
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    80
                                           green:((color & 0x0000FF00) >> 8)/255.0f 
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    81
                                            blue: (color & 0x000000FF)/255.0f 
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    82
                                           alpha:1.0f];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    83
    */
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    84
}
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    85
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    86
-(void) dealloc {
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3361
diff changeset
    87
    [ownerDictionary release];
3361
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    88
    [colorArray release];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    89
    [super dealloc];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    90
}
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    91
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    92
cfc6cd502f85 buttons for number of hogs in game config
koda
parents:
diff changeset
    93
@end