|
1 /* |
|
2 * Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 * |
|
18 * File created on 01/10/2011. |
|
19 */ |
|
20 |
|
21 |
|
22 #import "HWUtils.h" |
|
23 #import <sys/types.h> |
|
24 #import <sys/sysctl.h> |
|
25 #import <QuartzCore/QuartzCore.h> |
|
26 #import <CommonCrypto/CommonDigest.h> |
|
27 #import "PascalImports.h" |
|
28 #import "hwconsts.h" |
|
29 |
|
30 @implementation HWUtils |
|
31 |
|
32 +(NSString *)modelType { |
|
33 size_t size; |
|
34 // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space |
|
35 sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
|
36 char *name = (char *)malloc(sizeof(char) * size); |
|
37 // get the platform name |
|
38 sysctlbyname("hw.machine", name, &size, NULL, 0); |
|
39 NSString *modelId = [NSString stringWithUTF8String:name]; |
|
40 free(name); |
|
41 |
|
42 return modelId; |
|
43 } |
|
44 |
|
45 +(NSArray *)teamColors { |
|
46 // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing |
|
47 unsigned int colors[] = HW_TEAMCOLOR_ARRAY; |
|
48 NSMutableArray *array = [[NSMutableArray alloc] init]; |
|
49 |
|
50 int i = 0; |
|
51 while(colors[i] != 0) |
|
52 [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]]; |
|
53 |
|
54 NSArray *final = [NSArray arrayWithArray:array]; |
|
55 [array release]; |
|
56 return final; |
|
57 } |
|
58 |
|
59 @end |
|
60 |
|
61 |
|
62 @implementation UIColor (extra) |
|
63 |
|
64 +(UIColor *)darkYellowColor { |
|
65 return [UIColor colorWithRed:(CGFloat)0xFE/255 green:(CGFloat)0xC0/255 blue:0 alpha:1]; |
|
66 } |
|
67 |
|
68 +(UIColor *)lightYellowColor { |
|
69 return [UIColor colorWithRed:(CGFloat)0xF0/255 green:(CGFloat)0xD0/255 blue:0 alpha:1]; |
|
70 } |
|
71 |
|
72 +(UIColor *)darkBlueColor { |
|
73 return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x42/255 alpha:1]; |
|
74 } |
|
75 |
|
76 +(UIColor *)darkBlueColorTransparent { |
|
77 return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x42/255 alpha:0.58f]; |
|
78 } |
|
79 |
|
80 +(UIColor *)blackColorTransparent { |
|
81 if (IS_NOT_POWERFUL([HWUtils modelType])) |
|
82 return [UIColor blackColor]; |
|
83 else |
|
84 return [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]; |
|
85 } |
|
86 |
|
87 @end |
|
88 |
|
89 |
|
90 @implementation UILabel (extra) |
|
91 |
|
92 -(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title { |
|
93 return [self initWithFrame:frame |
|
94 andTitle:title |
|
95 withBorderWidth:1.5f |
|
96 withBorderColor:[UIColor darkYellowColor] |
|
97 withBackgroundColor:[UIColor darkBlueColor]]; |
|
98 } |
|
99 |
|
100 -(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title withBorderWidth:(CGFloat) borderWidth { |
|
101 return [self initWithFrame:frame |
|
102 andTitle:title |
|
103 withBorderWidth:borderWidth |
|
104 withBorderColor:[UIColor darkYellowColor] |
|
105 withBackgroundColor:[UIColor darkBlueColor]]; |
|
106 } |
|
107 |
|
108 -(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title withBorderWidth:(CGFloat) borderWidth |
|
109 withBorderColor:(UIColor *)borderColor withBackgroundColor:(UIColor *)backColor{ |
|
110 UILabel *theLabel = [self initWithFrame:frame]; |
|
111 theLabel.backgroundColor = backColor; |
|
112 |
|
113 if (title != nil) { |
|
114 theLabel.text = title; |
|
115 theLabel.textColor = [UIColor lightYellowColor]; |
|
116 theLabel.textAlignment = UITextAlignmentCenter; |
|
117 theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100]; |
|
118 } |
|
119 |
|
120 [theLabel.layer setBorderWidth:borderWidth]; |
|
121 [theLabel.layer setBorderColor:borderColor.CGColor]; |
|
122 [theLabel.layer setCornerRadius:8.0f]; |
|
123 [theLabel.layer setMasksToBounds:YES]; |
|
124 |
|
125 return theLabel; |
|
126 } |
|
127 |
|
128 @end |
|
129 |
|
130 |
|
131 @implementation NSString (extra) |
|
132 |
|
133 -(NSString *)MD5hash { |
|
134 const char *cStr = [self UTF8String]; |
|
135 unsigned char result[16]; |
|
136 CC_MD5( cStr, strlen(cStr), result ); |
|
137 return [NSString stringWithFormat: |
|
138 @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
|
139 result[0], result[1], result[2], result[3], result[4], result[5], |
|
140 result[6], result[7], result[8], result[9], result[10], result[11], |
|
141 result[12], result[13], result[14], result[15]]; |
|
142 } |
|
143 |
|
144 @end |