10 #import "CommodityFunctions.h" |
10 #import "CommodityFunctions.h" |
11 #import "UIImageExtra.h" |
11 #import "UIImageExtra.h" |
12 #import "QuartzCore/QuartzCore.h" |
12 #import "QuartzCore/QuartzCore.h" |
13 |
13 |
14 @implementation SquareButtonView |
14 @implementation SquareButtonView |
15 @synthesize colorArray; |
15 @synthesize colorArray, selectedColor, ownerDictionary; |
16 |
16 |
17 -(id) initWithFrame:(CGRect)frame { |
17 -(id) initWithFrame:(CGRect)frame { |
18 if ((self = [super initWithFrame:frame])) { |
18 if ((self = [super initWithFrame:frame])) { |
19 colorIndex = -1; |
19 colorIndex = -1; |
20 |
20 selectedColor = 0; |
|
21 |
21 // list of allowed colors |
22 // list of allowed colors |
22 NSArray *colors = [[NSArray alloc] initWithObjects:[NSNumber numberWithUnsignedInt:4421353], [NSNumber numberWithInt:4100897], nil]; |
23 NSArray *colors = [[NSArray alloc] initWithObjects: [NSNumber numberWithUnsignedInt:4421353], // bluette |
|
24 [NSNumber numberWithUnsignedInt:4100897], // greeeen |
|
25 [NSNumber numberWithUnsignedInt:10632635], // violett |
|
26 [NSNumber numberWithUnsignedInt:16749353], // oranngy |
|
27 [NSNumber numberWithUnsignedInt:14483456], // reddish |
|
28 [NSNumber numberWithUnsignedInt:7566195], // graaaay |
|
29 nil]; |
23 self.colorArray = colors; |
30 self.colorArray = colors; |
24 [colors release]; |
31 [colors release]; |
25 |
32 |
26 // set the color to the first available one |
33 // set the color to the first available one |
27 [self nextColor]; |
34 [self nextColor]; |
40 |
47 |
41 -(void) nextColor { |
48 -(void) nextColor { |
42 colorIndex++; |
49 colorIndex++; |
43 if (colorIndex >= [colorArray count]) |
50 if (colorIndex >= [colorArray count]) |
44 colorIndex = 0; |
51 colorIndex = 0; |
|
52 |
|
53 NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue]; |
|
54 [self selectColor:color]; |
45 |
55 |
46 NSUInteger color = [[self.colorArray objectAtIndex:colorIndex] unsignedIntValue]; |
56 [ownerDictionary setObject:[NSNumber numberWithInt:color] forKey:@"color"]; |
47 selectedColor = color; |
57 } |
48 |
58 |
49 UIGraphicsBeginImageContext(self.frame.size); |
59 -(void) selectColor:(NSUInteger) color { |
50 CGContextRef context = UIGraphicsGetCurrentContext(); |
60 if (color != selectedColor) { |
51 CGContextSetRGBFillColor(context, ((color & 0x00FF0000) >> 16)/255.0f, ((color & 0x0000FF00) >> 8)/255.0f, (color & 0x000000FF)/255.0f, 1.0f); |
61 selectedColor = color; |
52 CGContextFillRect(context, CGRectMake(1.1, 1.1, self.frame.size.width-2.2, self.frame.size.height-2.2)); |
62 |
53 |
63 UIGraphicsBeginImageContext(self.frame.size); |
54 UIImageView *resultingImage = [[UIImageView alloc] initWithImage: UIGraphicsGetImageFromCurrentImageContext()]; |
64 CGContextRef context = UIGraphicsGetCurrentContext(); |
55 UIGraphicsEndImageContext(); |
65 CGContextSetRGBFillColor(context, ((color & 0x00FF0000) >> 16)/255.0f, |
56 |
66 ((color & 0x0000FF00) >> 8)/255.0f, |
57 [self setImage:resultingImage.image forState:UIControlStateNormal]; |
67 (color & 0x000000FF)/255.0f, |
58 [resultingImage release]; |
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 } |
59 /* |
78 /* |
60 self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f |
79 self.backgroundColor = [UIColor colorWithRed:((color & 0x00FF0000) >> 16)/255.0f |
61 green:((color & 0x0000FF00) >> 8)/255.0f |
80 green:((color & 0x0000FF00) >> 8)/255.0f |
62 blue: (color & 0x000000FF)/255.0f |
81 blue: (color & 0x000000FF)/255.0f |
63 alpha:1.0f]; |
82 alpha:1.0f]; |
64 */ |
83 */ |
65 NSLog(@"index:%d, color:%d, %@",colorIndex, color, self.backgroundColor); |
|
66 } |
84 } |
67 |
85 |
68 |
|
69 -(void) dealloc { |
86 -(void) dealloc { |
|
87 [ownerDictionary release]; |
70 [colorArray release]; |
88 [colorArray release]; |
71 [super dealloc]; |
89 [super dealloc]; |
72 } |
90 } |
73 |
91 |
74 |
92 |