project_files/HedgewarsMobile/Classes/SquareButtonView.m
author koda
Sat, 03 Jul 2010 15:30:52 +0200
changeset 3619 1155d8970646
parent 3547 02875b1145b7
child 3697 d5b30d6373fc
permissions -rw-r--r--
partially apply new graphics to code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  HogButtonView.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 20/04/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#import "SquareButtonView.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import <QuartzCore/QuartzCore.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
#import "UIImageExtra.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
@implementation SquareButtonView
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
@synthesize colorArray, selectedColor, ownerDictionary;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
-(id) initWithFrame:(CGRect)frame {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
    if ((self = [super initWithFrame:frame])) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
        colorIndex = -1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
        selectedColor = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
        // list of allowed colors
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
        NSArray *colors = [[NSArray alloc] initWithObjects: [NSNumber numberWithUnsignedInt:4421353],    // bluette
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
                                                            [NSNumber numberWithUnsignedInt:4100897],    // greeeen
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
                                                            [NSNumber numberWithUnsignedInt:10632635],   // violett
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
                                                            [NSNumber numberWithUnsignedInt:16749353],   // oranngy
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
                                                            [NSNumber numberWithUnsignedInt:14483456],   // reddish
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
                                                            [NSNumber numberWithUnsignedInt:7566195],    // graaaay
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
                                                            nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
        self.colorArray = colors;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
        [colors release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
        // set the color to the first available one
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
        [self nextColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
        // this makes the button round and nice with a border
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
        [self.layer setCornerRadius:7.0f];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
        [self.layer setMasksToBounds:YES];        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
        [self.layer setBorderWidth:2];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
        // this changes the color at button press
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
        [self addTarget:self action:@selector(nextColor) forControlEvents:UIControlEventTouchUpInside];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    return self;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
-(void) nextColor {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    colorIndex++;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    if (colorIndex >= [colorArray count])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
        colorIndex = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
    NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
                                           green:((color & 0x0000FF00) >> 8)/255.0f 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
                                            blue: (color & 0x000000FF)/255.0f 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
                                           alpha:1.0f];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    [ownerDictionary setObject:[NSNumber numberWithInt:color] forKey:@"color"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
-(void) selectColor:(NSUInteger) color {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    if (color != selectedColor) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
        selectedColor = color;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
        colorIndex = [colorArray indexOfObject:[NSNumber numberWithUnsignedInt:color]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
        self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
                                               green:((color & 0x0000FF00) >> 8)/255.0f 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
                                                blue: (color & 0x000000FF)/255.0f 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
                                               alpha:1.0f];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    [ownerDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    [colorArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
@end