Skip to content
This repository has been archived by the owner on Nov 18, 2019. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
typeoneerror committed Oct 2, 2010
0 parents commit 18d2e38
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 0 deletions.
26 changes: 26 additions & 0 deletions GKAchievementHandler.h
@@ -0,0 +1,26 @@
//
// GKAchievementHandler.h
//
// Created by Benjamin Borowski on 9/30/10.
// Copyright 2010 Typeoneerror Studios. All rights reserved.
// $Id$
//

#import <Foundation/Foundation.h>
#import "GKAchievementNotification.h"

@interface GKAchievementHandler : NSObject <GKAchievementHandlerDelegate>
{
UIView *_topView;
NSMutableArray *_queue;
UIImage *_image;
}

@property (nonatomic, retain) UIImage *image;

+ (GKAchievementHandler *)defaultHandler;

- (void)notifyAchievement:(GKAchievementDescription *)achievement;
- (void)notifyAchievementMessage:(NSString *)message;

@end
107 changes: 107 additions & 0 deletions GKAchievementHandler.m
@@ -0,0 +1,107 @@
//
// GKAchievementHandler.m
//
// Created by Benjamin Borowski on 9/30/10.
// Copyright 2010 Typeoneerror Studios. All rights reserved.
// $Id$
//

#import <GameKit/GameKit.h>
#import "GKAchievementHandler.h"
#import "GKAchievementNotification.h"

static GKAchievementHandler *defaultHandler = nil;

@interface GKAchievementHandler(private)

- (void)displayNotification:(GKAchievementNotification *)notification;

@end

@implementation GKAchievementHandler(private)

- (void)displayNotification:(GKAchievementNotification *)notification
{
if (self.image != nil)
{
[notification setImage:self.image];
}
else
{
[notification setImage:nil];
}

[_topView addSubview:notification];
[notification animateIn];
}

@end

@implementation GKAchievementHandler

@synthesize image=_image;

+ (GKAchievementHandler *)defaultHandler
{
if (!defaultHandler) defaultHandler = [[self alloc] init];
return defaultHandler;
}

- (id)init
{
self = [super init];
if (self != nil)
{
_topView = [[UIApplication sharedApplication] keyWindow];
_queue = [[NSMutableArray alloc] initWithCapacity:0];
self.image = [UIImage imageNamed:@"gk-icon.png"];
}
return self;
}

- (void)dealloc
{
[_queue release];
[_image release];
[super dealloc];
}

- (void)notifyAchievement:(GKAchievementDescription *)achievement
{
GKAchievementNotification *notification = [[[GKAchievementNotification alloc] initWithAchievementDescription:achievement] autorelease];
notification.frame = kGKAchievementFrameStart;
notification.handlerDelegate = self;

[_queue addObject:notification];
if ([_queue count] == 1)
{
[self displayNotification:notification];
}
}

- (void)notifyAchievementMessage:(NSString *)message
{
GKAchievementNotification *notification = [[[GKAchievementNotification alloc] initWithMessage:message] autorelease];
notification.frame = kGKAchievementFrameStart;
notification.handlerDelegate = self;

[_queue addObject:notification];
if ([_queue count] == 1)
{
[self displayNotification:notification];
}
}

#pragma mark -
#pragma mark GKAchievementHandlerDelegate implementation

- (void)didHideAchievementNotification:(GKAchievementNotification *)notification
{
[_queue removeObjectAtIndex:0];
if ([_queue count])
{
[self displayNotification:(GKAchievementNotification *)[_queue objectAtIndex:0]];
}
}

@end
66 changes: 66 additions & 0 deletions GKAchievementNotification.h
@@ -0,0 +1,66 @@
//
// GKAchievementNotification.h
//
// Created by Benjamin Borowski on 9/30/10.
// Copyright 2010 Typeoneerror Studios. All rights reserved.
// $Id$
//

#import <UIKit/UIKit.h>

@class GKAchievementNotification;

#define kGKAchievementAnimeTime 0.4f
#define kGKAchievementDisplayTime 1.75f

#define kGKAchievementDefaultSize CGRectMake(0.0f, 0.0f, 284.0f, 52.0f);
#define kGKAchievementFrameStart CGRectMake(18.0f, -53.0f, 284.0f, 52.0f);
#define kGKAchievementFrameEnd CGRectMake(18.0f, 10.0f, 284.0f, 52.0f);

#define kGKAchievementText1 CGRectMake(10.0, 6.0f, 264.0f, 22.0f);
#define kGKAchievementText2 CGRectMake(10.0, 20.0f, 264.0f, 22.0f);
#define kGKAchievementText1WLogo CGRectMake(45.0, 6.0f, 229.0f, 22.0f);
#define kGKAchievementText2WLogo CGRectMake(45.0, 20.0f, 229.0f, 22.0f);

@protocol GKAchievementHandlerDelegate <NSObject>

@optional

- (void)didHideAchievementNotification:(GKAchievementNotification *)notification;
- (void)didShowAchievementNotification:(GKAchievementNotification *)notification;

- (void)willHideAchievementNotification:(GKAchievementNotification *)notification;
- (void)willShowAchievementNotification:(GKAchievementNotification *)notification;

@end

@interface GKAchievementNotification : UIView
{
GKAchievementDescription *_achievement;

NSString *_message; /**< Optional custom achievement message. */

UIImageView *_background;
UIImageView *_logo;

UILabel *_textLabel;
UILabel *_detailLabel;

id<GKAchievementHandlerDelegate> _handlerDelegate;
}

@property (nonatomic, retain) GKAchievementDescription *achievement;
@property (nonatomic, retain) NSString *message;
@property (nonatomic, retain) UIImageView *background;
@property (nonatomic, retain) UIImageView *logo;
@property (nonatomic, retain) UILabel *textLabel;
@property (nonatomic, retain) UILabel *detailLabel;
@property (nonatomic, retain) id<GKAchievementHandlerDelegate> handlerDelegate;

- (id)initWithAchievementDescription:(GKAchievementDescription *)achievement;
- (id)initWithMessage:(NSString *)message;
- (void)animateIn;
- (void)animateOut;
- (void)setImage:(UIImage *)image;

@end
197 changes: 197 additions & 0 deletions GKAchievementNotification.m
@@ -0,0 +1,197 @@
//
// GKAchievementNotification.m
//
// Created by Benjamin Borowski on 9/30/10.
// Copyright 2010 Typeoneerror Studios. All rights reserved.
// $Id$
//

#import <GameKit/GameKit.h>
#import "GKAchievementNotification.h"

@interface GKAchievementNotification(private)

- (void)animationInDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
- (void)animationOutDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
- (void)delegateCallback:(SEL)selector withObject:(id)object;

@end

@implementation GKAchievementNotification(private)

- (void)animationInDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[self delegateCallback:@selector(didShowAchievementNotification:) withObject:self];
[self performSelector:@selector(animateOut) withObject:nil afterDelay:kGKAchievementDisplayTime];
}

- (void)animationOutDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[self delegateCallback:@selector(didHideAchievementNotification:) withObject:self];
[self removeFromSuperview];
}

- (void)delegateCallback:(SEL)selector withObject:(id)object
{
if (self.handlerDelegate)
{
if ([self.handlerDelegate respondsToSelector:selector])
{
[self.handlerDelegate performSelector:selector withObject:object];
}
}
}

@end

@implementation GKAchievementNotification

@synthesize achievement=_achievement;
@synthesize background=_background;
@synthesize handlerDelegate=_handlerDelegate;
@synthesize detailLabel=_detailLabel;
@synthesize logo=_logo;
@synthesize message=_message;
@synthesize textLabel=_textLabel;

- (id)initWithAchievementDescription:(GKAchievementDescription *)achievement
{
CGRect frame = kGKAchievementDefaultSize;
self.achievement = achievement;
if (self = [self initWithFrame:frame])
{
}
return self;
}

- (id)initWithMessage:(NSString *)message
{
CGRect frame = kGKAchievementDefaultSize;
self.message = message;
if (self = [self initWithFrame:frame])
{
}
return self;
}

- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// create the GK background
UIImage *backgroundStretch = [[UIImage imageNamed:@"gk-notification.png"] stretchableImageWithLeftCapWidth:8.0f topCapHeight:0.0f];
UIImageView *tBackground = [[UIImageView alloc] initWithFrame:frame];
tBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth;
tBackground.image = backgroundStretch;
self.background = tBackground;
self.opaque = NO;
[tBackground release];
[self addSubview:self.background];

CGRect r1 = kGKAchievementText1;
CGRect r2 = kGKAchievementText2;

// create the text label
UILabel *tTextLabel = [[UILabel alloc] initWithFrame:r1];
tTextLabel.textAlignment = UITextAlignmentCenter;
tTextLabel.backgroundColor = [UIColor clearColor];
tTextLabel.textColor = [UIColor whiteColor];
tTextLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0f];
tTextLabel.text = NSLocalizedString(@"Achievement Unlocked", @"Achievemnt Unlocked Message");
self.textLabel = tTextLabel;
[tTextLabel release];

// detail label
UILabel *tDetailLabel = [[UILabel alloc] initWithFrame:r2];
tDetailLabel.textAlignment = UITextAlignmentCenter;
tDetailLabel.adjustsFontSizeToFitWidth = YES;
tDetailLabel.minimumFontSize = 10.0f;
tDetailLabel.backgroundColor = [UIColor clearColor];
tDetailLabel.textColor = [UIColor whiteColor];
tDetailLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:11.0f];

if (self.achievement)
{
tDetailLabel.text = self.achievement.achievedDescription;
}
else if (self.message)
{
tDetailLabel.text = self.message;
}

self.detailLabel = tDetailLabel;
[tDetailLabel release];

[self addSubview:self.textLabel];
[self addSubview:self.detailLabel];
}
return self;
}

- (void)animateIn
{
[self delegateCallback:@selector(willShowAchievementNotification:) withObject:self];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kGKAchievementAnimeTime];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:@selector(animationInDidStop:finished:context:)];
self.frame = kGKAchievementFrameEnd;
[UIView commitAnimations];
}

- (void)animateOut
{
[self delegateCallback:@selector(willHideAchievementNotification:) withObject:self];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kGKAchievementAnimeTime];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:@selector(animationOutDidStop:finished:context:)];
self.frame = kGKAchievementFrameStart;
[UIView commitAnimations];
}

- (void)setImage:(UIImage *)image
{
if (image)
{
if (!self.logo)
{
UIImageView *tLogo = [[UIImageView alloc] initWithFrame:CGRectMake(7.0f, 6.0f, 34.0f, 34.0f)];
tLogo.contentMode = UIViewContentModeCenter;
self.logo = tLogo;
[tLogo release];
[self addSubview:self.logo];
}
self.logo.image = image;
self.textLabel.frame = kGKAchievementText1WLogo;
self.detailLabel.frame = kGKAchievementText2WLogo;
}
else
{
if (self.logo)
{
[self.logo removeFromSuperview];
}
self.textLabel.frame = kGKAchievementText1;
self.detailLabel.frame = kGKAchievementText2;
}

}

- (void)dealloc
{
NSLog(@"dealloc: GKAchievementNotification");

self.handlerDelegate = nil;

[_background release];
[_textLabel release];
[_detailLabel release];
[_logo release];

[super dealloc];
}

@end
Empty file added README.md
Empty file.
Binary file added images/gk-icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gk-icon@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gk-notification.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gk-notification@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 18d2e38

Please sign in to comment.