project_files/HedgewarsMobile/Classes/HWUtils.m
changeset 6208 b831679e9467
parent 6108 7a8da11a6144
child 6209 074ab6ebcb3e
equal deleted inserted replaced
6207:cbc80c22691c 6208:b831679e9467
    20 
    20 
    21 
    21 
    22 #import "HWUtils.h"
    22 #import "HWUtils.h"
    23 #import <sys/types.h>
    23 #import <sys/types.h>
    24 #import <sys/sysctl.h>
    24 #import <sys/sysctl.h>
    25 #import <QuartzCore/QuartzCore.h>
       
    26 #import <CommonCrypto/CommonDigest.h>
       
    27 #import "PascalImports.h"
       
    28 #import "hwconsts.h"
    25 #import "hwconsts.h"
       
    26 
       
    27 static NSString *cachedModel = nil;
       
    28 static NSArray *cachedColors = nil;
    29 
    29 
    30 @implementation HWUtils
    30 @implementation HWUtils
    31 
    31 
    32 +(NSString *)modelType {
    32 +(NSString *)modelType {
    33     size_t size;
    33     if (cachedModel == nil) {
    34     // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
    34         size_t size;
    35     sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    35         // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
    36     char *name = (char *)malloc(sizeof(char) * size);
    36         sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    37     // get the platform name
    37         char *name = (char *)malloc(sizeof(char) * size);
    38     sysctlbyname("hw.machine", name, &size, NULL, 0);
    38         // get the platform name
    39     NSString *modelId = [NSString stringWithUTF8String:name];
    39         sysctlbyname("hw.machine", name, &size, NULL, 0);
    40     free(name);
       
    41 
    40 
    42     return modelId;
    41         cachedModel = [[NSString stringWithUTF8String:name] retain];
       
    42         free(name);
       
    43         DLog(@"Cache now contains: %@",cachedModel);
       
    44     }
       
    45     return cachedModel;
    43 }
    46 }
    44 
    47 
    45 +(NSArray *)teamColors {
    48 +(NSArray *)teamColors {
    46     // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
    49     if (cachedColors == nil) {
    47     unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
    50         // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
    48     NSMutableArray *array = [[NSMutableArray alloc] init];
    51         unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
       
    52         NSMutableArray *array = [[NSMutableArray alloc] init];
    49 
    53 
    50     int i = 0;
    54         int i = 0;
    51     while(colors[i] != 0)
    55         while(colors[i] != 0)
    52         [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
    56             [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
    53 
    57 
    54     NSArray *final = [NSArray arrayWithArray:array];
    58         cachedColors = [[NSArray arrayWithArray:array] retain];
    55     [array release];
    59         [array release];
    56     return final;
    60     }
       
    61     return cachedColors;
       
    62 }
       
    63 
       
    64 +(void) releaseCache {
       
    65     releaseAndNil(cachedModel);
       
    66     releaseAndNil(cachedColors);
    57 }
    67 }
    58 
    68 
    59 @end
    69 @end
    60 
       
    61 
       
    62 @implementation UITableView (extra)
       
    63 
       
    64 -(void) setBackgroundColorForAnyTable:(UIColor *) color {
       
    65     UIView *backView = [[UIView alloc] initWithFrame:self.frame];
       
    66     backView.backgroundColor = color;
       
    67     self.backgroundView = backView;
       
    68     [backView release];
       
    69     self.backgroundColor = [UIColor clearColor];
       
    70 }
       
    71 
       
    72 @end
       
    73 
       
    74 
       
    75 
       
    76 @implementation UIColor (extra)
       
    77 
       
    78 +(UIColor *)darkYellowColor {
       
    79     return [UIColor colorWithRed:(CGFloat)0xFE/255 green:(CGFloat)0xC0/255 blue:0 alpha:1];
       
    80 }
       
    81 
       
    82 +(UIColor *)lightYellowColor {
       
    83     return [UIColor colorWithRed:(CGFloat)0xF0/255 green:(CGFloat)0xD0/255 blue:0 alpha:1];
       
    84 }
       
    85 
       
    86 +(UIColor *)darkBlueColor {
       
    87     return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x42/255 alpha:1];
       
    88 }
       
    89 
       
    90 +(UIColor *)darkBlueColorTransparent {
       
    91     return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x55/255 alpha:0.6f];
       
    92 }
       
    93 
       
    94 +(UIColor *)blackColorTransparent {
       
    95     return [UIColor colorWithRed:0 green:0 blue:0 alpha:0.65f];
       
    96 }
       
    97 
       
    98 @end
       
    99 
       
   100 
       
   101 @implementation UILabel (extra)
       
   102 
       
   103 -(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title {
       
   104     return [self initWithFrame:frame
       
   105                       andTitle:title
       
   106                withBorderWidth:1.5f
       
   107                withBorderColor:[UIColor darkYellowColor]
       
   108            withBackgroundColor:[UIColor darkBlueColor]];
       
   109 }
       
   110 
       
   111 -(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth {
       
   112     return [self initWithFrame:frame
       
   113                       andTitle:title
       
   114                withBorderWidth:borderWidth
       
   115                withBorderColor:[UIColor darkYellowColor]
       
   116            withBackgroundColor:[UIColor darkBlueColorTransparent]];
       
   117 }
       
   118 
       
   119 -(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth
       
   120           withBorderColor:(UIColor *)borderColor withBackgroundColor:(UIColor *)backColor{
       
   121     UILabel *theLabel = [self initWithFrame:frame];
       
   122     theLabel.backgroundColor = backColor;
       
   123 
       
   124     if (title != nil) {
       
   125         theLabel.text = title;
       
   126         theLabel.textColor = [UIColor lightYellowColor];
       
   127         theLabel.textAlignment = UITextAlignmentCenter;
       
   128         theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100];
       
   129     }
       
   130 
       
   131     [theLabel.layer setBorderWidth:borderWidth];
       
   132     [theLabel.layer setBorderColor:borderColor.CGColor];
       
   133     [theLabel.layer setCornerRadius:8.0f];
       
   134     [theLabel.layer setMasksToBounds:YES];
       
   135 
       
   136     return theLabel;
       
   137 }
       
   138 
       
   139 @end
       
   140 
       
   141 
       
   142 @implementation NSString (extra)
       
   143 
       
   144 -(NSString *)MD5hash {
       
   145     const char *cStr = [self UTF8String];
       
   146     unsigned char result[16];
       
   147     CC_MD5( cStr, strlen(cStr), result );
       
   148     return [NSString stringWithFormat:
       
   149             @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
       
   150             result[0], result[1], result[2], result[3], result[4], result[5],
       
   151             result[6], result[7], result[8], result[9], result[10], result[11],
       
   152             result[12], result[13], result[14], result[15]];
       
   153 }
       
   154 
       
   155 @end