1/*
2 Project: GShisen
3
4 Copyright (C) 2003-2009 The GNUstep Application Project
5
6 Author: Enrico Sersale, Riccardo Mottola
7
8 Tile
9
10 This application is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This application is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 Library General Public License for more details.
19
20 You should have received a copy of the GNU General Public
21 License along with this library; if not, write to the Free
22 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 */
24
25#import "tile.h"
26#import "board.h"
27
28@implementation GSTile
29
30- (id)initOnBoard:(GSBoard *)aboard
31          iconRef:(NSString *)ref
32            group:(int)grp
33           rndpos:(int)rnd
34     isBorderTile:(BOOL)btile
35{
36    self = [super init];
37    if(self) {
38        [self setFrame: NSMakeRect(0, 0, 40, 56)];
39        isBorderTile = btile;
40        if(!isBorderTile) {
41            theBoard = aboard;
42            iconName = [[NSString alloc] initWithFormat:@"%@.tiff", ref];
43            iconSelName = [[NSString alloc] initWithFormat:@"%@-h.tiff", ref];
44            icon = [NSImage imageNamed: iconName];
45            group = grp;
46            rndpos = [[NSNumber alloc] initWithInt: rnd];
47            isSelect = NO;
48            isActive = YES;
49        } else {
50            isActive = NO;
51        }
52    }
53    return self;
54}
55
56- (void)dealloc
57{
58    if(!isBorderTile) {
59        //[icon release];
60        [iconName release];
61        [iconSelName release];
62        [rndpos release];
63    }
64    [super dealloc];
65}
66
67- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
68{
69    return YES;
70}
71
72- (void)setPositionOnBoard:(int)x posy:(int)y
73{
74    px = x;
75    py = y;
76}
77
78- (void)select
79{
80    int result = [theBoard prepareTilesToRemove: self];
81    if(!result) {
82        return;
83    } else {
84        isSelect = YES;
85        icon = [NSImage imageNamed: iconSelName];
86        [self setNeedsDisplay: YES];
87    }
88    if(result == 2)
89        [theBoard removeCurrentTiles];
90}
91
92- (void)hightlight
93{
94    icon = [NSImage imageNamed: iconSelName];
95    [self setNeedsDisplay: YES];
96}
97
98- (void)unselect
99{
100    isSelect = NO;
101    icon = [NSImage imageNamed: iconName];
102    [self setNeedsDisplay: YES];
103    [theBoard unSetCurrentTiles];
104}
105
106- (void)deactivate
107{
108    isActive = NO;
109    [self setNeedsDisplay: YES];
110}
111
112- (void)activate
113{
114    isActive = YES;
115    [self setNeedsDisplay: YES];
116}
117
118
119- (BOOL)isSelect
120{
121    return isSelect;
122}
123
124- (BOOL)isActive
125{
126    return isActive;
127}
128
129- (BOOL)isBorderTile
130{
131    return isBorderTile;
132}
133
134- (int)group
135{
136    return group;
137}
138
139- (NSNumber *)rndpos
140{
141    return rndpos;
142}
143
144- (int)px
145{
146    return px;
147}
148
149- (int)py
150{
151    return py;
152}
153
154- (void)mouseDown:(NSEvent *)theEvent
155{
156    if([theBoard gameState] != GAME_STATE_RUNNING)
157        return;
158    if(!isActive)
159        return;
160    if(!isSelect)
161        [self select];
162    else
163        [self unselect];
164}
165
166- (void)drawRect:(NSRect)rect
167{
168    if(!isActive || ([theBoard gameState] != GAME_STATE_RUNNING)) {
169        // This doesn't need to be done, since the board will take care of it.
170        //[[NSColor colorWithCalibratedRed: 0.1 green: 0.47 blue: 0 alpha: 1] set];
171        //NSRectFill(rect);
172    } else {
173        [icon compositeToPoint: NSZeroPoint operation: NSCompositeCopy];
174    }
175}
176
177@end
178
179
180