-
-
Save cnsoft/4116709 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 | |
{ | |
@private | |
SPImage *mImage; | |
float mRatio; | |
} | |
/// 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 | |
@synthesize ratio = mRatio; | |
- (id)initWithTexture:(SPTexture*)texture | |
{ | |
if ((self = [super init])) | |
{ | |
mRatio = 1.0f; | |
mImage = [SPImage imageWithTexture:texture]; | |
[self addChild:mImage]; | |
} | |
return self; | |
} | |
- (id)init | |
{ | |
return [self initWithTexture:[SPTexture emptyTexture]]; | |
} | |
- (void)update | |
{ | |
mImage.scaleX = mRatio; | |
[mImage setTexCoords:[SPPoint pointWithX:mRatio y:0.0f] ofVertex:1]; | |
[mImage setTexCoords:[SPPoint pointWithX:mRatio y:1.0f] ofVertex:3]; | |
} | |
- (void)setRatio:(float)value | |
{ | |
mRatio = MAX(0.0f, MIN(1.0f, value)); | |
[self update]; | |
} | |
+ (SXGauge *)gaugeWithTexture:(SPTexture *)texture | |
{ | |
return [[[SXGauge alloc] initWithTexture:texture] autorelease]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment