source: trunk/SapphirePosterChooser.m @ 271

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