project_files/HedgewarsMobile/Classes/SquareButtonView.m
author koda
Mon, 02 Aug 2010 00:55:24 +0200
changeset 3703 12d17c6e8855
parent 3697 d5b30d6373fc
child 3829 81db3c85784b
permissions -rw-r--r--
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
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];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    35
3547
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];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    38
        [self.layer setMasksToBounds:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
        [self.layer setBorderWidth:2];
3703
12d17c6e8855 halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents: 3697
diff changeset
    40
        [self.layer setBorderColor:[UICOLOR_HW_YELLOW_BODER CGColor]];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    41
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
        // this changes the color at button press
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
        [self addTarget:self action:@selector(nextColor) forControlEvents:UIControlEventTouchUpInside];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    return self;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
-(void) nextColor {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    colorIndex++;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    if (colorIndex >= [colorArray count])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
        colorIndex = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    55
    self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    56
                                           green:((color & 0x0000FF00) >> 8)/255.0f
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    57
                                            blue: (color & 0x000000FF)/255.0f
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
                                           alpha:1.0f];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    59
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    [ownerDictionary setObject:[NSNumber numberWithInt:color] forKey:@"color"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
-(void) selectColor:(NSUInteger) color {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    if (color != selectedColor) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
        selectedColor = color;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
        colorIndex = [colorArray indexOfObject:[NSNumber numberWithUnsignedInt:color]];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    67
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    68
        self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    69
                                               green:((color & 0x0000FF00) >> 8)/255.0f
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    70
                                                blue: (color & 0x000000FF)/255.0f
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
                                               alpha:1.0f];
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    [ownerDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    [colorArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
    [super dealloc];
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
@end