-
Notifications
You must be signed in to change notification settings - Fork 1
/
GLCardGroup.m
245 lines (191 loc) · 5.71 KB
/
GLCardGroup.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#import "GLTexture.h"
#import "MC3DVector.h"
#import "GLPlayer.h"
#import "GLCard.h"
#import "AnimationGroup.h"
#import "AnimatedFloat.h"
#import "AnimatedVec3.h"
#import "GLCardGroup.h"
#import "GLCamera.h"
#import "DisplayContainer.h"
#import "DisplayContainer.h"
#import "GLRenderer.h"
@implementation GLCardGroup
+(GLCardGroup*)cardGroup
{
return [[[GLCardGroup alloc] init] autorelease];
}
@synthesize player = _player;
@synthesize cards = _cards;
@synthesize draggedCard = _draggedCard;
@synthesize initialAngle = _initialAngle;
@synthesize initialIndex = _initialIndex;
@synthesize finalIndex = _finalIndex;
@synthesize showLabels = _showLabels;
@dynamic bendFactor;
@dynamic angleFlip;
-(GLfloat)bendFactor
{
return self.cards.liveObjects.count ? [[(GLCard*)[self.cards.liveObjects objectAtIndex:0] bendFactor] value] : 0;
}
-(void)setBendFactor:(GLfloat)bendFactor
{
for(GLCard* card in self.cards.liveObjects)
{
if(within(card.bendFactor.value, bendFactor, 0.001)) { continue; }
card.bendFactor = [AnimatedFloat floatWithValue:bendFactor];
}
}
-(GLfloat)angleFlip
{
return self.cards.liveObjects.count ? [[(GLCard*)[self.cards.liveObjects objectAtIndex:0] isFlipped] value] * 180 : 0;
}
-(BOOL)isAnimating
{
for(GLCard* card in self.cards.objects)
{
if(!card.dealt.hasEnded) return YES;
if(!card.death.hasEnded) return YES;
if(!card.isHeld.hasEnded) return YES;
if(!card.isFlipped.hasEnded) return YES;
if(!card.angleFan.hasEnded) return YES;
if(!card.bendFactor.hasEnded) return YES;
}
return NO;
}
-(id)init
{
self = [super init];
if(self)
{
self.cards = [[[DisplayContainer alloc] init] autorelease];
self.cards.delay = 0.2;
self.initialIndex = -1;
self.finalIndex = -1;
self.initialAngle = 0.0;
self.draggedCard = nil;
self.showLabels = YES;
}
return self;
}
-(void)setFlipped:(BOOL)flipped andThen:(SimpleBlock)work;
{
AnimationGroup* animationGroup = [AnimationGroup animationGroup];
for(GLCard* card in self.cards.liveObjects)
{
//GLfloat location = flipped ? (4 * card.position - 8) : 0;
//GLfloat angleFlip = flipped ? 180 : 0;
// [card.isFlipped setValue:location withSpeed:1];
// [card.angleFlip setValue:angleFlip withSpeed:180];
[card.isFlipped setValue:flipped withSpeed:1];
[animationGroup addAnimation:card.isFlipped];
}
[animationGroup finishAndThen:work];
}
-(void)layoutCards
{
NSUInteger cardCount = self.cards.liveObjects.count;
NSUInteger currentPosition = 0;
GLfloat delta = clipFloat(22.5 / (cardCount - 1), 0, 6);
GLfloat currentAngle = (cardCount - 1) * -delta / 2.0;
for(GLCard* card in self.cards.liveObjects)
{
card.position = currentPosition;
[card.angleFan setValue:currentAngle forTime:0.3];
currentPosition++;
currentAngle += delta;
}
}
-(void)updateCardsWithKeys:(NSArray*)keys held:(NSArray*)heldKeys andThen:(SimpleBlock)work
{
NSMutableArray* newKeys = [NSMutableArray array];
NSMutableDictionary* newDictionary = [NSMutableDictionary dictionary];
for(NSString* key in keys)
{
[newKeys addObject:key];
GLCard* card = [GLCard cardWithKey:key held:[heldKeys containsObject:key]];
card.cardGroup = self;
[newDictionary setObject:card forKey:key];
}
BOOL cardsKeysChanged = ![self.cards.keys isEqualToArray:newKeys];
[self.cards setLiveKeys:newKeys liveDictionary:newDictionary andThen:work];
if(cardsKeysChanged)
{
[self layoutCards];
}
}
-(void)makeControlPoints
{
for(GLCard* card in self.cards.objects)
{
[card makeControlPoints];
}
}
-(void)drawFronts
{
for(GLCard* card in self.cards.objects.reverseObjectEnumerator)
{
[card drawFront];
}
}
-(void)drawBacks
{
for(GLCard* card in self.cards.objects)
{
[card drawBack];
}
}
-(void)drawShadows
{
for(GLCard* card in self.cards.objects)
{
[card drawShadow];
}
}
-(void)drawLabels
{
for(GLCard* card in self.cards.objects)
{
[card drawLabel];
}
}
-(void)startDragForCard:(GLCard*)card
{
if(self.draggedCard) { return; }
self.draggedCard = card;
self.initialAngle = card.angleFan.value;
self.initialIndex = card.position;
}
-(void)dragCardToTarget:(int)target withDelta:(GLfloat)delta
{
if(self.draggedCard)
{
NSMutableArray* newKeys = [[[self.cards liveKeys] mutableCopy] autorelease];
[newKeys removeObject:self.draggedCard.key];
[newKeys insertObject:self.draggedCard.key atIndex:target];
BOOL cardsKeysChanged = ![self.cards.keys isEqualToArray:newKeys];
[self.cards setLiveKeys:newKeys liveDictionary:self.cards.liveDictionary andThen:nil];
if(cardsKeysChanged)
{
[self layoutCards];
}
self.finalIndex = target;
[self.draggedCard.angleFan setValue:self.initialAngle - delta * 25 / 480];
}
}
-(void)stopDrag
{
if(self.draggedCard)
{
if(self.initialIndex >= 0 && self.finalIndex >= 0)
{
[self.player.renderer.gameController moveCardIndex:self.initialIndex toIndex:self.finalIndex];
}
[self layoutCards];
self.draggedCard = nil;
self.initialAngle = 0.0;
self.initialIndex = -1;
self.finalIndex = -1;
}
}
@end