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

Last change on this file since 554 was 554, checked in by gbooker, 9 years ago

Importing should work now, but not tested

File size: 13.2 KB
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       
75        return self;
76}
77
78- (void)dealloc
79{
80        [[NSNotificationCenter defaultCenter] removeObserver: self];
81    [posterMarch removeFromSuperlayer];
82//    [posterMarch setIconSource: nil];  //This throws an exception
83        [posters release];
84        [posterLayers release];
85        [fileName release];
86        [movieTitle release];
87        [fileInfoText release];
88        [posterMarch release];
89        [defaultImage release];
90        [meta release];
91        [super dealloc];
92}
93
94- (void) resetLayout
95{
96    [super resetLayout];
97        [SapphireFrontRowCompat renderScene:[self scene]];
98}
99
100- (void) willBePushed
101{
102    // We're about to be placed on screen, but we're not yet there
103    // add the icon march layer to the scene
104    [self showIconMarch];
105   
106    // always call super
107    [super willBePushed];
108}
109
110- (void)doMyLayout
111{
112        NSRect master = [SapphireFrontRowCompat frameOfController:self];
113        NSSize txtSize = [SapphireFrontRowCompat textControl:fileInfoText renderedSizeWithMaxSize:NSMakeSize(master.size.width * 2.0f/3.0f, master.size.height * 0.4f)];
114        NSRect frame;
115        frame.origin.x = (master.size.width - txtSize.width) * 0.5f;
116        frame.origin.y = (master.size.height * 0.44f - txtSize.height) + master.size.height * 0.3f/0.8f + master.origin.y;
117        frame.size = txtSize;
118        [fileInfoText setFrame:frame];
119}
120
121- (void)wasPushed
122{
123        [self doMyLayout];
124        [[self list] reload];
125        [super wasPushed];
126}
127
128- (void) wasPopped
129{
130    // The user pressed Menu, removing us from the screen
131    // always call super
132    [super wasPopped];
133    // remove the icon march from the scene
134    [self hideIconMarch];
135}
136
137/*!
138 * @brief Override the layout
139 *
140 */
141- (NSRect)listRectWithSize:(NSRect)listFrame inMaster:(NSRect)master
142{
143        listFrame.size.height -= 2.5f*listFrame.origin.y;
144        listFrame.size.width*= 0.45f;
145        listFrame.origin.x = (master.size.width - listFrame.size.width) * 0.85f;
146        listFrame.origin.y = (master.size.height * 0.3f - listFrame.size.height) + master.size.height * 0.3f/0.8f + master.origin.y;
147        return listFrame;
148}
149
150- (void) itemSelected: (long) row
151{
152        /*User made a selection*/
153        //      if(selection==0)
154        //      {
155        /*User requested a menu refresh*/
156        //              [self resetLayout];
157        //      }
158        //      else
159        //      {
160        selectedPoster = row;
161        [[self stack] popController];
162        //      }
163}
164
165- (BOOL)okayToDisplay
166{
167        if([[self list]respondsToSelector:@selector(renderSelection)] || [SapphireFrontRowCompat usingFrontRow])
168                return [[SapphireSettings sharedSettings] displayPosterChooser];
169        else
170                return NO;
171}
172
173- (NSArray *)posters
174{
175        return posters;
176}
177
178- (void)setPosters:(NSArray *)posterList
179{
180        posters = [posterList retain];
181        if([posters count] > 5)
182        {
183                [posterMarch release];
184                posterMarch = nil;
185        }
186        [self loadPosters];
187    [posterMarch setIconSource: self];
188        [[self list] setDatasource:self];
189}
190
191- (void)loadPosters
192{
193        int i, count = [posters count];
194        posterLayers = [posters mutableCopy];
195        for(i=0; i<count; i++)
196                [self loadPoster:i];
197        [posterMarch reload] ;
198        [SapphireFrontRowCompat renderScene:[self scene]];
199}
200
201- (void)reloadPoster:(int)index
202{
203        [self loadPoster:index];
204        [posterMarch _updateIcons] ;
205        [SapphireFrontRowCompat renderScene:[self scene]];
206}
207
208- (void)setFileName:(NSString*)choosingForFileName
209{
210        fileName=[choosingForFileName retain];
211        if(movieTitle)
212                [SapphireFrontRowCompat setText:[NSString stringWithFormat:@"%@ (%@)",movieTitle,fileName] withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
213        else
214                [SapphireFrontRowCompat setText:fileName withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
215}
216
217- (void)setFile:(SapphireFileMetaData *)aMeta;
218{
219        [meta release];
220        meta = [aMeta retain];
221}
222
223- (NSString *)fileName
224{
225        return fileName;
226}
227
228- (void)setMovieTitle:(NSString *)theMovieTitle
229{
230        movieTitle = [theMovieTitle retain];
231        if(fileName)
232                [SapphireFrontRowCompat setText:[NSString stringWithFormat:@"%@ (%@)",movieTitle,fileName] withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
233        else
234                [SapphireFrontRowCompat setText:movieTitle withAtrributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes] forControl:fileInfoText];
235       
236        NSRect master = [SapphireFrontRowCompat frameOfController:self];
237        [fileInfoText setMaximumSize:NSMakeSize(master.size.width * 2.0f/3.0f, master.size.height * 0.4f)];
238        NSSize txtSize = [fileInfoText renderedSize];
239        NSRect frame;
240        frame.origin.x = (master.size.width - txtSize.width) * 0.5f;
241        frame.origin.y = (master.size.height * 0.44f - txtSize.height) + master.size.height * 0.3f/0.8f + master.origin.y;
242        frame.size = txtSize;
243        [fileInfoText setFrame:frame];
244}
245
246- (NSString *)movieTitle
247{
248        return movieTitle;
249}
250
251- (long)selectedPoster
252{
253        return selectedPoster;
254}
255
256- (long) iconCount
257{
258        return [posterLayers count];
259}
260
261- (NSDictionary *) iconInfoAtIndex: (long) index
262{
263        return [NSDictionary dictionaryWithObject:[posterLayers objectAtIndex:index] forKey:@"icon"];
264}
265
266- (id) iconAtIndex: (long) index
267{
268    if ( index >= [posterLayers count] )
269        return nil;
270       
271    return [posterLayers objectAtIndex:index];
272}
273
274
275- (long) itemCount
276{
277        return [posters count];
278}
279
280
281- (id) itemForRow: (long) row
282{
283        BRAdornedMenuItemLayer *result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:NO];
284        //      if(row==0)
285        //              [SapphireFrontRowCompat setTitle:BRLocalizedString(@"< Refresh Posters >", @"Reload poster images") forMenu:result];
286        //      else
287        [SapphireFrontRowCompat setTitle:[NSString stringWithFormat:@"Version %2d",row+1] forMenu:result];
288        return result;
289}
290
291- (NSString *) titleForRow: (long) row
292{
293        if(row > [posters count])
294                return nil;
295        else
296                return [NSString stringWithFormat:@"Version %2d",row+1];
297}
298
299- (long) rowForTitle: (NSString *) title
300{
301    long result = -1;
302    long i, count = [self itemCount];
303    for ( i = 0; i < count; i++ )
304    {
305        if ( [title isEqualToString: [self titleForRow: i]] )
306       {
307            result = i;
308            break;
309       }
310    }
311    return ( result );
312}
313
314/*!
315 * @brief load poster image layers
316 *
317 * @param The index of the poster to load
318 */
319- (void) loadPoster:(int)index;
320{
321        NSString *poster = [posters objectAtIndex:index];
322        NSString *posterDest=[NSString stringWithFormat:@"%@/%@",
323                [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/Poster_Buffer"],
324                [poster lastPathComponent]];
325        [posterLayers replaceObjectAtIndex:index withObject:[self getPosterLayer:posterDest]];
326}
327
328- (BRBlurryImageLayer *) getPosterLayer: (NSString *) thePosterPath
329{
330        if([SapphireFrontRowCompat usingFrontRow])
331        {
332                /*The marching icons has changed, dramatically, so we do the changes here*/
333                id ret = [SapphireFrontRowCompat imageAtPath:thePosterPath];
334                if(ret != nil)
335                        return ret;
336                else
337                        return defaultImage;
338        }
339    NSURL * posterURL = [NSURL fileURLWithPath: thePosterPath];
340       
341    if (posterURL==nil)
342                return nil;
343        CGImageRef posterImage=NULL;
344        CGImageSourceRef  sourceRef;   
345    sourceRef = CGImageSourceCreateWithURL((CFURLRef)posterURL, NULL);
346    if(sourceRef) {
347        posterImage = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL);
348        CFRelease(sourceRef);
349    }
350    if(posterImage==nil)
351                return defaultImage;
352       
353    struct BRBitmapDataInfo info;
354    info.internalFormat = GL_RGBA;
355    info.dataFormat = GL_BGRA;
356    info.dataType = GL_UNSIGNED_INT_8_8_8_8_REV;
357    info.width = 510;
358    info.height = 755;
359       
360    BRRenderContext * context = [[self scene] resourceContext];
361               
362    NSData * data = CreateBitmapDataFromImage(posterImage,info.width,info.height );
363    BRBitmapTexture * lucid = [[BRBitmapTexture alloc] initWithBitmapData: data
364                                                                                                                           bitmapInfo: &info
365                                                                                                                                  context: context
366                                                                                                                                   mipmap: YES];
367    [data release];
368       
369    BRBitmapTexture * blur = [BRBlurryImageLayer blurredImageForImage: posterImage
370                                                            inContext: context
371                                                                 size: NSMakeSize(510.0f, 755.0f)];
372       
373    CFRelease( posterImage );
374       
375    BRBlurryImageLayer * result = [BRBlurryImageLayer layerWithScene: [self scene]];
376       
377    [result setLucidImage: lucid withReflection: nil];
378    [result setBlurryImage: blur withReflection: nil];
379       
380    [lucid release];
381       
382    return ( result );
383}
384
385- (void) hideIconMarch
386{
387        /* Might want to free memory here since posters won't be chosen again */
388    [posterMarch removeFromSuperlayer];
389}
390
391- (void) showIconMarch
392{
393        NSRect frame = [SapphireFrontRowCompat frameOfController:self];
394    frame.size.width *= 0.50f;
395        if(![SapphireFrontRowCompat usingFrontRow])
396        {
397                frame.size.height *= 1.7f;
398                frame.origin.y=-200.0f;
399        }
400        else
401                frame.size.height = ([fileInfoText frame].origin.y - frame.origin.y) * 1.2f;
402    [posterMarch setFrame: frame];
403        if(posterMarch != nil)
404                [SapphireFrontRowCompat addSublayer:posterMarch toControl:self];
405}
406
407- (void)setSelectionForPoster:(double)sel
408{
409        if(posterMarch == nil)
410                return;
411        NSMethodSignature *signature = [posterMarch methodSignatureForSelector:@selector(setSelection:)];
412        NSInvocation *selInv = [NSInvocation invocationWithMethodSignature:signature];
413        [selInv setSelector:@selector(setSelection:)];
414        if(strcmp([signature getArgumentTypeAtIndex:2], "l"))
415        {
416                double dvalue = sel;
417                [selInv setArgument:&dvalue atIndex:2];
418        }
419        else
420        {
421                long lvalue = sel;
422                [selInv setArgument:&lvalue atIndex:2];
423        }
424        [selInv invokeWithTarget:posterMarch];
425}
426
427- (void) selectionChanged: (NSNotification *) note
428{
429        /* ATV version 1.1 */
430        if([(BRListControl *)[note object] respondsToSelector:@selector(renderSelection)])
431                [self setSelectionForPoster:[(BRListControl *)[note object] renderSelection]];
432        /* ATV version 1.0 */
433        else
434                [self setSelectionForPoster:[(BRListControl *)[note object] selection]];
435}
436
437- (id<BRMediaPreviewController>) previewControlForItem: (long) row
438{
439        if(posterMarch != nil)
440                return nil;
441        SapphireMediaPreview *preview = [[SapphireMediaPreview alloc] initWithScene:[self scene]];
442        SapphireMedia *asset = [[SapphireMedia alloc] initWithMediaURL:[NSURL fileURLWithPath:@"none"]];
443        NSString *poster = [posters objectAtIndex:row];
444        NSString *posterDest=[NSString stringWithFormat:@"%@/%@",
445                                                  [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/Poster_Buffer"],
446                                                  [poster lastPathComponent]];
447        [preview setShowsMetadataImmediately:NO];
448        SapphireDirectoryMetaData *parent = [meta parent];
449        [preview setMetaData:meta inMetaData:parent];
450        [preview setShowsMetadataImmediately:YES];
451        [asset setImagePath:posterDest];
452        [preview setAsset:asset];
453        [asset release];
454       
455        return preview;
456}
457
458- (id<BRMediaPreviewController>) previewControllerForItem: (long) row
459{
460        return [self previewControlForItem:row];
461}
462
463
464@end
Note: See TracBrowser for help on using the repository browser.