Skip to content

Commit

Permalink
added 'showStatsAt:' methods, added SPNotificationRootCreated notific…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
racarone committed Dec 21, 2015
1 parent 9dd4e87 commit 8594430
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
8 changes: 8 additions & 0 deletions sparrow/src/Classes/SPViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN

typedef void (^SPRootCreatedBlock)(SPSprite *root);

SP_EXTERN NSString *const SPNotificationRootCreated;

/** ------------------------------------------------------------------------------------------------
An SPViewController controls and displays a Sparrow display tree. It represents the main
Expand Down Expand Up @@ -137,6 +139,12 @@ typedef void (^SPRootCreatedBlock)(SPSprite *root);
/// @name Other methods
/// -------------------

/// Displays the statistics box at a certain position.
- (void)showStatsAt:(SPHAlign)horizontalAlign vAlign:(SPVAlign)verticalAlign;

/// Displays the statistics box at a certain position and scale.
- (void)showStatsAt:(SPHAlign)horizontalAlign vAlign:(SPVAlign)verticalAlign scale:(float)scale;

/// Executes a block in a special dispatch queue that is reserved for resource loading.
/// Before executing the block, Sparrow sets up an `EAGLContext` that shares rendering resources
/// with the main context. Thus, you can use this method to load textures through a background-
Expand Down
82 changes: 72 additions & 10 deletions sparrow/src/Classes/SPViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#import "SPView_Internal.h"
#import "SPViewController_Internal.h"

NSString *const SPNotificationRootCreated = @"SPNotificationRootCreated";

// --- private interface ---------------------------------------------------------------------------

@interface SPViewController()
Expand Down Expand Up @@ -149,10 +151,10 @@ - (void)setup
[self makeCurrent];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onActive:)
name:UIApplicationDidBecomeActiveNotification object:nil];
name:UIApplicationDidBecomeActiveNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onResign:)
name:UIApplicationWillResignActiveNotification object:nil];
name:UIApplicationWillResignActiveNotification object:nil];
}

- (void)setupContext
Expand Down Expand Up @@ -377,6 +379,51 @@ - (void)render
}
}

#pragma mark Stats

- (void)showStatsAt:(SPHAlign)horizontalAlign vAlign:(SPVAlign)verticalAlign
{
[self showStatsAt:horizontalAlign vAlign:verticalAlign scale:1.0];
}

- (void)showStatsAt:(SPHAlign)horizontalAlign vAlign:(SPVAlign)verticalAlign scale:(float)scale
{
if (_context == nil)
{
// Sparrow is not yet ready - we postpone this until it's initialized.
[[NSNotificationCenter defaultCenter] addObserverForName:SPNotificationRootCreated
object:self queue:nil usingBlock:^(NSNotification * _Nonnull note)
{
[self showStatsAt:horizontalAlign vAlign:verticalAlign scale:scale];

[[NSNotificationCenter defaultCenter]
removeObserver:self name:SPNotificationRootCreated object:nil];
}];
}
else
{
NSInteger stageWidth = _stage.width;
NSInteger stageHeight = _stage.height;

if (_statsDisplay == nil)
{
_statsDisplay = [[SPStatsDisplay alloc] init];
_statsDisplay.touchable = NO;
}

[_stage addChild:_statsDisplay];
_statsDisplay.scale = scale;

if (horizontalAlign == SPHAlignLeft) _statsDisplay.x = 0;
else if (horizontalAlign == SPHAlignRight) _statsDisplay.x = stageWidth - _statsDisplay.width;
else if (horizontalAlign == SPHAlignCenter) _statsDisplay.x = (stageWidth - _statsDisplay.width) / 2;

if (verticalAlign == SPVAlignTop) _statsDisplay.y = 0;
else if (verticalAlign == SPVAlignBottom) _statsDisplay.y = stageHeight - _statsDisplay.height;
else if (verticalAlign == SPVAlignCenter) _statsDisplay.y = (stageHeight - _statsDisplay.height) / 2;
}
}

#pragma mark Program Management

- (void)registerProgram:(SPProgram *)program name:(NSString *)name
Expand Down Expand Up @@ -418,6 +465,11 @@ - (void)executeInResourceQueueAsynchronously:(BOOL)async block:(dispatch_block_t

#pragma mark UIViewController

- (BOOL)prefersStatusBarHidden
{
return YES;
}

- (void)setView:(SPView *)view
{
if (view != _internalView)
Expand Down Expand Up @@ -452,7 +504,9 @@ - (void)loadView
_internalView.multipleTouchEnabled = YES;
#endif

[_viewPort setEmpty]; // reset viewport
SP_RELEASE_AND_NIL(_context);
_hasRenderedOnce = NO;
[_viewPort setEmpty];
}

- (void)viewDidLoad
Expand Down Expand Up @@ -691,16 +745,21 @@ - (BOOL)multitouchEnabled
#endif
}

- (void)setShowStats:(BOOL)showStats
- (BOOL)showStats
{
return _statsDisplay && _statsDisplay.parent;
}

- (void)setShowStats:(BOOL)value
{
if (showStats && !_statsDisplay && _context)
if (value == self.showStats) return;

if (value)
{
_statsDisplay = [[SPStatsDisplay alloc] init];
[_stage addChild:_statsDisplay];
if (_statsDisplay) [_stage addChild:_statsDisplay];
else [self showStatsAt:SPHAlignLeft vAlign:SPVAlignTop];
}

_showStats = showStats;
_statsDisplay.visible = showStats;
else [_statsDisplay removeFromParent];
}

- (void)setAntiAliasing:(NSInteger)antiAliasing
Expand Down Expand Up @@ -755,6 +814,9 @@ - (void)createRoot
else
{
[_stage addChild:_root atIndex:0];

[[NSNotificationCenter defaultCenter]
postNotificationName:SPNotificationRootCreated object:self];

if (_onRootCreated)
{
Expand Down

0 comments on commit 8594430

Please sign in to comment.