project_files/HedgewarsMobile/Classes/HogButtonView.m
changeset 4476 4bf74e158f44
parent 4474 499748f6e80f
child 4478 05029b4d8490
equal deleted inserted replaced
4474:499748f6e80f 4476:4bf74e158f44
     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 20/04/2010.
       
    19  */
       
    20 
       
    21 
       
    22 #import "HogButtonView.h"
       
    23 #import "CommodityFunctions.h"
       
    24 #import "UIImageExtra.h"
       
    25 #import "PascalImports.h"
       
    26 
       
    27 @implementation HogButtonView
       
    28 @synthesize singleHog, numberOfHogs, ownerDictionary;
       
    29 
       
    30 -(id) initWithFrame:(CGRect)frame {
       
    31     if ((self = [super initWithFrame:frame])) {
       
    32         self.backgroundColor = [UIColor clearColor];
       
    33 
       
    34         NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
       
    35         UIImage *normalHogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
       
    36         [normalHogFile release];
       
    37 
       
    38         self.singleHog = normalHogSprite;
       
    39         [normalHogSprite release];
       
    40         [self addTarget:self action:@selector(addOne) forControlEvents:UIControlEventTouchUpInside];
       
    41     }
       
    42     return self;
       
    43 }
       
    44 
       
    45 -(void) addOne {
       
    46     playSound(@"clickSound");
       
    47     self.highlighted = NO;
       
    48     NSInteger number = self.numberOfHogs;
       
    49     number++;
       
    50     [self drawManyHogs:number];
       
    51 }
       
    52 
       
    53 -(void) drawManyHogs:(NSInteger) hogs {
       
    54     if (numberOfHogs != hogs) {
       
    55         if (hogs <= HW_getMaxNumberOfHogs() && hogs >= 1)
       
    56             numberOfHogs = hogs;
       
    57         else {
       
    58             if (hogs > HW_getMaxNumberOfHogs())
       
    59                 numberOfHogs = 1;
       
    60             else
       
    61                 numberOfHogs = HW_getMaxNumberOfHogs();
       
    62         }
       
    63         [ownerDictionary setObject:[NSNumber numberWithInt:numberOfHogs] forKey:@"number"];
       
    64 
       
    65         UIImage *teamHogs = [[[UIImage alloc] init] autorelease];
       
    66         for (int i = 0; i < numberOfHogs; i++) {
       
    67             teamHogs = [singleHog mergeWith:teamHogs
       
    68                                     atPoint:CGPointMake(8, 0)
       
    69                                      ofSize:CGSizeMake(88, 32)];
       
    70         }
       
    71         [self setImage:teamHogs forState:UIControlStateNormal];
       
    72     }
       
    73 }
       
    74 
       
    75 -(void) dealloc {
       
    76     [ownerDictionary release];
       
    77     [singleHog release];
       
    78     [super dealloc];
       
    79 }
       
    80 
       
    81 
       
    82 @end