Skip to content

Instantly share code, notes, and snippets.

@cnsoft
Forked from PrimaryFeather/SXGauge.h
Created November 20, 2012 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnsoft/4116709 to your computer and use it in GitHub Desktop.
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.
#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
#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