project_files/HedgewarsMobile/Classes/SquareButtonView.m
author koda
Sun, 07 Nov 2010 19:00:41 +0100
changeset 4187 adb144a907aa
parent 3917 4c243b1eac97
child 4976 088d40d8aba2
permissions -rw-r--r--
remove last warnings from xcodeproj update credits
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    18
 * File created on 20/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3703
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "SquareButtonView.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
#import <QuartzCore/QuartzCore.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
#import "UIImageExtra.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
@implementation SquareButtonView
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
@synthesize colorArray, selectedColor, ownerDictionary;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
-(id) initWithFrame:(CGRect)frame {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    if ((self = [super initWithFrame:frame])) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
        colorIndex = -1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
        selectedColor = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
3917
4c243b1eac97 playing a bit with colors
koda
parents: 3829
diff changeset
    35
        self.colorArray = getAvailableColors();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
        // set the color to the first available one
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
        [self nextColor];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    39
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
        // this makes the button round and nice with a border
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
        [self.layer setCornerRadius:7.0f];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    42
        [self.layer setMasksToBounds:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
        [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
    44
        [self.layer setBorderColor:[UICOLOR_HW_YELLOW_BODER CGColor]];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    45
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
        // this changes the color at button press
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
        [self addTarget:self action:@selector(nextColor) forControlEvents:UIControlEventTouchUpInside];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    return self;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
-(void) nextColor {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
    colorIndex++;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    if (colorIndex >= [colorArray count])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
        colorIndex = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    59
    self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    60
                                           green:((color & 0x0000FF00) >> 8)/255.0f
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    61
                                            blue: (color & 0x000000FF)/255.0f
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
                                           alpha:1.0f];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    63
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    [ownerDictionary setObject:[NSNumber numberWithInt:color] forKey:@"color"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
-(void) selectColor:(NSUInteger) color {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    if (color != selectedColor) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
        selectedColor = color;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
        colorIndex = [colorArray indexOfObject:[NSNumber numberWithUnsignedInt:color]];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    71
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    72
        self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    73
                                               green:((color & 0x0000FF00) >> 8)/255.0f
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    74
                                                blue: (color & 0x000000FF)/255.0f
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
                                               alpha:1.0f];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    [ownerDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    [colorArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
@end