source: branches/CoreData/SapphireFrappliance/MetaDataImporting/SapphirePosterChooser.m @ 696

Revision 696, 13.2 KB checked in by gbooker, 5 years ago (diff)

Putting back the stuff that was lost on server

Line 
1/*
2 * SapphirePosterChooser.m
3 * Sapphire
4 *
5 * Created by Patrick Merrill on Oct. 11, 2007.
6 * Copyright 2007 Sapphire Development Team and/or www.nanopi.net
7 * All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
10 * General Public License as published by the Free Software Foundation; either version 3 of the License,
11 * or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
14 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 * Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with this program; if not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 */
20
21#import "SapphirePosterChooser.h"
22#import "SapphireFileMetaData.h"
23#import "SapphireSettings.h"
24#import "SapphireMediaPreview.h"
25#import "SapphireMedia.h"
26#import "SapphireMetaData.h"
27#import "SapphireDirectoryMetaData.h"
28#import <SapphireCompatClasses/SapphireFrontRowCompat.h>
29
30NSData *CreateBitmapDataFromImage(CGImageRef image, unsigned int width, unsigned int height);
31
32@interface BRListControl (definedin1_1)
33- (double)renderSelection;
34@end
35
36@interface SapphirePosterChooser (private)
37- (BRBlurryImageLayer *) getPosterLayer: (NSString *) thePosterPath;
38- (void) loadPoster:(int)index;
39- (void) hideIconMarch;
40- (void) showIconMarch;
41- (void) selectionChanged: (NSNotification *) note;
42@end
43
44@implementation SapphirePosterChooser
45
46- (id) initWithScene: (BRRenderScene *) scene
47{
48        self = [super initWithScene: scene];
49        if(!self)
50                return nil;
51        selectedPoster = -1;
52       
53        // we want to know when the list selection changes, so we can pass
54    // that information on to the icon march layer
55    [[NSNotificationCenter defaultCenter] addObserver: self
56                                             selector: @selector(selectionChanged:)
57                                                 name: @"ListControlSelectionChangedNotification"
58                                               object: [self list]];
59       
60        /* Set a control to display the fileName */
61        fileInfoText = [SapphireFrontRowCompat newTextControlWithScene:scene];
62        [SapphireFrontRowCompat setText:@"No File" withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
63        NSRect frame = [SapphireFrontRowCompat frameOfController:self];
64        frame.origin.y = frame.size.height / 1.25f;
65        frame.origin.x = (frame.size.width / 4.0f) ;
66        defaultImage = [[self getPosterLayer:[[NSBundle bundleForClass:[self class]] pathForResource:@"PH" ofType:@"png"]] retain];
67
68       
69        [fileInfoText setFrame: frame];
70        [self addControl: fileInfoText];
71       
72        /* Setup posterMarch controls */
73        posterMarch = [SapphireFrontRowCompat newMarchingIconLayerWithScene:scene];
74        [SapphireLayoutManager setCustomLayoutOnControl:self];
75       
76        return self;
77}
78
79- (void)dealloc
80{
81        [[NSNotificationCenter defaultCenter] removeObserver: self];
82    [posterMarch removeFromSuperlayer];
83//    [posterMarch setIconSource: nil];  //This throws an exception
84        [posters release];
85        [posterLayers release];
86        [fileName release];
87        [movieTitle release];
88        [fileInfoText release];
89        [posterMarch release];
90        [defaultImage release];
91        [meta release];
92        [super dealloc];
93}
94
95- (void) resetLayout
96{
97    [super resetLayout];
98        [SapphireFrontRowCompat renderScene:[self scene]];
99}
100
101- (void) willBePushed
102{
103    // We're about to be placed on screen, but we're not yet there
104   
105    // always call super
106    [super willBePushed];
107}
108
109- (void)doMyLayout
110{
111        NSRect master = [SapphireFrontRowCompat frameOfController:self];
112        NSSize txtSize = [SapphireFrontRowCompat textControl:fileInfoText renderedSizeWithMaxSize:NSMakeSize(master.size.width * 2.0f/3.0f, master.size.height * 0.4f)];
113        NSRect frame;
114        frame.origin.x = (master.size.width - txtSize.width) * 0.5f;
115        frame.origin.y = (master.size.height * 0.44f - txtSize.height) + master.size.height * 0.3f/0.8f + master.origin.y;
116        frame.size = txtSize;
117        [fileInfoText setFrame:frame];
118}
119
120- (void)wasPushed
121{
122        [self doMyLayout];
123        [[self list] reload];
124        [super wasPushed];
125}
126
127- (void) wasPopped
128{
129    // The user pressed Menu, removing us from the screen
130    // always call super
131    [super wasPopped];
132    // remove the icon march from the scene
133    [self hideIconMarch];
134}
135
136/*!
137 * @brief Override the layout
138 *
139 */
140- (NSRect)listRectWithSize:(NSRect)listFrame inMaster:(NSRect)master
141{
142        listFrame.size.height -= 2.5f*listFrame.origin.y;
143        listFrame.size.width*= 0.45f;
144        listFrame.origin.x = (master.size.width - listFrame.size.width) * 0.85f;
145        listFrame.origin.y = (master.size.height * 0.3f - listFrame.size.height) + master.size.height * 0.3f/0.8f + master.origin.y;
146        return listFrame;
147}
148
149- (void) itemSelected: (long) row
150{
151        /*User made a selection*/
152        //      if(selection==0)
153        //      {
154        /*User requested a menu refresh*/
155        //              [self resetLayout];
156        //      }
157        //      else
158        //      {
159        selectedPoster = row;
160        [[self stack] popController];
161        //      }
162}
163
164- (BOOL)okayToDisplay
165{
166        if([[self list]respondsToSelector:@selector(renderSelection)] || [SapphireFrontRowCompat usingFrontRow])
167                return [[SapphireSettings sharedSettings] displayPosterChooser];
168        else
169                return NO;
170}
171
172- (NSArray *)posters
173{
174        return posters;
175}
176
177- (void)setPosters:(NSArray *)posterList
178{
179        posters = [posterList retain];
180        if([posters count] > 5)
181        {
182                [posterMarch release];
183                posterMarch = nil;
184        }
185        [self loadPosters];
186    [posterMarch setIconSource: self];
187        [[self list] setDatasource:self];
188}
189
190- (void)loadPosters
191{
192        int i, count = [posters count];
193        posterLayers = [posters mutableCopy];
194        for(i=0; i<count; i++)
195                [self loadPoster:i];
196        [posterMarch reload] ;
197        [SapphireFrontRowCompat renderScene:[self scene]];
198}
199
200- (void)reloadPoster:(int)index
201{
202        [self loadPoster:index];
203        [posterMarch _updateIcons] ;
204        [SapphireFrontRowCompat renderScene:[self scene]];
205}
206
207- (void)setFileName:(NSString*)choosingForFileName
208{
209        fileName=[choosingForFileName retain];
210        if(movieTitle)
211                [SapphireFrontRowCompat setText:[NSString stringWithFormat:@"%@ (%@)",movieTitle,fileName] withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
212        else
213                [SapphireFrontRowCompat setText:fileName withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
214}
215
216- (void)setFile:(SapphireFileMetaData *)aMeta;
217{
218        [meta release];
219        meta = [aMeta retain];
220}
221
222- (NSString *)fileName
223{
224        return fileName;
225}
226
227- (void)setMovieTitle:(NSString *)theMovieTitle
228{
229        movieTitle = [theMovieTitle retain];
230        if(fileName)
231                [SapphireFrontRowCompat setText:[NSString stringWithFormat:@"%@ (%@)",movieTitle,fileName] withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
232        else
233                [SapphireFrontRowCompat setText:movieTitle withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
234       
235        NSRect master = [SapphireFrontRowCompat frameOfController:self];
236        [fileInfoText setMaximumSize:NSMakeSize(master.size.width * 2.0f/3.0f, master.size.height * 0.4f)];
237        NSSize txtSize = [fileInfoText renderedSize];
238        NSRect frame;
239        frame.origin.x = (master.size.width - txtSize.width) * 0.5f;
240        frame.origin.y = (master.size.height * 0.44f - txtSize.height) + master.size.height * 0.3f/0.8f + master.origin.y;
241        frame.size = txtSize;
242        [fileInfoText setFrame:frame];
243}
244
245- (NSString *)movieTitle
246{
247        return movieTitle;
248}
249
250- (long)selectedPoster
251{
252        return selectedPoster;
253}
254
255- (long) iconCount
256{
257        return [posterLayers count];
258}
259
260- (NSDictionary *) iconInfoAtIndex: (long) index
261{
262        return [NSDictionary dictionaryWithObject:[posterLayers objectAtIndex:index] forKey:@"icon"];
263}
264
265- (id) iconAtIndex: (long) index
266{
267    if ( index >= [posterLayers count] )
268        return nil;
269       
270    return [posterLayers objectAtIndex:index];
271}
272
273
274- (long) itemCount
275{
276        return [posters count];
277}
278
279
280- (id) itemForRow: (long) row
281{
282        BRAdornedMenuItemLayer *result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:NO];
283        //      if(row==0)
284        //              [SapphireFrontRowCompat setTitle:BRLocalizedString(@"< Refresh Posters >", @"Reload poster images") forMenu:result];
285        //      else
286        [SapphireFrontRowCompat setTitle:[NSString stringWithFormat:@"Version %2d",row+1] forMenu:result];
287        return result;
288}
289
290- (NSString *) titleForRow: (long) row
291{
292        if(row > [posters count])
293                return nil;
294        else
295                return [NSString stringWithFormat:@"Version %2d",row+1];
296}
297
298- (long) rowForTitle: (NSString *) title
299{
300    long result = -1;
301    long i, count = [self itemCount];
302    for ( i = 0; i < count; i++ )
303    {
304        if ( [title isEqualToString: [self titleForRow: i]] )
305       {
306            result = i;
307            break;
308       }
309    }
310    return ( result );
311}
312
313/*!
314 * @brief load poster image layers
315 *
316 * @param The index of the poster to load
317 */
318- (void) loadPoster:(int)index;
319{
320        NSString *poster = [posters objectAtIndex:index];
321        NSString *posterDest=[NSString stringWithFormat:@"%@/%@",
322                [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/Poster_Buffer"],
323                [poster lastPathComponent]];
324        [posterLayers replaceObjectAtIndex:index withObject:[self getPosterLayer:posterDest]];
325}
326
327- (BRBlurryImageLayer *) getPosterLayer: (NSString *) thePosterPath
328{
329        if([SapphireFrontRowCompat usingFrontRow])
330        {
331                /*The marching icons has changed, dramatically, so we do the changes here*/
332                id ret = [SapphireFrontRowCompat imageAtPath:thePosterPath];
333                if(ret != nil)
334                        return ret;
335                else
336                        return defaultImage;
337        }
338    NSURL * posterURL = [NSURL fileURLWithPath: thePosterPath];
339       
340    if (posterURL==nil)
341                return nil;
342        CGImageRef posterImage=NULL;
343        CGImageSourceRef  sourceRef;   
344    sourceRef = CGImageSourceCreateWithURL((CFURLRef)posterURL, NULL);
345    if(sourceRef) {
346        posterImage = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL);
347        CFRelease(sourceRef);
348    }
349    if(posterImage==nil)
350                return defaultImage;
351       
352    struct BRBitmapDataInfo info;
353    info.internalFormat = GL_RGBA;
354    info.dataFormat = GL_BGRA;
355    info.dataType = GL_UNSIGNED_INT_8_8_8_8_REV;
356    info.width = 510;
357    info.height = 755;
358       
359    BRRenderContext * context = [[self scene] resourceContext];
360               
361    NSData * data = CreateBitmapDataFromImage(posterImage,info.width,info.height );
362    BRBitmapTexture * lucid = [[BRBitmapTexture alloc] initWithBitmapData: data
363                                                                                                                           bitmapInfo: &info
364                                                                                                                                  context: context
365                                                                                                                                   mipmap: YES];
366    [data release];
367       
368    BRBitmapTexture * blur = [BRBlurryImageLayer blurredImageForImage: posterImage
369                                                            inContext: context
370                                                                 size: NSMakeSize(510.0f, 755.0f)];
371       
372    CFRelease( posterImage );
373       
374    BRBlurryImageLayer * result = [BRBlurryImageLayer layerWithScene: [self scene]];
375       
376    [result setLucidImage: lucid withReflection: nil];
377    [result setBlurryImage: blur withReflection: nil];
378       
379    [lucid release];
380       
381    return ( result );
382}
383
384- (void) hideIconMarch
385{
386        /* Might want to free memory here since posters won't be chosen again */
387    [posterMarch removeFromSuperlayer];
388}
389
390- (void) showIconMarch
391{
392        NSRect frame = [SapphireFrontRowCompat frameOfController:self];
393    frame.size.width *= 0.50f;
394        if(![SapphireFrontRowCompat usingFrontRow])
395        {
396                frame.size.height *= 1.7f;
397                frame.origin.y=-200.0f;
398        }
399        else
400                frame.size.height = ([fileInfoText frame].origin.y - frame.origin.y) * 1.2f;
401    [posterMarch setFrame: frame];
402        if(posterMarch != nil)
403                [SapphireFrontRowCompat addSublayer:posterMarch toControl:self];
404}
405
406- (void)setSelectionForPoster:(double)sel
407{
408        if(posterMarch == nil)
409                return;
410        NSMethodSignature *signature = [posterMarch methodSignatureForSelector:@selector(setSelection:)];
411        NSInvocation *selInv = [NSInvocation invocationWithMethodSignature:signature];
412        [selInv setSelector:@selector(setSelection:)];
413        if(strcmp([signature getArgumentTypeAtIndex:2], "l"))
414        {
415                double dvalue = sel;
416                [selInv setArgument:&dvalue atIndex:2];
417        }
418        else
419        {
420                long lvalue = sel;
421                [selInv setArgument:&lvalue atIndex:2];
422        }
423        [selInv invokeWithTarget:posterMarch];
424}
425
426- (void) selectionChanged: (NSNotification *) note
427{
428        /* ATV version 1.1 */
429        if([(BRListControl *)[note object] respondsToSelector:@selector(renderSelection)])
430                [self setSelectionForPoster:[(BRListControl *)[note object] renderSelection]];
431        /* ATV version 1.0 */
432        else
433                [self setSelectionForPoster:[(BRListControl *)[note object] selection]];
434}
435
436- (id<BRMediaPreviewController>) previewControlForItem: (long) row
437{
438        if(posterMarch != nil)
439                return nil;
440        SapphireMediaPreview *preview = [[SapphireMediaPreview alloc] initWithScene:[self scene]];
441        SapphireMedia *asset = [[SapphireMedia alloc] initWithMediaURL:[NSURL fileURLWithPath:@"none"]];
442        NSString *poster = [posters objectAtIndex:row];
443        NSString *posterDest=[NSString stringWithFormat:@"%@/%@",
444                                                  [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/Poster_Buffer"],
445                                                  [poster lastPathComponent]];
446        [preview setShowsMetadataImmediately:NO];
447        SapphireDirectoryMetaData *parent = [meta parent];
448        [preview setMetaData:meta inMetaData:parent];
449        [preview setShowsMetadataImmediately:YES];
450        [asset setImagePath:posterDest];
451        [preview setAsset:asset];
452        [asset release];
453       
454        return preview;
455}
456
457- (id<BRMediaPreviewController>) previewControllerForItem: (long) row
458{
459        return [self previewControlForItem:row];
460}
461
462
463@end
Note: See TracBrowser for help on using the repository browser.