Last active
September 25, 2015 04:58
-
-
Save PrimaryFeather/867241 to your computer and use it in GitHub Desktop.
This Sparrow extension class displays a texture, trimmed to its left side, depending on a ratio. This can be used to create a progress bar or a rest-time display.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "Sparrow.h" | |
/// An SXGauge displays a texture, trimmed to its left side, depending on a ratio. | |
/// This can be used to display a progress bar or a time gauge. | |
@interface SXGauge : SPSprite | |
/// Indicates how much of the texture is displayed. Range: 0.0f - 1.0f | |
@property (nonatomic, assign) float ratio; | |
/// Initializes a gauge with a certain texture | |
- (id)initWithTexture:(SPTexture*)texture; | |
/// Factory method. | |
+ (SXGauge *)gaugeWithTexture:(SPTexture *)texture; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SXGauge.h" | |
@implementation SXGauge | |
{ | |
SPImage *_image; | |
float _ratio; | |
} | |
@synthesize ratio = _ratio; | |
- (id)initWithTexture:(SPTexture*)texture | |
{ | |
if ((self = [super init])) | |
{ | |
_ratio = 1.0f; | |
_image = [SPImage imageWithTexture:texture]; | |
[self addChild:_image]; | |
} | |
return self; | |
} | |
- (id)init | |
{ | |
return [self initWithTexture:[SPTexture emptyTexture]]; | |
} | |
- (void)update | |
{ | |
_image.scaleX = _ratio; | |
[_image setTexCoordsWithX:_ratio y:0.0f ofVertex:1]; | |
[_image setTexCoordsWithX:_ratio y:1.0f ofVertex:3]; | |
} | |
- (void)setRatio:(float)value | |
{ | |
_ratio = MAX(0.0f, MIN(1.0f, value)); | |
[self update]; | |
} | |
+ (SXGauge *)gaugeWithTexture:(SPTexture *)texture | |
{ | |
return [[SXGauge alloc] initWithTexture:texture]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment