source: trunk/SapphirePosterChooser.m @ 250

Revision 250, 9.0 KB checked in by pmerrill, 6 years ago (diff)
Line 
1//
2//  SapphirePosterChooser.m
3//  Sapphire
4//
5//  Created by Patrick Merrill on 10/11/07.
6//  Copyright 2007 __www.nanopi.net__. All rights reserved.
7//
8
9#import "SapphirePosterChooser.h"
10#import "BackRowUtils.h"
11#import <BackRow/BackRow.h>
12
13
14@implementation SapphirePosterChooser
15
16/*!
17* @brief Creates a new poster chooser
18 *
19 * @param scene The scene
20 * @return The chooser
21 */
22- (id) initWithScene: (BRRenderScene *) scene
23{
24        self = [super initWithScene: scene];
25        if(!self)
26                return nil;
27        selectedPoster = -1;
28       
29        // we want to know when the list selection changes, so we can pass
30    // that information on to the icon march layer
31    [[NSNotificationCenter defaultCenter] addObserver: self
32                                             selector: @selector(selectionChanged:)
33                                                 name: @"ListControlSelectionChangedNotification"
34                                               object: [self list]];
35       
36        /* Set a control to display the fileName */
37        fileInfoText = [[BRTextControl alloc] initWithScene: scene];
38        [fileInfoText setTextAttributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes]];
39        [fileInfoText setText:@"No File"];
40        NSRect  frame = [[self masterLayer] frame];
41        frame.origin.y = frame.size.height / 1.25f;
42        frame.origin.x = (frame.size.width / 4.0f) ;
43       
44        [fileInfoText setFrame: frame];
45        [self addControl: fileInfoText];
46       
47        /* Setup posterMarch controls */
48        posterMarch = [[BRMarchingIconLayer alloc] initWithScene: scene];
49    [posterMarch setIconSource: self];
50       
51        [[self list] setDatasource:self];
52       
53        return self;
54}
55
56- (void)dealloc
57{
58        [[NSNotificationCenter defaultCenter] removeObserver: self];
59        [posters release];
60        [posterLayers release];
61        [fileName release];
62        [movieTitle release];
63        [fileInfoText release];
64    [posterMarch setIconSource: nil];
65    [posterMarch removeFromSuperlayer];
66        [posterMarch release];
67        [super dealloc];
68}
69
70- (void) resetLayout
71{
72    [super resetLayout];
73    [[self scene] renderScene];
74}
75
76- (void) willBePushed
77{
78    // We're about to be placed on screen, but we're not yet there
79    // add the icon march layer to the scene
80    [self showIconMarch];
81   
82    // always call super
83    [super willBePushed];
84}
85
86- (void) wasPopped
87{
88    // The user pressed Menu, removing us from the screen
89    // always call super
90    [super wasPopped];
91    // remove the icon march from the scene
92    [self hideIconMarch];
93}
94
95/*!
96* @brief Override the layout
97 *
98 */
99- (void)_doLayout
100{
101        //Shrink the list frame to make room for displaying the filename
102        [super _doLayout];
103        NSRect listFrame = [[_listControl layer] frame];
104        listFrame.size.height -= 2.5f*listFrame.origin.y;
105        listFrame.size.width*= 0.45f;
106        listFrame.origin.y *= 2.0f;
107        listFrame.origin.x *= 1.4f;
108        [[_listControl layer] setFrame:listFrame];
109}
110
111- (void) itemSelected: (long) row
112{
113        /*User made a selection*/
114        //      if(selection==0)
115        //      {
116        /*User requested a menu refresh*/
117        //              [self resetLayout];
118        //      }
119        //      else
120        //      {
121        selectedPoster = row;
122        [[self stack] popController];
123        //      }
124}
125
126/*!
127* @brief The list of movies to choose from
128 *
129 * @return The list of movies to choose from
130 */
131- (NSArray *)posters
132{
133        return posters;
134}
135
136/*!
137* @brief Sets the posters to choose from
138 *
139 * @param posterList The list of movies to choose from
140 */
141- (void)setPosters:(NSArray *)posterList
142{
143        posters = [posterList retain];
144        [self loadPosters];
145        [posterMarch _updateIcons] ;
146        [[self scene] renderScene];
147}
148
149/*!
150* @brief Sets the filename to display
151 *
152 * @param choosingForFileName The filename being choosen for
153 */
154- (void)setFileName:(NSString*)choosingForFileName
155{
156        fileName=[choosingForFileName retain];
157        [fileInfoText setTextAttributes: [[BRThemeInfo sharedTheme] paragraphTextAttributes]];
158        if(movieTitle)
159        {
160                [fileInfoText setText:[NSString stringWithFormat:@"%@ (%@)",movieTitle,fileName]];
161        }
162        else
163                [fileInfoText setText:fileName];       
164}
165
166/*!
167* @brief The filename we searched for
168 *
169 * @return The file name we searched for
170 */
171- (NSString *)fileName
172{
173        return fileName;
174}
175
176/*!
177* @brief Sets the string we searched for
178 *
179 * @param search The string we searched for
180 */
181- (void)setMovieTitle:(NSString *)theMovieTitle
182{
183        movieTitle = [theMovieTitle retain];
184        [fileInfoText setTextAttributes: [[BRThemeInfo sharedTheme] paragraphTextAttributes]];
185        if(fileName)
186        {
187                [fileInfoText setText:[NSString stringWithFormat:@"%@ (%@)",movieTitle,fileName]];
188        }
189        else
190                [fileInfoText setText:movieTitle];             
191}
192
193/*!
194* @brief The string we searched for
195 *
196 * @return The string we searched for
197 */
198- (NSString *)movieTitle
199{
200        return movieTitle;
201}
202
203/*!
204* @brief The item the user selected.  Special values are in the header file
205 *
206 * @return The user's selection
207 */
208- (long)selectedPoster
209{
210        return selectedPoster;
211}
212@end
213
214
215@implementation SapphirePosterChooser (IconDataSource)
216
217- (long) iconCount
218{
219                return [posterLayers count];
220}
221
222- (BRRenderLayer *) iconAtIndex: (long) index
223{
224    if ( index >= [posterLayers count] )
225        return ( nil );
226       
227    return [posterLayers objectAtIndex:index];
228}
229
230@end
231
232@implementation SapphirePosterChooser (ListDataSource)
233
234- (long) itemCount
235{
236        return [posters count];
237}
238
239
240- (id<BRMenuItemLayer>) itemForRow: (long) row
241{
242        BRAdornedMenuItemLayer *result = [BRAdornedMenuItemLayer adornedMenuItemWithScene:[self scene]];
243        //      if(row==0)
244        //              [[result textItem] setTitle:BRLocalizedString(@"< Refresh Posters >", @"Reload poster images")];
245        //      else
246        [[result textItem] setTitle:[NSString stringWithFormat:@"Version %2d",row+1]];
247        return result;
248}
249
250- (NSString *) titleForRow: (long) row
251{
252        if(row > [posters count])
253                return nil;
254        else
255                return [NSString stringWithFormat:@"Version %2d",row+1];
256}
257
258- (long) rowForTitle: (NSString *) title
259{
260    long result = -1;
261    long i, count = [self itemCount];
262    for ( i = 0; i < count; i++ )
263    {
264        if ( [title isEqualToString: [self titleForRow: i]] )
265       {
266            result = i;
267            break;
268       }
269    }
270    return ( result );
271}
272
273@end
274
275@implementation SapphirePosterChooser (IconListManagement)
276
277/*!
278* @brief load poster image layers
279 *
280 */
281- (void) loadPosters
282{
283        NSMutableArray * results = [NSMutableArray array];
284        if([posters count])
285        {
286                /*Get each result*/
287                NSEnumerator *resultEnum = [posters objectEnumerator];
288                NSString *result = nil;
289                while((result = [resultEnum nextObject]) != nil)
290                {
291                        /* Download agent work around - Have the chooser load the poster images from the web */
292//                      NSString *posterDest=[NSString stringWithFormat:@"%@/%@",
293//                              [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/Poster_Buffer"],
294//                              [result lastPathComponent]];
295                        NSString *posterPath=[NSString stringWithFormat:@"http://www.IMPAwards.com%@",result];
296                        /* use the tumbnail image for the poster chooser -faster but ugly */
297//                      posterPath=[posterPath stringByReplacingAllOccurancesOf:@"/posters/" withString:@"/thumbs/imp_"];
298                        [results addObject:[self getPosterLayer:posterPath]];
299                }
300        }
301        posterLayers=[results copy];
302}
303
304- (BRBlurryImageLayer *) getPosterLayer: (NSString *) thePosterPath
305{
306        /* Download agent work around - Have the chooser load the poster images from the web */
307//    NSURL * posterURL = [NSURL fileURLWithPath: thePosterPath];
308        NSURL * posterURL=[NSURL URLWithString:thePosterPath];
309    if (posterURL==nil)
310        return nil;
311       
312    CGImageRef posterImage = CreateImageForURL( posterURL );
313    if(posterImage==nil)
314        return nil;
315       
316    struct BRBitmapDataInfo info;
317    info.internalFormat = GL_RGBA;
318    info.dataFormat = GL_BGRA;
319    info.dataType = GL_UNSIGNED_INT_8_8_8_8_REV;
320    info.width = 510;
321    info.height = 755;
322       
323    BRRenderContext * context = [[self scene] resourceContext];
324               
325    NSData * data = CreateBitmapDataFromImage( posterImage, info.width, info.height );
326    BRBitmapTexture * lucid = [[BRBitmapTexture alloc] initWithBitmapData: data
327                                                                                                                           bitmapInfo: &info
328                                                                                                                                  context: context
329                                                                                                                                   mipmap: YES];
330    [data release];
331       
332    BRBitmapTexture * blur = [BRBlurryImageLayer blurredImageForImage: posterImage
333                                                            inContext: context
334                                                                 size: NSMakeSize(510.0f, 755.0f)];
335       
336    CFRelease( posterImage );
337       
338    BRBlurryImageLayer * result = [BRBlurryImageLayer layerWithScene: [self scene]];
339       
340    [result setLucidImage: lucid withReflection: nil];
341    [result setBlurryImage: blur withReflection: nil];
342       
343    [lucid release];
344       
345    return ( result );
346}
347
348- (void) hideIconMarch
349{
350    [posterMarch removeFromSuperlayer];
351}
352
353- (void) showIconMarch
354{
355    NSRect frame = [[self masterLayer] frame];
356    frame.size.width *= 0.50f;
357        frame.size.height *= 1.7f;
358        frame.origin.y=-200.0f;
359    [posterMarch setFrame: frame];     
360    [[[self scene] root] insertSublayer: posterMarch below: [self masterLayer]];
361}
362
363- (void) selectionChanged: (NSNotification *) note
364{
365        /* ATV version 1.1 */
366        if([(BRListControl *)[note object] respondsToSelector:@selector(renderSelection)])
367                [posterMarch setSelection: [(BRListControl *)[note object] renderSelection]];
368        /* ATV version 1.0 */
369        else
370                [posterMarch setSelection: [(BRListControl *)[note object] selection]];
371}
372@end
Note: See TracBrowser for help on using the repository browser.