-
Notifications
You must be signed in to change notification settings - Fork 12
/
SMFListDropShadowControl.m
344 lines (281 loc) · 8.95 KB
/
SMFListDropShadowControl.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// SMFListDropShadowControl.m
// SMFramework
//
// Created by Thomas Cool on 1/21/11.
// Copyright 2011 tomcool.org. All rights reserved.
//
#define ZOOM_TO_POINT CGPointMake(591.5999755859375, 284.39999389648438)
#import "SMFListDropShadowControl.h"
#import "SMFThemeInfo.h"
#import "SMFMenuItem.h"
#import "SMFDefines.h"
#import "SMFMockMenuItem.h"
#import "SMFAnimation.h"
/*
sender is a new variable to determine the bounds and position for zoom animations, if it descends from BRMenuItem we synthesize a stub class
that only has position and bounds variables, i can't figure out how to get the proper bounds/position from BRListControl from and standard BRMenuController
so what i do is take the blue lozenge from the list grab its position and modify the x value (the y variables are accurate here, only X needs an attitude adjustment)
*/
@implementation SMFListDropShadowControl
@synthesize cDelegate, cDatasource, list, isAnimated, sender;
-(id)init
{
self =[super init];
if (self!=nil) {
self.list = [[[BRListControl alloc]init]autorelease];
[self.list setDatasource:self];
self.isAnimated = FALSE; //up to you, can be false by default if you dont like it
self.backgroundColor=[[SMFThemeInfo sharedTheme]blackColor];
self.borderColor=[[SMFThemeInfo sharedTheme] whiteColor];
self.borderWidth=3.0;
self.inhibitsFocusForChildren = TRUE;
self.avoidsCursor = TRUE;
[self setContent:self.list];
}
return self;
}
-(void)reloadList
{
[list reload];
}
-(void)addToController:(BRController *)ctrl
{
CGRect f = [self rectForSize:CGSizeMake(528., 154.)];
[self setFrame:f];
if (self.isAnimated == TRUE)
{
CAAnimationGroup *zoomInAnimation = nil;
if (sender != nil)
{
[self updateSender];
zoomInAnimation = [SMFAnimation zoomInFadedToRect:[sender bounds]];
} else {
zoomInAnimation = [SMFAnimation zoomInFadedToRect:CGRectZero];
}
[self setZoomInPosition]; //if we dont set this the position goes haywire
[zoomInAnimation setValue:@"zoomInAnimation" forKey:@"Name"];
[zoomInAnimation setDelegate:self];
[self addAnimation:zoomInAnimation forKey:@"zoomInAnimation"];
}
[ctrl addControl:self];
[ctrl setFocusedControl:self];
[ctrl _setFocus:self];
}
-(void)controlWasActivated
{
// [list setSelection:0];
[self setFocusedControl:list];
[self _setFocus:list];
[super controlWasActivated];
}
- (float)heightForRow:(long)row
{
if (cDatasource && [cDatasource respondsToSelector:@selector(popupHeightForRow:)]) {
return [cDatasource popupHeightForRow:row];
}
return 0.0f;
}
- (BOOL)rowSelectable:(long)row
{
if(cDatasource && [cDatasource respondsToSelector:@selector(popupRowSelectable:)])
return [cDatasource popupRowSelectable:row];
return YES;
}
- (long)itemCount
{
if(cDatasource && [cDatasource respondsToSelector:@selector(popupItemCount)])
return [cDatasource popupItemCount];
return (long)3;
}
- (id)itemForRow:(long)row
{
if(cDatasource && [cDatasource respondsToSelector:@selector(popupItemForRow:)])
return [cDatasource popupItemForRow:row];
SMFMenuItem *it = [SMFMenuItem menuItem];
[it setTitle:@"Default Item"];
return it;
}
- (id)titleForRow:(long)row
{
if(cDatasource && [cDatasource respondsToSelector:@selector(popupTitleForRow:)])
return [cDatasource popupTitleForRow:row];
if (row>=[self itemCount])
return nil;
return [[self itemForRow:row] text];
}
- (long)defaultIndex
{
if(cDatasource && [cDatasource respondsToSelector:@selector(popupDefaultIndex)])
return [cDatasource popupDefaultIndex];
return 0;
}
-(void)itemSelected:(long)selected
{
if (cDelegate && [cDelegate respondsToSelector:@selector(popup:itemSelected:)])
[cDelegate popup:self itemSelected:selected];
else if (cDelegate && [cDelegate respondsToSelector:@selector(popupItemSelected:)]) {
[cDelegate popupItemSelected:selected];
}
else {
[self removeFromParent];
}
}
- (void)removeFromParent
{
if (self.isAnimated == TRUE)
{
[self setZoomOutPosition];
CAAnimationGroup *zoomOutAnimation = nil;
if (sender != nil)
zoomOutAnimation = [SMFAnimation zoomOutFadedToRect:[sender bounds]];
else
zoomOutAnimation = [SMFAnimation zoomOutFadedToRect:CGRectZero];
[zoomOutAnimation setDelegate:self];
[zoomOutAnimation setValue:@"removeFromParent" forKey:@"Name"];
[[self layer] addAnimation:zoomOutAnimation forKey:@"removeFromParent"];
} else {
[super removeFromParent];
}
}
- (void)actuallyRemove //deprecated
{ [super removeFromParent]; }
-(BOOL)brEventAction:(BREvent*)event
{
int remoteAction = [event remoteAction];
switch (remoteAction)
{
case kBREventRemoteActionMenu:
[self removeFromParent];
return YES;
break;
case kBREventRemoteActionPlay:
[self itemSelected:[list selection]];
return YES;
break;
}
return [super brEventAction:event];
}
-(CGRect)rectForSize:(CGSize)s
{
CGRect r;
r.size.width=s.width;
SMFMenuItem *a = [SMFMenuItem menuItem];
CGSize ss = [a sizeThatFits:s];
int it = [self itemCount];
if (it>6)
it=6;
r.size.height=52.+ss.height*it;
CGSize windowSize = [BRWindow maxBounds];
r.origin.y=(windowSize.height-r.size.height)/2.0f;
r.origin.x=(windowSize.width-r.size.width)/2.0f;
return r;
}
# pragma mark animation stuff
- (void)animationDidStart:(CAAnimation *)anim
{
//NSLog(@"animationDidStart: %@", anim);
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSString *animationName = [anim valueForKey:@"Name"];
if ([animationName isEqualToString:@"removeFromParent"])
{
[super removeFromParent];
}
[self removeAnimationForKey:animationName];
}
- (void)setZoomInPosition
{
CABasicAnimation *pos = [CABasicAnimation animationWithKeyPath:@"position"];
if (sender != nil)
{
pos.fromValue = [NSValue valueWithCGPoint:[sender position]];
} else {
pos.fromValue = [NSValue valueWithCGPoint:ZOOM_TO_POINT]; //
}
pos.toValue = [NSValue valueWithCGPoint:CGPointMake(640.0, 360.0)];
pos.fillMode = kCAFillModeForwards;
[[self layer] addAnimation:pos forKey:@"position"];
}
- (void)setZoomOutPosition
{
CABasicAnimation *pos = [CABasicAnimation animationWithKeyPath:@"position"];
pos.fromValue = [NSValue valueWithCGPoint:CGPointMake(640.0, 360.0)];
if (sender != nil)
{
pos.toValue = [NSValue valueWithCGPoint:[sender position]];
} else {
pos.toValue = [NSValue valueWithCGPoint:ZOOM_TO_POINT];
}
pos.fillMode = kCAFillModeForwards;
[[self layer] addAnimation:pos forKey:@"position"];
}
/*
if we are a BRMenuItem there are very minimal chances that you will get a usable position and bounds from it for zooming purposes
here are all the functions where i handle what is mentioned at the top (the comments about sender)
*/
- (void)updateSender
{
if ([sender isKindOfClass:[BRMenuItem class]]) //is or descends from BRMenuItem
{
id newSender = [self synthesizeMockItem]; //create our stub menu item that has 2 variables total.
if (newSender != nil)
{
// NSLog(@"setting new sender to: %@", newSender);
sender = newSender;
//NSLog(@"sender check: %@", sender);
}
}
}
// find the list given the BRMenuItem (or said subclass of BRMenuItem)
- (id)getListFromMenuItem:(id)menuItem
{
id listControl = [[[menuItem parent] parent] parent]; //parent = BRGridControl, grand parent = BRScrollControl, great grand parent = BRListControl
NSString *class = NSStringFromClass([listControl class]);
if (![class isEqualToString:@"BRListControl"])
{
NSLog(@"cant find list control!!!, found %@ instead!", class); //bail!!!
return nil;
}
return listControl;
}
//given the BRBlueGlowSelectionLozengeLayer control, spit out our stub class with the proper positioning.
- (id)synthesizeMockItemFrom:(id)theSender withX:(float)xValue
{
SMFMockMenuItem *menuItem = [[SMFMockMenuItem alloc] init];
CGPoint newPosition = [theSender position];
newPosition.x = xValue; //said attitude adjustment, without setting this x variable properly, all hell breaks loose.
[menuItem setBounds:[theSender bounds]];
[menuItem setPosition:newPosition];
return menuItem;
}
- (id)synthesizeMockItem
{
id theList = [self getListFromMenuItem:sender];
if (theList == nil)
return nil;
CGPoint listPosition = [theList position];
float xValue = listPosition.x;
NSEnumerator *controlEnum = [[theList controls] objectEnumerator];
id current = nil;
while ((current = [controlEnum nextObject]))
{
NSString *currentClass = NSStringFromClass([current class]);
if ([currentClass isEqualToString:@"BRBlueGlowSelectionLozengeLayer"])
{
return [self synthesizeMockItemFrom:current withX:xValue];
}
}
return nil;
}
-(void)dealloc
{
[sender release];
self.sender = nil;
self.cDelegate=nil;
self.cDatasource=nil;
self.list=nil;
[super dealloc];
}
@end